<template>
  <div>
    <print-config :print-config="printConfig"
                  :printCheckedData="printCheckedData"
                  @config-change="configChange"
                  style="float:left;"></print-config>
    <column-pages v-show="canSetColumns"
                  v-if="showColumnPages"
                  :columns-group="columnsGroup"
                  :columns-delete="columnsDelete"
                  @on-end="handleEnd"
                  style="float:left;"></column-pages>
    <div class="action-group">
      <Button type="primary"
              @click="doPrint">打印</Button>
    </div>

    <!-- 打印 -->
    <vuc-print :page-size="printConfig.pageSize"
               :row-height="printConfig.rowHeight"
               :table-data="tableData"
               :page-break="printConfig.pageBreak"
               :show-split="printConfig.showSplit"
               :show-table="showTable"
               :margin-left="printConfig.marginLeft"
               :margin-top="printConfig.marginTop"
               :margin-right="printConfig.marginRight"
               :margin-bottom="printConfig.marginBottom"
               :data-src="printConfig.dataSrc"
               :print-title="printConfig.printTitle"
               :columns-group="columnsGroup"
               :print-header="printHeader"
               :print-footer="printFooter"
               :space-row="printConfig.spaceRow"
               :vuc-table="vucTable"
               @on-print-end="handlePrintEnd"
               ref="easyPrint">
      <!-- <span>我想随意打点东西</span> -->
    </vuc-print>
    <div class="print-mask"
         v-if="showMask"></div>
  </div>
</template>

<script>

import PrintConfig from './print-config'
import ColumnPages from './column-pages'
// import clonedeep from 'clonedeep'
let tableData = []

export default {
  name: 'Printer',
  components: { PrintConfig, ColumnPages },
  props: {
    vucTable: null,
    tableData: tableData,
    // 打印头部,二维数组
    printHeader: {
      type: Array,
      default: () => [[{ type: 'pageNum', align: 'right' }]]
    },
    // 打印尾部,二维数组
    printFooter: {
      type: Array,
      default: () => [[]]
    },
    printTitle: {
      type: String,
      default: '表格打印'
    },
    canSetColumns: {
      type: Boolean,
      default: true
    },
    printCheckedData: {
      type: Boolean,
      default: true
    },
    printRowIndex: { // 是否打印序号
      type: Boolean,
      default: false
    }
  },
  data() {
    return {
      showTable: false,
      columnsGroup: [[]],
      columnsDelete: [],
      printConfig: {
        printTitle: this.printTitle,
        // 每页多少行,0不分页
        pageSize: 10,
        rowHeight: 36,
        marginLeft: 45,
        marginTop: 45,
        marginRight: 45,
        marginBottom: 45,
        maxColumnsPerPage: 15,
        // 分页线
        showSplit: true,
        // 强制分页
        pageBreak: true,
        dataSrc: 'all',
        spaceRow: false
      },
      showMask: false,
      showColumnPages: true
    }
  },
  computed: {
  },
  methods: {
    prepearPrint() {
      // console.log(this.vucTable.properyColumns)
      setTimeout(() => {
        let config = localStorage.getItem(this.vucTable.dataOfTableId + '_cfg')
        if (config) {
          config = JSON.parse(config)
          this.printConfig = Object.assign(this.printConfig, config.printConfig)
        }

        if (config && config.columnsGroup.length > 0) {
          this.showColumnPages = false
          for (let i = 0, len = config.columnsGroup.length; i < len; i++) {
            let group = []
            if (!config.columnsGroup[i]) {
              continue
            }
            for (let j = 0, size = config.columnsGroup[i].length; j < size; j++) {
              const iCol = this.vucTable.columns.findIndex(item => ((item.field && item.field === config.columnsGroup[i][j].field) || item.title === config.columnsGroup[i][j].title) && item.visible !== false)
              if (iCol >= 0) {
                group.push(this.vucTable.columns[iCol])
              }
            }
            if (group && group.length > 0) {
              this.$set(this.columnsGroup, i, group)
            }
          }
          setTimeout(() => {
            this.showColumnPages = true
          }, 100)
        } else {
          let printColumns = this.vucTable.columns.filter(item => item.visible !== false && item.type !== 'selection' && item.type !== 'tools')
          if (!this.printRowIndex) {
            // 不打印序号列
            printColumns = printColumns.filter(item => item.type !== 'index')
          }
          this.$set(this.columnsGroup, 0, printColumns)
        }
        if (config && config.columnsDelete.length > 0) {
          this.$set(this, 'columnsDelete', config.columnsDelete)
        }
      }, 300)
    },
    handleEnd() {
      // console.log(this.columnsGroup)
    },
    doPrint() {
      this.showMask = true
      this.$refs.easyPrint.print()
      this.$emit('saveConfig', { printConfig: this.printConfig, columnsGroup: this.columnsGroup, columnsDelete: this.columnsDelete })
    },
    handlePrintEnd() {
      this.showMask = false
      this.$emit('close')
    }
  },
  mounted() {
    // console.log(this.printTitle)
  }
}
</script>
<style lang="less" scoped>
.action-group {
  position: absolute;
  right: 32px;
  bottom: 16px;
}
.ivu-form-item {
  margin-bottom: 8px;
}
.number {
  width: 80px;
  min-width: 80px;
}
.print-mask {
  position: absolute;
  left: 0;
  top: 0;
  right: 0;
  bottom: 0;
  background: rgba(255, 255, 255, 0.5);
  &::after {
    content: '准备打印，请等待...';
    position: absolute;
    left: 45%;
    top: 50%;
  }
}
</style>
