<template>
  <div class="app-container">
    <el-card class="topCard">
      <div class="topDiv">
        <el-select v-model="searchForm.stationNumber" placeholder="请选择工位号" clearable size="small"
          style="width: 200px; margin-right: 10px" @change="handleStationChange">
          <el-option v-for="item in stationNumber" :key="item.value" :label="item.label" :value="item.value" />
        </el-select>
        <el-select
          v-model="searchForm.engineType"
          placeholder="请选择产品型号"
          clearable
          size="small"
          style="width: 150px; margin-right: 10px"
          @change="handleEngineTypeChange">
          <el-option v-for="item in engineType" :key="item.value" :label="item.label" :value="item.value" />
        </el-select>
        <el-button type="distribution" 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" style="width: 70%" 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="100px">
          <template slot-scope="scope">
            {{ scope.row.工位号 }}
          </template>
        </el-table-column>
        <!-- <el-table-column label="产品型号代码" width="140px">
          <template slot-scope="scope">
            {{ scope.row.产品型号代码 || scope.row.机型ID }}
          </template>
        </el-table-column> -->
        <el-table-column label="产品型号名称" width="100px">
          <template slot-scope="scope">
            <div style="white-space: pre-line !important;">{{ scope.row.产品型号名称 || scope.row.机型号 }}</div>
          </template>
        </el-table-column>
        <el-table-column label="检查内容" width="230px">
          <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 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="500px">
      <el-form ref="form" :model="form" :rules="rules" label-position="right" label-width="110px">
        <el-form-item label="工位号:" prop="stationNumber" style="display: inline-block">
          <el-select v-model="form.stationNumber" placeholder="请选择工位号" clearable size="small" style="width: 300px">
            <el-option v-for="item in stationNumber" :key="item.value" :label="item.label" :value="item.value" />
          </el-select>
        </el-form-item>
        <el-form-item label="产品型号:" prop="engineType" style="display: inline-block">
          <el-select v-model="form.engineType" placeholder="请选择产品型号" clearable size="small" style="width: 300px">
            <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="text" style="display: inline-block">
          <el-input v-model="form.text" :rows="2" type="textarea" clearable size="small" style="width: 300px"
            placeholder="请输入检查内容" />
        </el-form-item>
        <el-form-item label="检查要求:" prop="work" style="display: inline-block">
          <el-input v-model="form.work" :rows="2" type="textarea" clearable size="small" style="width: 300px"
            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="500px">
      <el-form ref="form1" :model="form1" :rules="rules1" label-position="right" label-width="110px">
        <el-form-item label="工位号:" prop="stationNumber" style="display: inline-block">
          <el-select v-model="form1.stationNumber" placeholder="请选择工位号" clearable size="small" style="width: 300px">
            <el-option
              v-for="item in stationNumber"
              :key="item.value"
              :label="item.label"
              :value="item.value"/>
          </el-select>
        </el-form-item>
        <el-form-item label="产品型号:" prop="engineType" style="display: inline-block">
          <el-select v-model="form1.engineType" placeholder="请选择产品型号" clearable size="small" style="width: 300px">
            <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="sort" style="display: inline-block">
          <el-input-number 
            v-model="form1.sort" 
            :min="1" 
            controls-position="right"
            size="small" 
            style="width: 300px"/>
        </el-form-item>
        <el-form-item label="检查内容:" prop="text" style="display: inline-block">
          <el-input
            v-model="form1.text"
            :rows="2"
            type="textarea"
            clearable
            size="small"
            style="width: 300px"
            placeholder="请输入检查内容"/>
        </el-form-item>
        <el-form-item label="检查要求:" prop="work" style="display: inline-block">
          <el-input
            v-model="form1.work"
            :rows="2"
            type="textarea"
            clearable
            size="small"
            style="width: 300px"
            placeholder="请输入检查要求"/>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button size="small" @click="callOff1('form1')">取消</el-button>
        <el-button 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 {
      searchForm: {
        stationNumber: '',
        engineType: ''
      },
      tableData: [],
      stationNumber: [],
      engineType: [],
      dialogFormVisible: false,
      dialogFormVisible1: false,
      dialogFormVisible4: false,
      title: '',
      form: {
        stationNumber: '',
        engineType: '',
        text: '',
        work: ''
      },
      rules: {
        stationNumber: [{ required: true, message: '请选择工位号', trigger: 'change' }],
        engineType: [{ required: true, message: '请选择产品型号', trigger: 'change' }],
        text: [{ required: true, message: '请输入检查内容', trigger: 'blur' }],
        work: [{ required: true, message: '请输入检查要求', trigger: 'blur' }]
      },
      form1: {
        id: '',
        stationNumber: '',
        engineType: '',
        sort: 1,
        text: '',
        work: ''
      },
      rules1: {
        stationNumber: [{ required: true, message: '请选择工位号', trigger: 'change' }],
        engineType: [{ required: true, message: '请选择产品型号', trigger: 'change' }],
        sort: [{ required: true, message: '请输入序号', trigger: 'blur' }],
        text: [{ required: true, message: '请输入检查内容', trigger: 'blur' }],
        work: [{ 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'
    ])
  },
  created() {
    this.getStationNumber()
    this.getEndStation()
  },
  mounted() {
    this.searchTable()
  },
  methods: {
    getStationNumber() {
      var param = []
      var Data = this.CreateData('11', 'MES_计划BOM_工位与名称_查询', param)
      this.ExecDatabase(Data).then(response => {
        for (let i = 0; i < response.data.length; i++) {
          this.stationNumber.push({
            label: `${response.data[i].工位号}-${response.data[i].工位名称}`,
            value: response.data[i].工位号
          })
        }
      })
    },
    // 获取产品型号
    getEndStation() {
      this.engineType = []
      var param = []
      var Data = this.CreateData('11', 'CATL_基础信息_机型信息_查询', 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].机型ID
          })
        }
      })
    },
    handleStationChange() {
      this.searchTable()
    },
    handleEngineTypeChange() {
      this.searchTable()
    },
    searchTable() {
      var param = []
      param[0] = ['工位号', this.searchForm.stationNumber]
      param[1] = ['产品型号代码', this.searchForm.engineType]
      var Data = this.CreateData('11', 'CATL_工位检查项_基础_查询', param)
      this.ExecDatabase(Data).then(response => {
        let rows = response.data
        if (this.searchForm.engineType) {
          rows = rows.filter(item => item.产品型号代码 === this.searchForm.engineType || item.机型ID === this.searchForm.engineType)
        }
        this.tableData = rows
      })
    },
    addTo() {
      this.title = '新增检查项'
      // 打开时默认带入当前筛选的工位与产品型号
      this.form.stationNumber = this.searchForm.stationNumber
      this.form.engineType = this.searchForm.engineType
      this.dialogFormVisible = true
    },
    submitAdd(formName) {
      this.$refs[formName].validate((valid) => {
        if (valid) {
          var param = []
          param[0] = ['工位号', this.form.stationNumber]
          param[1] = ['检查内容', this.form.text]
          param[2] = ['检查要求', this.form.work]
          // 传递产品型号代码与名称
          const engine = this.engineType.find(e => e.value === this.form.engineType)
          const engineName = engine ? engine.label : ''
          param[3] = ['产品型号代码', this.form.engineType]
          param[4] = ['产品型号名称', engineName]
          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()
              this.$refs[formName].resetFields()
            } else {
              this.$message.error('新增失败！')
            }
          })
        }
      })
    },
    editInformation(row) {
      this.title = '编辑检查项'
      this.form1.id = row.ID
      this.form1.stationNumber = row.工位号
      this.form1.engineType = row.产品型号代码 || row.机型ID || ''
      this.form1.sort = row.排序 || 1
      this.form1.text = row.检查内容
      this.form1.work = row.检查要求
      this.dialogFormVisible1 = true
    },
    submitUpdate(formName) {
      this.$refs[formName].validate((valid) => {
        if (valid) {
          var param = []
          param[0] = ['ID', this.form1.id]
          param[1] = ['排序', this.form1.sort]
          param[2] = ['工位号', this.form1.stationNumber]
          param[3] = ['检查内容', this.form1.text]
          param[4] = ['检查要求', this.form1.work]
          // 传递产品型号代码与名称
          const engine = this.engineType.find(e => e.value === this.form1.engineType)
          const engineName = engine ? engine.label : ''
          param[5] = ['产品型号代码', this.form1.engineType]
          param[6] = ['产品型号名称', engineName]
          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('修改失败！')
            }
          })
        }
      })
    },
    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('删除失败！')
          }
        })
      })
    },
    callOff(formName) {
      this.dialogFormVisible = false
      this.$refs[formName].resetFields()
    },
    callOff1(formName) {
      this.dialogFormVisible1 = false
      this.$refs[formName].resetFields()
    },
    callOff4() {
      this.dialogFormVisible4 = false
    },
    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)
          }
        })
      })
    },
    tableRowClassName({ rowIndex }) {
      if (rowIndex % 2 === 0) {
        return 'warning-row'
      }
      return 'success-row'
    }
  }
}
</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;
}

::v-deep .el-input-number .el-input__inner {
  text-align: left;
}
</style>
