<template>
  <div class="app-container">
    <el-card class="topCard">
      <div class="topDiv">
        <span style="margin-left: 10px">工位号：</span>
        <el-select v-model="stationNumberValue" placeholder="请选择工位号" size="small" style="width: 250px" @change="searchOrder">
          <el-option
            v-for="item in stationNumber"
            :key="item.value"
            :label="item.label"
            :value="item.value"
            size="mini"/>
        </el-select>
        <span style="margin-left: 10px">产品型号：</span>
        <el-select v-model="engineTypeValue" placeholder="请选择产品型号" size="small" style="width: 150px" @change="searchOrder">
          <el-option
            v-for="item in engineType"
            :key="item.value"
            :label="item.label"
            :value="item.value"
            size="mini"/>
        </el-select>
        <span style="margin-left: 10px">版本号：</span>
        <el-select v-model="version" placeholder="请选择版本号" size="small" style="width: 150px" @change="searchOrder">
          <el-option
            v-for="item in versionList"
            :key="item.value"
            :label="item.label"
            :value="item.value"
            size="mini"/>
        </el-select>
        <!--        <span style="margin-left: 10px">物料号：</span>-->
        <!--        <el-input v-model="materialCode" style="width:170px;" clearable placeholder="物料号" size="small"/>-->
        <!--        <span style="margin-left: 10px">是否验证：</span>-->
        <!--        <el-select v-model="isVerification" placeholder="是否验证" size="small" clearable style="width: 140px" @change="searchOrder()">-->
        <!--          <el-option-->
        <!--            v-for="item in verification"-->
        <!--            :key="item.value"-->
        <!--            :label="item.label"-->
        <!--            :value="item.value"-->
        <!--            size="mini"/>-->
        <!--        </el-select>-->
        <el-button type="primary" plain size="small" icon="el-icon-search" style="margin-left: 10px" @click="searchOrder()">查询物料</el-button>
        <el-button type="primary" size="small" icon="el-icon-circle-plus" @click="creatOrder('form')">创建物料</el-button>
        <!--        <el-button type="success" size="small" plain icon="el-icon-document-copy" @click="copyOrder('form4')">复制物料</el-button>-->
        <!--        <el-button type="info" plain size="small" icon="el-icon-upload2" @click="importOrder('form')">物料导入</el-button>-->
        <el-button :loading="exportLoading" :disabled="exportLoading" type="warning" plain size="small" icon="el-icon-download" @click="exportExcel()">物料导出</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="640px"
        style="width: 100%"
        border
        size="mini"
        @selection-change="handleSelectionChange">
        <el-table-column type="index" width="60" label="序号" align="center"/>
        <!--        <el-table-column align="center" label="物料号">-->
        <!--          <template slot-scope="scope">-->
        <!--            {{ scope.row.物料号 }}-->
        <!--          </template>-->
        <!--        </el-table-column>-->
        <el-table-column align="center" label="工位号" width="100px">
          <template slot-scope="scope">
            {{ scope.row.工位号 }}
          </template>
        </el-table-column>
        <el-table-column align="center" label="版本号" width="100px">
          <template slot-scope="scope">
            {{ scope.row.版本号 }}
          </template>
        </el-table-column>
        <el-table-column align="center" label="物料号" width="150px">
          <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="零件名称" min-width="120px">
          <template slot-scope="scope">
            {{ scope.row.零件名称 }}
          </template>
        </el-table-column>
        <el-table-column align="center" label="物料数量" width="100px">
          <template slot-scope="scope">
            {{ scope.row.零件数量 }}
          </template>
        </el-table-column>
        <el-table-column align="center" label="是否验证" width="100px">
          <template slot-scope="scope">
            <el-tag v-if="Number(scope.row.是否验证) === 1" size="mini" type="success">是</el-tag>
            <span v-else>&nbsp;</span>
          </template>
        </el-table-column>
        <el-table-column align="center" label="是否批次" width="100px">
          <template slot-scope="scope">
            <el-tag v-if="Number(scope.row.是否批次) === 1" size="mini" type="success">是</el-tag>
            <span v-else>&nbsp;</span>
          </template>
        </el-table-column>
        <el-table-column align="center" label="匹配规则" width="150px">
          <template slot-scope="scope">
            {{ scope.row.零件图号 }}
          </template>
        </el-table-column>
        <el-table-column align="center" label="匹配长度" width="100px">
          <template slot-scope="scope">
            <span v-if="Number(scope.row.是否验证) === 1">{{ scope.row.条码长度 }}</span>
            <span v-else>&nbsp;</span>
          </template>
        </el-table-column>
        <!-- <el-table-column align="center" label="匹配排序" width="100px">
          <template slot-scope="scope">
            <span v-if="Number(scope.row.是否验证) === 1">{{ scope.row.零件排序 }}</span>
            <span v-else>&nbsp;</span>
          </template>
        </el-table-column> -->
        <!--        <el-table-column align="center" label="是否主物料">-->
        <!--          <template slot-scope="scope">-->
        <!--            <span v-if="Number(scope.row.是否主物料) === 1">是</span>-->
        <!--            <span v-if="Number(scope.row.是否主物料) === 0">否</span>-->
        <!--          </template>-->
        <!--        </el-table-column>-->
        <el-table-column align="center" label="操作" width="150">
          <template slot-scope="scope">
            <el-button
              size="mini"
              type="text"
              icon="el-icon-edit"
              @click="handleEdit(scope.row)">修改</el-button>
            <el-button
              size="mini"
              type="text"
              icon="el-icon-delete"
              style="color:#ff0000;"
              @click="deleteOrder(scope.row)">删除</el-button>
          </template>
        </el-table-column>
      </el-table>
    </el-card>
    <el-dialog v-el-drag-dialog :visible.sync="dialogFormVisible" title="创建物料" center style="min-height: 200px;" width="800px">
      <el-form ref="form" :model="form" :rules="rules" label-position="right" label-width="80px">
        <el-row>
          <el-col :span="12">
            <el-form-item label="工位号" prop="stationNumberValue" style="display: inline-block">
              <el-select v-model="form.stationNumberValue" size="small" placeholder="请选择工位号" style="width: 250px">
                <el-option
                  v-for="item in stationNumber"
                  :key="item.value"
                  :label="item.label"
                  :value="item.value"/>
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <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 engineType1"
                  :key="item.value"
                  :label="item.label"
                  :value="item.value"/>
              </el-select>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="12">
            <el-form-item label="物料号" prop="orderNumber" style="display: inline-block">
              <el-input
                v-model="form.orderNumber"
                clearable
                size="small"
                placeholder="请输入物料号"/>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="物料名称" prop="partName" style="display: inline-block">
              <el-input
                v-model="form.partName"
                clearable
                size="small"
                placeholder="物料名称"/>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="12">
            <el-form-item label="数量" prop="number" style="display: inline-block">
              <el-input-number
                v-model="form.number"
                :min="1"
                :step="1"
                size="small"
                placeholder="请输入数量"/>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="物料版本" prop="version" style="display: inline-block">
              <el-select v-model="form.version" size="small" placeholder="请选择物料版本">
                <el-option
                  v-for="item in versionList"
                  :key="item.value"
                  :label="item.label"
                  :value="item.value"/>
              </el-select>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="12">
            <el-form-item label="是否验证" prop="verificationValue" style="display: inline-block">
              <el-select v-model="form.verificationValue" size="small" placeholder="请选择产品型号" @change="changeIsVerification">
                <el-option
                  v-for="item in verification"
                  :key="item.value"
                  :label="item.label"
                  :value="item.value"/>
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="是否批次" prop="batchValue" style="display: inline-block">
              <el-select v-model="form.batchValue" 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-col>
        </el-row>
        <el-row v-if="form.verificationValue === 1" style="margin: 10px 0">
          <el-divider>验证规则设定</el-divider>
        </el-row>
        <el-row v-if="form.verificationValue === 1">
          <el-col :span="10">
            <el-form-item label="规则" prop="verificationCode" style="display: inline-block">
              <el-input
                v-model="form.verificationCode"
                clearable
                size="small"
                placeholder="请输入匹配规则"/>
            </el-form-item>
          </el-col>
          <el-col :span="7">
            <el-form-item label="长度" prop="verificationLength" style="display: inline-block">
              <el-input-number
                v-model="form.verificationLength"
                :min="1"
                :step="1"
                size="small"
                placeholder="请输入匹配长度"/>
            </el-form-item>
          </el-col>
          <!-- <el-col :span="7">
            <el-form-item label="排序" prop="verificationOrder" style="display: inline-block">
              <el-input-number
                v-model="form.verificationOrder"
                :min="0"
                :step="1"
                size="small"
                placeholder="请输入匹配排序"/>
            </el-form-item>
          </el-col> -->
        </el-row>
      </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 v-el-drag-dialog :visible.sync="dialogFormVisible1" title="编辑物料" center style="min-height: 200px;" width="800px">
      <el-form ref="form" :model="form1" :rules="rules1" label-position="right" label-width="80px">
        <!--        <el-row>-->
        <!--          <el-col :span="12">-->
        <!--            <el-form-item label="工位号" prop="stationNumberValue" style="display: inline-block">-->
        <!--              <el-input-->
        <!--                v-model="form1.stationNumberValue"-->
        <!--                readonly-->
        <!--                size="small"-->
        <!--                placeholder="工位号"/>-->
        <!--            </el-form-item>-->
        <!--          </el-col>-->
        <!--          <el-col :span="12">-->
        <!--            <el-form-item label="产品型号" prop="engineTypeValue" style="display: inline-block">-->
        <!--              <el-input-->
        <!--                v-model="form1.engineTypeValue"-->
        <!--                readonly-->
        <!--                size="small"-->
        <!--                placeholder="工位号"/>-->
        <!--            </el-form-item>-->
        <!--          </el-col>-->
        <!--        </el-row>-->
        <el-row>
          <el-col :span="12">
            <el-form-item label="物料号" prop="orderNumber" style="display: inline-block">
              <el-input
                v-model="form1.orderNumber"
                clearable
                size="small"
                placeholder="请输入物料号"/>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="版本号" prop="version" style="display: inline-block">
              <el-input
                v-model="form1.version"
                readonly
                size="small"
                placeholder="请选择版本号"/>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="12">
            <el-form-item label="物料名称" prop="partName" style="display: inline-block">
              <el-input
                v-model="form1.partName"
                clearable
                size="small"
                placeholder="物料名称"/>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="数量" prop="number" style="display: inline-block">
              <el-input-number
                v-model="form1.number"
                :min="1"
                :step="1"
                size="small"
                placeholder="请输入数量"/>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="12">
            <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-col>
          <el-col :span="12">
            <el-form-item label="是否批次" prop="batchValue" style="display: inline-block">
              <el-select v-model="form1.batchValue" 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-col>
        </el-row>
        <el-row v-if="form1.verificationValue === 1" style="margin: 10px 0">
          <el-divider>验证规则设定</el-divider>
        </el-row>
        <el-row v-if="form1.verificationValue === 1">
          <el-col :span="10">
            <el-form-item label="规则" prop="verificationCode" style="display: inline-block">
              <el-input
                v-model="form1.verificationCode"
                clearable
                size="small"
                placeholder="请输入匹配规则"/>
            </el-form-item>
          </el-col>
          <el-col :span="7">
            <el-form-item label="长度" prop="verificationLength" style="display: inline-block">
              <el-input-number
                v-model="form1.verificationLength"
                :min="1"
                :step="1"
                size="small"
                placeholder="请输入匹配长度"/>
            </el-form-item>
          </el-col>
          <!-- <el-col :span="7">
            <el-form-item label="排序" prop="verificationOrder" style="display: inline-block">
              <el-input-number
                v-model="form1.verificationOrder"
                :min="0"
                :step="1"
                size="small"
                placeholder="请输入匹配排序"/>
            </el-form-item>
          </el-col> -->
        </el-row>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button size="small" @click="callOff1('form')">取消</el-button>
        <el-button type="primary" size="small" @click="submitAdd1('form')">确定</el-button>
      </div>
    </el-dialog>
    <el-dialog :visible.sync="dialogFormVisible3" title="物料导入" center style="min-height: 200px" width="1000px">
      <div class="app-container">
        <upload-excel-component :on-success="handleSuccess" :before-upload="beforeUpload" />
        <div style="margin-top: 5px">
          <el-table
            v-loading="loading"
            ref="multipleTable"
            :data="tableDataListAll"
            :header-cell-style="{ background: '#F5F5F5', fontSize: '16px' }"
            element-loading-text="数据加载中"
            element-loading-spinner="el-icon-loading"
            element-loading-background="rgba(0, 0, 0, 0.2)"
            height="300px"
            style="width: 100%"
            border
          >
            <el-table-column type="index" width="60" label="序号" align="center"/>
            <el-table-column align="center" label="工位号" width="80px">
              <template slot-scope="scope">
                {{ scope.row.工位号 }}
              </template>
            </el-table-column>
            <el-table-column align="center" label="产品型号" width="80px">
              <template slot-scope="scope">
                {{ scope.row.产品型号 }}
              </template>
            </el-table-column>
            <el-table-column align="center" label="物料号" width="120px">
              <template slot-scope="scope">
                {{ scope.row.物料号 }}
              </template>
            </el-table-column>
            <el-table-column align="center" label="零件名称" min-width="120px">
              <template slot-scope="scope">
                {{ scope.row.零件名称 }}
              </template>
            </el-table-column>
            <el-table-column align="center" label="物料数量" width="80px">
              <template slot-scope="scope">
                {{ scope.row.零件数量 }}
              </template>
            </el-table-column>
            <el-table-column align="center" label="是否验证" width="80px">
              <template slot-scope="scope">
                {{ scope.row.是否验证 }}
              </template>
            </el-table-column>
            <el-table-column align="center" label="匹配规则" width="120px">
              <template slot-scope="scope">
                {{ scope.row.匹配规则 }}
              </template>
            </el-table-column>
            <el-table-column align="center" label="匹配长度" width="80px">
              <template slot-scope="scope">
                {{ scope.row.条码长度 }}
              </template>
            </el-table-column>
            <!-- <el-table-column align="center" label="匹配排序" width="80px">
              <template slot-scope="scope">
                {{ scope.row.匹配排序 }}
              </template>
            </el-table-column> -->
          </el-table>
        </div>
        <div slot="footer" class="dialog-footer">
          <el-button size="small" @click="materialAddAllCancel()">取消</el-button>
          <el-button type="primary" icon="el-icon-upload" size="small" @click="materialAddAllSubmit()">确认导入</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 UploadExcelComponent from './UploadExcel.vue'
import { sleep } from '../../../utils/tool'
export default {
  name: 'UploadExcel',
  components: { UploadExcelComponent },
  data() {
    return {
      stationNumber: [],
      stationNumberValue: '',
      materialCode: '',
      stationName: '',
      worksheet: [],
      itemFile: [],
      unSpecification: [],
      method1: '', // 机床1加工方式
      method2: '', // 机床2加工方式
      method1Explain: '', // 机床1加工方式说明
      method2Explain: '', // 机床2加工方式说明
      stockQuantity: '', // 库存数量
      stockNum: '', // 库位号
      dialogFormVisible4: false,
      explain: false,
      orderNumber: [],
      orderNumberValue: '',
      engineTypeValue: '',
      version: '',
      versionList: [],
      exportLoading: false,
      engineType: [],
      engineType1: [],
      isVerification: '',
      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,
        version: '',
        verificationCode: '',
        verificationLength: 0,
        verificationOrder: 0,
        mainMaterialValue: 0,
        batchValue: 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' }],
        version: [{ required: true, message: '请选择物料版本', trigger: 'change' }],
        verificationLength: [{ required: true, message: '请输入验证长度', trigger: 'blur' }],
        verificationOrder: [{ required: true, message: '请输入验证顺序', trigger: 'blur' }],
        verificationCode: [{ required: true, message: '请输入验证规则', trigger: 'blur' }]
        // mainMaterialValue: [{ required: true, message: '请选择是否主物料', trigger: 'change' }]
      },
      form1: { // 修改订单
        partName: '',
        stationNumberValue: '',
        engineTypeValue: '',
        orderNumber: '',
        partDrawing: '',
        number: '',
        manufactor: '',
        version: '',
        verificationValue: 1,
        verificationCode: '',
        verificationLength: 0,
        verificationOrder: 0,
        mainMaterialValue: 0,
        batchValue: 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' }],
        verificationLength: [{ required: true, message: '请输入验证长度', trigger: 'blur' }],
        verificationOrder: [{ required: true, message: '请输入验证顺序', trigger: 'blur' }],
        verificationCode: [{ required: true, message: '请输入验证规则', trigger: 'blur' }]
        // 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: [],
      tableDataListAll: [],
      tableData: [],
      importData: [],
      dialogFormVisible: false, // 创建订单
      dialogFormVisible1: false, // 修改订单
      dialogFormVisible3: false, // 订单导入
      states: '', // 当前库存状态
      MIDGroup: [],
      total: 0, // 总条数
      pageList: 30, // 每页显示条数
      pageSize: 30, // 每页显示条数
      pageCurrent: 1, // 后台获取页
      loading: false
    }
  },
  watch: {
  },
  created() {
    this.getStationNumber()
    this.getEndStation()
    this.getVersion()
    setTimeout(() => {
      this.searchOrder()
    }, 1000)
  },
  methods: {
    removeAll() {
      this.worksheet = []
      this.itemFile = []
    },
    async selected(data) { // 选择需要导入的文件
      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 => {
        if (response.data.length > 0) this.engineTypeValue = response.data[0].发动机机型标志
        for (let i = 0; i < response.data.length; i++) {
          this.engineType.push({
            label: response.data[i].发动机型号,
            value: response.data[i].发动机机型标志
          })
          this.engineType1.push({
            label: response.data[i].发动机型号,
            value: response.data[i].发动机机型标志
          })
        }
        this.searchOrder()
      })
    },
    // 获取版本号
    getVersion() {
      this.versionList = []
      var param = []
      var Data = this.CreateData('11', '电子看板_装配零件_版本_查询', param)
      this.ExecDatabase(Data).then(response => {
        if (response.data.length > 0) this.version = response.data[0].版本号
        for (let i = 0; i < response.data.length; i++) {
          this.versionList.push({
            label: response.data[i].是否启用 === 1 ? `${response.data[i].版本号}【启用】` : response.data[i].版本号,
            value: response.data[i].版本号
          })
        }
      })
    },
    // 初始化获取订单号
    getStationNumber() {
      this.stationNumber = []
      var param = []
      var Data = this.CreateData('11', '工位与名称_视图_查询', param)
      this.ExecDatabase(Data).then(response => {
        // if (response.data.length > 0) this.stationNumberValue = response.data[0].工位号
        for (let i = 0; i < response.data.length; i++) {
          this.stationNumber.push({
            label: `【${response.data[i].工位号}】${response.data[i].工位名称}`,
            value: response.data[i].工位号,
            labelName: response.data[i].工位名称
          })
        }
      })
    },
    // 查询订单
    searchOrder() {
      var param = []
      const isVerification = this.isVerification === '' ? 9999 : this.isVerification
      param[0] = ['工位号', this.stationNumberValue]
      param[1] = ['产品型号代码', this.engineTypeValue]
      param[2] = ['是否验证', isVerification]
      param[3] = ['物料号', this.materialCode]
      param[4] = ['版本号', this.version]
      var Data = this.CreateData('11', '电子看板_装配零件_查询', param)
      this.ExecDatabase(Data).then(response => {
        this.tableData = []
        if (response.data.length !== 0) {
          this.tableData = response.data
        }
      })
    },
    changeIsVerification(value) {
      if (value === 1) {
        this.form.verificationCode = this.form.orderNumber
      } else {
        this.form.verificationCode = '****'
      }
    },
    // 创建订单
    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 = 0
      this.form.verificationCode = '****'
      this.form.verificationLength = 1
      this.form.verificationOrder = 0
      this.form.mainMaterialValue = 0
      this.form.batchValue = 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]
          param[9] = ['验证规则', this.form.verificationCode]
          param[10] = ['验证长度', this.form.verificationLength]
          param[11] = ['验证顺序', this.form.verificationOrder]
          param[12] = ['版本号', this.form.version]
          param[13] = ['批次追溯', this.form.batchValue]
          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('物料增加失败！')
            }
          })
        } 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, batchValue
      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].零件数量
        batchValue = this.tableData[i].是否批次
        await this.submitAdd5(stationNumberValue, orderNumber, partName, partDrawing, manufactor, verificationValue, mainMaterialValue, number, batchValue)
      }
      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, batchValue) {
      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]
      param[9] = ['批次追溯', batchValue]
      var Data = this.CreateData('12', '电子看板_装配零件_SPC_增加', param)
      return new Promise((resolve, reject) => {
        this.ExecDatabase(Data).then(response => {
          if (response.data[0].result === '1') {
            resolve(1)
          } else {
            reject(2)
          }
        })
      })
    },
    // 导入订单
    importOrder() {
      this.dialogFormVisible3 = true
    },
    // 删除订单
    deleteOrder(row) {
      this.$confirm('此操作将永久删除该行, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        var param = []
        param[0] = ['ID', row.ID]
        var Data = this.CreateData('12', '电子看板_装配零件_SPC_删除', 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()
      }
      console.log(row)
      this.form1.partName = row.零件名称
      this.form1.stationNumberValue = row.工位号
      this.form1.engineTypeValue = row.工位号
      this.form1.orderNumber = row.物料号
      // this.form1.partDrawing = row.零件图号
      this.form1.number = row.零件数量
      // this.form1.manufactor = row.生产厂家
      this.form1.verificationValue = Number(row.是否验证)
      this.form1.verificationCode = row.零件图号
      this.form1.verificationLength = row.条码长度
      this.form1.verificationOrder = row.零件排序
      this.form1.version = row.版本号
      // this.form1.mainMaterialValue = Number(row.是否主物料)
      this.form1.batchValue = Number(row.是否批次)
      this.form1.ID = row.ID
      this.dialogFormVisible1 = true
    },
    // 提交修改订单
    submitAdd1(formName) {
      this.$refs[formName].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[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]
          param[10] = ['验证规则', this.form1.verificationCode]
          param[11] = ['验证长度', this.form1.verificationLength]
          param[12] = ['验证顺序', this.form1.verificationOrder]
          param[13] = ['批次追溯', this.form1.batchValue]
          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(formName) {
      this.$refs[formName].resetFields()
      this.dialogFormVisible1 = false
    },
    // 多选
    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 => {
        this.tableData = response.data.rows
        this.total = response.data.total
        this.loading = false
      })
    },
    // 表格颜色变化
    tableRowClassName({ 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]
        param[12] = ['是否批次', 0] // 导入时默认设置为否
        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 = []
        const isVerification = this.isVerification === '' ? 9999 : this.isVerification
        param[0] = ['产品型号代码', this.engineTypeValue]
        param[1] = ['工位号', this.stationNumberValue]
        param[2] = ['是否验证', isVerification]
        param[3] = ['物料号', this.materialCode]
        param[4] = ['版本号', this.version]
        var exportData = JSON.parse(this.CreateData('2005', '电子看板_装配零件_查询', param))
        exportData.exportFileName = '物料扫码维护'
        exportData.exportColumns = '工位号,版本号,物料号,零件名称,零件数量=物料数量,是否验证,是否批次,零件图号=匹配规则,条码长度=匹配长度'
        var Data = JSON.stringify(exportData)
        this.runExcelExport(Data, 'exportLoading')
      } else {
        this.$message.warning('暂无数据')
      }
    },
    beforeUpload(file) {
      return true
    },
    handleSuccess({ results, header }) {
      const tableName = `基础数据_物料bom_导入临时表`
      console.log(tableName)
      var Data = this.CreateData('7', tableName, results, header)
      this.ExecDatabase(Data).then(response => {
        this.selectDate2()
      })
    },
    selectDate2() {
      this.tableDataListAll = []
      var param = []
      var Data = this.CreateData('11', '电子看板_装配零件_物料bom_导入临时表_查询', param)
      this.ExecDatabase(Data).then(response => {
        if (response.data.length !== 0) {
          this.tableDataListAll = response.data
        }
      })
    },
    materialAddAllSubmit() {
      if (this.tableDataListAll.length === 0) {
        this.$message.error('内容为空')
      } else {
        var param = []
        var Data = this.CreateData('12', '电子看板_装配零件_物料BOM_导入临时表_确认导入', param)
        this.ExecDatabase(Data).then(response => {
          if (response.data.length > 0) {
            if (response.data[0]['result'] === '1') {
              this.$message.success('导入成功！')
              this.dialogFormVisible3 = false
              this.searchOrder()
            } else {
              this.$message.error('导入失败！')
            }
          } else {
            this.$message.error('导入失败！')
          }
        })
      }
    },
    materialAddAllCancel() {
      this.dialogFormVisible3 = false
    },
    // 分页
    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;
  }

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