<template>
  <div class="customer-css">
    <el-dialog :title="titleCon" v-drag v-bind="$attrs" v-on="$listeners"
               width="610px" style="height: 680px;" class="customer-dialog">
      <el-container>
        <el-container style="height: 140px;">
          <el-form :inline="true" label-position="top" label-width="80px">
            <!-- 不良代码 和日期 -->
            <el-row>
              <el-col :span="6">
                <el-form-item>
                  <span slot="label" style="" @click="getBaseList(89)"><a herf="#">{{labels.defectCode}}</a></span>
                  <el-input ref="defectCode" v-model="pageData.defectCode" @blur="checkDefectDesc" @keydown="upCaseDefectCode"
                            style="width: 120px" @keyup.enter.native="getDefectDesc" ></el-input>
                </el-form-item>
              </el-col>
              <el-col :span="6">
                <el-form-item :label=labels.defectQty>
                  <el-input v-model="pageData.defectQty" style="width: 120px"></el-input>
                </el-form-item>
              </el-col>
              <el-col :span="6">
                <el-form-item :label=labels.reportDate >
                  <el-date-picker v-model="pageData.reportDate"
                                  style="width: 120px"  format="yyyy-MM-dd"
                                  value-format="yyyy-MM-dd">
                  </el-date-picker>
                </el-form-item>
              </el-col>
            </el-row>
            <el-row>
              <el-col :span="18">
                <el-form-item :label=labels.defectDesc>
                  <el-input :readonly="true" v-model="pageData.defectDesc" style="width: 495px"></el-input>
                </el-form-item>
              </el-col>
            </el-row>
          </el-form>
        </el-container>
        <el-container style="margin-left: 60px;">
          <el-form :inline="true" label-position="top"  label-width="80px">
            <!-- 不良代码 和日期 -->
            <el-row style="margin-top: 0px;">
              <el-col :span="6">
                <el-form-item style="margin-top: 15px;">
                  <el-button type="primary" style="margin-left: -10px; margin-bottom: 5px;"
                  @click="reportDefectFun">
                    {{ buttons.insertButton }}</el-button>
                </el-form-item>
              </el-col>
            </el-row>
            <el-row style="margin-top: 15px;">
              <el-col :span="6">
                <el-form-item style="margin-top: 15px;">
                  <el-button type="primary" @click="closeDialog"
                             style="margin-left: -10px; margin-bottom: 5px;">
                    {{buttons.closeButton}}</el-button>
                </el-form-item>
<!--                <el-form-item style="margin-top: 15px;">-->
<!--                  <el-button v-if="showDefault" @click="saveMultiLanguage()" type="primary">多语言设置</el-button>-->
<!--                </el-form-item>-->
              </el-col>
            </el-row>


          </el-form>
        </el-container>
      </el-container>

      <!-- 不良记录 -->
      <el-container>
        <fieldset class="customer-fieldset" style="margin-top: -35px; margin-bottom: 15px;">
          <legend>{{labels.defectList}}</legend>
          <el-container style="width: 580px;" class="customer-tab">
            <el-table
              height="250"
              :data="sfdcDefectList"
              border
              v-loading="dataListLoading"
              style="width: 100%; margin-top: -5px;">
              <el-table-column
                v-for="(item,index) in columnDefectArray" :key="index"
                :sortable="item.columnSortable"
                :prop="item.columnProp"
                :header-align="item.headerAlign"
                :show-overflow-tooltip="item.showOverflowTooltip"
                :align="item.align"
                :fixed="item.fixed==''?false: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-container>

        </fieldset>
      </el-container>


    </el-dialog>
    <!--列表的组件-->
    <Chooselist ref="baseList"  @getBaseData="getBaseData"></Chooselist>
  </div>
</template>

<script>
import Chooselist from '@/views/modules/common/Chooselist';/*列表组件*/
import {
  getScheduleDefectHist,
  checkDefectCode,
  reportDefectCode,
}
from '@/api/yieldReport/com_defect_roll.js';

import {
  searchSysLanguagePackList,
  searchSysLanguageParam,
  searchFunctionButtonList,
  saveButtonList,
  searchSysLanguage,
  searchLanguageListByLanguageCode,
  saveSysLanguageOne,
  searchPageLanguageData,
  removerLanguage,
  saveSysLanguageList
} from "@/api/sysLanguage.js";

var functionId = 'C10000011';
export default {
  data() {
    return {
      titleCon: '报告不良',
      showDefault: false,
      scheduleData: {
        site: this.$store.state.user.site,
        userName: this.$store.state.user.name,
        seqNo: '',
        orderNo: '',
        itemNo: 0,
        partNo: '',
        workCenterNo: '',
        workCenterDesc: '',
        resourceDesc: '',
        rollNo: '',
        partDesc: '',
        planStartTime: '',
        planFinishTime: '',
        qtyRequiredOriginal: 0,
        scheduledDate: '',
        shiftNo: '',
        preItemDesc: '',
        nextItemDesc: '',
        nextItemNo: 0,
        operatorId: '',
        functionName: '',
        currentRollFlag: false
      },
      pageData: {
        site: this.$store.state.user.site,
        userName: this.$store.state.user.name,
        orderNo: '',
        itemNo: '',
        seqNo: '',
        rollNo: '',
        operatorId: '',
        operatorName: '',
        reportDate: '',
        defectCode: '',
        defectDesc: '',
        defectQty: 0,
      },
      operatorData: {
        site: this.$store.state.user.site,
        username: this.$store.state.user.name,
        operatorId: '',
        operatorName: '',
        status: '',
        seqNo: '',
        showFlag: false
      },
      sfdcDefectList: [],
      columnDefectArray: [
        {
          userId: this.$store.state.user.name,
          functionId: 5303,
          serialNumber: '5305DefectHistSeqNo',
          tableId: "5305Material",
          tableName: "不良卷",
          columnProp: "histSeqNo",
          headerAlign: "center",
          align: "center",
          columnLabel: "序号",
          columnWidth: 60,
          columnHidden: false,
          columnImage: false,
          columnSortable: false,
          sortLv: 0,
          status: true,
          fixed: false
        },
        {
          userId: this.$store.state.user.name,
          functionId: 5303,
          serialNumber: '5305DefectDefectCode',
          tableId: "5305Defect",
          tableName: "不良卷",
          columnProp: "defectCode",
          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: 5303,
          serialNumber: '5305DefectDefectCode',
          tableId: "5305Defect",
          tableName: "不良卷",
          columnProp: "defectDesc",
          headerAlign: "center",
          align: "center",
          columnLabel: "不良描述",
          columnWidth: 160,
          columnHidden: false,
          columnImage: false,
          columnSortable: false,
          sortLv: 0,
          status: true,
          fixed: false
        },
        {
          userId: this.$store.state.user.name,
          functionId: 5303,
          serialNumber: '5305DefectDefectQty',
          tableId: "5305Defect",
          tableName: "不良卷",
          columnProp: "defectQty",
          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: 5303,
          serialNumber: '5305DefectReportedDate',
          tableId: "5305Defect",
          tableName: "不良卷",
          columnProp: "reportedDate",
          headerAlign: "center",
          align: "center",
          columnLabel: "报告日期",
          columnWidth: 125,
          columnHidden: false,
          columnImage: false,
          columnSortable: false,
          sortLv: 0,
          status: true,
          fixed: false
        },
        {
          userId: this.$store.state.user.name,
          functionId: 5303,
          serialNumber: '5305DefectReportedBy',
          tableId: "5305Defect",
          tableName: "不良卷",
          columnProp: "reportedBy",
          headerAlign: "center",
          align: "center",
          columnLabel: "报告人",
          columnWidth: 75,
          columnHidden: false,
          columnImage: false,
          columnSortable: false,
          sortLv: 0,
          status: true,
          fixed: false
        },
        {
          userId: this.$store.state.user.name,
          functionId: 5303,
          serialNumber: '5305DefectRemark',
          tableId: "5305Defect",
          tableName: "不良卷",
          columnProp: "remark",
          headerAlign: "center",
          align: "center",
          columnLabel: "备注",
          columnWidth: 160,
          columnHidden: false,
          columnImage: false,
          columnSortable: false,
          sortLv: 0,
          status: true,
          fixed: false
        }
      ],
      dataListLoading: false,
      buttons: {
        insertButton: '插入',
        closeButton: '关闭',
      },
      buttonList: [
        {
          functionId: functionId,
          languageValue: '插入',
          objectId: 'insertButton',
          objectType: 'button',
          tableId: '*'
        }, {
          functionId: functionId,
          languageValue: '关闭',
          objectId: 'closeButton',
          objectType: 'button',
          tableId: '*'
        },
      ],
      queryButton: {
        functionId: functionId,
        table_id: '*',
        languageCode: this.$i18n.locale,
        objectType: 'button'
      },
      labels: {
        titleCon: '报告不良',
        defectCode: '不良代码:',
        defectQty: '不良数量:',
        reportDate: '报告日期:',
        defectDesc: '不良原因:',
        defectList: '不良记录',
        defectQtyMustMoreTanZero: '不良数量必须大于0!',
        pleaseScanDefectCode: '请扫描不良代码!',
      },
      labelsList: [
        {
          functionId: functionId,
          languageValue: '报告不良',
          objectId: 'titleCon',
          objectType: 'label',
          tableId: '*'
        }, {
          functionId: functionId,
          languageValue: '不良代码:',
          objectId: 'defectCode',
          objectType: 'label',
          tableId: '*'
        }, {
          functionId: functionId,
          languageValue: '不良数量:',
          objectId: 'defectQty',
          objectType: 'label',
          tableId: '*'
        }, {
          functionId: functionId,
          languageValue: '报告日期:',
          objectId: 'reportDate',
          objectType: 'label',
          tableId: '*'
        }, {
          functionId: functionId,
          languageValue: '不良原因:',
          objectId: 'defectDesc',
          objectType: 'label',
          tableId: '*'
        }, {
          functionId: functionId,
          languageValue: '不良记录',
          objectId: 'defectList',
          objectType: 'label',
          tableId: '*'
        }, {
          functionId: functionId,
          languageValue: '不良数量必须大于0!',
          objectId: 'defectQtyMustMoreTanZero',
          objectType: 'label',
          tableId: '*'
        }, {
          functionId: functionId,
          languageValue: '请扫描不良代码!',
          objectId: 'pleaseScanDefectCode',
          objectType: 'label',
          tableId: '*'
        },
      ],
      queryLabel: {
        functionId: functionId,
        table_id: '*',
        languageCode: this.$i18n.locale,
        objectType: 'label'
      },
    }
  },
  /*组件*/
  components: {
    Chooselist,/*列表的组件*/
  },
  methods: {

    //初始化组件的参数
    init(scheduleData, operatorData) {
      //初始化参数
      this.scheduleData = scheduleData;
      //初始化操作员对象
      this.operatorData = JSON.parse(JSON.stringify(operatorData));
      //设置参数
      this.pageData.orderNo = scheduleData.orderNo;
      this.pageData.itemNo = scheduleData.itemNo;
      this.pageData.seqNo = scheduleData.seqNo;
      this.pageData.rollNo = scheduleData.rollNo;
      this.pageData.operatorId = operatorData.operatorId;
      this.pageData.operatorName = operatorData.operatorName;
      //清空数据
      this.pageData.defectQty = 0;
      this.pageData.defectCode = '';
      this.pageData.defectDesc = '';
      //重置时间
      this.pageData.reportDate = this.dayjs(new Date()).format('YYYY-MM-DD');
        //判断是否启用多语言
    // this.getMultiLanguageList();  //刷新多语言的信息
      //自动获取焦点
      this.$nextTick(() => {
        this.$refs.defectCode.focus();
      });
      //刷新当前不良记录列表
      this.refreshPageTable();
      //初始化标题
      this.titleCon = this.labels.titleCon;
    },

    /*关闭modal*/
    closeDialog(){
      //刷新报工的页面
      this.$emit('refreshPageData');
      //关闭当前的页面
      this.$emit('update:visible', false);
    },

    /*刷新页面的table数据*/
    refreshPageTable(){
      getScheduleDefectHist(this.pageData).then(({data}) => {
        this.sfdcDefectList = data.rows;
      });
    },

    /*检查不良代码*/
    getDefectDesc(){
      checkDefectCode(this.pageData).then(({data}) => {
        //判断是否存在异常
        if(data.code == 500){
          //this.$message.error(data.msg);
          this.pageData.defectDesc = ''
        }else{
          let resultMap = data.resultMap;
          this.pageData.defectDesc = resultMap.defectDesc;
        }
      })
    },

    /*不提示错误信息的*/
    checkDefectDesc(){
      checkDefectCode(this.pageData).then(({data}) => {
        //判断是否存在异常
        if(data.code == 500){
          this.pageData.defectDesc = ''
        }else{
          let resultMap = data.resultMap;
          this.pageData.defectDesc = resultMap.defectDesc;
        }
      })
    },

    /*报告不良代码*/
    reportDefectFun(){
      //判断数量是否正常
      if(this.pageData.defectQty <= 0){
        this.$message.error('不良数量必须大于0!');
        return false;
      }
      if(this.pageData.defectCode == null || this.pageData.defectCode == ''){
        this.$message.error('请扫描不良代码!');
        return false;
      }
      reportDefectCode(this.pageData).then(({data}) => {
        if(data.code == 500){
          this.$message.error(data.msg);
        }else{
         //清空信息二次报告
          this.pageData.defectQty = 0;
          this.pageData.defectCode = '';
          this.pageData.defectDesc = '';
          //刷新列表
          this.refreshPageTable();
        }
      });
    },

    /*强制大写属性编码*/
    upCaseDefectCode(){
      this.pageData.defectCode = this.pageData.defectCode.toUpperCase();
    },

    /*列表方法的回调*/
    getBaseData(val){
      if (this.tagNo === 89){
        this.pageData.defectCode = val.DefectCode;
        this.pageData.defectDesc = val.DefectDesc;
      }
    },

    // 获取基础数据列表
    getBaseList(val){
      this.tagNo = val
      this.$nextTick(() => {
        let strVal = "";
        let conSql = "";
        if (val === 89){
          strVal =  this.pageData.defectCode;
          conSql = " and A.workCenter_no = '" + this.scheduleData.workCenterNo + "'"
        }
        this.$refs.baseList.init(val, strVal, conSql)
      })
    },

    // 保存 默认配置 列
    async saveMultiLanguage() {
      // 保存页面 button label title 属性
      let buttons = this.buttonList;
      let labels = this.labelsList;
      await saveButtonList(buttons)
      await saveButtonList(labels)
    },

    getMultiLanguageList() {
      //首先查询当前按钮的多语言
      searchFunctionButtonList(this.queryButton).then(({data}) => {
        if (data && data.code == 0 ) {
          this.buttons = data.data
        } else {
          // saveButtonList(this.buttonList).then(({data}) => {
          // })
        }
      });
      //其次查询当前标签的多语言
      searchFunctionButtonList(this.queryLabel).then(({data}) => {
        if (data && data.code == 0 ) {
          this.labels = data.data
        } else {
          // saveButtonList(this.buttonList).then(({data}) => {
          // })
        }
      });
    },

  },
  watch:{
    pageData: {
      deep: true,
      handler: function (newV, oldV) {
        this.pageData.defectCode = this.pageData.defectCode.toUpperCase();
      }
    }
  },
  created() {
    // this.factoryList()
    // this.getLanguageList()
  },

}

</script>

<style scoped lang="scss">


</style>
