<template>
  <div class="app-container">
    <el-card class="topCard">
      <div class="topDiv">
        <el-button type="primary" icon="el-icon-circle-plus-outline" size="small" @click="addTo">新增</el-button>
      </div>
      <el-table
        ref="multipleTable"
        :data="tableData"
        :header-cell-style="{background:'#eef1f6',color:'#606266'}"
        :cell-class-name="tableRowClassName"
        height="680px"
        class="main"
        tooltip-effect="dark"
        border
        size="mini">
        <el-table-column align="center" width="60px" label="序号">
          <template slot-scope="scope">
            {{ scope.row.序号 }}
          </template>
        </el-table-column>
        <el-table-column label="适用范围" width="200px">
          <template slot-scope="scope">
            <div style="white-space: pre-line !important;">{{ scope.row.适用范围 }}</div>
          </template>
        </el-table-column>
        <el-table-column label="特殊需求">
          <template slot-scope="scope">
            <div style="white-space: pre-line !important;">{{ scope.row.特殊需求 }}</div>
          </template>
        </el-table-column>
        <el-table-column label="改制物料" width="120px">
          <template slot-scope="scope">
            <div style="white-space: pre-line !important;">{{ scope.row.改制物料 }}</div>
          </template>
        </el-table-column>
        <el-table-column label="改制工具" width="120px">
          <template slot-scope="scope">
            <div style="white-space: pre-line !important;">{{ scope.row.改制工具 }}</div>
          </template>
        </el-table-column>
        <el-table-column label="改制单号" width="200px">
          <template slot-scope="scope">
            <div style="white-space: pre-line !important;">{{ scope.row.改制单号 }}</div>
          </template>
        </el-table-column>
        <el-table-column label="工位号" width="120px">
          <template slot-scope="scope">
            <div style="white-space: pre-line !important;">{{ scope.row.工位号 }}</div>
          </template>
        </el-table-column>
        <!--        <el-table-column label="有效日期" width="100px">-->
        <!--          <template slot-scope="scope">-->
        <!--            <div style="white-space: pre-line !important;">{{ scope.row.有效日期 }}</div>-->
        <!--          </template>-->
        <!--        </el-table-column>-->
        <!--        <el-table-column label="生产确认" width="100px">-->
        <!--          <template slot-scope="scope">-->
        <!--            <div style="white-space: pre-line !important;">{{ scope.row.生产确认 }}</div>-->
        <!--          </template>-->
        <!--        </el-table-column>-->
        <el-table-column align="center" label="操作" width="100">
          <template slot-scope="scope">
            <el-button type="text" @click="editInformation(scope.row)"><i class="el-icon-edit"/></el-button>
            <el-button type="text" style="color: #ff0000" @click="deleteInformation(scope.row)"><i class="el-icon-delete"/></el-button>
          </template>
        </el-table-column>
      </el-table>
    </el-card>
    <el-dialog :visible.sync="dialogFormVisible" :title="title" center style="min-height: 200px;" width="700px">
      <el-form ref="form" :model="form" :rules="rules" label-position="right" label-width="90px">
        <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" style="width: 200px;" size="small"/>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="工位号:" prop="工位号" style="display: inline-block">
              <el-input
                v-model="form.工位号"
                clearable
                size="small"
                style="width: 200px"
                placeholder="请输入工位号"/>
            </el-form-item>
          </el-col>
        </el-row>
        <el-form-item label="适用范围:" prop="适用范围" style="display: inline-block">
          <el-input
            v-model="form.适用范围"
            type="textarea"
            clearable
            size="small"
            style="width: 525px"
            placeholder="请输入适用范围"/>
        </el-form-item>
        <el-form-item label="特殊需求:" prop="特殊需求" style="display: inline-block">
          <el-input
            v-model="form.特殊需求"
            :rows="2"
            type="textarea"
            clearable
            size="small"
            style="width: 525px"
            placeholder="请输入特殊需求"/>
        </el-form-item>
        <el-row>
          <el-col :span="12">
            <el-form-item label="改制物料:" prop="改制物料" style="display: inline-block">
              <el-input
                v-model="form.改制物料"
                clearable
                size="small"
                style="width: 200px"
                placeholder="请输入改制物料"/>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="改制工具:" prop="改制工具" style="display: inline-block">
              <el-input
                v-model="form.改制工具"
                clearable
                size="small"
                style="width: 200px"
                placeholder="请输入改制工具"/>
            </el-form-item>
          </el-col>
        </el-row>
        <el-form-item label="改制单号:" prop="改制单号" style="display: inline-block">
          <el-input
            v-model="form.改制单号"
            clearable
            size="small"
            style="width: 525px"
            placeholder="请输入改制单号"/>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button size="small" @click="callOff('form')">取消</el-button>
        <el-button v-if="title === '添加'" type="primary" size="small" @click="submitAdd('form')">确定</el-button>
      </div>
    </el-dialog>
    <el-dialog :visible.sync="dialogFormVisible1" :title="title" center style="min-height: 200px;" width="700px">
      <el-form ref="form1" :model="form1" :rules="rules1" label-position="right" label-width="90px">
        <el-row>
          <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" style="width: 200px;" size="small"/>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="工位号:" prop="工位号" style="display: inline-block">
              <el-input
                v-model="form1.工位号"
                clearable
                size="small"
                style="width: 200px"
                placeholder="请输入工位号"/>
            </el-form-item>
          </el-col>
        </el-row>
        <el-form-item label="适用范围:" prop="适用范围" style="display: inline-block">
          <el-input
            v-model="form1.适用范围"
            type="textarea"
            clearable
            size="small"
            style="width: 525px"
            placeholder="请输入适用范围"/>
        </el-form-item>
        <el-form-item label="特殊需求:" prop="特殊需求" style="display: inline-block">
          <el-input
            v-model="form1.特殊需求"
            :rows="2"
            type="textarea"
            clearable
            size="small"
            style="width: 525px"
            placeholder="请输入特殊需求"/>
        </el-form-item>
        <el-row>
          <el-col :span="12">
            <el-form-item label="改制物料:" prop="改制物料" style="display: inline-block">
              <el-input
                v-model="form1.改制物料"
                clearable
                size="small"
                style="width: 200px"
                placeholder="请输入改制物料"/>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="改制工具:" prop="改制工具" style="display: inline-block">
              <el-input
                v-model="form1.改制工具"
                clearable
                size="small"
                style="width: 200px"
                placeholder="请输入改制工具"/>
            </el-form-item>
          </el-col>
        </el-row>
        <el-form-item label="改制单号:" prop="改制单号" style="display: inline-block">
          <el-input
            v-model="form1.改制单号"
            clearable
            size="small"
            style="width: 525px"
            placeholder="请输入改制单号"/>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button size="small" @click="callOff1('form1')">取消</el-button>
        <el-button v-if="title === '编辑'" type="primary" size="small" @click="submitUpdate('form1')">确定</el-button>
      </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.stationNumberValue" placeholder="请选择工位号" size="small">
            <el-option
              v-for="item in stationNumber"
              :key="item.value"
              :label="item.label"
              :value="item.value"
              size="mini"/>
          </el-select>
        </el-form-item>
        <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 { mapGetters } from 'vuex'
export default {
  data() {
    return {
      tableData: [], // 总数据数组
      stationNumber: [],
      stationNumberValue: '',
      stationName: '',
      moduleDescription: [],
      moduleDescriptionValue: '',
      engineTypeValue: '',
      engineTypeValue1: '',
      engineTypeName: '',
      engineType: [],
      tagIDArr: [
        {
          label: 'lbl_datagrid_quality_name',
          value: 'lbl_datagrid_quality_name'
        }, {
          label: 'lbl_datagrid_quality_item1',
          value: 'lbl_datagrid_quality_item1'
        }, {
          label: 'lbl_datagrid_quality_item2',
          value: 'lbl_datagrid_quality_item2'
        }
      ],
      isDisable: [
        {
          label: '是',
          value: 1
        }, {
          label: '否',
          value: 0
        }
      ],
      isDisableValue: 0,
      labelText: '',
      labelNumber: '',
      dialogFormVisible: false,
      dialogFormVisible1: false,
      dialogFormVisible4: false,
      title: '',
      form: {
        number: '',
        适用范围: '',
        特殊需求: '',
        改制物料: '',
        改制工具: '',
        改制单号: '',
        工位号: '',
        有效日期: '',
        生产确认: ''
      },
      rules: {
        number: [{ required: true, message: '请输入序号', trigger: 'blur' }]
      },
      form1: {
        ID: '',
        number: '',
        适用范围: '',
        特殊需求: '',
        改制物料: '',
        改制工具: '',
        改制单号: '',
        工位号: '',
        有效日期: '',
        生产确认: ''
      },
      rules1: {
        number: [{ required: true, message: '请输入序号', trigger: 'blur' }]
      },
      form4: {
        engineTypeValue: '',
        stationNumberValue: ''
      },
      rules4: { // 订单下发验证
        engineTypeValue: [{ required: true, message: '请选择机型', trigger: 'blur' }],
        stationNumberValue: [{ required: true, message: '请选择工位号', trigger: 'blur' }]
      }
    }
  },
  computed: {
    ...mapGetters([
      'id',
      'token',
      'departmentName'
    ])
  },
  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 = ''
    //   }
    // },
    engineTypeValue1() {
      console.log(1234)
      for (let i = 0; i < this.engineType.length; i++) {
        if (this.engineType[i].value === this.engineTypeValue1) {
          this.engineTypeName = this.engineType[i].labelName
          console.log(this.engineTypeName, 'this.engineTypeName')
        }
      }
    }
  },
  created() {
    // this.getStationNumber()
    // this.getEndStation()
  },
  mounted() {
    setTimeout(() => {
      this.searchTable()
    }, 300)
  },
  methods: {
    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].工位号}】${response.data[i].工位名称}`,
            value: response.data[i].工位号,
            labelName: response.data[i].工位名称
          })
        }
      })
    },
    // 初始化获取产品型号
    getEndStation() {
      this.engineType = []
      var param = []
      var Data = this.CreateData('11', 'CATL_基础信息_机型信息_查询', param)
      this.ExecDatabase(Data).then(response => {
        if (response.data.length > 0) this.engineTypeValue = response.data[0].机型ID
        for (let i = 0; i < response.data.length; i++) {
          this.engineType.push({
            label: response.data[i].机型号,
            value: response.data[i].机型ID,
            labelName: response.data[i].机型号
          })
        }
      })
    },
    // 表格颜色变化
    tableRowClassName({ row, rowIndex }) {
      // console.log(row, rowIndex)
      if (rowIndex % 2 === 0) {
        return 'black'
      } else {
        return 'red'
      }
    },
    // 查询MES消息
    searchTable() {
      this.tableData = []
      var param = []
      var Data = this.CreateData('11', 'CATL_基础_装车单检查内容_特殊需求_查询', param)
      this.ExecDatabase(Data).then(response => {
        if (response.data.length !== 0) {
          this.tableData = response.data
        }
      })
    },
    copyOrder(formName) {
      if (this.tableData.length) {
        if (this.$refs[formName] !== undefined) {
          this.$refs[formName].resetFields()
        }
        this.dialogFormVisible4 = true
      } else {
        this.$message({
          message: '暂无可复制的数据！',
          type: 'error'
        })
      }
    },
    async submitAdd4() {
      let number, text
      for (let i = 0; i < this.tableData.length; i++) {
        number = this.tableData[i].工步号
        text = this.tableData[i].操作内容
        await this.submitAdd5(number, text)
      }
      this.$message({
        message: '增加成功',
        type: 'success'
      })
      this.dialogFormVisible4 = false
      this.engineTypeValue = this.form4.engineTypeValue
      this.stationNumberValue = this.form4.stationNumberValue
      this.searchTable()
    },
    async submitAdd5(number, text) {
      var param = []
      param[0] = ['工位号', this.form4.stationNumberValue]
      param[1] = ['产品型号代码', this.form4.engineTypeValue]
      param[2] = ['工步号', number]
      param[3] = ['操作内容', text]
      param[4] = ['发动机型号', this.form4.engineTypeValue]
      var Data = this.CreateData('12', '电子看板_工序内容_增加', 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)
          }
        })
      })
    },
    callOff4() {
      this.dialogFormVisible4 = false
    },
    addTo() {
      this.title = '添加'
      this.form.适用范围 = ''
      this.form.特殊需求 = ''
      this.form.改制物料 = ''
      this.form.改制工具 = ''
      this.form.改制单号 = ''
      this.form.工位号 = ''
      this.form.有效日期 = ''
      this.form.生产确认 = ''
      this.dialogFormVisible = true
    },
    submitAdd(formName) {
      this.$refs[formName].validate((valid) => {
        if (valid) {
          var param = []
          param[0] = ['序号', this.form.number]
          param[1] = ['适用范围', this.form.适用范围]
          param[2] = ['特殊需求', this.form.特殊需求]
          param[3] = ['改制物料', this.form.改制物料]
          param[4] = ['改制工具', this.form.改制工具]
          param[5] = ['改制单号', this.form.改制单号]
          param[6] = ['工位号', this.form.工位号]
          param[7] = ['有效日期', this.form.有效日期]
          var Data = this.CreateData('12', 'CATL_基础_装车单检查内容_特殊需求_新增', param)
          this.ExecDatabase(Data).then(response => {
            if (response.data[0].result === '1') {
              this.$message({
                message: '增加成功',
                type: 'success'
              })
              this.dialogFormVisible = false
              this.searchTable()
            } else {
              this.$message.error('增加失败！')
            }
          })
        } else {
          return false
        }
      })
    },
    callOff() {
      this.dialogFormVisible = false
      this.$refs['form'].resetFields()
      this.title = ''
    },
    callOff1() {
      this.dialogFormVisible1 = false
      this.$refs['form1'].resetFields()
      this.title = ''
    },
    editInformation(row) {
      this.labelNumber = row.ID
      this.form1.number = row.序号
      this.form1.适用范围 = row.适用范围
      this.form1.特殊需求 = row.特殊需求
      this.form1.改制物料 = row.改制物料
      this.form1.改制工具 = row.改制工具
      this.form1.改制单号 = row.改制单号
      this.form1.工位号 = row.工位号
      this.form1.有效日期 = row.有效日期
      this.form1.生产确认 = row.生产确认
      this.title = '编辑'
      this.dialogFormVisible1 = true
    },
    // 编辑消息
    submitUpdate(formName) {
      this.$refs[formName].validate((valid) => {
        if (valid) {
          var param = []
          param[0] = ['序号', this.form1.number]
          param[1] = ['适用范围', this.form1.适用范围]
          param[2] = ['特殊需求', this.form1.特殊需求]
          param[3] = ['改制物料', this.form1.改制物料]
          param[4] = ['改制工具', this.form1.改制工具]
          param[5] = ['改制单号', this.form1.改制单号]
          param[6] = ['工位号', this.form1.工位号]
          param[7] = ['有效日期', this.form1.有效日期]
          param[8] = ['ID', this.labelNumber]
          var Data = this.CreateData('12', 'CATL_基础_装车单检查内容_特殊需求_编辑', param)
          this.ExecDatabase(Data).then(response => {
            if (response.data[0].result === '1') {
              this.$message({
                message: '修改成功',
                type: 'success'
              })
              this.dialogFormVisible1 = false
              this.searchTable()
            } else {
              this.$message.error('修改失败！')
            }
          })
        } else {
          return false
        }
      })
    },
    deleteInformation(row) {
      this.$confirm('此操作将永久删除该行, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        var param = []
        param[0] = ['ID', row.ID]
        var Data = this.CreateData('12', 'CATL_基础_装车单检查内容_特殊需求_删除', param)
        this.ExecDatabase(Data).then(response => {
          if (response.data[0].result === '1') {
            this.$message({
              message: '删除成功',
              type: 'success'
            })
            this.searchTable()
          } else {
            this.$message.error('删除失败！')
          }
        })
      }).catch(() => {
        this.$message({
          type: 'info',
          message: '已取消删除'
        })
      })
    }
  }
}
</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 textarea{
  height: 100px;
}
</style>

