<template>
  <div class="app-container">
    <el-card class="topCard">
      <div class="topDiv">
        <el-button
          type="primary"
          plain
          size="small"
          icon="el-icon-search"
          style="margin-left: 10px"
          @click="searchOrder()">查询计划生产数据
        </el-button>
      </div>
      <el-table
        v-loading="loading"
        ref="multipleTable"
        :data="tableData"
        :header-cell-style="{background:'#eef1f6',color:'#606266'}"
        :cell-class-name="tableRowClassName"
        element-loading-text="数据加载中"
        element-loading-spinner="el-icon-loading"
        element-loading-background="rgba(0, 0, 0, 0.2)"
        class="main"
        tooltip-effect="dark"
        height="740px"
        style="width: 100%"
        border
        size="mini"
        @selection-change="handleSelectionChange">
        <el-table-column align="center" label="排序" width="60">
          <template slot-scope="scope">
            {{ scope.row.排序 }}
          </template>
        </el-table-column>
        <el-table-column align="center" label="项目名称">
          <template slot-scope="scope">
            {{ scope.row.制造编号 }}
          </template>
        </el-table-column>
        <el-table-column align="center" label="项目名称">
          <template slot-scope="scope">
            {{ scope.row.项目名称 }}
          </template>
        </el-table-column>
        <el-table-column align="center" label="间隔号">
          <template slot-scope="scope">
            {{ scope.row.间隔号 }}
          </template>
        </el-table-column>
        <el-table-column align="center" label="单元号">
          <template slot-scope="scope">
            {{ scope.row.单元号 }}
          </template>
        </el-table-column>
        <el-table-column align="center" label="DS/ES">
          <template slot-scope="scope">
            {{ scope.row.机型文本 }}
          </template>
        </el-table-column>
        <el-table-column align="center" label="附件类型">
          <template slot-scope="scope">
            {{ scope.row.附件类型 }}
          </template>
        </el-table-column>
        <el-table-column align="center" label="是否上线" width="80">
          <template slot-scope="scope">
            <el-tag v-if="scope.row.是否上线 == 0" size="mini" type="info">未上线</el-tag>
            <el-tag v-else-if="scope.row.是否上线 == 1" size="mini" type="success">已上线</el-tag>
          </template>
        </el-table-column>
        <el-table-column align="center" label="上线时间" width="140">
          <template slot-scope="scope">
            {{ scope.row.上线时间 }}
          </template>
        </el-table-column>
        <el-table-column align="center" label="是否下线" width="80">
          <template slot-scope="scope">
            <el-tag v-if="scope.row.是否下线 == 0" size="mini" type="info">未下线</el-tag>
            <el-tag v-else-if="scope.row.是否下线 == 1" size="mini" type="success">已下线</el-tag>
          </template>
        </el-table-column>
        <el-table-column align="center" label="下线时间" width="140">
          <template slot-scope="scope">
            {{ scope.row.下线时间 }}
          </template>
        </el-table-column>
        <el-table-column align="center" label="A10状态" width="80">
          <template slot-scope="scope">
            <el-tag v-if="scope.row.A10 == 0" size="mini" type="info">未开始</el-tag>
            <el-tag v-else-if="scope.row.A10 == 1" size="mini" type="success">已完成</el-tag>
          </template>
        </el-table-column>
        <el-table-column align="center" label="A20状态" width="80">
          <template slot-scope="scope">
            <el-tag v-if="scope.row.A20 == 0" size="mini" type="info">未开始</el-tag>
            <el-tag v-else-if="scope.row.A20 == 1" size="mini" type="success">已完成</el-tag>
          </template>
        </el-table-column>
        <el-table-column align="center" label="A30状态" width="80">
          <template slot-scope="scope">
            <el-tag v-if="scope.row.A30 == 0" size="mini" type="info">未开始</el-tag>
            <el-tag v-else-if="scope.row.A30 == 1" size="mini" type="success">已完成</el-tag>
          </template>
        </el-table-column>
        <el-table-column align="center" label="A40状态" width="80">
          <template slot-scope="scope">
            <el-tag v-if="scope.row.A40 == 0" size="mini" type="info">未开始</el-tag>
            <el-tag v-else-if="scope.row.A40 == 1" size="mini" type="success">已完成</el-tag>
          </template>
        </el-table-column>
        <el-table-column align="center" label="A50状态" width="80">
          <template slot-scope="scope">
            <el-tag v-if="scope.row.A50 == 0" size="mini" type="info">未开始</el-tag>
            <el-tag v-else-if="scope.row.A50 == 1" size="mini" type="success">已完成</el-tag>
          </template>
        </el-table-column>
        <el-table-column align="center" label="A60状态" width="80">
          <template slot-scope="scope">
            <el-tag v-if="scope.row.A60 == 0" size="mini" type="info">未开始</el-tag>
            <el-tag v-else-if="scope.row.A60 == 1" size="mini" type="success">已完成</el-tag>
          </template>
        </el-table-column>

      </el-table>
      <!--<div class="block" style="margin-top: 10px;">-->
      <!--  <el-pagination-->
      <!--    v-if="!loading"-->
      <!--    :current-page="pageCurrent"-->
      <!--    :page-sizes="[15, 30, 40]"-->
      <!--    :page-size="pageSize"-->
      <!--    :total="total"-->
      <!--    layout="total, sizes, prev, pager, next, jumper"-->
      <!--    @size-change="handleSizeChange"-->
      <!--    @current-change="handleCurrentChange"/>-->
      <!--</div>-->
    </el-card>
    <el-dialog :visible.sync="dialogFormVisible" title="创建物料" center style="min-height: 200px;" width="400px">
      <el-form ref="form" :model="form" :rules="rules" label-position="right" label-width="110px">
        <el-form-item label="工位号" prop="stationNumberValue" style="display: inline-block">
          <el-select v-model="form.stationNumberValue" size="small" placeholder="请选择工位号">
            <el-option
              v-for="item in stationNumber"
              :key="item.value"
              :label="item.label"
              :value="item.value"/>
          </el-select>
        </el-form-item>
        <el-form-item label="物料号" prop="engineTypeValue" style="display: inline-block">
          <el-select v-model="form.engineTypeValue" size="small" placeholder="请选择物料号">
            <el-option
              v-for="item in engineType"
              :key="item.value"
              :label="item.label"
              :value="item.value"/>
          </el-select>
        </el-form-item>
        <!--        <el-form-item label="物料号" prop="orderNumber" style="display: inline-block">-->
        <!--          <el-input-->
        <!--            v-model="form.orderNumber"-->
        <!--            clearable-->
        <!--            size="small"-->
        <!--            placeholder="请输入物料号"/>-->
        <!--        </el-form-item>-->
        <el-form-item label="零件图号" prop="partDrawing" style="display: inline-block">
          <el-input
            v-model="form.partDrawing"
            clearable
            size="small"
            placeholder="请输入零件图号"/>
        </el-form-item>
        <el-form-item label="零件名称" prop="partName" style="display: inline-block">
          <el-input
            v-model="form.partName"
            clearable
            size="small"
            placeholder="零件名称"/>
        </el-form-item>
        <!--        <el-form-item label="数量" prop="number" style="display: inline-block">-->
        <!--          <el-input-->
        <!--            v-model="form.number"-->
        <!--            clearable-->
        <!--            size="small"-->
        <!--            placeholder="请输入数量"/>-->
        <!--        </el-form-item>-->
        <!--        <el-form-item label="生产厂家" prop="manufactor" style="display: inline-block">-->
        <!--          <el-input-->
        <!--            v-model="form.manufactor"-->
        <!--            clearable-->
        <!--            size="small"-->
        <!--            placeholder="请输入生产厂家"/>-->
        <!--        </el-form-item>-->
        <el-form-item label="是否验证" prop="verificationValue" style="display: inline-block">
          <el-select v-model="form.verificationValue" size="small" placeholder="请选择产品型号">
            <el-option
              v-for="item in verification"
              :key="item.value"
              :label="item.label"
              :value="item.value"/>
          </el-select>
        </el-form-item>
        <!--        <el-form-item label="是否主物料" prop="mainMaterialValue" style="display: inline-block">-->
        <!--          <el-select v-model="form.mainMaterialValue" size="small" placeholder="请选择产品型号">-->
        <!--            <el-option-->
        <!--              v-for="item in mainMaterial"-->
        <!--              :key="item.value"-->
        <!--              :label="item.label"-->
        <!--              :value="item.value"/>-->
        <!--          </el-select>-->
        <!--        </el-form-item>-->
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button size="small" @click="callOff('form')">取消</el-button>
        <el-button type="primary" size="small" @click="submitAdd('form')">确定</el-button>
      </div>
    </el-dialog>
    <el-dialog :visible.sync="dialogFormVisible1" title="编辑物料" center style="min-height: 200px" width="400px">
      <el-form ref="form1" :model="form1" :rules="rules1" label-position="right" label-width="110px">
        <el-form-item label="工位号" prop="stationNumberValue" style="display: inline-block">
          <el-input
            v-model="form1.stationNumberValue"
            readonly
            size="small"
            placeholder="工位号"/>
        </el-form-item>
        <el-form-item label="零件图号" prop="partDrawing" style="display: inline-block">
          <el-input
            v-model="form1.partDrawing"
            readonly
            size="small"
            placeholder="请输入零件图号"/>
        </el-form-item>
        <!--        <el-form-item label="物料号" prop="orderNumber" style="display: inline-block">-->
        <!--          <el-input-->
        <!--            v-model="form1.orderNumber"-->
        <!--            clearable-->
        <!--            size="small"-->
        <!--            placeholder="请输入物料号"/>-->
        <!--        </el-form-item>-->
        <el-form-item label="零件名称" prop="partName" style="display: inline-block">
          <el-input
            v-model="form1.partName"
            clearable
            size="small"
            placeholder="零件名称"/>
        </el-form-item>
        <!--        <el-form-item label="数量" prop="number" style="display: inline-block">-->
        <!--          <el-input-->
        <!--            v-model="form1.number"-->
        <!--            clearable-->
        <!--            size="small"-->
        <!--            placeholder="请输入数量"/>-->
        <!--        </el-form-item>-->
        <!--        <el-form-item label="生产厂家" prop="manufactor" style="display: inline-block">-->
        <!--          <el-input-->
        <!--            v-model="form1.manufactor"-->
        <!--            clearable-->
        <!--            size="small"-->
        <!--            placeholder="请输入生产厂家"/>-->
        <!--        </el-form-item>-->
        <el-form-item label="是否验证" prop="verificationValue" style="display: inline-block">
          <el-select v-model="form1.verificationValue" size="small" placeholder="请选择产品型号">
            <el-option
              v-for="item in verification"
              :key="item.value"
              :label="item.label"
              :value="item.value"/>
          </el-select>
        </el-form-item>
        <!--        <el-form-item label="是否主物料" prop="mainMaterialValue" style="display: inline-block">-->
        <!--          <el-select v-model="form1.mainMaterialValue" size="small" placeholder="请选择产品型号">-->
        <!--            <el-option-->
        <!--              v-for="item in mainMaterial"-->
        <!--              :key="item.value"-->
        <!--              :label="item.label"-->
        <!--              :value="item.value"/>-->
        <!--          </el-select>-->
        <!--        </el-form-item>-->
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button size="small" @click="callOff1()">取消</el-button>
        <el-button type="primary" size="small" @click="submitAdd1()">确定</el-button>
      </div>
    </el-dialog>
    <el-dialog :visible.sync="dialogFormVisible3" title="物料导入" center style="min-height: 200px" width="900px">
      <div class="app-container">
        <upload-excel-component style="margin-left: 15%" @on-selected-file="selected"/>
        <div style="margin-left: 395px">
          <span v-show="worksheet.length > 1" style="cursor: pointer;width: 50px;" @click="removeAll"><i
            class="el-icon-close"/></span>
          <el-upload :on-remove="handleRemove" :file-list="itemFile" style="width: 220px" action=""/>
        </div>
        <div v-if="worksheet.length !== 0" style="margin: 3px 0 0 395px">
          <el-button icon="el-icon-upload2" type="primary" size="small" @click="basicPartsToSQL">导入到数据库
          </el-button>
        </div>
      </div>
    </el-dialog>
    <el-dialog :visible.sync="dialogFormVisible4" title="数据复制" center style="min-height: 200px" width="400px">
      <el-form ref="form1" :model="form4" :rules="rules4" label-position="right" label-width="110px">
        <el-form-item label="产品型号" prop="engineTypeValue" style="display: inline-block">
          <el-select v-model="form4.engineTypeValue" placeholder="请选择产品型号" size="small">
            <el-option
              v-for="item in engineType"
              :key="item.value"
              :label="item.label"
              :value="item.value"
              size="mini"/>
          </el-select>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button size="small" @click="callOff4()">取消</el-button>
        <el-button type="primary" size="small" @click="submitAdd4()">确定</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { sleep } from '../../../utils/tool'

export default {
  data() {
    return {
      stationNumber: [],
      stationNumberValue: '',
      stationName: '',
      worksheet: [],
      itemFile: [],
      unSpecification: [],
      method1: '', // 机床1加工方式
      method2: '', // 机床2加工方式
      method1Explain: '', // 机床1加工方式说明
      method2Explain: '', // 机床2加工方式说明
      stockQuantity: '', // 库存数量
      stockNum: '', // 库位号
      dialogFormVisible4: false,
      explain: false,
      orderNumber: [],
      orderNumberValue: '',
      engineTypeValue: '',
      engineType: [],
      OKWarehouseValue: '',
      OKWarehouse: [],
      NGWarehouseValue: '',
      NGWarehouse: [],
      verification: [
        {
          label: '否',
          value: 0
        }, {
          label: '是',
          value: 1
        }
      ],
      mainMaterial: [
        {
          label: '否',
          value: 0
        }, {
          label: '是',
          value: 1
        }
      ],
      form: { // 创建订单
        partName: '',
        stationNumberValue: '',
        orderNumber: '',
        engineTypeValue: '',
        partDrawing: '',
        number: '',
        manufactor: '',
        verificationValue: 1,
        mainMaterialValue: 0
      },
      rules: { // 创建订单验证
        partName: [{ required: true, message: '请输入零件名称', trigger: 'blur' }],
        stationNumberValue: [{ required: true, message: '请选择工位号', trigger: 'change' }],
        // orderNumber: [{ required: true, message: '请输入物料号', trigger: 'blur' }],
        engineTypeValue: [{ required: true, message: '请选择机型', trigger: 'blur' }],
        partDrawing: [{ required: true, message: '请输入零件图号', trigger: 'blur' }],
        // number: [{ required: true, message: '请输入数量', trigger: 'blur' }],
        // manufactor: [{ required: true, message: '请输入生产厂家', trigger: 'blur' }],
        verificationValue: [{ required: true, message: '请选择是否验证', trigger: 'change' }]
        // mainMaterialValue: [{ required: true, message: '请选择是否主物料', trigger: 'change' }]
      },
      form1: { // 修改订单
        partName: '',
        stationNumberValue: '',
        orderNumber: '',
        partDrawing: '',
        number: '',
        manufactor: '',
        verificationValue: 1,
        mainMaterialValue: 0,
        ID: ''
      },
      rules1: { // 修改订单验证
        partName: [{ required: true, message: '请输入零件名称', trigger: 'blur' }],
        // orderNumber: [{ required: true, message: '请输入物料号', trigger: 'blur' }],
        // partDrawing: [{ required: true, message: '请输入零件图号', trigger: 'blur' }],
        // number: [{ required: true, message: '请输入数量', trigger: 'blur' }],
        // manufactor: [{ required: true, message: '请输入生产厂家', trigger: 'blur' }],
        verificationValue: [{ required: true, message: '请选择是否验证', trigger: 'change' }]
        // stationNumberValue: [{ required: true, message: '请选择工位号', trigger: 'change' }]
        // mainMaterialValue: [{ required: true, message: '请选择是否主物料', trigger: 'change' }]
      },
      form2: { // 订单下发
        planQuantity: '',
        orderNumber: '',
        engineTypeValue: '',
        MID: '',
        IssuedQuantity: 1
      },
      form4: {
        engineTypeValue: ''
      },
      rules4: { // 订单下发验证
        engineTypeValue: [{ required: true, message: '请选择机型', trigger: 'blur' }]
      },
      // planTime: [new Date(new Date().getTime() - 3600 * 1000 * 24 * 3000), new Date(new Date().getTime())],
      planTime: [],
      tableData: [],
      importData: [],
      dialogFormVisible: false, // 创建订单
      dialogFormVisible1: false, // 修改订单
      dialogFormVisible3: false, // 订单导入
      states: '', // 当前库存状态
      MIDGroup: [],
      total: 0, // 总条数
      pageList: 30, // 每页显示条数
      pageSize: 30, // 每页显示条数
      pageCurrent: 1, // 后台获取页
      loading: false
    }
  },
  watch: {
    stationNumberValue() {
      for (let i = 0; i < this.stationNumber.length; i++) {
        if (this.stationNumber[i].value === this.stationNumberValue) {
          console.log(this.stationNumber[i].label, 'this.stationNumber[i].label')
          this.stationName = this.stationNumber[i].labelName
        }
      }
      if (!this.stationNumberValue) {
        this.stationName = ''
      }
    }
  },
  created() {
    this.getStationNumber()
    this.getEndStation()
  },
  methods: {
    removeAll() {
      this.worksheet = []
      this.itemFile = []
    },
    async selected(data) { // 选择需要导入的文件
      console.log(data, 1111)
      this.worksheet = data.worksheet // 获取每个多选文件的表格坐标及值
      this.itemFile = data.itemFile
    },
    handleRemove(file) { // 移除文件
      for (let i = 0; i < this.itemFile.length; i++) {
        if (this.itemFile[i].name === file.name) {
          this.worksheet.splice(i, 1)
          this.itemFile.splice(i, 1)
        }
      }
    },
    /* 验证内容是否英文数字以及下划线*/
    // isPassword(rule, value, callback) {
    //   const reg = /^[_a-zA-Z0-9]+$/
    //   if (value === '' || value === undefined || value == null) {
    //     callback()
    //   } else {
    //     if (!reg.test(value) || value.length !== 7) {
    //       callback(new Error('订单仅由数字和字母组成，且只能位7位'))
    //     } else {
    //       callback()
    //     }
    //   }
    // },
    // 初始化获取产品型号
    getEndStation() {
      this.engineType = []
      var param = []
      var Data = this.CreateData('11', 'MES_基本变量_机型代码_查询', param)
      this.ExecDatabase(Data).then(response => {
        for (let i = 0; i < response.data.length; i++) {
          if (response.data.length) this.engineTypeValue = response.data[0].发动机型号
          this.engineType.push({
            label: response.data[i].发动机型号,
            value: response.data[i].发动机型号
          })
        }
      }).then(() => {
        this.searchOrder()
      })
    },
    // 初始化获取订单号
    getStationNumber() {
      this.stationNumber = []
      var param = []
      var Data = this.CreateData('11', 'MES_计划BOM_工位与名称_查询', param)
      this.ExecDatabase(Data).then(response => {
        // if (response.data.length) {
        //   this.stationNumberValue = response.data[0].工位号
        //   this.stationName = response.data[0].工序名称
        // }
        for (let i = 0; i < response.data.length; i++) {
          this.stationNumber.push({
            label: response.data[i].工位号,
            value: response.data[i].工位号,
            labelName: response.data[i].工位名称
          })
        }
      })
    },
    // 查询订单
    searchOrder() {
      this.tableData = []
      var param = []
      var Data = this.CreateData('11', '合力_订单生产状态_查询', param)
      this.ExecDatabase(Data).then(response => {
        if (response.data.length !== 0) {
          this.tableData = response.data
        }
      })
    },
    // 创建订单
    creatOrder(formName) {
      if (this.$refs[formName] !== undefined) {
        this.$refs[formName].resetFields()
      }
      this.form.partName = ''
      this.form.stationNumberValue = ''
      this.form.orderNumber = ''
      this.form.engineTypeValue = ''
      this.form.partDrawing = ''
      this.form.number = ''
      this.form.manufactor = ''
      this.form.verificationValue = 1
      this.form.mainMaterialValue = 0
      this.dialogFormVisible = true
    },
    // 提交新增订单
    submitAdd(formName) {
      this.$refs[formName].validate((valid) => {
        if (valid) {
          var param = []
          param[0] = ['工位号', this.form.stationNumberValue]
          param[1] = ['发动机型号', this.form.engineTypeValue]
          param[2] = ['物料号', this.form.orderNumber]
          param[3] = ['零件名称', this.form.partName]
          param[4] = ['零件图号', this.form.partDrawing]
          param[5] = ['生产厂家', this.form.manufactor]
          param[6] = ['是否验证', this.form.verificationValue]
          param[7] = ['是否主物料', this.form.mainMaterialValue]
          param[8] = ['零件数量', this.form.number]
          var Data = this.CreateData('11', '电子看板_装配零件_SPC_增加', param)
          this.ExecDatabase(Data).then(response => {
            if (response.data[0]['result'] === '1') {
              this.$message({
                message: response.data[0]['msg'],
                type: 'success'
              })
              this.searchOrder()
            } else {
              this.$message.error(response.data[0]['msg'])
            }
          })
        } else {
          return false
        }
      })
    },
    // 取消新增订单
    callOff(formName) {
      this.dialogFormVisible = false
      this.$refs[formName].resetFields()
    },
    copyOrder(formName) {
      if (this.tableData.length) {
        if (this.$refs[formName] !== undefined) {
          this.$refs[formName].resetFields()
        }
        this.dialogFormVisible4 = true
      } else {
        this.$message({
          message: '暂无可复制的数据！',
          type: 'error'
        })
      }
    },
    callOff4() {
      this.dialogFormVisible4 = false
    },
    async submitAdd4() {
      let stationNumberValue, orderNumber, partName, partDrawing, manufactor, verificationValue, mainMaterialValue,
        number
      for (let i = 0; i < this.tableData.length; i++) {
        stationNumberValue = this.tableData[i].工位号
        orderNumber = this.tableData[i].物料号
        partName = this.tableData[i].零件名称
        partDrawing = this.tableData[i].零件图号
        manufactor = this.tableData[i].生产厂家
        verificationValue = this.tableData[i].是否验证
        mainMaterialValue = this.tableData[i].是否主物料
        number = this.tableData[i].零件数量
        await this.submitAdd5(stationNumberValue, orderNumber, partName, partDrawing, manufactor, verificationValue, mainMaterialValue, number)
      }
      this.$message({
        message: '增加成功',
        type: 'success'
      })
      this.dialogFormVisible4 = false
      this.engineTypeValue = this.form4.engineTypeValue
      this.searchOrder()
    },
    async submitAdd5(stationNumberValue, orderNumber, partName, partDrawing, manufactor, verificationValue, mainMaterialValue, number) {
      var param = []
      param[0] = ['工位号', stationNumberValue]
      param[1] = ['发动机型号', this.form4.engineTypeValue]
      param[2] = ['物料号', orderNumber]
      param[3] = ['零件名称', partName]
      param[4] = ['零件图号', partDrawing]
      param[5] = ['生产厂家', manufactor]
      param[6] = ['是否验证', verificationValue]
      param[7] = ['是否主物料', mainMaterialValue]
      param[8] = ['零件数量', number]
      var Data = this.CreateData('12', '电子看板_装配零件_SPC_增加', param)
      return new Promise((resolve, reject) => {
        this.ExecDatabase(Data).then(response => {
          if (response.data[0].result === '1') {
            console.log('1')
            resolve(1)
          } else {
            console.log('0')
            reject(2)
          }
        })
      })
    },
    // 导入订单
    importOrder() {
      this.dialogFormVisible3 = true
    },
    // 删除订单
    deleteOrder(row) {
      this.$confirm('此操作将永久删除该缓存数据, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        var param = []
        param[0] = ['总成号', row.总成号]
        var Data = this.CreateData('12', '合力_轮毂桥壳缓存数据_不合格件_删除', param)
        this.ExecDatabase(Data).then(response => {
          if (response.data[0].result === '1') {
            this.$message.success('删除成功')
            this.searchOrder()
          } else {
            this.$message.error('删除失败')
          }
        })
      }).catch(() => {
        this.$message({
          type: 'info',
          message: '已取消删除'
        })
      })
    },
    // 修改订单
    handleEdit(row) {
      if (this.$refs['form1'] !== undefined) {
        this.$refs['form1'].resetFields()
      }
      this.form1.partName = row.零件名称
      this.form1.stationNumberValue = row.工位号
      // this.form1.orderNumber = row.物料号
      this.form1.partDrawing = row.零件图号
      // this.form1.number = row.零件数量
      // this.form1.manufactor = row.生产厂家
      this.form1.verificationValue = Number(row.是否验证)
      // this.form1.mainMaterialValue = Number(row.是否主物料)
      this.form1.ID = row.id
      this.dialogFormVisible1 = true
    },
    // 提交修改订单
    submitAdd1() {
      this.$refs['form1'].validate((valid) => {
        if (valid) {
          var param = []
          param[0] = ['ID', this.form1.ID]
          param[1] = ['物料号', this.form1.orderNumber]
          param[2] = ['零件名称', this.form1.partName]
          param[3] = ['零件图号', this.form1.partDrawing]
          // param[5] = ['完成数量', this.form.quantityCompletion]
          param[4] = ['生产厂家', this.form1.manufactor]
          param[5] = ['是否验证', this.form1.verificationValue]
          param[6] = ['是否主物料', this.form1.mainMaterialValue]
          param[7] = ['零件数量', this.form1.number]
          param[8] = ['工位号', this.form1.stationNumberValue]
          param[9] = ['发动机型号', this.engineTypeValue]
          var Data = this.CreateData('12', '电子看板_装配零件_SPC_修改', param)
          this.ExecDatabase(Data).then(response => {
            if (response.data[0].result === '1') {
              this.$message({
                message: '修改成功',
                type: 'success'
              })
              this.searchOrder()
            } else {
              this.$message.error('修改失败！')
            }
          })
          this.dialogFormVisible1 = false
        } else {
          return false
        }
      })
    },
    // 取消修改订单
    callOff1() {
      this.dialogFormVisible1 = false
      this.$refs['form1'].resetFields()
    },
    // 多选
    handleSelectionChange(val) {
      this.MIDGroup = []
      for (let i = 0; i < val.length; i++) {
        this.MIDGroup.push(val[i].MID)
      }
    },
    handleAdd() {
      if (this.tableData.length === 0) {
        this.$message.error('内容为空')
      } else {
        var param = []
        var Data = this.CreateData('11', '确认导入物料字典_新增', param)
        this.ExecDatabase(Data).then(response => {
          if (response.data.length > 0) {
            this.notes = ''
            for (var i = 0; i < response.data.length; i++) {
              this.notes = response.data[i].导入结果 + '、 ' + this.notes
            }
            this.$message({
              message: '物料字典信息更新成功',
              type: 'warning'
            })
            this.isShow = true
            this.tableData = []
          } else {
            this.tableData = []
            this.$message({
              message: '物料字典信息更新成功',
              type: 'success'
            })
          }
        })
      }
    },
    // 查询
    searchTable() {
      var param = []
      var Data = this.CreateData('11', '生产计划导入_确认导入前临时表_查询', param, this.pageSize, this.pageList)
      this.ExecDatabase(Data).then(response => {
        console.log(response.data)
        this.tableData = response.data.rows
        this.total = response.data.total
        this.loading = false
      })
    },
    // 表格颜色变化
    tableRowClassName({ row, rowIndex }) {
      // console.log(row, rowIndex)
      if (rowIndex % 2 === 0) {
        return 'black'
      } else {
        return 'red'
      }
    },
    async basicPartsToSQL() {
      try {
        this.loading = true
        this.err = 0
        for (let j = this.worksheet.length - 1; j >= 0; j--) {
          let partName,
            stationNumberValue,
            orderNumber,
            engineTypeValue,
            engineTypeNumber,
            partDrawing,
            partBatchNumber,
            number,
            manufactor,
            verificationValue,
            mainMaterialValue,
            isPreservation
          // var param = []
          // param[0] = ['物料号', this.worksheet[j].L2.v]
          // console.log('物料号', this.worksheet[j].L2.v)
          // var Data = this.CreateData('11', '电子看板_装配零件_总物料表_导入校验', param)
          // const response = await this.ExecDatabase(Data)
          // const res = response.data
          // if (res[0].Column1 !== 0) {
          //   this.$notify.error(`${this.worksheet[j].L2.v}已经存在，请确认文件是否正确`)
          //   this.loading = false
          //   this.disabled = false
          //   return
          // }
          for (let i = 0; i < 999; i++) {
            const attr = i + 2
            stationNumberValue = this.worksheet[j][`A${attr}`] ? this.worksheet[j][`A${attr}`].v : ''
            engineTypeNumber = this.worksheet[j][`B${attr}`] ? this.worksheet[j][`B${attr}`].v : ''
            partDrawing = this.worksheet[j][`C${attr}`] ? this.worksheet[j][`C${attr}`].v : ''
            partName = this.worksheet[j][`D${attr}`] ? this.worksheet[j][`D${attr}`].v : ''
            number = this.worksheet[j][`E${attr}`] ? this.worksheet[j][`E${attr}`].v : ''
            partBatchNumber = this.worksheet[j][`F${attr}`] ? this.worksheet[j][`F${attr}`].v : ''
            manufactor = this.worksheet[j][`G${attr}`] ? this.worksheet[j][`G${attr}`].v : ''
            engineTypeValue = this.worksheet[j][`H${attr}`] ? this.worksheet[j][`H${attr}`].v : ''
            verificationValue = this.worksheet[j][`I${attr}`] ? this.worksheet[j][`I${attr}`].v : ''
            mainMaterialValue = this.worksheet[j][`J${attr}`] ? this.worksheet[j][`J${attr}`].v : ''
            isPreservation = this.worksheet[j][`K${attr}`] ? this.worksheet[j][`K${attr}`].v : ''
            orderNumber = this.worksheet[j][`L${attr}`] ? this.worksheet[j][`L${attr}`].v : ''
            if (!stationNumberValue && !engineTypeNumber &&
              !partDrawing && !partName && !number && !partBatchNumber &&
              !manufactor && !engineTypeValue && !verificationValue && !mainMaterialValue && !isPreservation && !orderNumber) {
              break
            } else if (!orderNumber) { // 名称不能为空
              this.$notify.error(`物料号不能为空`)
              break
            } else if (Number(partDrawing) <= 0) { // 数量不能为0
              this.$notify.error(`零件图号不能为空`)
              break
            } else {
              if (orderNumber) {
                await sleep(50)
                await this.insertToSQL(stationNumberValue,
                  engineTypeNumber,
                  partDrawing,
                  partName,
                  number,
                  partBatchNumber,
                  manufactor,
                  engineTypeValue,
                  verificationValue,
                  mainMaterialValue,
                  isPreservation,
                  orderNumber)
              } else {
                this.loading = false
                this.disabled = false
                this.$notify.error(`请确认导入文件是否正确`)
                return
              }
            }
          }
          this.itemFile.splice(j, 1)
          this.worksheet.splice(j, 1)
          this.err === 0 ? this.$message.success(`导入完成`) : this.$message.error(`导入失败物料个数为${this.err}，请检查文件是否正确`)
        }
        this.loading = false
        this.searchOrder()
      } catch (e) {
        this.err++
        this.loading = false
        this.$notify.error(`导入数据错误,请检查导入文件是否正确`)
        console.log(`导入数据错误${e}`)
      }
    },
    async insertToSQL(stationNumberValue,
      engineTypeNumber,
      partDrawing,
      partName,
      number,
      partBatchNumber,
      manufactor,
      engineTypeValue,
      verificationValue,
      mainMaterialValue,
      isPreservation,
      orderNumber) {
      try {
        var param = []
        param[0] = ['工位号', stationNumberValue]
        param[1] = ['产品型号代码', engineTypeNumber]
        param[2] = ['零件图号', partDrawing]
        param[3] = ['零件名称', partName]
        param[4] = ['零件数量', number]
        param[5] = ['零件批号', partBatchNumber]
        param[6] = ['生产厂家', manufactor]
        param[7] = ['发动机型号', engineTypeValue]
        param[8] = ['是否验证', verificationValue]
        param[9] = ['是否主物料', mainMaterialValue]
        param[10] = ['是否保存', isPreservation]
        param[11] = ['物料号', orderNumber]
        var Data = this.CreateData('12', '物料数据_导入Excel', param)
        const response = await this.ExecDatabase(Data)
        const res = response.data
        if (res[0].result === '0') {
          this.$notify.error(`${orderNumber}导入失败`)
        } else {
          this.$notify.error(`${orderNumber}导入成功`)
        }
      } catch (e) {
        this.err++
        this.$message.error(`导入数据错误`)
        console.log(`导入数据错误${e}`)
      }
    },
    exportExcel() {
      if (this.tableData.length !== 0) {
        var param = []
        param[0] = ['发动机型号', this.engineTypeValue]
        param[1] = ['工位号', this.stationNumberValue]
        var Data = this.CreateData('2001', '电子看板_装配零件_总物料表_导出新', param)
        this.exportExcel_NPOI(Data)
      } else {
        this.$message.warning('暂无数据')
      }
    },
    // 分页
    handleSizeChange(val) {
      this.pageCurrent = 1
      this.pageSize = val
      this.searchOrder()
    },
    handleCurrentChange(val) {
      this.pageCurrent = val
      this.searchOrder()
    }
  }
}
</script>

<style scoped>
.topCard {
  margin: 5px;
}

.topDiv {
  margin-bottom: 10px;
}

.el-select {
  width: 200px
}

.el-input {
  width: 200px
}

.text {
  font-size: 14px;
}

.item {
  padding: 18px 0;
}

.box-card {
  position: absolute;
  right: 6px;
  top: 80px;
  width: 300px;
}

.el-table .warning-row {
  background: oldlace;
}

.el-table .success-row {
  background: #f0f9eb;
}

/deep/ .el-input.is-disabled .el-input__inner {
  color: #606266 !important;
}

/deep/ .el-input.is-disabled .el-input__inner {
  background-color: white !important;
}
</style>
