<template>
  <div class="customer-css">
    <!--  查询时间和产品  -->
    <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: 85px"></el-input>
      </el-form-item>
      <el-form-item :label="'物料编码:'">
        <el-input v-model="searchData.partNo" style="width: 120px"></el-input>
      </el-form-item>
      <el-form-item >
        <span slot="label" style="" @click="getBaseList(88)"><a herf="#">机台编号:</a></span>
        <el-input v-model="searchData.resourceId" style="width: 110px"></el-input>
      </el-form-item>
      <el-form-item :label="'计划日期:'">
        <el-date-picker
          style="width: 120px"
          v-model="searchData.date1"
          type="date"
          value-format="yyyy-MM-dd"
          placeholder="选择日期">
        </el-date-picker>
      </el-form-item>
      <el-form-item :label="' '">
        <el-date-picker
          style="width: 120px"
          v-model="searchData.date2"
          type="date"
          value-format="yyyy-MM-dd"
          placeholder="选择日期">
        </el-date-picker>
      </el-form-item>
      <el-form-item :label="'生产订单号:'">
        <el-input v-model="searchData.orderNo" style="width: 120px" ></el-input>
      </el-form-item>
      <el-form-item label="客户名称">
        <el-input v-model="searchData.customerName" style="width: 120px"></el-input>
      </el-form-item>
      <el-form-item :label="'是否打印：'">
        <el-select filterable v-model="searchData.printFlag" style="width: 80px">
          <el-option label="是" value="Y"></el-option>
          <el-option label="否" value="N"></el-option>
          <el-option label="全部" value=""></el-option>
        </el-select>
      </el-form-item>
      <el-form-item :label="''" style="margin-left: -5px;">
        <el-checkbox style="margin-top: 20px;" true-label="Y" false-label="N" v-model="searchData.closedFlag">显示已关闭计划
        </el-checkbox>
      </el-form-item>
    </el-form>
    <el-form :inline="true" label-position="top" label-width="100px" style="margin-top: 5px;">
      <el-form-item  style="margin-left: -9px;">
        <el-button type="primary" @click="refreshTables()" class="customer-bun-mid"
                   style="margin-left: 10px;  margin-bottom: 5px;">查询
        </el-button>
        <download-excel
          :fields="fields()"
          :data="exportData"
          type="xls"
          :name="exportName"
          :footer="exportFooter"
          :fetch="createExportData"
          :before-generate="startDownload"
          :before-finish="finishDownload"
          worksheet="导出信息"
          class="el-button el-button--primary el-button--medium">
          导出
        </download-excel>
      </el-form-item>
      <el-form-item :label="'已关闭,已达数量'">
        <el-card class="customer-el-card customer-el-card-pink" style="margin-left:100px"></el-card>
      </el-form-item>
      <el-form-item :label="'已关闭,未达数量'">
        <el-card class="customer-el-card customer-el-card-orange" style="margin-left:100px"></el-card>
      </el-form-item>
      <el-form-item :label="'未关闭,未达数量'">
        <el-card class="customer-el-card customer-el-card-blue" style="margin-left:100px"></el-card>
      </el-form-item>
<!--      :row-class-name="routingRowClassName2"-->
      <div class="mainTable">
        <el-table :height="height"
                :data="dataList"
                border
                :row-class-name="routingRowClassName2"
                v-loading="dataListLoading"
                style="width: 100%;margin-top: -20px">
        <el-table-column
          prop=""
          header-align="center"
          align="center"
          width="70"
          label="操作">
          <template slot-scope="scope" class="foo_container">
            <a type="text" size="small" @click="printAction(scope.row)">打印标签</a>
<!--            <a type="text" size="small" @click="print(scope.row)">关闭</a>-->
          </template>
        </el-table-column>
        <el-table-column
          v-for="(item,index) in columnArray" :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-form :inline="true" label-position="top" label-width="100px" style="margin-top: 0px;">
          <span>合计：排产数量</span>
          <el-input v-model="sum1" readonly style="width: 60px"></el-input>
          <span>已完成数量</span>
          <el-input v-model="sum2" readonly style="width: 60px"></el-input>
          <span>已入库数量</span>
          <el-input v-model="sum3" readonly style="width: 60px"></el-input>
        </el-form>
      </div>
    </el-form>

    <el-dialog title="打印份数" :close-on-click-modal="false" v-drag :visible.sync="printModalFlag" width="200px">
      <el-form :inline="true" label-position="top" style="margin-left: 22px;margin-top: -5px;">
        <el-form-item :label="'打印份数：'">
          <el-input v-model="printQty"  style="width: 130px;"  onkeyup="value=value.replace(/^(0+)|[^\d]+/g,'')"></el-input>
        </el-form-item>
      </el-form>
      <el-footer style="height:40px;margin-top: 20px;text-align:center">
        <el-button type="primary" @click="doPrint()">开始打印</el-button>
        <el-button type="primary" @click="printModalFlag = false">关闭</el-button>
      </el-footer>
    </el-dialog>
    <Chooselist ref="baseList"  @getBaseData="getBaseData"></Chooselist>
  </div>
</template>

<script>
  import {
    printMaterialLabel,
  } from "@/views/modules/production/print_serialNo_label.js"
  import {
    getProduceScheduleList
  } from '@/api/production/dailyPlan.js'
  import {
    printSerialNo,
    getScheduleSerialNo,
  } from '@/api/production/pallet.js'
  import Chooselist from '@/views/modules/common/Chooselist'
  export default {
    name: 'search_schedule',
    components: {
      Chooselist
    },
    data () {
      return {
        sum1:'',
        sum2:'',
        sum3:'',
        printModalFlag:false,
        printQty:'',
        // 导出 start
        exportData: [],
        exportName: "排产日计划清单"+this.dayjs().format('YYYYMMDDHHmmss'),
        exportFooter: [],
        // 导出 end
        height:200,
        dataList:[],
        dataListLoading: false,
        columnArray: [
          {
            userId: this.$store.state.user.name,
            functionId: 5302,
            serialNumber: '5302ProduceScheduledDate',
            tableId: "5302Produce",
            tableName: "待生产工单",
            columnProp: "scheduledDate",
            headerAlign: "center",
            align: "left",
            columnLabel: "排产日期",
            columnWidth: 80,
            columnHidden: false,
            columnImage: false,
            columnSortable: true,
            sortLv: 0,
            status: true,
            fixed: false
          },
          {
            userId: this.$store.state.user.name,
            functionId: 5302,
            serialNumber: '5302ProduceItemDesc',
            tableId: "5302Produce",
            tableName: "待生产工单",
            columnProp: "printFlag",
            headerAlign: "center",
            align: "left",
            columnLabel: "是否打印",
            columnWidth: 60,
            columnHidden: false,
            columnImage: false,
            columnSortable: false,
            sortLv: 0,
            status: true,
            fixed: false
          },
          {
            userId: this.$store.state.user.name,
            functionId: 5302,
            serialNumber: '5302ProduceOrderNo',
            tableId: "5302Produce",
            tableName: "待生产工单",
            columnProp: "orderNo",
            headerAlign: "center",
            align: "left",
            columnLabel: "生产订单号",
            columnWidth: 120,
            columnHidden: false,
            columnImage: false,
            columnSortable: false,
            sortLv: 0,
            status: true,
            fixed: false
          },
          {
            userId: this.$store.state.user.name,
            functionId: 5302,
            serialNumber: '5302ProducePartNo',
            tableId: "5302Produce",
            tableName: "待生产工单",
            columnProp: "partNo",
            headerAlign: "center",
            align: "left",
            columnLabel: "产品编码",
            columnWidth: 110,
            columnHidden: false,
            columnImage: false,
            columnSortable: true,
            sortLv: 0,
            status: true,
            fixed: false
          },
          {
            userId: this.$store.state.user.name,
            functionId: 5302,
            serialNumber: '5302ProducePartDesc',
            tableId: "5302Produce",
            tableName: "待生产工单",
            columnProp: "partDesc",
            headerAlign: "center",
            align: "left",
            columnLabel: "产品名称",
            columnWidth: 180,
            columnHidden: false,
            columnImage: false,
            columnSortable: false,
            sortLv: 0,
            status: true,
            fixed: false
          },
          {
            userId: this.$store.state.user.name,
            functionId: 5302,
            serialNumber: '5302ProduceResourceId',
            tableId: "5302Produce",
            tableName: "待生产工单",
            columnProp: "resourceId",
            headerAlign: "center",
            align: "left",
            columnLabel: "机台",
            columnWidth: 60,
            columnHidden: false,
            columnImage: false,
            columnSortable: false,
            sortLv: 0,
            status: true,
            fixed: false
          },
          {
            userId: this.$store.state.user.name,
            functionId: 5302,
            serialNumber: '5302ProduceTimeRequired',
            tableId: "5302Produce",
            tableName: "待生产工单",
            columnProp: "timeRequired",
            headerAlign: "center",
            align: "right",
            columnLabel: "需求时间",
            columnWidth: 60,
            columnHidden: false,
            columnImage: false,
            columnSortable: false,
            sortLv: 0,
            status: true,
            fixed: false
          },
          {
            userId: this.$store.state.user.name,
            functionId: 5302,
            serialNumber: '5302ProduceQtyRequired',
            tableId: "5302Produce",
            tableName: "待生产工单",
            columnProp: "qtyRequired",
            headerAlign: "center",
            align: "right",
            columnLabel: "需求数量",
            columnWidth: 60,
            columnHidden: false,
            columnImage: false,
            columnSortable: false,
            sortLv: 0,
            status: true,
            fixed: false
          },
          {
            userId: this.$store.state.user.name,
            functionId: 5302,
            serialNumber: '5302ProduceQtyApprove',
            tableId: "5302Produce",
            tableName: "待生产工单",
            columnProp: "qtyApprove",
            headerAlign: "center",
            align: "right",
            columnLabel: "已完成数量",
            columnWidth: 70,
            columnHidden: false,
            columnImage: false,
            columnSortable: false,
            sortLv: 0,
            status: true,
            fixed: false
          },
          {
            userId: this.$store.state.user.name,
            functionId: 5302,
            serialNumber: '5302ProduceQtyReported',
            tableId: "5302Produce",
            tableName: "待生产工单",
            columnProp: "qtyReported",
            headerAlign: "center",
            align: "right",
            columnLabel: "已入库数量",
            columnWidth: 70,
            columnHidden: false,
            columnImage: false,
            columnSortable: false,
            sortLv: 0,
            status: true,
            fixed: false
          },
          {
            userId: this.$store.state.user.name,
            functionId: 5302,
            serialNumber: '5302ProduceClosedFlag',
            tableId: "5302Produce",
            tableName: "待生产工单",
            columnProp: "closedFlag",
            headerAlign: "center",
            align: "left",
            columnLabel: "已关闭",
            columnWidth: 50,
            columnHidden: false,
            columnImage: false,
            columnSortable: false,
            sortLv: 0,
            status: true,
            fixed: false
          },
          {
            userId: this.$store.state.user.name,
            functionId: 5302,
            serialNumber: '5302ProduceCustomerId',
            tableId: "5302Produce",
            tableName: "待生产工单",
            columnProp: "customerId",
            headerAlign: "center",
            align: "left",
            columnLabel: "客户编号",
            columnWidth: 100,
            columnHidden: false,
            columnImage: false,
            columnSortable: true,
            sortLv: 0,
            status: true,
            fixed: false
          },
          {
            userId: this.$store.state.user.name,
            functionId: 5302,
            serialNumber: '5302ProduceCustomerName',
            tableId: "5302Produce",
            tableName: "待生产工单",
            columnProp: "customerName",
            headerAlign: "center",
            align: "left",
            columnLabel: "客户名称",
            columnWidth: 180,
            columnHidden: false,
            columnImage: false,
            columnSortable: true,
            sortLv: 0,
            status: true,
            fixed: false
          },
          {
            userId: this.$store.state.user.name,
            functionId: 5302,
            serialNumber: '5302ProduceTechInfo',
            tableId: "5302Produce",
            tableName: "待生产工单",
            columnProp: "techInfo",
            headerAlign: "center",
            align: "left",
            columnLabel: "技术注意事项",
            columnWidth: 180,
            columnHidden: false,
            columnImage: false,
            columnSortable: false,
            sortLv: 0,
            status: true,
            fixed: false
          },
          {
            userId: this.$store.state.user.name,
            functionId: 5302,
            serialNumber: '5302ProduceItemNo',
            tableId: "5302Produce",
            tableName: "待生产工单",
            columnProp: "itemNo",
            headerAlign: "center",
            align: "right",
            columnLabel: "工序号",
            columnWidth: 50,
            columnHidden: false,
            columnImage: false,
            columnSortable: false,
            sortLv: 0,
            status: true,
            fixed: false
          },
          {
            userId: this.$store.state.user.name,
            functionId: 5302,
            serialNumber: '5302ProduceItemDesc',
            tableId: "5302Produce",
            tableName: "待生产工单",
            columnProp: "itemDesc",
            headerAlign: "center",
            align: "left",
            columnLabel: "工序描述",
            columnWidth: 80,
            columnHidden: false,
            columnImage: false,
            columnSortable: false,
            sortLv: 0,
            status: true,
            fixed: false
          },
          {
            userId: this.$store.state.user.name,
            functionId: 5302,
            serialNumber: '5302ProduceSpec',
            tableId: "5302Produce",
            tableName: "待生产工单",
            columnProp: "spec",
            headerAlign: "center",
            align: "left",
            columnLabel: "规格型号",
            columnWidth: 80,
            columnHidden: false,
            columnImage: false,
            columnSortable: false,
            sortLv: 0,
            status: true,
            fixed: false
          },
          {
            userId: this.$store.state.user.name,
            functionId: 5302,
            serialNumber: '5302ProduceScheduledSeqNo',
            tableId: "5302Produce",
            tableName: "待生产工单",
            columnProp: "seqNo",
            headerAlign: "center",
            align: "right",
            columnLabel: "日计划单号",
            columnWidth: 100,
            columnHidden: false,
            columnImage: false,
            columnSortable: true,
            sortLv: 0,
            status: true,
            fixed: false
          },
          {
            userId: this.$store.state.user.name,
            functionId: 5302,
            serialNumber: '5302ProduceScheduledSeqNo',
            tableId: "5302Produce",
            tableName: "待生产工单",
            columnProp: "scheduledSeqNo",
            headerAlign: "center",
            align: "right",
            columnLabel: "次序号",
            columnWidth: 80,
            columnHidden: false,
            columnImage: false,
            columnSortable: true,
            sortLv: 0,
            status: true,
            fixed: false
          },
          {
            userId: this.$store.state.user.name,
            functionId: 5302,
            serialNumber: '5302ProduceRemark',
            tableId: "5302Produce",
            tableName: "待生产工单",
            columnProp: "remark",
            headerAlign: "center",
            align: "left",
            columnLabel: "备注",
            columnWidth: 80,
            columnHidden: false,
            columnImage: false,
            columnSortable: false,
            sortLv: 0,
            status: true,
            fixed: false
          },

          {
            userId: this.$store.state.user.name,
            functionId: 5302,
            serialNumber: '5302ProduceShiftNo',
            tableId: "5302Produce",
            tableName: "待生产工单",
            columnProp: "shiftNo",
            headerAlign: "center",
            align: "left",
            columnLabel: "班次",
            columnWidth: 80,
            columnHidden: false,
            columnImage: false,
            columnSortable: false,
            sortLv: 0,
            status: true,
            fixed: false
          },

          {
            userId: this.$store.state.user.name,
            functionId: 5302,
            serialNumber: '5302ProduceSite',
            tableId: "5302Produce",
            tableName: "待生产工单",
            columnProp: "site",
            headerAlign: "center",
            align: "left",
            columnLabel: "工厂编号",
            columnWidth: 80,
            columnHidden: false,
            columnImage: false,
            columnSortable: false,
            sortLv: 0,
            status: true,
            fixed: false
          },

          // {
          //   userId: this.$store.state.user.name,
          //   functionId: 5302,
          //   serialNumber: '5302ProduceCustomerName',
          //   tableId: "5302Produce",
          //   tableName: "待生产工单",
          //   columnProp: "customerName",
          //   headerAlign: "center",
          //   align: "center",
          //   columnLabel: "客户名称",
          //   columnWidth: 80,
          //   columnHidden: false,
          //   columnImage: false,
          //   columnSortable: false,
          //   sortLv: 0,
          //   status: true,
          //   fixed: false
          // },
          // {
          //   userId: this.$store.state.user.name,
          //   functionId: 5302,
          //   serialNumber: '5302ProduceCustPartNo',
          //   tableId: "5302Produce",
          //   tableName: "待生产工单",
          //   columnProp: "custPartNo",
          //   headerAlign: "center",
          //   align: "center",
          //   columnLabel: "客户产品料号",
          //   columnWidth: 80,
          //   columnHidden: false,
          //   columnImage: false,
          //   columnSortable: false,
          //   sortLv: 0,
          //   status: true,
          //   fixed: false
          // },
          // {
          //   userId: this.$store.state.user.name,
          //   functionId: 5302,
          //   serialNumber: '5302ProduceQtyRequiredOriginal',
          //   tableId: "5302Produce",
          //   tableName: "待生产工单",
          //   columnProp: "qtyRequiredOriginal",
          //   headerAlign: "center",
          //   align: "center",
          //   columnLabel: "原始派工单需求数量",
          //   columnWidth: 80,
          //   columnHidden: false,
          //   columnImage: false,
          //   columnSortable: false,
          //   sortLv: 0,
          //   status: true,
          //   fixed: false
          // },
          {
            userId: this.$store.state.user.name,
            functionId: 5302,
            serialNumber: '5302ProduceClosedBy',
            tableId: "5302Produce",
            tableName: "待生产工单",
            columnProp: "closedBy",
            headerAlign: "center",
            align: "left",
            columnLabel: "关闭人",
            columnWidth: 80,
            columnHidden: false,
            columnImage: false,
            columnSortable: false,
            sortLv: 0,
            status: true,
            fixed: false
          },
          {
            userId: this.$store.state.user.name,
            functionId: 5302,
            serialNumber: '5302ProduceClosedDate',
            tableId: "5302Produce",
            tableName: "待生产工单",
            columnProp: "closedDate",
            headerAlign: "center",
            align: "left",
            columnLabel: "关闭日期",
            columnWidth: 80,
            columnHidden: false,
            columnImage: false,
            columnSortable: false,
            sortLv: 0,
            status: true,
            fixed: false
          },

        ],
        searchData:{
          printFlag:'',
          site:'',
          partNo:'',
          resourceId:'',
          date1:new Date(),
          date2:'',
          orderNo:'',
          closedFlag:'N',
          customerName:'',
        },
        rowData:'',
      }
    },
    mounted () {
      this.$nextTick(() => {
        this.height = window.innerHeight - 195
      })
    },
    methods: {
      getBaseData(val){
        if (this.tagNo === 88){
          this.searchData.resourceId = val.ResourceID
        }

      },
      // 获取基础数据列表
      getBaseList(val){
        this.tagNo = val
        this.$nextTick(() => {

          let strVal = "";
          if (val === 88){
            strVal =  this.searchData.resourceId
          }
          this.$refs.baseList.init(val,strVal)
        })
      },
      //导出excel
      createExportData () {

        return this.dataList;

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

      },
      finishDownload () {

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

        return s
      },
      // 导出 end
      refreshTables () {
        getProduceScheduleList(this.searchData).then(({data}) => {
          //设置查询数据
          this.dataList = data.rows;
          this.sum1=0;
          this.sum2=0;
          this.sum3=0;
          if(data.rows.length>0){
            for (let i = 0; i < data.rows.length; i++) {
              this.sum1=Number(this.sum1)+Number(data.rows[i].qtyRequired);
              this.sum2=Number(this.sum2)+Number(data.rows[i].qtyApprove);
              this.sum3=Number(this.sum3)+Number(data.rows[i].qtyReported);
            }

          }
        })
      },
      printAction (row) {
        this.rowData = row;
        this.printQty = 1;
        this.printModalFlag = true;
      },
      doPrint () {
        if (this.printQty == "" || this.printQty == 0 || this.printQty == null) {
          this.$alert('请输入大于0的打印份数!', '错误', {
            confirmButtonText: '确定'
          })
          return false;
        }
        if (this.printQty > 20) {
          this.$alert('打印份数过大请重新设置!', '错误', {
            confirmButtonText: '确定'
          })
          return false;
        }
        let array=[];
        let data = {
          site: this.rowData.site,
          orderNo: this.rowData.orderNo,
          itemNo: this.rowData.itemNo,
          scheduleDate: this.rowData.scheduledDate,
          shiftNo: this.rowData.shiftNo,
          seqNo: this.rowData.seqNo,
          qty: this.printQty,
          user: this.$store.state.user.name
        }
        // array.push(data);

        //   printSerialNo(array).then(({data}) => {
        //     if(data.code=="200"){
        //       this.$message({
        //         message: '打印成功',
        //         type: 'success',
        //         duration: 1500,
        //         onClose: () => {
        //         }
        //       })
        //     }else {
        //       this.$alert(data.msg, '错误', {
        //         confirmButtonText: '确定'
        //       })
        //     }
        //   })
        // }

        getScheduleSerialNo(data).then(({data}) => {
          if (data.code == 0) {
            if (data.total == 0) {
              this.$alert("该日计划下不存在序列号请联系管理员！", '错误', {
                confirmButtonText: '确定'
              })
              return false;
            }else {
              for (let i = 0; i <data.total ; i++) {
                for(let x=0;x<this.printQty;x++){
                  let printData={serialNo:data.rows[i].serialNo}
                  array.push(printData);
                }
              }
              printMaterialLabel(array);
              this.refreshTables()
              this.printModalFlag = false;
            }
          } else {
            this.$alert(data.msg, '错误', {
              confirmButtonText: '确定'
            })
          }

        })
      },
      routingRowClassName2 ({row, rowIndex}) {
        let date = new Date()
        let planDate = new Date(row.planFinishTime.replace('-', '/'))
        //判断当前行颜色参数
        if (row.closedFlag == 'Y' && row.qtyReported >= row.qtyRequired) {
          return 'customer-row-1'
        } else if (row.closedFlag == 'Y' && row.qtyReported < row.qtyRequired) {
          return 'customer-row-2'
        } else if (planDate < date) {
          return 'customer-row-3'
        }
        //判断当前行颜色参数
        // if (row.printFlag == "Y") {
        //   return 'customer-row-2';
        // }
      },
    }

  }
</script>

<style scoped>

</style>
