<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: 1500px">
          <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="工位号" prop="OpName" align="center" width="150px">
            <template slot-scope="scope">
              {{ scope.row.OpName }}
            </template>
          </el-table-column>
          <el-table-column label="刀具代码" prop="OpName" align="center" width="150px">
            <template slot-scope="scope">
              {{ scope.row.ToolNumber }}
            </template>
          </el-table-column>
          <el-table-column label="刀具名称" prop="OpName" align="center" width="150px">
            <template slot-scope="scope">
              {{ scope.row.ToolName }}
            </template>
          </el-table-column>
          <el-table-column label="规格型号" prop="OpName" align="center" width="150px">
            <template slot-scope="scope">
              {{ scope.row.ToolModel }}
            </template>
          </el-table-column>
          <el-table-column label="类别" prop="OpName" align="center" width="80px">
            <template slot-scope="scope">
              {{ scope.row.Category }}
            </template>
          </el-table-column>
          <el-table-column label="寿命" prop="OpName" align="center" width="80px">
            <template slot-scope="scope">
              {{ scope.row.ToolLife }}
            </template>
          </el-table-column>
          <el-table-column label="刃数片数" prop="OpName" align="center" width="80px">
            <template slot-scope="scope">
              {{ scope.row.EdgesNumber }}
            </template>
          </el-table-column>
          <el-table-column label="更新时间" prop="CreateTime" align="center" width="150px">
            <template slot-scope="scope">
              {{ scope.row.CreateTime }}
            </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-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-number v-model="form.寿命" min="0" 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-number v-model="form.刃数片数" min="1" 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: '',
      toolValue: '',
      toolNumber: '',
      equiptmentNumber: '',
      Equiptment: [],
      opName: '',
      process: [],
      form: {
        刀具代码: '',
        刀具名称: '',
        规格型号: '',
        寿命: '',
        刃数片数: '',
        类别: '',
        设备编号: ''
      },
      rules: {
        刀具代码: [{ required: true, message: '请输入刀具代码' }],
        刀具名称: [{ required: true, message: '请输入刀具名称' }],
        设备编号: [{ 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()
    this.getEquiptment()
  },
  methods: {
    getEquiptment() {
      var param = []
      var Data = this.CreateData('11', 'Equipment_Search', param)
      this.ExecDatabase(Data).then(response => {
        for (let i = 0; i < response.data.length; i++) {
          this.Equiptment.push({
            label: response.data[i].EquipmentName,
            value: response.data[i].EquipmentNumber
          })
        }
      })
    },
    searchTable() {
      var param = []
      var Data = this.CreateData('11', 'ToolInformation_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.form.刃数片数 = ''
      this.form.设备编号 = ''
      this.form.类别 = ''
      this.dialogFormVisible = true
    },
    handleEdit(row) {
      this.title = '编辑刀具'
      if (this.$refs['form'] !== undefined) {
        this.$refs['form'].resetFields()
      }
      this.toolValue = row.ID
      this.equiptmentNumber = row.EquipmentNumber
      this.toolNumber = row.ToolNumber
      this.form.刀具名称 = row.ToolName
      this.form.刀具代码 = row.ToolNumber
      this.form.规格型号 = row.ToolModel
      this.form.设备编号 = row.EquipmentNumber
      this.form.寿命 = row.ToolLife
      this.form.刃数片数 = row.EdgesNumber
      this.form.类别 = row.Category
      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] = ['ToolNumber', this.toolNumber]
          param[1] = ['EquipmentNumber', this.equiptmentNumber]
          var Data = this.CreateData('11', 'ToolInformation_IsRepeat', param)
          this.ExecDatabase(Data).then(response => {
            if (Number(response.data[0].Column1) === 0) {
              var param = []
              param[0] = ['ToolNumber', this.form.刀具代码]
              param[1] = ['ToolName', this.form.刀具名称]
              param[2] = ['EquipmentNumber', this.form.设备编号]
              param[3] = ['ToolModel', this.form.规格型号]
              param[4] = ['ToolLife', this.form.寿命]
              param[5] = ['EdgesNumber', this.form.刃数片数]
              param[6] = ['Category', this.form.类别]
              var Data = this.CreateData('12', 'ToolInformation_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('添加失败')
                }
              })
            } else {
              this.$message.warning('设备和刀具关联重复')
            }
          })
        }
      })
    },
    submitEdit(formName) {
      this.$refs[formName].validate((valid) => {
        if (valid) {
          if (this.form.刀具代码 === this.toolNumber && this.form.设备编号 === this.equiptmentNumber) {
            var param = []
            param[0] = ['ToolNumber', this.form.刀具代码]
            param[1] = ['ToolName', this.form.刀具名称]
            param[2] = ['EquipmentNumber', this.form.设备编号]
            param[3] = ['ToolModel', this.form.规格型号]
            param[4] = ['ToolLife', this.form.寿命]
            param[5] = ['EdgesNumber', this.form.刃数片数]
            param[6] = ['Category', this.form.类别]
            param[7] = ['ID', this.toolValue]
            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] = ['ToolNumber', this.toolNumber]
            param1[1] = ['EquipmentNumber', this.equiptmentNumber]
            var Data1 = this.CreateData('11', 'ToolInformation_IsRepeat', param1)
            this.ExecDatabase(Data1).then(response => {
              if (Number(response.data[0].Column1) === 0) {
                var param = []
                param[0] = ['ToolNumber', this.form.刀具代码]
                param[1] = ['ToolName', this.form.刀具名称]
                param[2] = ['EquipmentNumber', this.form.设备编号]
                param[3] = ['ToolModel', this.form.规格型号]
                param[4] = ['ToolLife', this.form.寿命]
                param[5] = ['EdgesNumber', this.form.刃数片数]
                param[6] = ['Category', this.form.类别]
                param[7] = ['ID', this.toolValue]
                var Data = this.CreateData('12', 'ToolInformation_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('编辑失败')
                  }
                })
              } 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', 'ToolInformation_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>

