<template>
  <div class="app-container">
    <el-card class="topCard">
      <el-col :span="11">
        <div class="topDiv">
          <span>配方名称：</span>
          <el-input v-model="repiceName" style="width:150px;" placeholder="配方名称" size="small"/>
          <el-button type="primary" plain size="small" icon="el-icon-order" @click="searchTable()">查询</el-button>
          <el-button type="primary" icon="el-icon-circle-plus-outline" size="small" @click="addTo">新增</el-button>
          <el-button type="success" size="small" plain icon="el-icon-document-copy" @click="copyOrder('form4')">复制配方</el-button>
          <el-table
            ref="multipleTable"
            :data="tableData"
            :header-cell-style="{background:'#eef1f6',color:'#606266'}"
            :cell-class-name="tableRowClassName"
            height="370px"
            class="main"
            tooltip-effect="dark"
            style="width: 100%"
            border
            size="mini"
            @row-click="currentRepice">
            <el-table-column align="center" label="配方号" width="100">
              <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="操作" width="200">
              <template slot-scope="scope">
                <el-button type="text" icon="el-icon-edit" size="small" @click="editInformation(scope.row)">编辑</el-button>
                <el-button type="text" icon="el-icon-delete" size="small" style="color: red" @click="deleteInformation(scope.row)">删除</el-button>
              </template>
            </el-table-column>
          </el-table>
        </div>
        <div class="topDiv">
          <span>工位号：</span>
          <el-select v-model="stationNumberValue" placeholder="工位号" clearable size="small" style="width: 120px">
            <el-option
              v-for="item in stationNumber"
              :key="item.value"
              :label="item.label"
              :value="item.value"
              size="mini"/>
          </el-select>
          <el-input v-model="stationName" readonly style="width:160px;" size="small"/>
          <span style="margin-left: 10px">操作名：</span>
          <el-input v-model="operationName" style="width:150px;" placeholder="操作名" size="small"/>
          <el-button type="primary" plain size="small" icon="el-icon-order" @click="searchTableOperation()">查询</el-button>
          <el-button type="primary" icon="el-icon-circle-plus-outline" size="small" @click="addTo">新增</el-button>
          <el-table
            ref="multipleTable"
            :data="tableData3"
            :header-cell-style="{background:'#eef1f6',color:'#606266'}"
            :cell-class-name="tableRowClassName"
            height="370px"
            class="main"
            tooltip-effect="dark"
            style="width: 100%"
            border
            size="mini">
            <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="120px">
              <template slot-scope="scope">
                {{ scope.row.工位号 }}
              </template>
            </el-table-column>
            <el-table-column align="center" label="操作名称">
              <template slot-scope="scope">
                {{ scope.row.操作名称 }}
              </template>
            </el-table-column>
            <el-table-column align="center" label="操作" width="200">
              <template slot-scope="scope">
                <el-button type="text" icon="el-icon-edit" size="small" @click="editInformation(scope.row)">编辑</el-button>
                <el-button type="text" icon="el-icon-delete" size="small" style="color: red" @click="deleteInformation(scope.row)">删除</el-button>
              </template>
            </el-table-column>
          </el-table>
        </div>
      </el-col>
      <el-col :span="12">
        <div class="topDiv" style="margin-left: 10px">
          <span style="margin-left: 10px">配方列表（请选择左侧配方查看）,选择工位号进行筛选</span>
          <el-select v-model="stationRepiceValue" placeholder="工位号" clearable size="small" style="width: 120px" @change="changeTableData">
            <el-option
              v-for="item in stationRepiceNumber"
              :key="item.value"
              :label="item.label"
              :value="item.value"
              size="mini"/>
          </el-select>
          <el-table
            ref="multipleTable"
            :data="tableData1"
            :header-cell-style="{background:'#eef1f6',color:'#606266'}"
            :cell-class-name="tableRowClassName"
            height="784px"
            class="main"
            tooltip-effect="dark"
            style="width: 100%"
            border
            size="mini">
            <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="操作号码" width="100px">
              <template slot-scope="scope">
                {{ scope.row.操作号码 }}
              </template>
            </el-table-column>
            <el-table-column align="center" label="操作名称">
              <template slot-scope="scope">
                {{ scope.row.操作名称 }}
              </template>
            </el-table-column>
            <el-table-column align="center" label="操作" width="100">
              <template slot-scope="scope">
                <el-button type="text" icon="el-icon-edit" size="small" @click="editInformation(scope.row)">编辑</el-button>
              </template>
            </el-table-column>
          </el-table>
        </div>
      </el-col>
    </el-card>
    <el-dialog :visible.sync="dialogFormVisible" :title="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="stationNumber" style="display: inline-block">
          <el-select v-model="form.stationNumber" placeholder="请选择工位号" clearable size="small" style="width: 200px">
            <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="moduleDescription" style="display: inline-block">
          <el-select v-model="form.moduleDescription" placeholder="请选择产品型号" clearable size="small" style="width: 200px">
            <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-item label="序号:" prop="number" style="display: inline-block">
          <el-input v-model="form.number" style="width:200px;" placeholder="请输入序号" size="small"/>
        </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: 200px"
            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="400px">
      <el-form ref="form1" :model="form1" :rules="rules1" label-position="right" label-width="110px">
        <el-form-item label="序号:" prop="number" style="display: inline-block">
          <el-input v-model="form1.number" style="width:200px;" placeholder="请输入序号" size="small"/>
        </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: 200px"
            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 {
      stationRepiceNumberTemp: [],
      stationRepiceNumber: [],
      stationRepiceValue: '',
      isSearchRepice: false,
      operationName: '',
      repiceName: '',
      tableData: [], // 总数据数组
      tableData1: [], // 总数据数组
      tableData1Temp: [], // 总数据数组
      tableData3: [], // 总数据数组
      stationNumber: [],
      stationNumberValue: '',
      stationName: '',
      moduleDescription: [],
      moduleDescriptionValue: '',
      engineTypeValue: '',
      engineTypeValue1: '',
      engineTypeName: '',
      engineType: [],
      isDisableValue: 0,
      labelText: '',
      labelNumber: '',
      dialogFormVisible: false,
      dialogFormVisible1: false,
      dialogFormVisible4: false,
      title: '',
      form: {
        stationNumber: '',
        moduleDescription: '',
        number: '',
        text: ''
      },
      rules: {
        stationNumber: [{ required: true, message: '请选择工位号', trigger: 'change' }],
        moduleDescription: [{ required: true, message: '请选择产品型号', trigger: 'change' }],
        number: [{ required: true, message: '请输入序号', trigger: 'blur' }],
        text: [{ required: true, message: '请输入操作内容', trigger: 'blur' }]
      },
      form1: {
        number: '',
        text: ''
      },
      rules1: {
        number: [{ required: true, message: '请输入序号', trigger: 'blur' }],
        text: [{ 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 = ''
      }
    }
  },
  created() {
    this.getStationNumber()
  },
  mounted() {
    setTimeout(() => {
      this.searchTable()
      this.searchTableOperation()
    }, 300)
  },
  methods: {
    getStationNumber() {
      this.stationNumber = []
      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].工位号,
            value: response.data[i].工位号,
            labelName: response.data[i].工位名称
          })
        }
      })
    },
    // 表格颜色变化
    tableRowClassName({ row, rowIndex }) {
      // console.log(row, rowIndex)
      if (rowIndex % 2 === 0) {
        return 'black'
      } else {
        return 'red'
      }
    },
    // 查询配方
    searchTable() {
      this.tableData = []
      var param = []
      param[0] = ['配方名称', this.repiceName]
      var Data = this.CreateData('11', '北汽福田_配方_配方查询', param)
      this.ExecDatabase(Data).then(response => {
        console.log(response.data)
        if (response.data.length !== 0) {
          this.tableData = response.data
        }
      })
    },
    currentRepice(row) {
      if (this.isSearchRepice) {
        this.$message({
          message: '正在查询，请稍等！',
          type: 'warning'
        })
        return
      }
      this.isSearchRepice = true
      this.tableData1 = []
      this.stationRepiceNumberTemp = []
      this.stationRepiceNumber = []
      this.stationRepiceValue = ''
      var param = []
      param[0] = ['配方号', row['配方号']]
      var Data = this.CreateData('11', '北汽福田_配方_配方详细查询', param)
      this.ExecDatabase(Data).then(response => {
        if (response.data.length !== 0) {
          this.tableData1 = response.data
          this.tableData1Temp = response.data
          for (let i = 0; i < this.tableData1.length; i++) {
            if (this.stationRepiceNumberTemp.indexOf(this.tableData1[i].工位号) === -1) {
              this.stationRepiceNumberTemp.push(this.tableData1[i].工位号)
              this.stationRepiceNumber.push({
                label: this.tableData1[i].工位号,
                value: this.tableData1[i].工位号
              })
            }
          }
        }
        this.isSearchRepice = false
      })
    },
    changeTableData(value) {
      if (value === '') {
        this.tableData1 = this.tableData1Temp
        return
      }
      this.tableData1 = this.tableData1Temp.filter(item => item.工位号 === value)
    },
    // 北汽福田_配方_配方操作查询
    searchTableOperation() {
      this.tableData3 = []
      var param = []
      param[0] = ['操作名称', this.operationName]
      param[1] = ['工位号', this.stationNumberValue]
      var Data = this.CreateData('11', '北汽福田_配方_配方操作查询', param)
      this.ExecDatabase(Data).then(response => {
        console.log(response.data)
        if (response.data.length !== 0) {
          this.tableData3 = 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]
      console.log(param)
      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.dialogFormVisible = true
    },
    submitAdd(formName) {
      this.$refs[formName].validate((valid) => {
        if (valid) {
          var param = []
          param[0] = ['工位号', this.form.stationNumber]
          param[1] = ['产品型号代码', this.form.moduleDescription]
          param[2] = ['工步号', this.form.number]
          param[3] = ['操作内容', this.form.text]
          param[4] = ['发动机型号', this.form.moduleDescription]
          console.log(param)
          var Data = this.CreateData('12', '电子看板_工序内容_增加', 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.form.stationNumber = ''
      this.form.moduleDescription = ''
      this.form.number = ''
      this.form.text = ''
      this.engineTypeName = ''
      this.title = ''
    },
    callOff1() {
      this.dialogFormVisible1 = false
      this.$refs['form1'].resetFields()
      this.form.stationNumber = ''
      this.form.moduleDescription = ''
      this.form.number = ''
      this.form.text = ''
      this.engineTypeName = ''
      this.title = ''
    },
    editInformation(row) {
      this.labelNumber = row.ID
      this.form1.number = row.工步号
      this.form1.text = 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.text]
          param[2] = ['ID', this.labelNumber]
          console.log(param)
          var Data = this.CreateData('12', '电子看板_工序内容_修改_要点理由YC', 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', '电子看板_工序内容_删除', 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;
}
</style>

