<template>
  <div class="mod-config">
    <el-form :inline="true"  label-position="top" label-width="100px" >
      <el-form-item :label="'混炼任务单号：'">
        <el-input v-model="searchData.taskNo" style="width: 120px"></el-input>
      </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 :label="'合并日期：'">
        <el-date-picker
          style="width: 130px"
          v-model="searchData.startDate"
          type="date"
          value-format="yyyy-MM-dd"
          placeholder="选择日期">
        </el-date-picker>
      </el-form-item>
      <el-form-item style="margin-top: 23px;">
        <laber style="margin-left: -9px;font-size: 19px">&#10142</laber>
      </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 :label="'是否报工：'">
        <el-select filterable v-model="searchData.reportFlag" style="width: 120px">
          <el-option label="全部" value=""></el-option>
          <el-option label="否" value="N"></el-option>
          <el-option label="是" value="Y"></el-option>
        </el-select>
      </el-form-item>
      <el-form-item :label="' '">
      <el-button @click="search()" style="margin-left: 0px;margin-top: 0px" type="primary">查询</el-button>
      </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="160"
        label="操作">
        <template slot-scope="scope" class="foo_container">
          <a type="text" size="small" @click="printHunlianCard(scope.row)">混炼任务单</a>
          <a type="text" v-if="scope.row.reportFlag=='N'" size="small" @click="reportTaskModal(scope.row)">报工</a>
          <a type="text" v-if="scope.row.reportFlag=='Y'&&scope.row.wareHouseFlag=='N'" size="small" @click="cancelReportTask(scope.row)">取消报工</a>
          <a type="text" size="small" @click="printPartCard(scope.row)">物料标识卡</a>
          <a type="text" size="small" @click="detailModal(scope.row)">查看周计划明细</a>
          <a type="text" v-if="scope.row.reportFlag=='N'" size="small" @click="cancelTask(scope.row)">取消任务</a>
        </template>
      </el-table-column>
      <el-table-column
        prop="taskNo"
        header-align="center"
        align="left"
        min-width="80"
        label="混炼任务单号">
      </el-table-column>
      <el-table-column
        prop="partNo"
        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="taskQty"
        header-align="center"
        align="right"
        min-width="100"
        label="混炼胶数量">
      </el-table-column>
      <el-table-column
        prop="createDate"
        header-align="center"
        align="left"
        min-width="70"
        label="合并日期">
      </el-table-column>
      <el-table-column
        prop="createBy"
        header-align="center"
        align="left"
        min-width="70"
        label="创建人">
      </el-table-column>
      <el-table-column
        prop="reportFlag"
        header-align="center"
        align="left"
        min-width="70"
        label="是否报工">
      </el-table-column>
      <el-table-column
        prop="reportQty"
        header-align="center"
        align="right"
        min-width="70"
        label="报工数量">
      </el-table-column>
      <el-table-column
        prop="wareHouseFlag"
        header-align="center"
        align="left"
        min-width="70"
        label="是否入库">
      </el-table-column>
    </el-table>
    <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>
    <el-dialog title="保存报工" :close-on-click-modal="false" v-drag :visible.sync="reportModalFlag" width="470px">
      <el-form :inline="true" label-position="top" style="margin-left: 7px;margin-top: -5px;">
        <el-form-item :label="'任务单号：'">
          <el-input v-model="reportData.taskNo" disabled style="width: 130px"></el-input>
        </el-form-item>
        <el-form-item :label="'所需数量：'">
          <el-input v-model="reportData.qtyRequired" disabled style="width: 130px"></el-input>
        </el-form-item>
        <el-form-item :label="'库存数量：'">
          <el-input v-model="reportData.qtyOnHand" 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="reportData.qtyReal" type="number" style="width: 130px"></el-input>
        </el-form-item>
        <el-form-item :label="'库存抵扣数量：'">
          <el-input v-model="reportData.qtyRef" type="number" style="width: 130px"></el-input>
        </el-form-item>
      </el-form>
      <el-footer style="height:40px;margin-top: 20px;text-align:center">
        <el-button type="primary" @click="reportSave()">保存</el-button>
        <el-button type="primary" @click="reportModalFlag = false">关闭</el-button>
      </el-footer>
    </el-dialog>

    <Chooselist ref="baseList"  @getBaseData="getBaseData"></Chooselist>
  </div>

</template>

<script>
  import {
    getHunlianTaskData,getHunlianTaskDetailData
    ,cancelTask,reportTask,cancelReportTask,getPartNoOnHand,getHunLianPrintData
  } from "@/api/production.js"
  import {
    printTransNoLabel,
  } from "@/views/modules/print/print_transNo_label.js"
  import {
    printHunlianLabel,
  } from "@/views/modules/print/print_hunlian_card.js"
  import {
    printPartLabel,
  } from "@/views/modules/print/print_part_label.js"
  import decimalUtil from '../../../utils/decimalUtil'
  import Chooselist from '@/views/modules/common/Chooselist'
  export default {
    components: {
      Chooselist
    },
    data () {
      return {
        searchData:{
          taskNo:'',
          partNo:'',
          startDate:'',
          endDate:'',
          reportFlag:'N',
        },
        height:200,
        tagNo:'',
        tableData:[],
        detailList:[],
        detailFlag:false,
        reportModalFlag:false,
        reportData:{
          site:'',
          taskNo:'',
          qtyRequired: '',
          reportQty:'',
          qtyOnHand:'',
          qtyReal:'',
          qtyRef:'',
        }
      }
    },
    mounted() {
      this.$nextTick(()=>{
        this.height = window.innerHeight - 180;
      })
    },
    methods: {
      getBaseList (val,type) {
        this.tagNo = val
        this.$nextTick(() => {
          let strVal = ''
          if (val === 5) {
            strVal = this.searchData.partNo
          }
          this.$refs.baseList.init(val, strVal)
        })
      },
      /* 列表方法的回调 */
      getBaseData (val) {
        if (this.tagNo === 5) {
          this.searchData.partNo = val.PartNo
        }
      },

      search(){
        getHunlianTaskData(this.searchData).then(({data}) => {

          this.tableData = data.rows;
        })
      },
      detailModal(row){
        let inData={
          site:row.site,
          taskNo:row.taskNo,
        }
        getHunlianTaskDetailData(inData).then(({data}) => {
          this.detailList = data.rows;
        })
        this.detailFlag=true;

      },
      getPartNoOnHand(site,partNo){
        let inData={
          site:site,
          partNo:partNo,
        }
        getPartNoOnHand(inData).then(({data}) => {
          if (data && data.code === 0) {
            if(data.row!=null){
              this.reportData.qtyOnHand=data.row.qtyOnHand;
            }else {
              this.reportData.qtyOnHand=0;
            }

          } else {
            this.$alert(data.msg, '错误', {
              confirmButtonText: '确定'
            })
          }
        })
      },
      printHunlianCard(row){
        getHunLianPrintData(row).then(({data}) => {
          if (data && data.code === 0) {
              if(data.rows.length>0){
                let array=[];
                array.push(data.rows[0])

                printHunlianLabel(array)
              }

          }
        })

      },
      printPartCard(row){
        let array=[];
        let data={
          // printerName:'大标签打印机',
          orderNo:'*',
          userId:this.$store.state.user.name,
          partNo:row.partNo,
          sScheduledDate:row.createDate,
          operatorName:this.$store.state.user.name,
          partDesc:row.partDesc,
          qtyRequired:row.taskQty,
          toExpireDays:row.toExpireDays,
          remark:row.remark,
        }
        array.push(data)
        printPartLabel(array)
      },
      cancelTask(row){
        this.$confirm(`是否删除混炼任务单?`, '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }).then(() => {
          cancelTask(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(() => {
        })

      },
      reportTaskModal(row){

        this.reportData.site=row.site;
        this.reportData.taskNo=row.taskNo;
        this.reportData.qtyRequired=row.taskQty;
        this.reportData.reportQty='';
        this.reportData.partNo=row.partNo;
        this.reportData.userId=row.userId;
        this.reportData.qtyReal=0;
        this.reportData.qtyRef=0;
        this.getPartNoOnHand(row.site,row.partNo);
        this.reportModalFlag=true;
      },
      cancelReportTask(row){
        this.$confirm(`是否取消报工?`, '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }).then(() => {
          cancelReportTask(row).then(({data}) => {
            if (data && data.code === 0) {
              this.search ();
            } else {
              this.$alert(data.msg, '错误', {
                confirmButtonText: '确定'
              })
            }
          })
        }).catch(() => {
        })
      },
      reportSave(){
        this.reportData.reportQty=Number(Number(this.reportData.qtyReal)+Number(this.reportData.qtyRef)).toFixed(2);
        if(this.reportData.reportQty<this.reportData.qtyRequired){
          this.$alert('报工数量不足，无法报工！', '错误', {
            confirmButtonText: '确定'
          })
          return false
        }
        this.$confirm(`确认是否报工?`, '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }).then(() => {
          reportTask(this.reportData).then(({data}) => {
            if (data && data.code === 0) {
              this.search ();
              this.reportModalFlag=false;
              data.printData.seqNo='*'
              let inList=[];
              inList.push(data.printData)
              printTransNoLabel(inList)
              this.$message({
                message: '操作成功',
                type: 'success',
                duration: 1500,
                onClose: () => {
                }
              })
            } else {
              this.$alert(data.msg, '错误', {
                confirmButtonText: '确定'
              })
            }
          })
        }).catch(() => {
        })
      }
    },
    created() {
    }
  }
</script>

<style >
  .el-table .cell {
    padding: 0px;
  }
</style>
