<template>
  <div class="mod-config">
    <el-form :inline="true"  label-position="top" label-width="100px" >
      <el-form-item :label="'派工单号：'">
        <el-input v-model="searchData.seqNo" type="number" style="width: 80px"></el-input>
      </el-form-item>
      <el-form-item :label="'反馈日期：'">
        <el-date-picker
          style="width: 120px"
          v-model="searchData.startDate"
          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: 120px"
          v-model="searchData.endDate"
          type="date"
          value-format="yyyy-MM-dd"
          placeholder="选择日期">
        </el-date-picker>
      </el-form-item>
      <el-form-item label="异常情况原因：" >
        <el-select v-model="searchData.abnormalCode" style="width: 120px">
          <el-option label="全部" value="" ></el-option>
          <el-option
            v-for="(item,index) in abnormalList"
            :key="index"
            :label="item.abnormalDesc"
            :value="item.abnormalCode"
          >
          </el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="状态：" >
        <el-select v-model="searchData.active" 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="已填写长久处理"></el-option>
          <el-option label="已完结" value="已完结"></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-button @click="addAbnormal()" 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--primary el-button--medium">
          {{'导出'}}
        </download-excel>
      </el-form-item>
    </el-form>
    <el-table
      :height="height"
      :data="tableData"
      border
      style="width: 100%">
      <el-table-column
        prop="site"
        header-align="center"
        align="left"
        width="60"
        label="工厂编号">
      </el-table-column>
      <el-table-column
        prop="orderNo"
        header-align="center"
        align="left"
        width="80"
        label="生产订单">
      </el-table-column>
      <el-table-column
        prop="seqNo"
        header-align="center"
        align="left"
        width="60"
        label="派工单号">
      </el-table-column>
      <el-table-column
        prop="partNo"
        header-align="center"
        align="left"
        width="60"
        label="物料编码">
      </el-table-column>
      <el-table-column
        prop="partDesc"
        header-align="center"
        align="left"
        width="140"
        label="规格型号">
      </el-table-column>
      <el-table-column
        prop="operatorDesc"
        header-align="center"
        align="left"
        width="60"
        label="操作员">
      </el-table-column>
      <el-table-column
        prop="productionLineDesc"
        header-align="center"
        align="left"
        width="60"
        label="生产线">
      </el-table-column>
      <el-table-column
        prop="feedbackDate"
        header-align="center"
        align="left"
        width="130"
        label="反馈时间">
      </el-table-column>
      <el-table-column
        prop="feedbackBy"
        header-align="center"
        align="left"
        width="80"
        label="反馈人">
      </el-table-column>
      <el-table-column
        prop="status"
        header-align="center"
        align="left"
        width="100"
        label="状态">
      </el-table-column>
      <el-table-column
        prop="abnormalDesc"
        header-align="center"
        align="left"
        width="100"
        label="异常原因">
      </el-table-column>
      <el-table-column
        prop="abnormalRemark"
        header-align="center"
        align="left"
        min-width="300"
        label="异常描述">
      </el-table-column>
      <el-table-column
        fixed="right"
        header-align="center"
        align="center"
        width="230"
        label="操作">
        <template slot-scope="scope">
          <a type="text" size="small" @click="getDetailModel(scope.row)">查看</a>
          <a type="text" size="small" v-if="scope.row.status!='已完结'" @click="getDetailModel2(scope.row)">紧急处理方式</a>
          <a type="text" size="small" v-if="scope.row.status!='已完结'" @click="getDetailModel3(scope.row)">长久处理方式</a>
          <a type="text" size="small" v-if="scope.row.status!='已完结'" @click="closeFeedBack(scope.row)">结束</a>
          <a type="text" size="small" v-if="scope.row.status=='已完结'" @click="openFeedBack(scope.row)">开启</a>
        </template>
      </el-table-column>
    </el-table>
    <el-dialog title="新增反馈" :close-on-click-modal="false" v-drag :visible.sync="addModalFlag" width="620px" >
      <el-form :inline="true" label-position="top" style="margin-left: 7px;margin-top: -5px;">
        <el-form-item :label="'异常原因：'">
          <el-select v-model="addData.abnormalCode"  style="width: 120px">
            <el-option
              v-for="(item,index) in abnormalList"
              :key="index"
              :label="item.abnormalDesc"
              :value="item.abnormalCode"
            >
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item :label="'派工单号：'">
          <el-input v-model="addData.seqNo" type="number" @blur="seqNoCheck()" style="width: 85px"></el-input>
        </el-form-item>
        <el-form-item :label="'生产订单：'">
          <el-input v-model="addData.orderNo" disabled style="width: 100px"></el-input>
        </el-form-item>
        <el-form-item :label="'机台：'">
          <el-input v-model="addData.resourceDesc" disabled style="width: 100px"></el-input>
        </el-form-item>
        <el-form-item :label="'班次：'">
          <el-input v-model="addData.shiftNo" disabled style="width: 100px"></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="addData.abnormalRemark" type="textarea"  :rows="3" resize='none' style="width: 562px"></el-input>
        </el-form-item>
      </el-form>
      <el-footer style="height:30px;margin-top: 50px;text-align:center">
        <el-button type="primary" @click="saveAbnormalData()" >保存</el-button>
        <el-button type="primary" @click="addModalFlag = false">关闭</el-button>
      </el-footer>
    </el-dialog>
    <el-dialog title="异常反馈" :close-on-click-modal="false" v-drag :visible.sync="modalFlag" width="650px" >
      <el-form :inline="true" label-position="top" style="margin-left: 7px;margin-top: -5px;">
        <span style="font-size: 12px">异常原因:</span>
        <el-input v-model="abnormalFeedBackData.abnormalDesc"  disabled style="width: 180px"></el-input>
      </el-form>
      <el-form :inline="true" label-position="top" style="margin-left: 7px;margin-top: 0px;">
        <el-form-item label="异常情况描述">
        <el-input v-model="abnormalFeedBackData.abnormalRemark" type="textarea" :disabled="disableFlag1" :rows="3" resize='none' style="width: 562px"></el-input>
        </el-form-item>
      </el-form>
      <el-form :inline="true" label-position="top" style="margin-left: 7px;margin-top: 40px;">
        <el-form-item label="紧急处理方式">
          <el-input v-model="abnormalFeedBackData.treatmentTemporary" type="textarea" :disabled="disableFlag2" :rows="3" resize='none' style="width: 562px"></el-input>
        </el-form-item>
      </el-form>
      <el-form :inline="true" label-position="top" style="margin-left: 7px;margin-top: 40px;">
        <span style="font-size: 12px">工艺变更记录:</span>
      </el-form>
      <el-form :inline="true" label-position="top" style="margin-left: 7px;margin-top: 0px;">
        <span style="font-size: 12px">&nbsp温度&nbsp&nbsp&nbsp</span> <el-input v-model="abnormalFeedBackData.temperatureTemporary" :disabled="disableFlag2" style="width: 70px"></el-input>
        <span style="font-size: 12px">压力&nbsp&nbsp&nbsp</span> <el-input v-model="abnormalFeedBackData.pressureTemporary" :disabled="disableFlag2" style="width: 70px"></el-input>
        <span style="font-size: 12px">时间&nbsp&nbsp&nbsp</span> <el-input v-model="abnormalFeedBackData.timeTemporary" :disabled="disableFlag2" style="width: 70px"></el-input>
        <span style="font-size: 12px">投料量</span> <el-input v-model="abnormalFeedBackData.feedingTemporary" :disabled="disableFlag2" style="width: 70px"></el-input>
        <span style="font-size: 12px">机台&nbsp&nbsp&nbsp</span> <el-input v-model="abnormalFeedBackData.resourceTemporary" :disabled="disableFlag2" style="width: 70px"></el-input>

      </el-form>
      <el-form :inline="true" label-position="top" style="margin-left: 7px;margin-top: 0px;">
        <span style="font-size: 12px">&nbsp功率&nbsp&nbsp&nbsp</span> <el-input v-model="abnormalFeedBackData.efficiencyTemporary" :disabled="disableFlag2" style="width: 70px"></el-input>
        <span style="font-size: 12px">速度&nbsp&nbsp&nbsp</span> <el-input v-model="abnormalFeedBackData.speedTemporary" :disabled="disableFlag2" style="width: 70px"></el-input>
        <span style="font-size: 12px">材料&nbsp&nbsp&nbsp</span> <el-input v-model="abnormalFeedBackData.materialTemporary" :disabled="disableFlag2" style="width: 70px"></el-input>
        <span style="font-size: 12px">模具&nbsp&nbsp&nbsp</span> <el-input v-model="abnormalFeedBackData.mouldTemporary" :disabled="disableFlag2" style="width: 70px;margin-left: 2px"></el-input>
        <span style="font-size: 12px">工装&nbsp&nbsp&nbsp</span> <el-input v-model="abnormalFeedBackData.workClothesTemporary" :disabled="disableFlag2" style="width: 70px"></el-input>
      </el-form>
      <el-form :inline="true" label-position="top" style="margin-left: 7px;margin-top: 0px;">
        <el-form-item label="长久处理方式">
          <el-input v-model="abnormalFeedBackData.treatment" type="textarea" :disabled="disableFlag3" :rows="3" resize='none' style="width: 562px"></el-input>
        </el-form-item>
      </el-form>
      <el-form :inline="true" label-position="top" style="margin-left: 7px;margin-top: 40px;">
        <span style="font-size: 12px">工艺变更记录:</span>
      </el-form>
      <el-form :inline="true" label-position="top" style="margin-left: 7px;margin-top: 0px;">
        <span style="font-size: 12px">&nbsp温度&nbsp&nbsp&nbsp</span> <el-input v-model="abnormalFeedBackData.temperature" :disabled="disableFlag3" style="width: 70px"></el-input>
        <span style="font-size: 12px">压力&nbsp&nbsp&nbsp</span> <el-input v-model="abnormalFeedBackData.pressure" :disabled="disableFlag3" style="width: 70px"></el-input>
        <span style="font-size: 12px">时间&nbsp&nbsp&nbsp</span> <el-input v-model="abnormalFeedBackData.time" :disabled="disableFlag3" style="width: 70px"></el-input>
        <span style="font-size: 12px">投料量</span> <el-input v-model="abnormalFeedBackData.feeding" :disabled="disableFlag3" style="width: 70px"></el-input>
        <span style="font-size: 12px">机台&nbsp&nbsp&nbsp</span> <el-input v-model="abnormalFeedBackData.resource" :disabled="disableFlag3" style="width: 70px"></el-input>
      </el-form>
      <el-form :inline="true" label-position="top" style="margin-left: 7px;margin-top: 0px;">
        <span style="font-size: 12px">&nbsp功率&nbsp&nbsp&nbsp</span> <el-input v-model="abnormalFeedBackData.efficiency" :disabled="disableFlag3" style="width: 70px"></el-input>
        <span style="font-size: 12px">速度&nbsp&nbsp&nbsp</span> <el-input v-model="abnormalFeedBackData.speed" :disabled="disableFlag3" style="width: 70px"></el-input>
        <span style="font-size: 12px">材料&nbsp&nbsp&nbsp</span> <el-input v-model="abnormalFeedBackData.material" :disabled="disableFlag3" style="width: 70px"></el-input>
        <span style="font-size: 12px">模具&nbsp&nbsp&nbsp</span> <el-input v-model="abnormalFeedBackData.mould" :disabled="disableFlag3" style="width: 70px;margin-left: 2px"></el-input>
        <span style="font-size: 12px">工装&nbsp&nbsp&nbsp</span> <el-input v-model="abnormalFeedBackData.workClothes" :disabled="disableFlag3" style="width: 70px"></el-input>
      </el-form>
      <el-footer style="height:30px;margin-top: 10px;text-align:center">
        <el-button type="primary" @click="saveAbnormalDetail()" v-if="disableFlag4">保存</el-button>
        <el-button type="primary" @click="modalFlag = false">关闭</el-button>

      </el-footer>
    </el-dialog>

  </div>
</template>

<script>
  import {
    getAbnormalCodeList,
    getShopOrderData,
    getAbnormalFeedBackList,
    saveAbnormalData,
    saveAbnormalDetail,
    openFeedBack,
    closeFeedBack,
  } from "@/api/abnormal.js"

  export default {
    name: 'AbnormalFeedBack',
    data () {
      return {

        searchData:{
          userId:this.$store.state.user.name,
          startDate:'',
          endDate:'',
          abnormalCode:'',
          site:'',
          active:'',
        },
        // 导出 start
        exportData: [],
        exportDataStandard: {
          "工厂编号": "site",
          "生产订单": "orderNo",
          "派工单号": "seqNo",
          "物料编码": "partNo",
          "规格型号": "partDesc",
          "操作员": "operatorDesc",
          "生产线": "productionLineDesc",
          "反馈时间": "feedbackDate",
          "反馈人": "feedbackBy",
          "状态": "status",
          "异常原因": "abnormalDesc",
          "异常描述": "abnormalRemark",
          "紧急处理方式": "treatmentTemporary",
          "温度": "temperatureTemporary",
          "压力": "pressureTemporary",
          "时间": "timeTemporary",
          "投料量": "feedingTemporary",
          "机台": "resourceTemporary",
          "功率": "efficiencyTemporary",
          "速度": "speedTemporary",
          "材料": "materialTemporary",
          "模具": "mouldTemporary",
          "工装": "workClothesTemporary",
          "长久处理方式": "treatment",
          "温度 ": "temperature",
          "压力 ": "pressure",
          "时间 ": "time",
          "投料量 ": "feeding",
          "机台 ": "resource",
          "功率 ": "efficiency",
          "速度 ": "speed",
          "材料 ": "material",
          "模具 ": "mould",
          "工装 ": "workClothes",
        },
        exportName: "异常反馈清单"+this.getStrDate(),
        exportHeader: ["异常反馈清单"],
        exportFooter: [],
        exportDefaultValue: "",
        // 导出 end
        dataListLoading:false,
        // 导出 end
        height:200,
        tableData:[],
        addData:{
          site:'',
          seqNo:'',
          orderNo:'',
          shiftNo:'',
          resourceDesc:'',
          feedbackBy:this.$store.state.user.name,
          abnormalCode:'',
          abnormalRemark:'',
        },

        addModalFlag:false,
        modalFlag:false,
        disableFlag4:false,
        disableFlag1:false,
        disableFlag2:false,
        disableFlag3:false,
        abnormalList:[],
        abnormalFeedBackData:{

        },
      }
    },
    mounted() {
      this.$nextTick(()=>{
        this.height = window.innerHeight - 210;
      })
    },
    methods: {
      getAllAbnormalItem(){
        getAbnormalCodeList().then(({data}) => {
          this.abnormalList = data.rows;
        })
      },
      search(){
        getAbnormalFeedBackList(this.searchData).then(({data}) => {
          this.tableData = data.rows;
        })
      },
      addAbnormal(){
        this.addData.seqNo='',
          this.addData.shiftNo='',
          this.addData.site='',
          this.addData.resourceDesc='',
          this.addData.orderNo='',
          this.addData.abnormalRemark='',
          this.addData.abnormalCode='',
        this.getAllAbnormalItem();
        this.addModalFlag=true;
      },
      seqNoCheck(){
        let inData={
          seqNo:this.addData.seqNo
        }
        getShopOrderData(inData).then(({data}) => {
          let rows=data.rows;
          if(rows.length==0){
            this.addData.seqNo='',
            this.addData.shiftNo='',
            this.addData.site='',
            this.addData.resourceDesc='',
            this.addData.orderNo='',
            this.$alert('该派工单不存在！', '错误', {
              confirmButtonText: '确定'
            })
            return false
          }else {
            this.addData.shiftNo=data.rows[0].sShiftNo
            this.addData.site=data.rows[0].site
            this.addData.resourceDesc=data.rows[0].resourceDesc
            this.addData.orderNo=data.rows[0].orderNo
          }
        })
      },
      saveAbnormalData(){
        if( this.addData.seqNo==null||this.addData.seqNo==''){
          this.$alert("请输入派工单号", '错误', {
            confirmButtonText: '确定'
          })
          return false;
        }
        if( this.addData.abnormalCode==null||this.addData.abnormalCode==''){
          this.$alert("请选择异常原因", '错误', {
            confirmButtonText: '确定'
          })
          return false;
        }
        if( this.addData.abnormalRemark==null||this.addData.abnormalRemark==''){
          this.$alert("请输入描述", '错误', {
            confirmButtonText: '确定'
          })
          return false;
        }
        saveAbnormalData(this.addData).then(({data}) => {
          if (data && data.code === 0) {
            this.addModalFlag=false
            this.search()
            this.$message({
              message: '操作成功',
              type: 'success',
              duration: 1500,
              onClose: () => {
              }
            })
          } else {
            this.$alert(data.msg, '错误', {
              confirmButtonText: '确定'
            })
          }
        })
      },
      getDetailModel(row){
        this.abnormalFeedBackData=JSON.parse(JSON.stringify(row));
        this.disableFlag1=true;
        this.disableFlag2=true;
        this.disableFlag3=true;
        this.disableFlag4=false;
        this.modalFlag=true;
      },
      getDetailModel2(row){
        this.abnormalFeedBackData=JSON.parse(JSON.stringify(row));
        if(this.abnormalFeedBackData.status=='已录入'){
          this.abnormalFeedBackData.status='已填写紧急处理'
        }
        this.disableFlag1=true;
        this.disableFlag2=false;
        this.disableFlag3=true;
        this.disableFlag4=true;
        this.modalFlag=true;
      },
      getDetailModel3(row){
        this.abnormalFeedBackData=JSON.parse(JSON.stringify(row));
        if(this.abnormalFeedBackData.status=='已录入'||this.abnormalFeedBackData.status=='已填写紧急处理'){
          this.abnormalFeedBackData.status='已填写长久处理'
        }
        this.disableFlag1=true;
        this.disableFlag2=true;
        this.disableFlag3=false;
        this.disableFlag4=true;
        this.modalFlag=true;
      },
      saveAbnormalDetail(){
        if( this.disableFlag2==false&&(this.abnormalFeedBackData.treatmentTemporary==null||this.abnormalFeedBackData.treatmentTemporary=='')){
          this.$alert("请填写处理方式", '错误', {
            confirmButtonText: '确定'
          })
          return false;
        }
        if( this.disableFlag3==false&&(this.abnormalFeedBackData.treatment==null||this.abnormalFeedBackData.treatment=='')){
          this.$alert("请填写处理方式", '错误', {
            confirmButtonText: '确定'
          })
          return false;
        }
        saveAbnormalDetail(this.abnormalFeedBackData).then(({data}) => {
          if (data && data.code === 0) {
            this.modalFlag=false
            this.search()
            this.$message({
              message: '操作成功',
              type: 'success',
              duration: 1500,
              onClose: () => {
              }
            })
          } else {
            this.$alert(data.msg, '错误', {
              confirmButtonText: '确定'
            })
          }
        })
      },
      closeFeedBack(row){
        this.$confirm(`是否关闭此反馈单?`, '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }).then(() => {
          closeFeedBack(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(() => {
        })
      },
      openFeedBack(row){
        this.$confirm(`是否开启此反馈单?`, '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }).then(() => {
          openFeedBack(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(() => {
        })
      },
      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() {
      this.getAllAbnormalItem();
    }
  }
</script>

<style scoped>

</style>
