<template>
  <div class="app-container">
    <el-card class="topCard">
      <div class="topDiv">
        <span style="margin-left: 10px">订单号：</span>
        <el-select v-model="orderNumberValue" placeholder="请选择订单" clearable size="small" style="width: 150px">
          <el-option
            v-for="item in orderNumber"
            :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="请选择产品型号" clearable size="small" style="width: 150px">
          <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-date-picker
          v-model="startPlanTime"
          type="datetime"
          size="small"
          value-format="yyyy-MM-dd HH:mm:ss"
          placeholder="选择开始时间"/>
        <span style="margin-left: 10px">结束时间：</span>
        <el-date-picker
          v-model="endPlanTime"
          type="datetime"
          size="small"
          value-format="yyyy-MM-dd HH:mm:ss"
          placeholder="选择结束时间"/>
        <el-button type="primary" plain size="small" icon="el-icon-search" style="margin-left: 10px" @click="pageSize=15;pageCurrent=1;searchOrder()">查询订单</el-button>
        <el-button type="distribution" size="small" icon="el-icon-circle-plus" @click="creatOrder('form')">创建订单</el-button>
        <el-button type="info" plain size="small" icon="el-icon-upload2" @click="importOrder('form')">订单导入</el-button>
        <el-button type="info" 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"
        style="width: 100%;max-height: 700px"
        border
        size="mini"
        @selection-change="handleSelectionChange">
        <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="完成数量">
          <template slot-scope="scope">
            {{ scope.row.完成数量 }}
          </template>
        </el-table-column>
        <el-table-column align="center" label="状态">
          <template slot-scope="scope">
            <el-tag v-if="parseInt(scope.row.订单状态) === 1" effect="plain" style="width: 70px" type="success">生产中</el-tag>
            <el-tag v-if="parseInt(scope.row.订单状态) === 2" effect="plain" style="width: 70px" type="info">未上线</el-tag>
            <el-tag v-if="parseInt(scope.row.订单状态) === 3" effect="plain" style="width: 70px" type="warning">已完成</el-tag>
          </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="操作" min-width="150">
          <template slot-scope="scope">
            <el-button
              :disabled="parseInt(scope.row.订单状态) !== 0"
              size="mini"
              type="text"
              icon="el-icon-edit"
              @click="handleEdit(scope.row)">修改</el-button>
            <el-button
              :disabled="parseInt(scope.row.订单状态) !== 0"
              size="mini"
              type="text"
              icon="el-icon-delete"
              @click="deleteOrder(scope.row)">删除</el-button>
            <el-button
              :disabled="parseInt(scope.row.订单状态) !== 2"
              size="mini"
              type="text"
              icon="el-icon-success"
              @click="ordersIssuedEditor(scope.row)">订单下发</el-button>
          </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="orderNumber" style="display: inline-block">
          <el-input
            v-model="form.orderNumber"
            clearable
            size="small"
            placeholder="请输入订单号"/>
        </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="planQuantity" style="display: inline-block">
          <el-input
            v-model="form.planQuantity"
            clearable
            size="small"
            placeholder="请输入计划数量"/>
        </el-form-item>
        <el-form-item label="排序" prop="quantityCompletion" style="display: inline-block">
          <el-input
            v-model="form.quantityCompletion"
            clearable
            size="small"
            placeholder="请输入排序"/>
        </el-form-item>
        <el-form-item label="计划开始时间" prop="startPlanTime" style="display: inline-block">
          <el-date-picker
            v-model="form.startPlanTime"
            size="small"
            type="datetime"
            value-format="yyyy-MM-dd HH:mm:ss"
            placeholder="选择开始时间"/>
        </el-form-item>
        <el-form-item label="计划结束时间" prop="endPlanTime" style="display: inline-block">
          <el-date-picker
            v-model="form.endPlanTime"
            size="small"
            type="datetime"
            value-format="yyyy-MM-dd HH:mm:ss"
            placeholder="选择结束时间"/>
        </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="orderNumber" style="display: inline-block">
          <el-input
            v-model="form1.orderNumber"
            readonly
            disabled
            size="small"
            placeholder="请输入订单号"/>
        </el-form-item>
        <el-form-item label="产品型号" prop="engineTypeValue" style="display: inline-block">
          <el-select v-model="form1.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="planQuantity" style="display: inline-block">
          <el-input
            v-model="form1.planQuantity"
            clearable
            size="small"
            placeholder="请输入计划数量"/>
        </el-form-item>
        <el-form-item label="排序" prop="quantityCompletion" style="display: inline-block">
          <el-input
            v-model="form1.rank"
            clearable
            size="small"
            placeholder="请输入排序"/>
        </el-form-item>
        <el-form-item label="计划开始时间" prop="startPlanTime" style="display: inline-block">
          <el-date-picker
            v-model="form1.startPlanTime"
            size="small"
            type="datetime"
            value-format="yyyy-MM-dd HH:mm:ss"
            placeholder="选择开始时间"/>
        </el-form-item>
        <el-form-item label="计划结束时间" prop="endPlanTime" style="display: inline-block">
          <el-date-picker
            v-model="form1.endPlanTime"
            size="small"
            type="datetime"
            value-format="yyyy-MM-dd HH:mm:ss"
            placeholder="选择结束时间"/>
        </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>
  </div>
</template>

<script>
import UploadExcelComponent from './UploadExcel.vue'
import { sleep } from '../../../utils/tool'
export default {
  name: 'UploadExcel',
  components: { UploadExcelComponent },
  data() {
    return {
      worksheet: [],
      itemFile: [],
      unSpecification: [],
      method1: '', // 机床1加工方式
      method2: '', // 机床2加工方式
      method1Explain: '', // 机床1加工方式说明
      method2Explain: '', // 机床2加工方式说明
      stockQuantity: '', // 库存数量
      stockNum: '', // 库位号
      explain: false,
      orderNumber: [],
      orderNumberValue: '',
      OrderNumber_check: 0,
      engineTypeValue: '',
      engineType: [],
      OKWarehouseValue: '',
      OKWarehouse: [],
      NGWarehouseValue: '',
      NGWarehouse: [],
      engineType_check: 0,
      form: { // 创建订单
        planQuantity: '',
        orderNumber: '',
        engineTypeValue: '',
        startPlanTime: '',
        endPlanTime: '',
        quantityCompletion: ''
      },
      rules: { // 创建订单验证
        planQuantity: [{ required: true, message: '请输入计划数量', trigger: 'blur' }],
        orderNumber: [{ required: true, message: '请输入订单号', trigger: 'blur' }],
        engineTypeValue: [{ required: true, message: '请选择机型', trigger: 'blur' }],
        startPlanTime: [{ required: true, message: '请选择计划开始时间', trigger: 'blur' }],
        endPlanTime: [{ required: true, message: '请选择计划结束时间', trigger: 'blur' }],
        quantityCompletion: [{ required: true, message: '请输入计划完成数量', trigger: 'blur' }]
      },
      form1: { // 修改订单
        planQuantity: '',
        orderNumber: '',
        engineTypeValue: '',
        startPlanTime: '',
        endPlanTime: '',
        quantityCompletion: 0,
        MID: '',
        rank: ''
      },
      rules1: { // 修改订单验证
        planQuantity: [{ required: true, message: '请输入计划数量', trigger: 'blur' }],
        orderNumber: [{ required: true, message: '请输入订单号', trigger: 'blur' }],
        engineTypeValue: [{ required: true, message: '请选择机型', trigger: 'blur' }],
        startPlanTime: [{ required: true, message: '请选择计划开始时间', trigger: 'blur' }],
        endPlanTime: [{ required: true, message: '请选择计划结束时间', trigger: 'blur' }],
        quantityCompletion: [{ required: true, message: '请输入计划完成数量', trigger: 'blur' }]
      },
      form2: { // 订单下发
        planQuantity: '',
        orderNumber: '',
        engineTypeValue: '',
        MID: '',
        IssuedQuantity: 1
      },
      rules2: { // 订单下发验证
        planQuantity: [{ required: true, message: '请输入计划数量', trigger: 'blur' }],
        orderNumber: [{ required: true, message: '请输入订单号', trigger: 'blur' }],
        engineTypeValue: [{ required: true, message: '请选择机型', trigger: 'blur' }],
        IssuedQuantity: [{ required: true, message: '请输入订单下发数量', trigger: 'blur' }]
      },
      form3: { // 订单下发
        planQuantity: '',
        orderNumber: '',
        engineTypeValue: '',
        MID: '',
        IssuedQuantity: 1
      },
      rules3: { // 订单下发验证
        planQuantity: [{ required: true, message: '请输入计划数量', trigger: 'blur' }],
        orderNumber: [{ required: true, message: '请输入订单号', trigger: 'blur' }],
        engineTypeValue: [{ required: true, message: '请选择机型', trigger: 'blur' }],
        IssuedQuantity: [{ required: true, message: '请输入订单下发数量', trigger: 'blur' }]
      },
      // planTime: [new Date(new Date().getTime() - 3600 * 1000 * 24 * 3000), new Date(new Date().getTime())],
      planTime: [],
      startPlanTime: '',
      startPlanTime_check: 0,
      endPlanTime: '',
      endPlanTime_check: 0,
      tableData: [],
      importData: [],
      dialogFormVisible: false, // 创建订单
      dialogFormVisible1: false, // 修改订单
      dialogFormVisible3: false, // 订单导入
      states: '', // 当前库存状态
      MIDGroup: [],
      total: 0, // 总条数
      pageList: 15, // 每页显示条数
      pageSize: 15, // 每页显示条数
      pageCurrent: 1, // 后台获取页
      loading: false
    }
  },
  created() {
    this.getOrderNumber()
    this.getEndStation()
    this.searchOrder()
  },
  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++) {
          this.engineType.push({
            label: response.data[i].发动机型号,
            value: response.data[i].发动机型号
          })
        }
      })
    },
    // 初始化获取订单号
    getOrderNumber() {
      this.orderNumber = []
      var param = []
      param[0] = ['开始时间', this.planTime[0]]
      param[1] = ['结束时间', this.planTime[1]]
      var Data = this.CreateData('11', '订单计划_订单号_查询', param)
      this.ExecDatabase(Data).then(response => {
        for (let i = 0; i < response.data.length; i++) {
          this.orderNumber.push({
            label: response.data[i].订单号,
            value: response.data[i].订单号
          })
        }
      })
    },
    // 查询订单
    searchOrder() {
      this.tableData = []
      this.startPlanTime ? this.startPlanTime_check = 1 : this.startPlanTime_check = 0
      this.endPlanTime ? this.endPlanTime_check = 1 : this.endPlanTime_check = 0
      this.orderNumberValue ? this.OrderNumber_check = 1 : this.OrderNumber_check = 0
      this.engineTypeValue ? this.engineType_check = 1 : this.engineType_check = 0
      if (this.startPlanTime === null) { this.startPlanTime = '' }
      if (this.endPlanTime === null) { this.endPlanTime = '' }
      var param = []
      param[0] = ['计划开始时间_check', this.startPlanTime_check]
      param[1] = ['计划开始时间', this.startPlanTime]
      param[2] = ['计划完成时间_check', this.endPlanTime_check]
      param[3] = ['计划完成时间', this.endPlanTime]
      param[4] = ['订单号_check', this.OrderNumber_check]
      param[5] = ['订单号', this.orderNumberValue]
      param[6] = ['产品型号_check', this.engineType_check]
      param[7] = ['产品型号', this.engineTypeValue]
      param[8] = ['PageCurrent', this.pageCurrent]
      param[9] = ['PageSize', this.pageSize]
      param[10] = ['PageCount', '1111', 'int', '1']
      param[11] = ['ItemCount', '1111', 'int', '1']
      var Data = this.CreateData('11', 'MES_计划BOM_发动机订单计划_查询分页', param)
      this.ExecDatabase(Data).then(response => {
        console.log(response, 66)
        if (response.data.result.length !== 0) {
          this.tableData = response.data.result
        }
        this.total = parseInt(response.data.output[0].ItemCount)
        this.loading = false
      })
    },
    // 创建订单
    creatOrder(formName) {
      if (this.$refs[formName] !== undefined) {
        this.$refs[formName].resetFields()
      }
      this.form.planQuantity = ''
      this.form.orderNumber = ''
      this.form.startPlanTime = ''
      this.form.endPlanTime = ''
      this.dialogFormVisible = true
    },
    // 提交新增订单
    submitAdd(formName) {
      this.$refs[formName].validate((valid) => {
        if (valid) {
          var param = []
          param[0] = ['订单号', this.form.orderNumber]
          param[1] = ['产品型号', this.form.engineTypeValue]
          param[2] = ['计划开始时间', this.form.startPlanTime]
          param[3] = ['计划完成时间', this.form.endPlanTime]
          param[4] = ['计划数量', this.form.planQuantity]
          // param[5] = ['完成数量', this.form.quantityCompletion]
          param[5] = ['排序', this.form.quantityCompletion]
          // param[6] = ['结果', '1111', 'int', '1']
          var Data = this.CreateData('12', 'MES_计划BOM_发动机订单计划_订单手动增加', param)
          this.ExecDatabase(Data).then(response => {
            if (response.data[0].result === '1') {
              this.$message({
                message: '订单创建成功',
                type: 'success'
              })
              this.pageCurrent = 1
              this.pageSize = 15
              this.searchOrder()
            } else if (response.data[0].result.结果 === '2') {
              this.$message.error('订单重复，创建失败！')
            } else {
              this.$message.error('订单创建失败！')
            }
          })
          this.dialogFormVisible = false
        } else {
          return false
        }
      })
    },
    // 取消新增订单
    callOff(formName) {
      this.dialogFormVisible = false
      this.$refs[formName].resetFields()
    },
    // 导入订单
    importOrder() {
      this.dialogFormVisible3 = true
    },
    // 删除订单
    deleteOrder(row) {
      this.$confirm('此操作将永久删除该行, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        var param = []
        param[0] = ['MID', row.MID]
        var Data = this.CreateData('12', '订单计划_删除', param)
        this.ExecDatabase(Data).then(response => {
          if (response.data[0].result === '1') {
            this.$message({
              message: '订单删除成功',
              type: 'success'
            })
            this.pageCurrent = 1
            this.pageSize = 15
            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.planQuantity = row.计划数量
      this.form1.orderNumber = row.订单号
      this.form1.startPlanTime = row.计划开始时间
      this.form1.endPlanTime = row.计划完成时间
      this.form1.quantityCompletion = row.完成数量
      this.form1.engineTypeValue = row.产品型号
      this.form1.rank = row.排序
      this.form1.MID = row.MID
      this.dialogFormVisible1 = true
    },
    // 提交修改订单
    submitAdd1() {
      this.$refs['form1'].validate((valid) => {
        if (valid) {
          var param = []
          param[0] = ['订单号', this.form1.orderNumber]
          param[1] = ['产品型号', this.form1.engineTypeValue]
          param[2] = ['计划开始时间', this.form1.startPlanTime]
          param[3] = ['计划完成时间', this.form1.endPlanTime]
          param[4] = ['计划数量', this.form1.planQuantity]
          param[5] = ['MID', this.form1.MID]
          param[6] = ['排序', this.form1.rank]
          var Data = this.CreateData('12', '订单计划_修改', param)
          this.ExecDatabase(Data).then(response => {
            if (response.data[0].result === '1') {
              this.$message({
                message: '订单修改成功',
                type: 'success'
              })
              this.pageCurrent = 1
              this.pageSize = 15
              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)
      }
    },
    // 订单下发编辑
    ordersIssuedEditor(row) {
      this.$confirm(`确定下达订单<p style="color: deepskyblue;display: inline-block"> ${row.订单号}</p> 吗？`, '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning',
        dangerouslyUseHTMLString: true
      }).then(() => {
        var param = []
        param[0] = ['MID', row.MID]
        var Data = this.CreateData('12', 'MES_计划BOM_发动机订单计划_订单下发', param)
        this.ExecDatabase(Data).then(response => {
          if (response.data[0].result === '1') {
            this.$message({
              message: '订单下发成功！',
              type: 'success'
            })
            this.pageCurrent = 1
            this.pageSize = 15
            this.searchOrder()
          } else {
            this.$message.error('订单下发失败！')
          }
        })
      })
    },
    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 sort,
            orderNumber,
            productModel,
            numberPlan,
            startDate,
            endDate
          var param = []
          param[0] = ['订单号', this.worksheet[j].B2.v]
          console.log('订单号', this.worksheet[j].B2.v)
          var Data = this.CreateData('11', 'MES_计划BOM_发动机订单计划_订单导入校验', param)
          const response = await this.ExecDatabase(Data)
          const res = response.data
          if (res[0].Column1 !== 0) {
            this.$notify.error(`${this.worksheet[j].C2.v}已经存在，请确认文件是否正确`)
            this.loading = false
            this.disabled = false
            return
          } else {
            for (let i = 0; i < 999; i++) {
              const attr = i + 2
              sort = this.worksheet[j][`A${attr}`] ? this.worksheet[j][`A${attr}`].v : ''
              orderNumber = this.worksheet[j][`B${attr}`] ? this.worksheet[j][`B${attr}`].v : ''
              productModel = this.worksheet[j][`C${attr}`] ? this.worksheet[j][`C${attr}`].v : ''
              numberPlan = this.worksheet[j][`D${attr}`] ? this.worksheet[j][`D${attr}`].v : ''
              startDate = this.worksheet[j][`E${attr}`] ? this.worksheet[j][`E${attr}`].v : ''
              endDate = this.worksheet[j][`F${attr}`] ? this.worksheet[j][`F${attr}`].v : ''
              console.log(sort, orderNumber, productModel, numberPlan, startDate, endDate)
              if (!sort && !orderNumber && !productModel && !numberPlan && !startDate && !endDate) {
                break
              } else if (!orderNumber) { // 名称不能为空
                this.$notify.error(`订单号不能为空`)
                break
              } else if (Number(numberPlan) <= 0) { // 数量不能为0
                this.$notify.error(`计划数量不能为空`)
                break
              } else {
                if (orderNumber) {
                  await sleep(50)
                  await this.insertToSQL(sort, orderNumber, productModel, numberPlan, startDate, endDate)
                } 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(sort, orderNumber, productModel, numberPlan, startDate, endDate) {
      console.log(startDate, endDate, 'startDate, endDate')
      try {
        var param = []
        param[0] = ['排序', sort]
        param[1] = ['订单号', orderNumber]
        param[2] = ['产品型号', productModel]
        param[3] = ['计划数量', numberPlan]
        param[4] = ['计划开始时间', startDate]
        param[5] = ['计划完成时间', endDate]
        param[6] = ['导入类型', '1', 'int', '1']
        var Data = this.CreateData('12', 'MES_计划BOM_发动机订单计划_订单导入', param)
        const response = await this.ExecDatabase(Data)
        const res = response.data
        if (res.hasOwnProperty('output') === false) {
          if (res[0].result === '0') {
            this.$notify.error(`${orderNumber}导入失败`)
          }
        } else {
          if (res.result[0].result === '0') {
            this.$notify.error(`${numberPlan}导入失败`)
          }
          if (res.output[0].导入类型 === '2') {
            this.$notify.warning(`${numberPlan}存在重复数据`)
          }
        }
      } catch (e) {
        this.err++
        this.$message.error(`导入数据错误`)
        console.log(`导入数据错误${e}`)
      }
    },
    exportExcel() {
      if (this.tableData.length !== 0) {
        this.startPlanTime ? this.startPlanTime_check = 1 : this.startPlanTime_check = 0
        this.endPlanTime ? this.endPlanTime_check = 1 : this.endPlanTime_check = 0
        this.orderNumberValue ? this.OrderNumber_check = 1 : this.OrderNumber_check = 0
        this.engineTypeValue ? this.engineType_check = 1 : this.engineType_check = 0
        if (this.startPlanTime === null) { this.startPlanTime = '' }
        if (this.endPlanTime === null) { this.endPlanTime = '' }
        var param = []
        param[0] = ['计划开始时间_check', this.startPlanTime_check]
        param[1] = ['计划开始时间', this.startPlanTime]
        param[2] = ['计划完成时间_check', this.endPlanTime_check]
        param[3] = ['计划完成时间', this.endPlanTime]
        param[4] = ['订单号_check', this.OrderNumber_check]
        param[5] = ['订单号', this.orderNumberValue]
        param[6] = ['产品型号_check', this.engineType_check]
        param[7] = ['产品型号', this.engineTypeValue]
        var Data = this.CreateData('2001', 'MES_计划BOM_发动机订单计划_订单导出', 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;
  }
</style>

