<template>
  <div class="app-container">
    <el-card class="topCard">
      <div class="topDiv">
        <span style="margin-left: 10px">机型：</span>
        <el-select v-model="engineTypeValue1" placeholder="请选择产品型号" clearable size="small" style="width: 150px" @change="searchTable()">
          <el-option
            v-for="item in engineType1"
            :key="item.label"
            :label="item.label"
            :value="item.value"
            size="mini"/>
        </el-select>
        <el-button type="primary" plain size="small" icon="el-icon-order" @click="searchTable()">查询</el-button>
        <el-button type="distribution" icon="el-icon-circle-plus-outline" size="small" @click="add()">新增</el-button>
      </div>
      <el-table
        ref="multipleTable"
        :data="tableData"
        :header-cell-style="{background:'#eef1f6',color:'#606266'}"
        :cell-class-name="tableRowClassName"
        height="770px"
        class="main"
        tooltip-effect="dark"
        style="width: 100%"
        border
        size="mini">
        <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="工序号">
          <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="edit(scope.row)">编辑</el-button>
          </template>
        </el-table-column>
      </el-table>
    </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="engineTypeValue" style="display: inline-block">
          <el-select v-model="form.engineTypeValue" placeholder="请选择产品型号" clearable size="small">
            <el-option
              v-for="item in engineType1"
              :key="item.label"
              :label="item.label"
              :value="item.value"
              size="mini"/>
          </el-select>
        </el-form-item>
        <el-form-item label="节拍:" prop="rhythm" style="display: inline-block">
          <el-input
            v-model="form.rhythm"
            clearable
            size="small"
            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="addRhythm('form')">确定</el-button>
      </div>
    </el-dialog>
    <el-dialog :visible.sync="dialogFormVisible1" title="工艺节拍编辑" center style="min-height: 200px;" width="400px">
      <el-form ref="editForm" :model="editForm" :rules="editRules" label-position="right" label-width="110px">
        <el-form-item label="机型:" prop="type" style="display: inline-block">
          <el-select v-model="editForm.type" placeholder="请选择产品型号" clearable size="small">
            <el-option
              v-for="item in engineType1"
              :key="item.label"
              :label="item.label"
              :value="item.value"
              size="mini"/>
          </el-select>
        </el-form-item>
        <el-form-item label="工序号:" prop="stationNumber" style="display: inline-block">
          <el-input
            v-model="editForm.stationNumber"
            clearable
            size="small"
            placeholder="请输入工序号"/>
        </el-form-item>
        <el-form-item label="工位名称:" prop="name" style="display: inline-block">
          <el-input
            v-model="editForm.name"
            clearable
            size="small"
            placeholder="请输入工位名称"/>
        </el-form-item>
        <el-form-item label="节拍:" prop="rhythm" style="display: inline-block">
          <el-input
            v-model="editForm.rhythm"
            clearable
            size="small"
            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="submitEdit('editForm')">确定</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
export default {
  data() {
    return {
      rhythm: '',
      engineTypeValue1: '',
      engineType1: [],
      tableData: [], // 总数据数组
      stationNumber: [],
      stationNumberValue: '',
      dialogFormVisible: false,
      dialogFormVisible1: false,
      form: {
        rhythm: '',
        engineTypeValue: ''
      },
      editForm: {
        id: '',
        type: '',
        stationNumber: '',
        name: '',
        rhythm: ''
      },
      rules: {
        rhythm: [{ required: true, message: '请输入节拍', trigger: 'blur' }],
        engineTypeValue: [{ required: true, message: '请选择机型', trigger: 'blur' }]
      },
      editRules: {
        rhythm: [{ required: true, message: '请输入', trigger: 'blur' }],
        name: [{ required: true, message: '请输入', trigger: 'blur' }],
        stationNumber: [{ required: true, message: '请输入', trigger: 'blur' }],
        type: [{ required: true, message: '请选择机型', trigger: 'blur' }]
      }
    }
  },
  created() {
    this.getProductType()
  },
  methods: {
    // 初始化获取产品型号
    getProductType() {
      this.engineType1 = []
      var param = []
      var Data = this.CreateData('11', 'MES_基本变量_机型代码_查询', param)
      this.ExecDatabase(Data).then(response => {
        this.engineTypeValue1 = response.data[0].发动机机型标志
        this.searchTable()
        for (let i = 0; i < response.data.length; i++) {
          this.engineType1.push({
            label: response.data[i].发动机型号,
            value: response.data[i].发动机机型标志
          })
        }
      })
    },
    // 表格颜色变化
    tableRowClassName({ row, rowIndex }) {
      if (rowIndex % 2 === 0) {
        return 'black'
      } else {
        return 'red'
      }
    },
    // 查询信息
    searchTable() {
      this.tableData = []
      var param = []
      param[0] = ['机型代码', this.engineTypeValue1]
      var Data = this.CreateData('11', 'MES_基本数据_工位节拍_查询', param)
      this.ExecDatabase(Data).then(response => {
        if (response.data.length !== 0) {
          this.tableData = response.data
        }
      })
    },
    add() {
      this.dialogFormVisible = true
    },
    callOff() {
      this.dialogFormVisible = false
      this.dialogFormVisible1 = false
      this.$refs['form'].resetFields()
      this.$refs['editForm'].resetFields()
    },
    // 新增节拍
    addRhythm(formName) {
      this.$refs[formName].validate((valid) => {
        if (valid) {
          var param = []
          param[0] = ['基本机型', this.form.engineTypeValue]
          param[1] = ['工序节拍', this.rhythm]
          var Data = this.CreateData('12', 'MES_基本数据_工位节拍_增加', param)
          this.ExecDatabase(Data).then(response => {
            if (response.data[0].result === '1') {
              this.$message({
                message: '新增成功',
                type: 'success'
              })
              this.searchTable()
            } else {
              this.$message.error('新增失败！')
            }
          })
          this.dialogFormVisible = false
        } else {
          return false
        }
      })
    },
    // 编辑
    edit(row) {
      // console.log(row, 'row')
      this.dialogFormVisible1 = true
      this.editForm.id = row.id
      this.editForm.type = row.发动机机型标志
      this.editForm.stationNumber = row.工序号
      this.editForm.name = row.工位名称
      this.editForm.rhythm = row.工序节拍
    },
    //  提交编辑
    submitEdit(formName) {
      this.$refs[formName].validate((valid) => {
        if (valid) {
          var param = []
          param[0] = ['id', this.editForm.id]
          param[1] = ['基本机型', this.editForm.type]
          param[2] = ['工序号', this.editForm.stationNumber]
          param[3] = ['工位名称', this.editForm.name]
          param[4] = ['工序节拍', this.editForm.rhythm]
          var Data = this.CreateData('12', 'MES_基本数据_工位节拍_修改', param)
          this.ExecDatabase(Data).then(response => {
            console.log(response, '修改结果')
            if (response.data[0].result === '1') {
              this.$message({
                message: '编辑成功',
                type: 'success'
              })
              this.searchTable()
            } else {
              this.$message.error('编辑失败！')
            }
          })
          this.dialogFormVisible1 = false
        } else {
          return false
        }
      })
    }
  }
}
</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>

