<template>
  <div class="mod-config">
    <el-row>
      <el-col :span="24">
        <el-button @click="getDataList()" type="primary">查询</el-button>
        <download-excel
          :fields="fields()"
          :data="exportData"
          type="xls"
          :name="exportName"
          :header="exportHeader"
          :footer="exportFooter"
          :defaultValue="exportDefaultValue"
          :fetch="createExportData"
          :before-generate="startDownload"
          :before-finish="finishDownload"
          worksheet="导出信息"
          class="el-button el-button--primary el-button--medium">
          导出
        </download-excel>
      </el-col>
    </el-row>
    <el-row>
      <el-col :span="24">
        <el-form :inline="true" label-position="top">
          <el-form-item :label="'工具编码'">
            <el-input style="width: 100px" v-model="queryTool.toolId" ></el-input>
          </el-form-item>
          <el-form-item :label="'工具实例编码'">
            <el-input style="width: 100px" v-model="queryTool.toolInstanceId" ></el-input>
          </el-form-item>
          <el-form-item :label="'工具名称'">
            <el-input style="width: 100px" v-model="queryTool.toolDescription"></el-input>
          </el-form-item>
          <el-form-item>
            <span slot="label" @click="getBaseList(3,1)"><el-link>供应商编码</el-link> </span>
            <el-input style="width: 100px" v-model="queryTool.supplierId"></el-input>
          </el-form-item>
          <el-form-item :label="'接收日期'">
            <el-date-picker style="width: 120px" v-model="queryTool.startDate" value-format='yyyy-MM-dd 00:00:00' format = 'yyyy-MM-dd'
            ></el-date-picker> -
            <el-date-picker style="width: 120px" v-model="queryTool.endDate" value-format='yyyy-MM-dd 23:59:59' format = 'yyyy-MM-dd'
            ></el-date-picker>
          </el-form-item>
          <el-form-item :label="'使用比例'">
            <el-input type="number" style="width: 50px" v-model="queryTool.startProp"></el-input> -
            <el-input type="number" style="width: 50px" v-model="queryTool.endProp"></el-input>
          </el-form-item>
        </el-form>
      </el-col>
    </el-row>
    <el-row>
      <el-col :span="24">
        <el-table
          :height="height"
          :data="dataList"
          border
          v-loading="dataListLoading"
          highlight-current-row>
          <el-table-column
            fixed="left"
            header-align="center"
            align="left"
            width="100"
            :label="'操作'">
            <template slot-scope="scope">
              <el-link @click="printMethod(scope.row,'Y')">工具标签</el-link>
              <el-link @click="printMethod(scope.row,'N')">| 网版</el-link>
            </template>
          </el-table-column>
          <el-table-column
            v-for="(item,index) in columnList" :key="index"
            :sortable="item.columnSortable"
            :prop="item.columnProp"
            :header-align="item.headerAlign"
            :show-overflow-tooltip="item.showOverflowTooltip"
            :align="item.align"
            :fixed="item.fixed==''?false:item.fixed"
            :min-width="item.columnWidth"
            :label="item.columnLabel">
            <template slot-scope="scope">
              <span v-if="!item.columnHidden">{{ scope.row[item.columnProp] }}</span>
              <span v-if="item.columnImage"><img :src="scope.row[item.columnProp]" style="width: 100px; height: 80px"/></span>
            </template>
          </el-table-column>
        </el-table>
        <el-pagination
          @size-change="sizeChangeHandle"
          @current-change="currentChangeHandle"
          :current-page="pageIndex"
          :page-sizes="[100, 200, 500]"
          :page-size="pageSize"
          :total="totalPage"
          layout="total, sizes, prev, pager, next, jumper">
        </el-pagination>
      </el-col>
    </el-row>
    <Chooselist ref="baseList" @getBaseData="getBaseData"></Chooselist>
  </div>
</template>

<script>
import getLodop from '@/utils/LodopFuncs.js'
import column from '../common/column'
import Chooselist from '@/views/modules/common/Chooselist'
import {getToolInfoList} from '@/api/knifemold/tool-info.js'
import {printToolData} from '@/api/knifemold/receive.js'
var functionId = '106001005'
export default {
  components: {column, Chooselist},
  data () {
    return {
      buttons: {
        search: '查询',
        download: '导出'
      },
      queryButton: {
        functionId: functionId,
        table_id: '*',
        languageCode: this.$i18n.locale,
        objectType: 'button'
      },
      queryLabel: {
        functionId: functionId,
        table_id: '*',
        languageCode: this.$i18n.locale,
        objectType: 'label'
      },
      height: 0,
      // 组件显示控制
      visible: false,
      showDefault: this.$store.state.user.userDev,
      // 数据查询属性
      tagNo: '',
      dataListLoading: false,
      queryTool: {
        limit: 500,
        page: 1,
        supplierId: '',
        toolId: '',
        toolInstanceId: '',
        locationId: '',
        toolDescription: '',
        startDate: '',
        endDate: '',
        startProp: null,
        endProp: null
      },
      pageIndex: 1,
      pageSize: 100,
      totalPage: 0,
      // 数据集
      dataList: [],
      // ------------ 页面属性 ----------
      // 默认table 查询参数
      queryTable: {
        functionId: this.$route.meta.menuId,
        tableId: 'toolInfo',
        languageCode: this.$i18n.locale
      },
      // 用户table 查询参数
      queryTableUser: {
        userId: this.$store.state.user.name,
        functionId: this.$route.meta.menuId,
        tableId: 'toolInfo',
        status: true,
        languageCode: this.$i18n.locale
      },
      // 展示列集
      columnList: [
        {
          userId: this.$store.state.user.name,
          serialNumber: 'toolDetail1101ToolId',
          tableId: 'toolOIRecord',
          tableName: 'common',
          columnProp: 'toolId',
          headerAlign: 'center',
          align: 'left',
          columnLabel: '工具编码',
          columnHidden: false,
          columnImage: false,
          columnSortable: false,
          sortLv: 0,
          status: true,
          fixed: 'left',
          columnWidth: 140
        },
        {
          userId: this.$store.state.user.name,
          serialNumber: 'toolDetail1101ToolInstanceId',
          tableId: 'toolOIRecord',
          tableName: 'common',
          columnProp: 'toolInstanceId',
          headerAlign: 'center',
          align: 'left',
          columnLabel: '工具实例编码',
          columnHidden: false,
          columnImage: false,
          columnSortable: false,
          sortLv: 0,
          status: true,
          fixed: '',
          columnWidth: 140
        },
        {
          userId: this.$store.state.user.name,
          serialNumber: 'toolOIRecordtype',
          tableId: 'toolOIRecord',
          tableName: 'common',
          columnProp: 'toolDescription',
          headerAlign: 'center',
          align: 'left',
          columnLabel: '工具名称',
          columnHidden: false,
          columnImage: false,
          columnSortable: false,
          sortLv: 0,
          status: true,
          fixed: '',
          columnWidth: 200
        },
        {
          userId: this.$store.state.user.name,
          serialNumber: 'toolOIRecordCreateDate',
          tableId: 'toolOIRecord',
          tableName: 'common',
          columnProp: 'createDate',
          headerAlign: 'center',
          align: 'left',
          columnLabel: '接收日期',
          columnHidden: false,
          columnImage: false,
          columnSortable: false,
          sortLv: 0,
          status: true,
          fixed: '',
          columnWidth: 120
        },
        {
          userId: this.$store.state.user.name,
          serialNumber: 'toolOIRecordStatus',
          tableId: 'toolOIRecord',
          tableName: 'common',
          columnProp: 'status',
          headerAlign: 'center',
          align: 'left',
          columnLabel: '状态',
          columnHidden: false,
          columnImage: false,
          columnSortable: false,
          sortLv: 0,
          status: true,
          fixed: '',
          columnWidth: 100
        },
        {
          userId: this.$store.state.user.name,
          serialNumber: 'toolOIRecordCurrLocationId',
          tableId: 'toolOIRecord',
          tableName: 'common',
          columnProp: 'currLocationId',
          headerAlign: 'center',
          align: 'left',
          columnLabel: '库位编码',
          columnHidden: false,
          columnImage: false,
          columnSortable: false,
          sortLv: 0,
          status: true,
          fixed: '',
          columnWidth: 120
        },
        {
          userId: this.$store.state.user.name,
          serialNumber: 'toolOIRecordCurrLocationId',
          tableId: 'toolOIRecord',
          tableName: 'common',
          columnProp: 'locationName',
          headerAlign: 'center',
          align: 'left',
          columnLabel: '库位名称',
          columnHidden: false,
          columnImage: false,
          columnSortable: false,
          sortLv: 0,
          status: true,
          fixed: '',
          columnWidth: 160
        },
        {
          userId: this.$store.state.user.name,
          serialNumber: 'toolOIRecordActualuseQty',
          tableId: 'toolOIRecord',
          tableName: 'common',
          columnProp: 'actualuseQty',
          headerAlign: 'center',
          align: 'right',
          columnLabel: '已使用寿命',
          columnHidden: false,
          columnImage: false,
          columnSortable: false,
          sortLv: 0,
          status: true,
          fixed: '',
          columnWidth: 100
        },
        {
          userId: this.$store.state.user.name,
          serialNumber: 'toolOIRecordEstuseQty',
          tableId: 'toolOIRecord',
          tableName: 'common',
          columnProp: 'estuseQty',
          headerAlign: 'center',
          align: 'right',
          columnLabel: '预计使用寿命',
          columnHidden: false,
          columnImage: false,
          columnSortable: false,
          sortLv: 0,
          status: true,
          fixed: '',
          columnWidth: 100
        },
        {
          userId: this.$store.state.user.name,
          serialNumber: 'toolOIRecordProp',
          tableId: 'toolOIRecord',
          tableName: 'common',
          columnProp: 'prop',
          headerAlign: 'center',
          align: 'right',
          columnLabel: '使用比例',
          columnHidden: false,
          columnImage: false,
          columnSortable: false,
          sortLv: 0,
          status: true,
          fixed: '',
          columnWidth: 100
        }
      ],
      // 用户table 配置集合
      userColumnList: [],
      // ------------ 页面属性 end ----------
      // 导出 start
      exportData: [],
      exportName: this.$route.meta.title,
      exportHeader: [this.$route.meta.title],
      exportFooter: [],
      exportDefaultValue: ''
    }
  },
  activated () {
  },
  mounted () {
    this.$nextTick(() => {
      this.height = window.innerHeight - 220
    })
  },
  methods: {
    printMethod (row, printType) {
      let dto = {
        site: this.$store.state.user.site,
        toolInstanceId: row.toolInstanceId
      }
      printToolData(dto).then(({data}) => {
        if (data.code === 0) {
          let printDataList = []
          let printData = {
            SupplierName: data.data.SupplierName,
            createDate: data.data.createDate,
            createdBy: data.data.createdBy,
            estUseQty: data.data.estUseQty,
            locationId: data.data.locationId,
            phaseInDate: data.data.phaseInDate,
            site: data.data.site,
            spec: data.data.spec,
            status: data.data.status,
            supplierId: data.data.supplierId,
            toolDescription: data.data.toolDescription,
            toolId: data.data.toolId,
            toolInstanceId: row.toolInstanceId
          }
          printDataList.push(printData)
          if (printType === 'Y') {
            this.labelPrintGJ(printDataList)
          } else {
            this.labelPrintWB(printDataList)
          }
        }
      })
    },
    labelPrintWB (printDataList) {
      const LODOP = getLodop()
      if (LODOP) {
        // 循环调用打印机
        for (let i = 0; i < printDataList.length; i++) {
          let printData = printDataList[i]
          LODOP.NewPage()
          LODOP.SET_PRINT_PAGESIZE(0, 700, 180, '')
          LODOP.SET_PRINT_MODE('PRINT_NOCOLLATE', 1)
          LODOP.ADD_PRINT_RECT(2, 5, 250, 67, 0, 1)
          LODOP.ADD_PRINT_LINE(19, 5, 18, 255, 0, 1)
          LODOP.ADD_PRINT_LINE(36, 5, 35, 205, 0, 1)
          LODOP.ADD_PRINT_LINE(54, 5, 53, 205, 0, 1)
          LODOP.ADD_PRINT_LINE(2, 65, 69, 66, 0, 1)
          LODOP.ADD_PRINT_LINE(68, 205, 18, 206, 0, 1)
          LODOP.ADD_PRINT_BARCODE(21, 208, 90, 60, 'QRCode', printData.toolInstanceId)
          LODOP.ADD_PRINT_TEXT(5, 9, 60, 14, '工具名称')
          LODOP.ADD_PRINT_TEXT(21, 9, 60, 15, '工具编号')
          LODOP.ADD_PRINT_TEXT(38, 9, 60, 15, '实例编号')
          LODOP.ADD_PRINT_TEXT(55, 9, 60, 15, '经办人')
          LODOP.ADD_PRINT_TEXT(4, 70, 180, 14, printData.toolDescription)
          LODOP.SET_PRINT_STYLEA(0, 'Alignment', 2)
          LODOP.ADD_PRINT_TEXT(21, 71, 133, 14, printData.toolId)
          LODOP.ADD_PRINT_TEXT(39, 71, 133, 14, printData.toolInstanceId)
          LODOP.ADD_PRINT_TEXT(55, 71, 133, 14, printData.createdBy)
        }
        LODOP.PREVIEW()
      }
    },
    labelPrintGJ (printDataList) {
      const LODOP = getLodop()
      if (LODOP) {
        // 循环调用打印机
        for (let i = 0; i < printDataList.length; i++) {
          let printData = printDataList[i]
          LODOP.NewPage()
          LODOP.SET_PRINT_PAGESIZE(0, 700, 540, '')
          LODOP.ADD_PRINT_RECT(6, 5, 250, 190, 0, 1)
          LODOP.ADD_PRINT_LINE(24, 5, 23, 255, 0, 1)
          LODOP.ADD_PRINT_LINE(41, 5, 40, 255, 0, 1)
          LODOP.ADD_PRINT_LINE(58, 5, 57, 255, 0, 1)
          LODOP.ADD_PRINT_LINE(75, 5, 74, 255, 0, 1)
          LODOP.ADD_PRINT_LINE(92, 5, 91, 255, 0, 1)
          LODOP.ADD_PRINT_LINE(109, 5, 108, 255, 0, 1)
          LODOP.ADD_PRINT_LINE(126, 5, 125, 178, 0, 1)
          LODOP.ADD_PRINT_LINE(144, 5, 143, 178, 0, 1)
          LODOP.ADD_PRINT_LINE(162, 5, 161, 178, 0, 1)
          LODOP.ADD_PRINT_LINE(179, 5, 178, 255, 0, 1)
          LODOP.ADD_PRINT_LINE(23, 65, 195, 66, 0, 1)
          LODOP.ADD_PRINT_LINE(178, 177, 108, 178, 0, 1)
          LODOP.ADD_PRINT_BARCODE(110, 183, 110, 85, 'QRCode', printData.toolInstanceId)
          LODOP.ADD_PRINT_TEXT(9, 94, 100, 14, '工具标签')
          LODOP.SET_PRINT_STYLEA(0, 'Bold', 1)
          LODOP.ADD_PRINT_TEXT(26, 9, 60, 14, '工具名称')
          LODOP.ADD_PRINT_TEXT(43, 9, 60, 15, '规格型号')
          LODOP.ADD_PRINT_TEXT(60, 9, 60, 15, '工具编号')
          LODOP.ADD_PRINT_TEXT(77, 9, 60, 15, '实例编号')
          LODOP.ADD_PRINT_TEXT(94, 9, 60, 15, '厂商')
          LODOP.ADD_PRINT_TEXT(111, 9, 60, 15, '入库日期')
          LODOP.ADD_PRINT_TEXT(129, 9, 60, 15, '库位')
          LODOP.ADD_PRINT_TEXT(147, 9, 60, 15, '经办人')
          LODOP.ADD_PRINT_TEXT(181, 9, 60, 15, '检验签章')
          LODOP.ADD_PRINT_TEXT(25, 72, 180, 14, printData.toolDescription)
          LODOP.SET_PRINT_STYLEA(0, 'Alignment', 2)
          LODOP.ADD_PRINT_TEXT(42, 72, 180, 14, printData.spec) // 规格
          LODOP.ADD_PRINT_TEXT(60, 72, 180, 14, printData.toolId)
          LODOP.ADD_PRINT_TEXT(78, 71, 180, 14, printData.toolInstanceId)
          LODOP.ADD_PRINT_TEXT(94, 71, 180, 14, printData.SupplierName)
          LODOP.ADD_PRINT_TEXT(111, 71, 95, 14, this.dayjs(printData.createDate).format('YYYY-MM-DD'))
          LODOP.ADD_PRINT_TEXT(130, 71, 95, 14, printData.locationId)
          LODOP.ADD_PRINT_TEXT(148, 71, 95, 14, printData.createdBy)
        }
        LODOP.PREVIEW()
      }
    },
    // ---------------  业务  -------------------
    getDataList () {
      this.dataListLoading = true
      this.queryTool.limit = this.pageSize
      this.queryTool.page = this.pageIndex
      getToolInfoList(this.queryTool).then(({data}) => {
        if (data.code === 0) {
          this.dataList = data.data.list
          this.pageIndex = data.data.currPage
          this.pageSize = data.data.pageSize
          this.totalPage = data.data.totalCount
        }
        this.dataListLoading = false
      })
    },
    getBaseData (val) {
      if (this.tagNo === 3) {
        this.queryTool.supplierId = val.SupplierID
      }
    },
    // 获取基础数据列表
    getBaseList (val, number) {
      this.tagNo = val
      this.$nextTick(() => {
        let strVal = ''
        if (val === 3) {
          strVal = this.queryTool.supplierId
        }
        this.$refs.baseList.init(val, strVal)
      })
    },

    //  --------- 分页 ----------
    // 每页数
    sizeChangeHandle (val) {
      this.pageSize = val
      this.pageIndex = 1
      this.getDataList()
    },
    // 当前页
    currentChangeHandle (val) {
      this.pageIndex = val
      this.getDataList()
    },
    //  --------- 分页 end ----------
    //  --------- 导出 ----------
    fields () {
      let json = "{"
      this.columnList.forEach((item, index) => {
        if (index == this.columnList.length - 1) {
          json += "\"" + item.columnLabel + "\"" + ":" + "\"" + item.columnProp + "\""
        } else {
          json += "\"" + item.columnLabel + "\"" + ":" + "\"" + item.columnProp + "\"" + ","
        }
      })
      json += "}"
      let s = eval("(" + json + ")")
      return s
    },
    createExportData () {
      // 点击导出按钮之后,开始导出数据之前的执行函数,返回值为需要下载的数据
      return this.dataList
    },
    startDownload () {
    },
    finishDownload () {
    }
    //  --------- 导出 end ----------
  },
  created () {
    this.getTableUserColumn()
  }
}
</script>
<style>

</style>
