<template>
  <div class="app-container">
    <el-card style="height: 835px">
      <div style="margin-top: 7px; margin-bottom: 7px">
        <el-tooltip effect="dark" content="添加新设备" placement="top">
          <el-button type="distribution" icon="el-icon-circle-plus-outline" size="small" @click="ADD()">设备</el-button>
        </el-tooltip>
      </div>
      <div>
        <el-table :data="tableData" border size="small" stripe highlight-current-row height="740px" style="width: 770px">
          <el-table-column type="index" label="序号" align="center" width="70px"/>
          <el-table-column label="设备编号" prop="EquipmentNumber" align="center" width="150px" >
            <template slot-scope="scope">
              {{ scope.row.EquipmentNumber }}
            </template>
          </el-table-column>
          <el-table-column label="设备名称" prop="EquipmentName" align="center" width="150px" >
            <template slot-scope="scope">
              {{ scope.row.EquipmentName }}
            </template>
          </el-table-column>
          <el-table-column label="设备型号" align="center" width="150px" >
            <template slot-scope="scope">
              {{ scope.row.EquipmentModel }}
            </template>
          </el-table-column>
          <el-table-column label="工位号" prop="OpName" align="center" width="150px">
            <template slot-scope="scope">
              {{ scope.row.OpName }}
            </template>
          </el-table-column>
          <el-table-column label="操作" align="center">
            <template slot-scope="scope">
              <el-button type="text" size="mini" icon="el-icon-edit" style="margin-right:10px" @click="handleEdit(scope.row)"/>
              <el-button type="text" size="mini" icon="el-icon-delete" @click="handleDelete(scope.row)"/>
            </template>
          </el-table-column>
        </el-table>
      </div>
    </el-card>
    <el-dialog v-el-drag-dialog :title="title" :visible.sync="dialogFormVisible" center style="min-height: 300px" width="700px">
      <el-form ref="form" :model="form" :rules="rules" label-position="right" label-width="110px" class="demo-ruleForm">
        <el-row>
          <el-col :span = "12">
            <el-form-item label="设备编号" prop="设备编号" >
              <el-input v-model="form.设备编号" placeholder="设备编号" size="small" style="width:200px" clearable />
            </el-form-item>
          </el-col>
          <el-col :span = "12">
            <el-form-item label="设备名称" prop="设备名称" >
              <el-input v-model="form.设备名称" placeholder="设备名称" size="small" style="width:200px" clearable />
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span = "12">
            <el-form-item label="设备型号" prop="设备型号" >
              <el-input v-model="form.设备型号" placeholder="设备型号" size="small" style="width:200px" clearable />
            </el-form-item>
          </el-col>
          <el-col :span = "12">
            <el-form-item label="工位号" prop="工位号" >
              <el-input v-model="form.工位号" placeholder="工位号" size="small" style="width:200px" clearable />
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="callOff('form')">取消</el-button>
        <el-button v-show="title==='新增设备'" type="primary" @click="submitAdd('form')">确定</el-button>
        <el-button v-show="title==='编辑设备'" type="primary" @click="submitEdit('form')">确定</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { mapGetters } from 'vuex'
export default {
  data() {
    return {
      routineCheckType: [],
      dialogFormVisible: false,
      title: '',
      equipmentValue: '',
      equipmentName: '',
      opName: '',
      process: [],
      form: {
        设备名称: '',
        设备型号: '',
        设备编号: '',
        工位号: ''
      },
      rules: {
        设备名称: [{ required: true, message: '请输入设备名称' }],
        设备编号: [{ required: true, message: '请输入设备编号' }],
        // 设备型号: [{ required: true, message: '请输入设备型号' }],
        // 加工工艺: [{ required: true, message: '请选择加工工艺' }],
        工位号: [{ required: true, message: '请输入工位号' }]
      },
      original: '',
      total: 0, // 分页总数
      tableData: [], // 设备信息表
      pageCurrent: 1, // 分页当前页
      pageSize: 20 // 分页每页显示条数
    }
  },
  computed: {
    ...mapGetters([
      'id',
      'token'
    ])
  },
  created() {
    this.searchTable()
  },
  methods: {
    searchTable() {
      var param = []
      var Data = this.CreateData('11', 'EquipmentTabulation_Search', param)
      this.ExecDatabase(Data).then(response => {
        this.tableData = response.data
      })
    },
    ADD() {
      this.title = '新增设备'
      if (this.$refs['form'] !== undefined) {
        this.$refs['form'].resetFields()
      }
      this.form.设备名称 = ''
      this.form.设备型号 = ''
      this.form.设备编号 = ''
      this.form.工位号 = ''
      this.dialogFormVisible = true
    },
    handleEdit(row) {
      this.title = '编辑设备'
      if (this.$refs['form'] !== undefined) {
        this.$refs['form'].resetFields()
      }
      this.equipmentValue = row.ID
      this.form.设备名称 = row.EquipmentName
      this.form.设备型号 = row.EquipmentModel
      this.form.设备编号 = row.EquipmentNumber
      this.form.工位号 = row.OpName
      this.dialogFormVisible = true
    },
    callOff(formName) {
      this.dialogFormVisible = false
      this.$refs[formName].resetFields()
    },
    submitAdd(formName) {
      this.$refs[formName].validate((valid) => {
        if (valid) {
          var param = []
          param[0] = ['EquipmentName', this.form.设备名称]
          param[1] = ['EquipmentModel', this.form.设备型号]
          param[2] = ['EquipmentNumber', this.form.设备编号]
          param[3] = ['OpName', this.form.工位号]
          var Data = this.CreateData('12', 'EquipmentTabulation_Insert', param)
          this.ExecDatabase(Data).then(response => {
            if (response.data[0].result === '1') {
              this.$message.success('添加成功')
              this.searchTable()
              this.dialogFormVisible = false
            } else {
              this.$message.error('添加失败')
            }
          })
          /* var param = []
          param[0] = ['工位', this.form.工位号]
          var Data = this.CreateData('11', '设备管理_设备信息_验证工位号是否重复', param)
          this.ExecDatabase(Data).then(response => {
            if (Number(response.data[0].Column1) === 0) {
              var param = []
              param[0] = ['设备名称', this.form.设备名称]
              param[1] = ['设备型号', this.form.设备型号]
              param[2] = ['加工工艺', this.form.设备编号]
              param[3] = ['工位', this.form.工位号]
              var Data = this.CreateData('12', '设备管理_设备信息_增加', param)
              this.ExecDatabase(Data).then(response => {
                if (response.data[0].result === '1') {
                  this.$message.success('添加成功')
                  this.searchTable()
                  this.dialogFormVisible = false
                } else {
                  this.$message.error('添加失败')
                }
              })
            } else {
              this.$message.warning('工位号重复')
            }
          })*/
        }
      })
    },
    submitEdit(formName) {
      this.$refs[formName].validate((valid) => {
        if (valid) {
          var param = []
          param[0] = ['EquipmentName', this.form.设备名称]
          param[1] = ['EquipmentModel', this.form.设备型号]
          param[2] = ['EquipmentNumber', this.form.设备编号]
          param[3] = ['OpName', this.form.工位号]
          param[5] = ['ID', this.equipmentValue]
          var Data = this.CreateData('12', 'EquipmentTabulation_Update', param)
          this.ExecDatabase(Data).then(response => {
            if (response.data[0].result === '1') {
              this.$message.success('编辑成功')
              this.searchTable()
              this.dialogFormVisible = false
            } else {
              this.$message.error('编辑失败')
            }
          })
          /* if (this.form.工位号 === this.original) {
            var param = []
            param[0] = ['设备名称', this.form.设备名称]
            param[1] = ['设备型号', this.form.设备型号]
            param[2] = ['加工工艺', this.form.设备编号]
            param[3] = ['工位', this.form.工位号]
            param[5] = ['设备流水号', this.equipmentValue]
            var Data = this.CreateData('12', '设备管理_设备信息_编辑', param)
            this.ExecDatabase(Data).then(response => {
              if (response.data[0].result === '1') {
                this.$message.success('编辑成功')
                this.searchTable()
                this.dialogFormVisible = false
              } else {
                this.$message.error('编辑失败')
              }
            })
          } else {
            var param1 = []
            param1[0] = ['工位', this.form.工位号]
            var Data1 = this.CreateData('11', '设备管理_设备信息_验证工位号是否重复', param1)
            this.ExecDatabase(Data1).then(response => {
              if (Number(response.data[0].Column1) === 0) {
                var param = []
                param[0] = ['设备名称', this.form.设备名称]
                param[1] = ['设备型号', this.form.设备型号]
                param[2] = ['加工工艺', this.form.加工工艺]
                param[3] = ['工位', this.form.工位号]
                param[4] = ['工时单价', this.form.工时单价]
                param[5] = ['月设备加工能力', this.form.月设备加工能力]
                param[6] = ['设备流水号', this.equipmentValue]
                param[7] = ['原工位', this.original]
                var Data = this.CreateData('12', '设备管理_设备信息_编辑', param)
                this.ExecDatabase(Data).then(response => {
                  if (response.data[0].result === '1') {
                    this.$message.success('编辑成功')
                    this.searchTable()
                    this.dialogFormVisible = false
                  } else {
                    this.$message.error('编辑失败')
                  }
                })
              } else {
                this.$message.warning('工位号重复')
              }
            })
          }*/
        }
      })
    },
    handleDelete(row) {
      this.$confirm('是否删除?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        var param = []
        param[0] = ['ID', row.ID]
        var Data = this.CreateData('12', 'EquipmentTabulation_Delete', param)
        this.ExecDatabase(Data).then(response => {
          if (response.data[0].result === '1') {
            this.$message.success('删除成功')
            this.searchTable()
          } else {
            this.$message.error('删除失败')
          }
        })
      }).catch(() => {
        this.$message({
          type: 'info',
          message: '已取消删除'
        })
      })
    },
    handleSizeChanges(val) {
      this.pageCurrent = 1
      this.pageSize = val
      this.searchTable()
    },
    handleCurrentChanges(val) {
      this.pageCurrent = val
      this.searchTable()
    }
  }
}
</script>

