<template>
  <div class="mod-config">
    <el-form :inline="true" label-position="top" label-width="100px" style="margin-top: -5px;">
      <el-form-item :label="'工厂编号：'">
        <el-input v-model="searchData.site" style="width: 130px"></el-input>
      </el-form-item>
      <el-form-item :label="'订单号：'">
        <el-input v-model="searchData.orderNo" style="width: 130px"></el-input>
      </el-form-item>
      <el-form-item :label="'类型：'">
        <el-select filterable v-model="searchData.type" style="width: 130px">
          <el-option label="全部" value=""></el-option>
          <el-option label="采购订单" value="采购入库"></el-option>
          <el-option label="分卷订单" value="分切入库"></el-option>
          <el-option label="期初入库" value="期初入库"></el-option>
        </el-select>
        <el-button @click="getData()" type="primary">查询</el-button>
        <el-button @click="getSaveModel()" type="primary" style="margin-left: 0px;">供应商批号录入</el-button>
        <download-excel
          :fields="fields()"
          :data="exportData"
          type="xls"
          :name="exportName"
          :header="exportHeader"
          :footer="exportFooter"
          :fetch="createExportData"
          :before-generate="startDownload"
          :before-finish="finishDownload"
          worksheet="导出信息"
          class="el-button el-button--primary el-button--medium">
          {{ buttons.download }}
        </download-excel>
        <download-excel
          :fields="fields2()"
          :data="exportData2"
          type="xls"
          :name="exportName2"
          :header="exportHeader2"
          :footer="exportFooter2"
          :fetch="createExportData2"
          :before-generate="startDownload2"
          :before-finish="finishDownload2"
          worksheet="导出信息"
          class="el-button el-button--primary el-button--medium">
          {{ buttons.download2 }}
        </download-excel>
      </el-form-item>
    </el-form>
    <el-table
      :height="300"
      :data="dataList"
      border
      @row-click="changeData"
      highlight-current-row
      v-loading="dataListLoading"
      style="width: 100%;">
      <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"
        :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-table
      :height="height"
      :data="dataList2"
      border
      v-loading="dataListLoading"
      style="width: 100%;">
      <el-table-column
        header-align="center"
        align="center"
        width="120"
        label="操作">
        <template slot-scope="scope">
          <a type="text" size="small" @click="deleteRoll(scope.row)">删除卷</a>
          <a type="text" size="small" @click="printRollLabel(scope.row)">打印卷标签</a>
        </template>
      </el-table-column>
      <el-table-column
        v-for="(item,index) in columnList2" :key="index"
        :sortable="item.columnSortable"
        :prop="item.columnProp"
        :header-align="item.headerAlign"
        :show-overflow-tooltip="item.showOverflowTooltip"
        :align="item.align"
        :fixed="item.fixed"
        :mid-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-dialog width="430px" :title="'供应商批号录入'" :close-on-click-modal="false" v-drag :visible.sync="modelFlag2">
      <el-form :inline="true" label-position="top" label-width="100px" style="margin-top: 0px;">
        <el-form-item :label="'工厂编码：'">
          <el-input v-model="saveData.site" style="width: 80px"></el-input>
        </el-form-item>
        <el-form-item :label="'卷号：'">
          <el-input v-model="saveData.rollNo" style="width: 150px"></el-input>
          <el-button @click="goSaveSuppRollNo()" type="primary" style="margin-left: 10px;">保存</el-button>
          <el-button @click="modelFlag2=false" type="primary" style="margin-left: 10px;">关闭</el-button>
        </el-form-item>
      </el-form>
      <el-form :inline="true" label-position="top" label-width="100px" style="margin-top: 0px;">
        <el-form-item :label="'供应商批号：'">
          <el-input v-model="saveData.suppRollNo" style="width: 369px"></el-input>
        </el-form-item>
      </el-form>
      <el-footer style="height:10px;margin-top: 20px;text-align:center">
      </el-footer>
    </el-dialog>
  </div>
</template>

<script>
  import {
    searchOrderRoll,
    searchCRollInfoDetail,
    saveSuppRollNo,
    doSaveSuppRollNo,
    deleteRoll,
    getPrintCRollInfo,/*2022-04-26 LR 打印卷标签请求*/
  } from "@/api/purchasingManagement/materialSplit.js"
  /*打印标签专用的js*/
  import {
    printMaterialLabel,
  } from "@/views/modules/yieldReport/print_roll_label.js"

  export default {
    //查询-订单卷信息
    name: "searchOrderRoll",
    data() {
      return {
        // 导出 start
        exportData: [],
        exportName: "订单卷信息主表"+this.dayjs().format('YYYYMMDDHHmmss'),
        exportHeader: ["订单卷信息主表"],
        exportFooter: [],
        // 导出 end
        // 导出 start
        exportData2: [],
        exportName2: "订单卷信息明细表"+this.dayjs().format('YYYYMMDDHHmmss'),
        exportHeader2: ["订单卷信息明细表"],
        exportFooter2: [],
        // 导出 end
        currentRow: {},
        height: 200,
        modelFlag:false,
        modelFlag2: false,
        modelInputFlag:true,
        selectList:[],
        searchData: {
          site:'',
          orderNo:'',
          type:'',
          user:this.$store.state.user.name
        },
        dataList:[],
        dataList2:[],
        dataListLoading: false,
        buttons:{
          download:'导出上表',
          download2:'导出下表',
        },
        saveData: {
          site:'',
          rollNo:'',
          suppRollNo:'',
        },
        columnList: [
          {
            userId: this.$store.state.user.name,
            functionId: 6035,
            serialNumber: '6035TableOrderNo',
            tableId: "6035Table",
            tableName: "查询订单卷信息主表",
            columnProp: "orderNo",
            headerAlign: "center",
            align: "left",
            columnLabel: "订单号",
            columnHidden: false,
            columnImage: false,
            columnSortable: false,
            sortLv: 0,
            status: true,
            fixed: false,
            columnWidth: 80
          },
          {
            userId: this.$store.state.user.name,
            functionId: 6035,
            serialNumber: '6035TableOrderDate',
            tableId: "6035Table",
            tableName: "查询订单卷信息主表",
            columnProp: "orderDate",
            headerAlign: "center",
            align: "left",
            columnLabel: "订单日期",
            columnHidden: false,
            columnImage: false,
            columnSortable: false,
            sortLv: 0,
            status: true,
            fixed: false,
            columnWidth: 100
          },
          {
            userId: this.$store.state.user.name,
            functionId: 6035,
            serialNumber: '6035TablePlanReceiptDate',
            tableId: "6035Table",
            tableName: "查询订单卷信息主表",
            columnProp: "planReceiptDate",
            headerAlign: "center",
            align: "left",
            columnLabel: "要求交货日期",
            columnHidden: false,
            columnImage: false,
            columnSortable: false,
            sortLv: 0,
            status: true,
            fixed: false,
            columnWidth: 100
          },
          {
            userId: this.$store.state.user.name,
            functionId: 6035,
            serialNumber: '6035TableSupplierID',
            tableId: "6035Table",
            tableName: "查询订单卷信息主表",
            columnProp: "supplierID",
            headerAlign: "center",
            align: "left",
            columnLabel: "供应商编码",
            columnHidden: false,
            columnImage: false,
            columnSortable: false,
            sortLv: 0,
            status: true,
            fixed: false,
            columnWidth: 100
          },
          {
            userId: this.$store.state.user.name,
            functionId: 6035,
            serialNumber: '6035TableStatus',
            tableId: "6035Table",
            tableName: "查询订单卷信息主表",
            columnProp: "status",
            headerAlign: "center",
            align: "left",
            columnLabel: "订单状态",
            columnHidden: false,
            columnImage: false,
            columnSortable: false,
            sortLv: 0,
            status: true,
            fixed: false,
            columnWidth: 80
          },
          {
            userId: this.$store.state.user.name,
            functionId: 6035,
            serialNumber: '6035TableSupplierName',
            tableId: "6035Table",
            tableName: "查询订单卷信息主表",
            columnProp: "supplierName",
            headerAlign: "center",
            align: "left",
            columnLabel: "供应商名称",
            columnHidden: false,
            columnImage: false,
            columnSortable: false,
            sortLv: 0,
            status: true,
            fixed: false,
            columnWidth: 180
          },
          {
            userId: this.$store.state.user.name,
            functionId: 6035,
            serialNumber: '6035TableOrderItemNo',
            tableId: "6035Table",
            tableName: "查询订单卷信息主表",
            columnProp: "orderItemNo",
            headerAlign: "center",
            align: "left",
            columnLabel: "订单序号",
            columnHidden: false,
            columnImage: false,
            columnSortable: false,
            sortLv: 0,
            status: true,
            fixed: false,
            columnWidth: 80
          },
          {
            userId: this.$store.state.user.name,
            functionId: 6035,
            serialNumber: '6035TablePartNo',
            tableId: "6035Table",
            tableName: "查询订单卷信息主表",
            columnProp: "partNo",
            headerAlign: "center",
            align: "left",
            columnLabel: "物料编码",
            columnHidden: false,
            columnImage: false,
            columnSortable: false,
            sortLv: 0,
            status: true,
            fixed: false,
            columnWidth: 120
          },
          {
            userId: this.$store.state.user.name,
            functionId: 6035,
            serialNumber: '6035TablePartDescription',
            tableId: "6035Table",
            tableName: "查询订单卷信息主表",
            columnProp: "partDescription",
            headerAlign: "center",
            align: "left",
            columnLabel: "物料名称",
            columnHidden: false,
            columnImage: false,
            columnSortable: false,
            sortLv: 0,
            status: true,
            fixed: false,
            columnWidth: 200
          },
          {
            userId: this.$store.state.user.name,
            functionId: 6035,
            serialNumber: '6035TableQuantity',
            tableId: "6035Table",
            tableName: "查询订单卷信息主表",
            columnProp: "quantity",
            headerAlign: "center",
            align: "left",
            columnLabel: "订单数量",
            columnHidden: false,
            columnImage: false,
            columnSortable: false,
            sortLv: 0,
            status: true,
            fixed: false,
            columnWidth: 80
          },
          {
            userId: this.$store.state.user.name,
            functionId: 6035,
            serialNumber: '6035TableCancelledFlag',
            tableId: "6035Table",
            tableName: "查询订单卷信息主表",
            columnProp: "cancelledFlag",
            headerAlign: "center",
            align: "left",
            columnLabel: "是否取消",
            columnHidden: false,
            columnImage: false,
            columnSortable: false,
            sortLv: 0,
            status: true,
            fixed: false,
            columnWidth: 80
          },
          {
            userId: this.$store.state.user.name,
            functionId: 6035,
            serialNumber: '6035TableSAPOrderNo',
            tableId: "6035Table",
            tableName: "查询订单卷信息主表",
            columnProp: "sAPOrderNo",
            headerAlign: "center",
            align: "left",
            columnLabel: "SAP订单识别号",
            columnHidden: false,
            columnImage: false,
            columnSortable: false,
            sortLv: 0,
            status: true,
            fixed: false,
            columnWidth: 120
          },
          {
            userId: this.$store.state.user.name,
            functionId: 6035,
            serialNumber: '6035TableSite',
            tableId: "6035Table",
            tableName: "查询订单卷信息主表",
            columnProp: "site",
            headerAlign: "center",
            align: "left",
            columnLabel: "工厂编号",
            columnHidden: false,
            columnImage: false,
            columnSortable: false,
            sortLv: 0,
            status: true,
            fixed: false,
            columnWidth: 80
          },
          {
            userId: this.$store.state.user.name,
            functionId: 6035,
            serialNumber: '6035TableCreatedDate',
            tableId: "6035Table",
            tableName: "查询订单卷信息主表",
            columnProp: "createdDate",
            headerAlign: "center",
            align: "left",
            columnLabel: "分卷日期",
            columnHidden: false,
            columnImage: false,
            columnSortable: false,
            sortLv: 0,
            status: true,
            fixed: false,
            columnWidth: 180
          },
          {
            userId: this.$store.state.user.name,
            functionId: 6035,
            serialNumber: '6035TableCreatedBy',
            tableId: "6035Table",
            tableName: "查询订单卷信息主表",
            columnProp: "createdBy",
            headerAlign: "center",
            align: "left",
            columnLabel: "分卷人",
            columnHidden: false,
            columnImage: false,
            columnSortable: false,
            sortLv: 0,
            status: true,
            fixed: false,
            columnWidth: 100
          },
          {
            userId: this.$store.state.user.name,
            functionId: 6035,
            serialNumber: '6035TableType',
            tableId: "6035Table",
            tableName: "查询订单卷信息主表",
            columnProp: "type",
            headerAlign: "center",
            align: "left",
            columnLabel: "类型",
            columnHidden: false,
            columnImage: false,
            columnSortable: false,
            sortLv: 0,
            status: true,
            fixed: false,
            columnWidth: 100
          }
        ],
        columnList2: [
          {
            userId: this.$store.state.user.name,
            functionId: 6035,
            serialNumber: '6035Table2RollNo',
            tableId: "6035Table",
            tableName: "查询订单卷信息明细表",
            columnProp: "rollNo",
            headerAlign: "center",
            align: "left",
            columnLabel: "卷号",
            columnHidden: false,
            columnImage: false,
            columnSortable: false,
            sortLv: 0,
            status: true,
            fixed: false,
            columnWidth: 150
          },
          {
            userId: this.$store.state.user.name,
            functionId: 6035,
            serialNumber: '6035Table2RollQty',
            tableId: "6035Table",
            tableName: "查询订单卷信息明细表",
            columnProp: "rollQty",
            headerAlign: "center",
            align: "right",
            columnLabel: "卷数量",
            columnHidden: false,
            columnImage: false,
            columnSortable: false,
            sortLv: 0,
            status: true,
            fixed: false,
            columnWidth: 60
          },
          {
            userId: this.$store.state.user.name,
            functionId: 6035,
            serialNumber: '6035Table2SuppDeliveryNote',
            tableId: "6035Table",
            tableName: "查询订单卷信息明细表",
            columnProp: "suppDeliveryNote",
            headerAlign: "center",
            align: "left",
            columnLabel: "供应商送货单号",
            columnHidden: false,
            columnImage: false,
            columnSortable: false,
            sortLv: 0,
            status: true,
            fixed: false,
            columnWidth: 100
          },
          {
            userId: this.$store.state.user.name,
            functionId: 6035,
            serialNumber: '6035Table2RollDate',
            tableId: "6035Table",
            tableName: "查询订单卷信息明细表",
            columnProp: "rollDate",
            headerAlign: "center",
            align: "left",
            columnLabel: "入库日期",
            columnHidden: false,
            columnImage: false,
            columnSortable: false,
            sortLv: 0,
            status: true,
            fixed: false,
            columnWidth: 120
          },
          {
            userId: this.$store.state.user.name,
            functionId: 6035,
            serialNumber: '6035Table2ExpiredDate',
            tableId: "6035Table",
            tableName: "查询订单卷信息明细表",
            columnProp: "expiredDate",
            headerAlign: "center",
            align: "left",
            columnLabel: "失效日期",
            columnHidden: false,
            columnImage: false,
            columnSortable: false,
            sortLv: 0,
            status: true,
            fixed: false,
            columnWidth: 120
          },
          {
            userId: this.$store.state.user.name,
            functionId: 6035,
            serialNumber: '6035Table2CreatedDate',
            tableId: "6035Table",
            tableName: "查询订单卷信息明细表",
            columnProp: "createdDate",
            headerAlign: "center",
            align: "left",
            columnLabel: "分卷日期",
            columnHidden: false,
            columnImage: false,
            columnSortable: false,
            sortLv: 0,
            status: true,
            fixed: false,
            columnWidth: 120
          },
          {
            userId: this.$store.state.user.name,
            functionId: 6035,
            serialNumber: '6035Table2CreatedBy',
            tableId: "6035Table",
            tableName: "查询订单卷信息明细表",
            columnProp: "createdBy",
            headerAlign: "center",
            align: "left",
            columnLabel: "分卷人",
            columnHidden: false,
            columnImage: false,
            columnSortable: false,
            sortLv: 0,
            status: true,
            fixed: false,
            columnWidth: 60
          },
          {
            userId: this.$store.state.user.name,
            functionId: 6035,
            serialNumber: '6035Table2Status',
            tableId: "6035Table",
            tableName: "查询订单卷信息明细表",
            columnProp: "status",
            headerAlign: "center",
            align: "left",
            columnLabel: "卷状态",
            columnHidden: false,
            columnImage: false,
            columnSortable: false,
            sortLv: 0,
            status: true,
            fixed: false,
            columnWidth: 60
          },
          {
            userId: this.$store.state.user.name,
            functionId: 6035,
            serialNumber: '6035Table2SuppRollNoFlag',
            tableId: "6035Table",
            tableName: "查询订单卷信息明细表",
            columnProp: "suppRollNoFlag",
            headerAlign: "center",
            align: "left",
            columnLabel: "已录入供应商批号",
            columnHidden: false,
            columnImage: false,
            columnSortable: false,
            sortLv: 0,
            status: true,
            fixed: false,
            columnWidth: 120
          },
          {
            userId: this.$store.state.user.name,
            functionId: 6035,
            serialNumber: '6035Table2SuppRollNo',
            tableId: "6035Table",
            tableName: "查询订单卷信息明细表",
            columnProp: "suppRollNo",
            headerAlign: "center",
            align: "left",
            columnLabel: "供应商批号",
            columnHidden: false,
            columnImage: false,
            columnSortable: false,
            sortLv: 0,
            status: true,
            fixed: false,
            columnWidth: 300
          }
        ],
      }
    },
    mounted() {
      this.$nextTick(() => {
        this.height = window.innerHeight - 500;
      })
    },
    methods: {
      getData(){
        searchOrderRoll(this.searchData).then(({data}) => {
          this.dataList = data.rows
          let inData={
            site: data.rows[0].site,
            orderRef1: data.rows[0].orderNo,
            orderRef3: data.rows[0].orderItemNo,
          }
          searchCRollInfoDetail(inData).then(({data}) => {
            this.dataList2 = data.rows

          })
        })
      },
      //单击切换订单
      changeData(row) {
        this.currentRow = JSON.parse(JSON.stringify(row));
        let inData={
          site:row.site,
          orderRef1:row.orderNo,
          orderRef3:row.orderItemNo,
        }
        searchCRollInfoDetail(inData).then(({data}) => {
          this.dataList2 = data.rows

        })
      },
      //供应商批号录入
      getSaveModel() {
        this.saveData = {
          site:this.$store.state.user.site,
          rollNo:'',
          suppRollNo:''
        };
        this.modelFlag2 = true;
      },
      /*设置选中行的参数*/
      setCurrentRow(row, column, event) {
        this.currentRow = JSON.parse(JSON.stringify(row));
      },
      goSaveSuppRollNo() {
        if (this.saveData.site == '' || this.saveData.site == null) {
          this.$alert("请输入工厂编码！", '错误', {
            confirmButtonText: '确定'
          })
          return false;
        }
        if (this.saveData.rollNo == '' || this.saveData.rollNo == null) {
          this.$alert("请输入卷号！", '错误', {
            confirmButtonText: '确定'
          })
          return false;
        }
        if (this.saveData.suppRollNo == '' || this.saveData.suppRollNo == null) {
          this.$alert("请输入供应商批号！", '错误', {
            confirmButtonText: '确定'
          })
          return false;
        }
        saveSuppRollNo(this.saveData).then(({data}) => {
            if (data.rows == null) {
              this.$alert("该卷号不存在，请输入！", '错误', {
                confirmButtonText: '确定'
              })
              return false;
            } else {
              if (data.rows.suppRollNoFlag == 'Y') {
                this.$confirm(`该卷已经录入供应商批号,确认要重新录入吗？`, '提示', {
                  confirmButtonText: '确定',
                  cancelButtonText: '取消',
                  type: 'warning'

                }).then(() => {
                  doSaveSuppRollNo(this.saveData).then(({data}) => {
                    if (data && data.code == 200) {
                      this.modelFlag2 = false
                      this.$message({
                        message: '操作成功',
                        type: 'success',
                        duration: 1500,
                        onClose: () => {
                        }
                      })
                    } else {
                      this.$alert(data.msg, '错误', {
                        confirmButtonText: '确定'
                      })
                    }
                  })
                })
              }else{
                doSaveSuppRollNo(this.saveData).then(({data}) => {
                  if (data && data.code == 200) {
                    this.modelFlag2 = false
                    this.$message({
                      message: '操作成功',
                      type: 'success',
                      duration: 1500,
                      onClose: () => {
                      }
                    })
                  } else {
                    this.$alert(data.msg, '错误', {
                      confirmButtonText: '确定'
                    })
                  }
                })
              }
            }
          }
        )
      },
      deleteRoll(row) {
        this.$confirm(`确认要删除该卷？`, '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }).then(() => {
          deleteRoll(row).then(({data}) => {
            if (data && data.code == 200) {
              let inData={
                site:this.currentRow.site,
                orderRef1:this.currentRow.orderNo,
                orderRef3:this.currentRow.orderItemNo,
              }
              searchCRollInfoDetail(inData).then(({data}) => {
                this.dataList2 = data.rows

              })
             this.$message.success('操作成功')
            } else {
              this.$alert(data.msg, '错误', {
                confirmButtonText: '确定'
              })
            }
          })
        })
      },
      //导出excel
      createExportData() {

        return this.dataList;

      },
      startDownload() {
        //  this.exportData = this.dataList

      },
      finishDownload() {

      },
      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
      },
      // 导出 end
      //导出excel
      createExportData2() {

        return this.dataList2;

      },
      startDownload2() {
        //  this.exportData = this.dataList

      },
      finishDownload2() {

      },
      fields2() {
        let json = "{"
        this.columnList2.forEach((item, index) => {
          if (index == this.columnList2.length - 1) {
            json += "\"" + item.columnLabel + "\"" + ":" + "\"" + item.columnProp + "\""
          } else {
            json += "\"" + item.columnLabel + "\"" + ":" + "\"" + item.columnProp + "\"" + ","
          }
        })
        json += "}"
        let s = eval("(" + json + ")")

        return s
      },
      // 导出 end

      /*打印卷标签*/
      printRollLabel(currentRow){
        /*组装打印需要的数据*/
        let requestData = {site: currentRow.site, rollNo: currentRow.rollNo};
        getPrintCRollInfo(requestData).then(({data}) => {
          //打印材料卷标签
          let printList = data.rows;
          printMaterialLabel(printList);
        });
      },

    },
    created() {

    }
  }
</script>

<style >
  .el-textarea__inner {
    padding: 5px 5px;
  }
</style>
