<template>
  <div class="mod-config">
    <el-form :inline="true"  label-position="top" label-width="100px" >
      <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-input v-model="searchData.seqNo" style="width: 120px" onkeyup="value=value.replace(/^(0+)|[^\d]+/g,'')"></el-input>
      </el-form-item>
      <el-form-item :label="'检验单类型'">
        <el-select v-model="searchData.toTypeFlagDb" style="width: 120px">
          <el-option label="全部" value=""></el-option>
          <el-option label="混炼胶入库" value="W"></el-option>
          <el-option label="开工检验单" value="I"></el-option>
          <el-option label="进料检验" value="Q"></el-option>
          <el-option label="物料过期送检" value="S"></el-option>
          <el-option label="报废超标确认" value="B"></el-option>
          <el-option label="异常反馈检验单" value="A"></el-option>
        </el-select>
      </el-form-item>
      <el-form-item :label="'是否检验'">
        <el-select v-model="searchData.inspectedFlag" style="width: 120px">
          <el-option label="全部" value=""></el-option>
          <el-option label="是" value="Y"></el-option>
          <el-option label="否" value="N"></el-option>
        </el-select>
      </el-form-item>
      <el-form-item :label="'是否检验通过'">
        <el-select v-model="searchData.needReceiveFlag" style="width: 120px">
          <el-option label="全部" value=""></el-option>
          <el-option label="是" value="Y"></el-option>
          <el-option label="否" value="N"></el-option>
        </el-select>
      </el-form-item>
    </el-form>
    <el-form :inline="true"  label-position="top" label-width="100px" style="margin-top: 0px;">
      <el-form-item :label="'派工日期：'">
        <el-date-picker
          style="width: 130px"
          v-model="searchData.startDate"
          value-format="yyyy-MM-dd"
          placeholder="选择日期">
        </el-date-picker>
      </el-form-item>
      <el-form-item :label="'至: '">
        <el-date-picker
          style="width: 130px"
          v-model="searchData.endDate"
          type="date"
          value-format="yyyy-MM-dd"
          placeholder="选择日期">
        </el-date-picker>
      </el-form-item>
      <el-form-item >
        <span slot="label" style="" @click="getBaseList(5)"><a herf="#">产品编码</a></span>
        <el-input v-model="searchData.partNo" style="width: 120px"></el-input>
      </el-form-item>
      <el-form-item >
        <span slot="label" style="" @click="getBaseList(24)"><a herf="#">加工中心编码</a></span>
        <el-input v-model="searchData.workCenterNo" style="width: 120px"></el-input>
      </el-form-item>
      <el-form-item >
        <span slot="label" style="" @click="getBaseList(26)"><a herf="#">操作员姓名</a></span>
        <el-input v-model="searchData.operatorName" style="width: 120px"></el-input>
      </el-form-item>
      <el-form-item :label="' '">
        <el-button @click="search()" style="margin-left: 0px;margin-top:0px" type="primary">查询</el-button>
<!--        <download-excel-->
<!--          :fields="exportDataStandard"-->
<!--          :data="tableData"-->
<!--          type="xlsx"-->
<!--          :name="exportName"-->
<!--          :header="exportHeader"-->
<!--          :footer="exportFooter"-->
<!--          :defaultValue="exportDefaultValue"-->
<!--          :fetch="createExportData"-->
<!--          :before-generate="startDownload"-->
<!--          :before-finish="finishDownload"-->
<!--          worksheet="导出信息"-->
<!--          class="el-button el-button&#45;&#45;primary el-button&#45;&#45;medium">-->
<!--          {{'导出'}}-->
<!--        </download-excel>-->
      </el-form-item>
    </el-form>
    <el-table
      :height="height"
      :data="tableData"
      border
      style="width: 100%">
      <el-table-column
        prop=""
        header-align="center"
        align="center"
        min-width="180"
        fixed="left"
        label="操作">
        <template slot-scope="scope" class="foo_container">
          <a v-if="scope.row.inspectedFlag=='N'" type="text" size="small" @click="inspectModal(scope.row)">检验</a>
          <a v-if="scope.row.inspectedFlag=='Y'" type="text" size="small" @click="toPrint(scope.row)">补打结果</a>
          <a v-if="scope.row.inspectedFlag=='Y'&&scope.row.toTypeFlagDb!='S'" type="text" size="small" @click="cancelInspectResult(scope.row)">取消检验结果</a>
          <a v-if="scope.row.toTypeFlag=='混炼胶入库'" type="text" size="small" @click="detailModal(scope.row)">混炼明细</a>
          <a v-if="scope.row.toTypeFlagDb=='I'&&scope.row.inspectedFlag=='N'" type="text" size="small" @click="cancelInspect(scope.row)">删除检验单</a>
        </template>
      </el-table-column>
      <el-table-column
        prop="site"
        header-align="center"
        align="left"
        min-width="70"
        label="工厂编码">
      </el-table-column>
      <el-table-column
        prop="transNo"
        header-align="center"
        align="left"
        min-width="130"
        label="检验单号">
      </el-table-column>
      <el-table-column
        prop="transDate"
        header-align="center"
        align="left"
        min-width="130"
        label="生成日期">
      </el-table-column>
      <el-table-column
        prop="shiftNo"
        header-align="center"
        align="left"
        min-width="60"
        label="班次">
      </el-table-column>
      <el-table-column
        prop="orderNo"
        header-align="center"
        align="left"
        min-width="100"
        label="生产订单号">
      </el-table-column>
      <el-table-column
        prop="inspectedFlag"
        header-align="center"
        align="left"
        min-width="70"
        label="是否检验">
      </el-table-column>
      <el-table-column
        prop="inspectResult"
        header-align="center"
        align="left"
        min-width="70"
        label="检验结果">
      </el-table-column>
      <el-table-column
        prop="toTypeFlag"
        header-align="center"
        align="left"
        min-width="120"
        label="检验单类型">
      </el-table-column>
      <el-table-column
        prop="needReceiveFlag"
        header-align="center"
        align="left"
        min-width="70"
        label="检验通过">
      </el-table-column>
      <el-table-column
        prop="partNo"
        header-align="center"
        align="left"
        min-width="80"
        label="物料编码">
      </el-table-column>
      <el-table-column
        prop="partDescSpec"
        header-align="center"
        align="left"
        min-width="160"
        label="物料名称规格">
      </el-table-column>
      <el-table-column
        prop="workCenterDesc"
        header-align="center"
        align="left"
        min-width="100"
        label="加工中心">
      </el-table-column>
      <el-table-column
        prop="operatorName"
        header-align="center"
        align="left"
        min-width="70"
        label="操作员">
      </el-table-column>
      <el-table-column
        prop="inspectRemark"
        header-align="center"
        align="left"
        min-width="150"
        label="检验备注">
      </el-table-column>
      <el-table-column
        prop="transQty"
        header-align="center"
        align="left"
        min-width="160"
        label="派工单数量">
      </el-table-column>
      <el-table-column
        prop="seqNo"
        header-align="center"
        align="left"
        min-width="60"
        label="派工单号">
      </el-table-column>

    </el-table>
    <el-dialog title="录入检验结果" :close-on-click-modal="false" v-drag :visible.sync="inspectFlag" width="480px">
      <el-form :inline="true" label-position="top" style="margin-left: 7px;margin-top: -5px;" @submit.native.prevent>
        <el-form-item :label="'检验单号：'">
          <el-input v-model="inspectData.transNo" disabled style="width: 130px"></el-input>
        </el-form-item>
        <el-form-item :label="'生产订单号：'">
          <el-input v-model="inspectData.orderNo" disabled style="width: 130px"></el-input>
        </el-form-item>
        <el-form-item :label="'物料编码：'">
          <el-input v-model="inspectData.partNo" disabled style="width: 130px" ></el-input>
        </el-form-item>
      </el-form>
        <el-form :inline="true" label-position="top" style="margin-left: 7px;margin-top: 0px;">
        <el-form-item :label="'规格/型号：'">
          <el-input v-model="inspectData.partDescSpec" disabled style="width: 417px;"  ></el-input>
        </el-form-item>
      </el-form>
      <el-form :inline="true" label-position="top" style="margin-left: 7px;margin-top: 0px;"  @submit.native.prevent>
        <el-form-item :label="'检验结果：'">
          <el-select v-model="inspectData.inspectResult" style="width: 120px">
            <el-option label="合格" value="合格"></el-option>
            <el-option label="不合格" value="不合格"></el-option>
            <el-option label="让步接收" value="让步接收"></el-option>
            <el-option label="签字确认" value="合格" v-if="inspectData.toTypeFlagDb==='B'"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="检验备注">
          <el-input  type="textarea" style="width: 282px" v-model="inspectData.inspectRemark"></el-input>
        </el-form-item>
      </el-form>
      <el-form :inline="true" label-position="top" style="margin-left: 7px;margin-top: -5px;">
        <el-form-item :label="'新失效日期：'" v-if="inspectData.toTypeFlagDb==='S'">
          <el-date-picker
            style="width: 120px"
            v-model="inspectData.newDate"
            type="date"
            value-format="yyyy-MM-dd"
            format = "yyyy-MM-dd"
            placeholder="选择日期">
          </el-date-picker>
        </el-form-item>
      </el-form>
      <el-footer style="height:40px;margin-top: 30px;text-align:center">
        <el-button type="primary" @click="inspectSave()">保存</el-button>
        <el-button type="primary" @click="inspectFlag = false">取消</el-button>
      </el-footer>
    </el-dialog>
    <el-dialog  :close-on-click-modal="false" :close-on-press-escape="false" v-drag title="周计划清单" :visible.sync="detailFlag" width="600px">
      <el-table
        height="300"
        :data="detailList"
        border
        style="width: 100%">
        <el-table-column
          prop="taskNo"
          header-align="center"
          align="left"
          min-width="80"
          label="混炼任务单号">
        </el-table-column>
        <el-table-column
          prop="orderNo"
          header-align="center"
          align="left"
          min-width="70"
          label="周计划号">
        </el-table-column>
        <el-table-column
          prop="partDesc"
          header-align="center"
          align="left"
          min-width="100"
          label="混炼胶名称">
        </el-table-column>
        <el-table-column
          prop="orderQty"
          header-align="center"
          align="right"
          min-width="70"
          label="周计划数量">
        </el-table-column>
        <el-table-column
          prop="hunlianQty"
          header-align="center"
          align="right"
          min-width="70"
          label="混炼胶数量">
        </el-table-column>
        <el-table-column
          prop="orderDate"
          header-align="center"
          align="left"
          min-width="100"
          label="周计划日期">
        </el-table-column>

      </el-table>
      <span slot="footer" class="dialog-footer">
      <el-button @click="detailFlag = false" type="primary">取消</el-button>
    </span>
    </el-dialog>
   <Chooselist ref="baseList"  @getBaseData="getBaseData"></Chooselist>
  </div>
</template>

<script>
  import {
    getHunlianTaskData,getHunlianTaskDetailData
  } from "@/api/production.js"
  import {
    getInspectData,
    inspectSave,
    inspectPrint,
    cancelInspect,
    cancelInspectResult,
  } from '@/api/production/inspect.js'
  import Chooselist from '@/views/modules/common/Chooselist'
  import {
    printInspectLabel,
  } from "@/views/modules/print/print_inspect_label.js"
  import {
    printInspectLabelForB,
  } from "@/views/modules/print/print_inspect_labelForB.js"
  import {
    printInspectStockLabel,
  } from "@/views/modules/print/print_inspect_labelForPartStock.js"

  export default {
    name: 'soscheduleRouting',
    components: {
      Chooselist
    },
    data () {
      return {
        detailList:[],
        detailFlag:false,
        // 导出 start
        exportData: [],
        exportDataStandard: {
          '派工单号': 'seqNo',
          '关联单号': 'orderNo',
          '关联单号类型': 'scheduleType',
          '工序号': 'itemNo',
          '工序名称': 'itemDesc',
          '派工数量': 'qtyRequired',
          '报工数量': 'qtyReported',
          '合格数量': 'qtyApprove',
          '需求时间': 'timeRequired',
          '派工日期': 'sScheduledDate',
          '操作员姓名': 'operatorName',
          '产品编码': 'partNo',
          '产品名称': 'partDesc',
          '机台ID': 'sResourceID',
          '班次': 'sShiftNo',
          '加工中心': 'sWorkCenterNo',

        },
        exportName: '派工单列表' + this.getStrDate(),
        exportHeader: ['派工单列表'],
        exportFooter: [],
        exportDefaultValue: '',

        // 导出 end
        height: 200,
        tableData: [],
        searchData: {
          orderNo: '',
          workCenterNo: '',
          partNo: '',
          site: '',
          toTypeFlagDb:'',
          inspectedFlag:'',
          needReceiveFlag:'N',
          endDate:'',
          startDate:'',
          operatorName:'',
          seqNo:'',
        },
        height2:600,
        inspectData:{
          user:this.$store.state.user.name,
          transNo:'',
          orderNo:'',
          partNo:'',
          partDescSpec:'',
          site:'',
          toTypeFlag:'',
          toTypeFlagDb:'',
          inspectResult:'',
          inspectRemark:'',
          operatorName:'',
          seqNo:'',
          newDate:'',
          confirmedTransNo:'',
        },
        inspectFlag:false,
      }
    },
    mounted () {
      this.$nextTick(() => {
        this.height = window.innerHeight - 210
        this.height2 = window.innerHeight -100
      })
    },
    methods: {
      detailModal(row){
        let inData={
          site:row.site,
          taskNo:row.orderNo,
        }
        getHunlianTaskDetailData(inData).then(({data}) => {
          this.detailList = data.rows;
        })
        this.detailFlag=true;

      },
      // 获取基础数据列表S
      getBaseList (val, type) {
        this.tagNo = val
        this.$nextTick(() => {
          let strVal = ''
          if (val === 24) {
            strVal = this.searchData.workCenterNo
          }
          if (val === 5) {
            strVal = this.searchData.partNo
          }
          if (val === 26) {
            strVal = this.searchData.operatorName
          }
          if (val === 88) {
            strVal = this.searchData.sResourceID
          }
          this.$refs.baseList.init(val, strVal)
        })
      },
      /* 列表方法的回调 */
      getBaseData (val) {
        if (this.tagNo === 24) {
          this.searchData.workCenterNo = val.WorkCenterNo
        }  else if (this.tagNo === 5) {
          this.searchData.partNo = val.PartNo
        }
        if (this.tagNo === 26) {
          this.searchData.operatorName = val.OperatorName
        }
        if (this.tagNo === 88) {
          this.searchData.sResourceID = val.ResourceID
        }
      },

      search () {
        getInspectData(this.searchData).then(({data}) => {
          this.tableData = data.rows
        })
      },
      inspectModal(row){
        this.inspectData.transNo=row.transNo;
        this.inspectData.orderNo=row.orderNo;
        this.inspectData.partNo=row.partNo;
        this.inspectData.partDescSpec=row.partDescSpec;
        this.inspectData.site=row.site;
        this.inspectData.toTypeFlag=row.toTypeFlag;
        this.inspectData.toTypeFlagDb=row.toTypeFlagDb;
        this.inspectData.transDate=row.transDate;
        this.inspectData.transQty=row.transQty;
        this.inspectData.operatorName=row.operatorName;
        this.inspectData.inspectResult= JSON.parse(JSON.stringify(row.inspectResult));
        this.inspectData.inspectRemark= JSON.parse(JSON.stringify(row.inspectRemark));
        this.inspectData.seqNo=row.seqNo;
        this.inspectData.newDate='';
        this.inspectData.confirmedTransNo=row.confirmedTransNo
        this.inspectData.user=this.$store.state.user.name
        this.inspectFlag=true;
      },
      inspectSave(){
        if(this.inspectData.inspectResult==''||this.inspectData.inspectResult==null){
          this.$alert('请选择检验结果！', '错误', {
            confirmButtonText: '确定'
          })
          return false
        }
        if(this.inspectData.toTypeFlagDb=='S'&&(this.inspectData.newDate==''||this.inspectData.newDate==null)){
          this.$alert('请选择新的失效日期！', '错误', {
            confirmButtonText: '确定'
          })
          return false
        }
        inspectSave(this.inspectData).then(({data}) => {
          if (data && data.code === 0) {
            this.search ();
            let printData=data.row
            printData.userId=this.$store.state.user.name
            if(this.inspectData.toTypeFlagDb=='S'){
              let array=[];
              array.push(printData)
              printInspectStockLabel(array)
            }else if(this.inspectData.toTypeFlagDb=='B'){
              let array=[];
              array.push(printData)
              printInspectLabelForB(array)
            }else if(this.inspectData.toTypeFlagDb=='A'){


            }else {
              if(this.inspectData.toTypeFlagDb!='I'){
                printData.seqNo=printData.orderNo
              }
              let array=[];
              array.push(printData)
              printInspectLabel(array)
            }
            this.inspectFlag=false
            this.$message({
              message: '操作成功',
              type: 'success',
              duration: 1500,
              onClose: () => {
              }
            })
          }else {
            this.$alert(data.msg, '错误', {
              confirmButtonText: '确定'
            })
          }
        })
      },
      cancelInspectResult(row){
        this.$confirm(`是否取消检验结果?`, '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }).then(() => {
          cancelInspectResult(row).then(({data}) => {
            if (data && data.code === 0) {
              this.search ();
              this.$message({
                message: '操作成功',
                type: 'success',
                duration: 1500,
                onClose: () => {
                }
              })
            }else {
              this.$alert(data.msg, '错误', {
                confirmButtonText: '确定'
              })
            }
          })
        }).catch(() => {
        })
      },
      cancelInspect(row){
        if(row.toTypeFlag==='硫化检验单'){
          this.$alert('这个检验单不是重复的检验单无法删除', '错误', {
            confirmButtonText: '确定'
          })

        }else {
          this.$confirm(`是否删除这条检验单?`, '提示', {
            confirmButtonText: '确定',
            cancelButtonText: '取消',
            type: 'warning'
          }).then(() => {
            cancelInspect(row).then(({data}) => {
              if (data && data.code === 0) {
                this.search();
                this.$message({
                  message: '操作成功',
                  type: 'success',
                  duration: 1500,
                  onClose: () => {
                  }
                })
              } else {
                this.$alert(data.msg, '错误', {
                  confirmButtonText: '确定'
                })
              }
            })
          }).catch(() => {
          })
        }
      },
      toPrint(row){
        if(row.toTypeFlagDb=='S') {
          inspectPrint(row).then(({data}) => {
            if (data && data.code === 0) {
              let printData=data.row
              printData.userId=this.$store.state.user.name
              let array=[];
              array.push(printData)
              printInspectStockLabel(array)
            }else {
              this.$alert(data.msg, '错误', {
                confirmButtonText: '确定'
              })
            }
          })
        }else  if(row.toTypeFlagDb=='B') {
          inspectPrint(row).then(({data}) => {
            if (data && data.code === 0) {
              let printData=data.row
              printData.qtyRequired=row.transQty
              printData.userId=this.$store.state.user.name
              let array=[];
              array.push(printData)
              printInspectLabelForB(array)
            }else {
              this.$alert(data.msg, '错误', {
                confirmButtonText: '确定'
              })
            }
          })
        }else{
          inspectPrint(row).then(({data}) => {
            if (data && data.code === 0) {
              let printData=data.row
              printData.userId=this.$store.state.user.name
              if(row.toTypeFlagDb!='I'){
                printData.seqNo=printData.orderNo
              }
              if(row.toTypeFlag=='硫化检验单(重复)'){
                printData.type='(变更)'
              }else {
                printData.type=''
              }

              let array=[];
              array.push(printData)
              printInspectLabel(array)
            }else {
              this.$alert(data.msg, '错误', {
                confirmButtonText: '确定'
              })
            }
          })
        }
      },
      createExportData () {
        return this.tableData
      },
      startDownload () {
        //  this.exportData = this.dataList

      },
      finishDownload () {

      },
      getStrDate () {
        let dd = new Date()
        let Y = dd.getFullYear()
        let M = (dd.getMonth() + 1) < 10 ? '0' + (dd.getMonth() + 1) : (dd.getMonth() + 1)// 获取当前月份的日期，不足10补0
        let D = dd.getDate() < 10 ? '0' + dd.getDate() : dd.getDate()// 获取当前几号，不足10补0
        let H = dd.getHours() < 10 ? '0' + dd.getHours() : dd.getHours()
        let MM = dd.getMinutes() < 10 ? '0' + dd.getMinutes() : dd.getMinutes()
        let S = dd.getSeconds() < 10 ? '0' + dd.getSeconds() : dd.getSeconds()
        return Y + M + D + H + MM + S
      },
    },
    created () {
    }
  }
</script>

<style scoped>
  .input_left{
    text-align: left;
  }
  .input_reight{
    text-align: right;
  }
  /deep/ input::-webkit-inner-spin-button {
    -webkit-appearance: none !important;
  }
  input[type='number'] {
    -moz-appearance: textfield !important;
  }

</style>
