<template>
  <div class="app-container">
    <el-card>
      <span>设备名称:</span>
      <el-input v-model="equipmentName" placeholder="设备名称" size="small" clearable style="width:200px;margin-left: 10px"/>
      <el-button type="success" size="small" icon="el-icon-search" style="margin-left:30px" @click="pageCurrent = 1;fetchData()">查询</el-button>
      <el-button type="primary" size="small" icon="el-icon-circle-plus-outline" style="margin-left:10px" @click="handleAdd()">增加</el-button>
    </el-card>

    <el-card>
      <el-table :data="List" border size="small" height="500px">
        <el-table-column label="设备名称" align="center" >
          <template slot-scope="scope">
            {{ scope.row.设备名称 }}
          </template>
        </el-table-column>
        <el-table-column label="设备编号" align="center" >
          <template slot-scope="scope">
            {{ scope.row.设备编号 }}
          </template>
        </el-table-column>
        <el-table-column label="设备型号" align="center" >
          <template slot-scope="scope">
            {{ scope.row.设备型号 }}
          </template>
        </el-table-column>
        <el-table-column label="设备性能指标" align="center" >
          <template slot-scope="scope">
            {{ scope.row.设备性能指标 }}
          </template>
        </el-table-column>
        <el-table-column label="设备加工能力" align="center" >
          <template slot-scope="scope">
            {{ scope.row.设备加工能力工时 }}
          </template>
        </el-table-column>
        <el-table-column label="设备加工工艺" align="center" >
          <template slot-scope="scope">
            {{ scope.row.工艺属性 }}
          </template>
        </el-table-column>
        <el-table-column label="班次类型" align="center" >
          <template slot-scope="scope">
            {{ scope.row.班次类型 }}
          </template>
        </el-table-column>
        <el-table-column label="设备工时系数" align="center" >
          <template slot-scope="scope">
            {{ scope.row.设备工时系数 }}
          </template>
        </el-table-column>
        <el-table-column label="整改工时系数" align="center" >
          <template slot-scope="scope">
            {{ scope.row.整改工时比例 }}
          </template>
        </el-table-column>
        <el-table-column label="预留工时系数" align="center">
          <template slot-scope="scope">
            {{ scope.row.设备预留工时系数 }}
          </template>
        </el-table-column>
        <el-table-column label="操作" align="center" width="200px" fixed="right" >
          <template slot-scope="scope">
            <el-button type="primary" size="mini" icon="el-icon-edit" style="margin-left:10px" @click="handleEdit(scope.row)">编辑</el-button>
            <el-button type="danger" size="mini" icon="el-icon-delete" @click="handleDelete(scope.row)">删除</el-button>
          </template>
        </el-table-column>
      </el-table>
      <div class="block" style="margin-top: 10px;">
        <el-pagination
          :current-page="pageCurrent"
          :page-sizes="[10, 20, 30, 40]"
          :page-size="pageSize"
          :total="total"
          layout="total, sizes, prev, pager, next, jumper"
          @size-change="handleSizeChange"
          @current-change="handleCurrentChange"/>
      </div>
    </el-card>
    <el-card>
      <span>有效时间：</span>
      <el-date-picker
        v-model="time"
        type="date"
        size="small"
        value-format="yyyy-MM-dd"
        placeholder="选择日期"/>
      <el-button type="primary" size="small" icon="el-icon-setting" style="margin-left:10px" @click="setUp">设定</el-button>
      <el-button type="success" size="small" icon="el-icon-edit-outline" style="margin-left:10px" @click="generate">生成设备加工能力</el-button>
    </el-card>

    <el-dialog :title="title" :visible.sync="dialogFormVisible" center style="min-height: 300px" width="800px">
      <el-form ref="form" :model="form" :rules="rules" label-position="left" label-width="120px" 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-select v-model="form.设备加工工艺" placeholder="设备加工工艺" filterable size="small" style="width:200px" clearable >
                <el-option
                  v-for="item in processingName"
                  :key="item.value"
                  :label="item.label"
                  :value="item.value"/>
              </el-select>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row v-show="title!=='增加'">
          <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 v-show="title!=='增加'">
          <el-col :span = "12">
            <el-form-item label="班次类型" prop="班次类型" >
              <el-select v-model="form.班次类型" placeholder="班次类型" size="small" style="width:200px" clearable >
                <el-option
                  v-for="item in typeOfShift"
                  :key="item.value"
                  :label="item.label"
                  :value="item.value"/>
              </el-select>
            </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 v-show="title!=='增加'">
          <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 type="primary" @click="callOff('form')">取消</el-button>
        <el-button type="primary" @click="submit('form')">确定</el-button>
      </div>
    </el-dialog>
  </div>

</template>

<script>
import { getData, initProcessing, addTable, editTable, deleteTable, setUp, generate } from '@/api/ManufacturingCenter/WorkshopEquipmentManagement'
export default {
  data() {
    return {
      equipmentName: '', // 设备名称
      List: [],
      check: 0,
      time: '',
      pageCurrent: 1,
      pageSize: 10,
      total: null,
      title: '',
      dialogFormVisible: false,
      processingName: [],
      processing1: '', // 工艺名称简称
      equipmentSerialNumber: '', // 设备流水号
      typeOfShift: [
        { value: '0', label: 0 },
        { value: '1', label: 1 },
        { value: '2', label: 2 },
        { value: '3', label: 3 },
        { value: '4', label: 4 }],
      form: {
        设备名称: '',
        设备编号: '',
        设备型号: '',
        设备性能指标: '',
        设备加工能力: '',
        设备加工工艺: '',
        班次类型: '',
        设备工时系数: '',
        整改工时系数: '',
        预留工时系数: ''
      },
      rules: {
        设备名称: [{ required: true, message: '请选择设备名称', change: 'blur' }],
        设备编号: [{ required: true, message: '请选择设备编号', change: 'blur' }],
        设备型号: [{ required: true, message: '请选择设备型号', change: 'blur' }],
        设备加工工艺: [{ required: true, message: '请选择设备加工工艺', trigger: 'blur' }]
        // 设备加工能力: [{ required: true, message: '请选择设备加工能力', change: 'blur' }],
        // 班次类型: [{ required: true, message: '请选择班次类型', trigger: 'blur' }],
        // 设备工时系数: [{ required: true, message: '请选择设备工时系数', change: 'blur' }],
        // 整改工时系数: [{ required: true, message: '请选择整改工时系数', change: 'blur' }],
        // 预留工时系数: [{ required: true, message: '请选择预留工时系数', change: 'blur' }]
      }
    }
  },
  created() {
    this.initialization()
    this.fetchData()
  },
  methods: {
    fetchData() {
      this.List = []
      if (this.equipmentName !== '') {
        this.check = 1
      } else {
        this.check = 0
      }
      getData(this.check, this.equipmentName, this.pageCurrent, this.pageSize).then(response => {
        this.List = response.data.rows
        this.total = response.data.total
      })
    },
    initialization() {
      initProcessing().then(response => {
        for (let i = 0; i < response.data.length; i++) {
          this.processingName.push({
            label: response.data[i].工艺名称,
            value: response.data[i].工艺编号,
            processing: response.data[i].工艺名称简称
          })
        }
      })
    },
    handleAdd() {
      if (this.$refs['form'] !== undefined) {
        this.$refs['form'].resetFields()
      }
      this.title = '增加'
      this.form = {
        设备名称: '',
        设备编号: '',
        设备型号: '',
        设备加工工艺: ''
      }
      this.form.设备性能指标 = 1
      this.form.设备加工能力 = 1
      this.form.班次类型 = 1
      this.form.设备工时系数 = 1
      this.form.整改工时系数 = 0
      this.form.预留工时系数 = 0
      this.dialogFormVisible = true
    },
    handleEdit(row) {
      if (this.$refs['form'] !== undefined) {
        this.$refs['form'].resetFields()
      }
      this.title = '编辑'
      this.dialogFormVisible = true
      this.form.设备名称 = row.设备名称
      this.form.设备编号 = row.设备编号
      this.form.设备型号 = row.设备型号
      this.form.设备性能指标 = row.设备性能指标
      this.form.设备加工能力 = row.设备加工能力工时
      this.form.设备加工工艺 = parseInt(row.工艺属性代码)
      this.form.班次类型 = row.班次类型
      this.form.设备工时系数 = row.设备工时系数
      this.form.整改工时系数 = row.整改工时比例
      this.form.预留工时系数 = row.设备预留工时系数
      this.equipmentSerialNumber = row.设备流水号
    },
    submit(formName) {
      this.$refs[formName].validate((valid) => {
        if (valid) {
          if (this.title === '增加') {
            this.submitAdd()
          } else if (this.title === '编辑') {
            this.submitEdit()
          }
        }
      })
    },
    submitAdd() {
      for (let i = 0; i < this.processingName.length; i++) {
        if (this.form.设备加工工艺 === this.processingName[i].value) {
          this.processing1 = this.processingName[i].processing
        }
      }
      addTable(this.form.设备名称, this.form.设备型号, this.form.设备性能指标, this.form.设备加工能力, this.form.设备工时系数, this.processing1, this.form.设备加工工艺, this.form.班次类型, this.form.整改工时系数, this.form.设备编号, this.form.预留工时系数).then(response => {
        if (response.data[0].result === '1') {
          this.$notify({
            title: '成功',
            message: '信息添加成功',
            type: 'success'
          })
          this.dialogFormVisible = false
          this.fetchData()
        } else {
          this.$message.error('信息添加失败')
        }
      })
    },
    submitEdit() {
      editTable(this.equipmentSerialNumber, this.form.设备名称, this.form.设备型号, this.form.设备性能指标, this.form.设备加工能力, this.form.设备工时系数, this.form.班次类型, this.form.设备加工工艺, this.form.整改工时系数, this.form.设备编号, this.form.预留工时系数).then(response => {
        if (response.data[0].result === '1') {
          this.$notify({
            title: '成功',
            message: '信息编辑成功',
            type: 'success'
          })
          this.dialogFormVisible = false
          this.title = ''
          this.fetchData()
        } else {
          this.$message.error('信息编辑失败')
        }
      })
    },
    callOff(formName) {
      this.dialogFormVisible = false
      this.$refs[formName].resetFields()
    },
    handleDelete(row) {
      this.$confirm('此操作将永久删除该行, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        if (this.List && this.List.length === 1 && this.pageCurrent > 1) {
          this.pageCurrent--
        }
        deleteTable(row.设备流水号).then(response => {
          if (response.data[0].result === '1') {
            this.$message.success('删除成功')
            this.fetchData()
          } else { this.$message.error('删除失败') }
        })
      }).catch(() => {
        this.$message({
          type: 'info',
          message: '已取消删除'
        })
      })
    },
    setUp() {
      if (this.time !== '') {
        setUp(this.time).then(response => {
          if (response.data[0].result === '1') {
            this.$message.success('设定成功')
          } else { this.$message.error('设定失败') }
        })
      } else {
        this.$message.warning('请选择有效时间')
      }
    },
    generate() {
      generate().then(response => {
        if (response.data[0].result === '1') {
          this.$message.success('生成成功')
        } else { this.$message.error('生成失败') }
      })
    },
    handleSizeChange(val) {
      this.pageSize = val
      this.pageCurrent = 1
      this.fetchData()
    },
    handleCurrentChange(val) {
      this.pageCurrent = val
      this.fetchData()
    }
  }
}
</script>
<style scoped>
  .el-card{
    margin-bottom: 15px;
  }
</style>
