<template>
  <div class="customer-css">
    <el-dialog :title="titleCon" v-drag v-bind="$attrs" v-on="$listeners"
               width="900px" style="height: 415px;" class="customer-dialog">
      <el-form :inline="true" label-position="top" style="height: 265px;"
               label-width="80px">
        <!-- 菜单信息 -->
        <el-row>
          <el-col :span="3" >
            <el-form-item :label=labels.currentTime>
              <el-date-picker style="width: 100px;"
                              v-model="pageData.reportDate"
                              format="yyyy-MM-dd"
                              value-format="yyyy-MM-dd"
                              placeholder="">
              </el-date-picker>
            </el-form-item>
          </el-col>
          <el-col :span="3" style="margin-top: 27px;">
            <el-form-item :label="''">
              <el-form-item :label="''">
                <el-time-picker style="width: 100px;"
                                v-model="pageData.reportTime"
                                format="HH:mm:ss"
                                value-format="HH:mm:ss"
                                placeholder="">
                </el-time-picker>
              </el-form-item>
            </el-form-item>
          </el-col>
          <el-col :span="8">
              <el-form-item class="customer-button">
                <el-button type="primary" @click="refreshPageData" style="margin-left: 10px; margin-bottom: 5px;">
                 {{buttons.refreshButton}}</el-button>
              </el-form-item>
              <el-form-item class="customer-button">
                <el-button @click="endTuningAndStartProduceOperation" type="primary" style="margin-left: 10px; margin-bottom: 5px;">
                  {{buttons.endTuningButton}}</el-button>
              </el-form-item>
              <el-form-item class="customer-button">
                <el-button type="primary" @click="closeDialog" style="margin-left: 10px; margin-bottom: 5px;">
                  {{buttons.closeButton}}</el-button>
              </el-form-item>
            <el-button v-if="showDefault" @click="saveMultiLanguage()" type="primary">多语言设置</el-button>
            </el-col>
        </el-row>
        <!-- 主材料 -->
        <el-form>
          <fieldset class="customer-fieldset" style="width: 830px;">
            <legend>{{labels.primaryMaterial}}</legend>
            <el-table height="180"
               :cell-style="customerCellStyle" :cell-class-name="customerCellClassName"
              :data="sfdcMaterialList"
              border
              v-loading="dataListLoading"
              style="width: 100%; margin-top: -5px;">
              <el-table-column

                v-for="(item,index) in columnMaterialArray" :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">
                  <!--<el-input type="number" class="table-input" align="right" @blur="checkValidQty(scope.row)"-->
                  <el-input type="number" class="table-input" align="right" v-if="item.columnHidden"
                            v-model="scope.row[item.columnProp]"></el-input>
                  <span v-else>{{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>
          </fieldset>
        </el-form>
      </el-form>


    </el-dialog>
  </div>
</template>

<script>
import {
  getSfdcMaterialByRollNo,
  endTuningAndStartProduce,
  getSystemTimeData,
} from '@/api/yieldReport/com_end_tuning.js';

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

var functionId = 'C10000010';

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: '',
        workCenterDesc: '',
        resourceDesc: '',
        rollNo: '',
        partDesc: '',
        planStartTime: '',
        planFinishTime: '',
        qtyRequiredOriginal: 0,
        scheduledDate: '',
        shiftNo: '',
        preItemDesc: '',
        nextItemDesc: '',
        nextItemNo: 0,
        currentRollFlag: false
      },
      pageData: {
        site: this.$store.state.user.site,
        username: this.$store.state.user.name,
        seqNo: '',
        orderNo: '',
        itemNo: 0,
        operatorId: '',
        reportDate: '',
        reportTime: '',
      },
      operatorData: {
        site: this.$store.state.user.site,
        username: this.$store.state.user.name,
        operatorId: '',
        operatorName: '',
        status: '',
        seqNo: '',
        showFlag: false
      },
      sfdcMaterialList: [],
      columnMaterialArray: [
        {
          userId: this.$store.state.user.name,
          functionId: 5303,
          serialNumber: '5303MaterialReportDate',
          tableId: "5303Material",
          tableName: "派工单材料",
          columnProp: "reportDate",
          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: 5305,
          serialNumber: '5305MaterialReportedBy',
          tableId: "5305Material",
          tableName: "派工单材料",
          columnProp: "reportedBy",
          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: 5305,
          serialNumber: '5305MaterialRmRollNo',
          tableId: "5305Material",
          tableName: "派工单材料",
          columnProp: "rmRollNo",
          headerAlign: "center",
          align: "center",
          columnLabel: "材料卷号",
          columnWidth: 100,
          columnHidden: false,
          columnImage: false,
          columnSortable: false,
          sortLv: 0,
          status: true,
          fixed: false
        },
        {
          userId: this.$store.state.user.name,
          functionId: 5305,
          serialNumber: '5305MaterialPartNo',
          tableId: "5305Material",
          tableName: "派工单材料",
          columnProp: "partNo",
          headerAlign: "center",
          align: "center",
          columnLabel: "零部件编码",
          columnWidth: 100,
          columnHidden: false,
          columnImage: false,
          columnSortable: false,
          sortLv: 0,
          status: true,
          fixed: false
        },
        {
          userId: this.$store.state.user.name,
          functionId: 5305,
          serialNumber: '5305MaterialStartDate',
          tableId: "5305Material",
          tableName: "派工单材料",
          columnProp: "startDate",
          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: 5305,
          serialNumber: '5305MaterialTransQty',
          tableId: "5305Material",
          tableName: "派工单材料",
          columnProp: "transQty",
          headerAlign: "center",
          align: "right",
          columnLabel: "数量",
          columnWidth: 80,
          columnHidden: false,
          columnImage: false,
          columnSortable: false,
          sortLv: 0,
          status: true,
          fixed: false
        },
        {
          userId: this.$store.state.user.name,
          functionId: 5305,
          serialNumber: '5305MaterialNetIssueQty',
          tableId: "5305Material",
          tableName: "派工单材料",
          columnProp: "netIssueQty",
          headerAlign: "center",
          align: "right",
          columnLabel: "本卷调机数量",
          columnWidth: 100,
          columnHidden: true,
          columnImage: false,
          columnSortable: false,
          sortLv: 0,
          status: true,
          fixed: false
        },
        {
          userId: this.$store.state.user.name,
          functionId: 5305,
          serialNumber: '5305MaterialKeyRMFlag',
          tableId: "5305Material",
          tableName: "派工单材料",
          columnProp: "keyRMFlag",
          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: 5305,
          serialNumber: '5305MaterialSAPBOMItemNo',
          tableId: "5305Material",
          tableName: "派工单材料",
          columnProp: "sAPBOMItemNo",
          headerAlign: "center",
          align: "center",
          columnLabel: "SAP BOM序号",
          columnWidth: 100,
          columnHidden: false,
          columnImage: false,
          columnSortable: false,
          sortLv: 0,
          status: true,
          fixed: false
        }
      ],
      dataListLoading: false,
      buttons: {
        refreshButton: '刷新',
        endTuningButton: '结束调机',
        closeButton: '关闭',
      },
      buttonList: [
        {
          functionId: functionId,
          languageValue: '刷新',
          objectId: 'refreshButton',
          objectType: 'button',
          tableId: '*'
        }, {
          functionId: functionId,
          languageValue: '结束调机',
          objectId: 'endTuningButton',
          objectType: 'button',
          tableId: '*'
        }, {
          functionId: functionId,
          languageValue: '关闭',
          objectId: 'closeButton',
          objectType: 'button',
          tableId: '*'
        },
      ],
      queryButton: {
        functionId: functionId,
        table_id: '*',
        languageCode: this.$i18n.locale,
        objectType: 'button'
      },
      labels: {
        titleCon: '卷调机-卷材料结算',
        currentTime: '当前时间:',
        primaryMaterial: '主材料',
        materialConsumedCantBeNull: '材料上本卷耗用数量不能为空!',
        materialConsumedMustMoreTanZero: '材料耗用数量必须大于0!',
        materialConsumedMoreTanDelivery: '材料耗用数量大于发料数量!',
      },
      labelsList: [
        {
          functionId: functionId,
          languageValue: '卷调机-卷材料结算',
          objectId: 'titleCon',
          objectType: 'label',
          tableId: '*'
        }, {
          functionId: functionId,
          languageValue: '当前时间:',
          objectId: 'currentTime',
          objectType: 'label',
          tableId: '*'
        }, {
          functionId: functionId,
          languageValue: '主材料',
          objectId: 'primaryMaterial',
          objectType: 'label',
          tableId: '*'
        }, {
          functionId: functionId,
          languageValue: '材料耗用数量大于发料数量!',
          objectId: 'materialConsumedMoreTanDelivery',
          objectType: 'label',
          tableId: '*'
        }, {
          functionId: functionId,
          languageValue: '良品总数量必须大于等于0!',
          objectId: 'approvedQtyMustMoreTanZero',
          objectType: 'label',
          tableId: '*'
        }, {
          functionId: functionId,
          languageValue: '该时间段内没有匹配的班次信息,请联系相关人员!',
          objectId: 'noMatchShiftInfo',
          objectType: 'label',
          tableId: '*'
        },
      ],
      queryLabel: {
        functionId: functionId,
        table_id: '*',
        languageCode: this.$i18n.locale,
        objectType: 'label'
      },
    }
  },
  methods: {
    /*初始化页面参数*/
    init(pageData, operatorData) {
      //设置参数
      this.pageData.orderNo = pageData.orderNo;
      this.pageData.itemNo = pageData.itemNo;
      this.pageData.seqNo = pageData.seqNo;
      this.pageData.rollNo = pageData.rollNo;
      this.pageData.operatorId = operatorData.operatorId;
      //重置时间
      //this.pageData.reportDate = this.dayjs(new Date()).format('YYYY-MM-DD');
      //this.pageData.reportTime = this.dayjs(new Date()).format('HH:mm:ss');
      //初始化操作员对象
      this.operatorData = JSON.parse(JSON.stringify(operatorData));
      this.getMultiLanguageList();  //刷新多语言的信息
      //刷新当前的服务器时间
      this.refreshSystemTime();
      //初始化标题
      this.titleCon = this.labels.titleCon;
      //刷新当前派工单的信息
      this.refreshPageData();
    },

    /*刷新服务器的时间*/
    refreshSystemTime(){
      getSystemTimeData().then(({data}) => {
        //判断是否操作成功
        if(data.code == 200){
          //重置系统的时间
          this.pageData.reportDate = data.resultMap.sysDate;
          this.pageData.reportTime = data.resultMap.sysTime;
        }
      });
    },

    /*关闭modal*/
    closeDialog(){
      this.$emit('update:visible', false);
    },

    /*刷新当前的页面参数*/
    refreshPageData(){
      //刷新材料的数据
      this.refreshSfdcMaterialTable();
    },

    //刷新当前卷的时间数据
    refreshSfdcMaterialTable(){
      getSfdcMaterialByRollNo(this.pageData).then(({data}) => {
        this.sfdcMaterialList = data.rows;
      });
    },

    //结束调机和开始生产
    endTuningAndStartProduceOperation(){
      let requestData = {'pageData': JSON.stringify(this.pageData), 'materialList': JSON.stringify(this.sfdcMaterialList)};
      endTuningAndStartProduce(requestData).then(({data}) => {
        //判断操作是否成功
        if(data.code == 500){
          this.$message.error(data.msg);
        }else{
          //关闭上机dialog的页面
          this.$emit('closeDialog');
          //关闭当前的页面
          this.closeDialog();
        }
      });
    },

    /*检查数据是否有效*/
    checkValidQty(row){
      //判断当前是否为空
      if(row.netIssueQty == ''){
        this.$message.error(this.labels.materialConsumedCantBeNull);
        row.netIssueQty = 0;//重新赋值
        return false;
      }
      //判断当前是否有效
      if(row.netIssueQty <= 0){
        this.$message.error(this.labels.materialConsumedMustMoreTanZero);
        return false;
      }
      //判断是否超限
      if(row.netIssueQty > row.transQty){
        this.$message.error(this.materialConsumedMoreTanDelivery);
        row.netIssueQty = 0;//重新赋值
        return false;
      }
    },

    /*添加定制的css类*/
    customerCellClassName({row, column, rowIndex, columnIndex}) {
      if(column.property == 'netIssueQty'){
        return 'customer-number-cell';
      }
    },

    /*添加定制的cess样式*/
    customerCellStyle({row, column, rowIndex, columnIndex}) {
      if(column.property == 'netIssueQty'){
        return 'padding: 0px 0px;';
      }
    },

    // 保存 默认配置 列
    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}) => {
          // })
        }
      });
    },

  },
  created() {
  }
}

</script>

<style scoped lang="scss">
/*调节页面button和input的上下间距*/
.customer-css .customer-button{
  margin-top: 25px;
}

/*调节样式*/
.customer-item{
  margin-top: -10px;
}
/*fieldset下table的样式*/
.customer-fieldset /deep/ .el-table__header th.is-leaf{
  line-height: 16px;
}

/deep/ .customer-tab .el-tabs__content{
  padding: 0px !important;
}

/*table中input 修改样式*/
/deep/ div.table-input {
  padding: 0px 0px;
  height: 25px !important;
}
/*table中input*/
div.table-input /deep/ input.el-input__inner{
  padding: 0px 0px;
  height: 23px !important;
  text-align: right;
}


</style>
