<template>
  <div class="app-container">
    <el-row>
      <el-col :span="6">
        <el-card>
          <span>工艺分类：</span>
          <el-button type="primary" icon="el-icon-circle-plus-outline" size="small" @click="addProcessClassification()">新增</el-button>
          <el-button size="small" type="warning" icon="el-icon-edit" @click="editProcessClassification()">编辑</el-button>
          <el-button size="small" type="danger" icon="el-icon-delete" @click="delProcessClassification()">删除</el-button>
        </el-card>
      </el-col>
      <el-col :span="18">
        <el-card>
          <el-button type="primary" icon="el-icon-circle-plus-outline" size="small" @click="addProcess">新增工艺名称</el-button>
        </el-card>
      </el-col>
    </el-row>
    <el-row>
      <el-col :span="6">
        <el-card>
          <el-table
            v-loading="loading1"
            ref="multipleTable"
            :data="tableData1"
            stripe
            tooltip-effect="dark"
            style="width: 100%"
            height="700">
            <el-table-column align="center" label=" " width="50px">
              <template slot-scope="scope">
                <el-radio :label="scope.row.工艺分类流水号" v-model="radio" @change.native="getCurrentRow(scope.row.工艺分类流水号, scope.row.工艺分类名称)">
                  {{ a }}
                </el-radio>
              </template>
            </el-table-column>
            <el-table-column align="left" label="工艺分类">
              <template slot-scope="scope">
                {{ scope.row.工艺分类名称 }}
              </template>
            </el-table-column>
          </el-table>
        </el-card>
      </el-col>

      <el-col :span="18">
        <el-card >
          <el-table
            :data="tableData2"
            :row-key="getRowKeys"
            :expand-row-keys="expands"
            height="700"
            width="100%"
            @expand-change="searchTechnologicalRequirementsOfProcessNum">
            <el-table-column type="expand">
              <template slot-scope="scope">
                <el-table :data="tableData3" border>
                  <el-table-column label="工艺要求">
                    <template slot-scope="scope">
                      {{ scope.row.工艺要求 }}
                    </template>
                  </el-table-column>
                  <el-table-column label="操作" fixed="right" width="200px">
                    <template slot-scope="scope">
                      <el-button
                        size="mini"
                        type="warning"
                        icon="el-icon-edit"
                        @click="editTechnologicalRequirements(scope.row)">编辑</el-button>
                      <el-button
                        size="mini"
                        type="danger"
                        icon="el-icon-delete"
                        @click="delTechnologicalRequirements(scope.row)">删除</el-button>
                    </template>
                  </el-table-column>
                </el-table>
              </template>
            </el-table-column>
            <el-table-column label="工艺名称">
              <template slot-scope="scope">
                {{ scope.row.工艺名称 }}
              </template>
            </el-table-column>
            <el-table-column>
              <template slot-scope="scope">
                <el-button
                  size="mini"
                  type="warning"
                  icon="el-icon-edit"
                  @click="editProcess(scope.row)">编辑工艺</el-button>
                <el-button
                  size="mini"
                  type="danger"
                  icon="el-icon-delete"
                  @click="delProcess(scope.row)">删除工艺</el-button>
                <el-button
                  size="mini"
                  type="primary"
                  icon="el-icon-circle-plus-outline"
                  @click="addTechnologicalRequirements(scope.row)">新增要求</el-button>
              </template>
            </el-table-column>
          </el-table>
        </el-card>
      </el-col>
    </el-row>
    <el-dialog :visible.sync="dialogFormVisible1" title="增加工艺分类" center style="min-height: 300px" width="400px">
      <el-form ref="form1" :model="form1" :rules="rules1" label-position="left" label-width="110px" >
        <el-form-item label="工艺分类" prop="ProcessClassification">
          <el-input v-model="form1.ProcessClassification" size="small" clearable style="width:200px"/>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="calloff">取 消</el-button>
        <el-button type="primary" @click="submit1('form1')">确 定</el-button>
      </div>
    </el-dialog>

    <el-dialog :visible.sync="dialogFormVisible2" title="编辑工艺分类" center style="min-height: 300px" width="400px">
      <el-form ref="form1" :model="form1" :rules="rules1" label-position="left" label-width="110px" >
        <el-form-item label="工艺分类" prop="ProcessClassification">
          <el-input v-model="form1.ProcessClassification" size="small" clearable style="width:200px"/>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="calloff">取 消</el-button>
        <el-button type="primary" @click="submit1('form1')">确 定</el-button>
      </div>
    </el-dialog>

    <el-dialog :visible.sync="dialogFormVisible3" title="增加工艺" center style="min-height: 300px" width="400px">
      <el-form ref="form2" :model="form2" :rules="rules2" label-position="left" label-width="110px" >
        <el-form-item label="工艺名称" prop="Process">
          <el-input v-model="form2.Process" size="small" clearable style="width:200px"/>
        </el-form-item>
        <el-form-item label="工艺名称简称" prop="ProcessShort">
          <el-input v-model="form2.ProcessShort" size="small" clearable style="width:200px"/>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="calloff">取 消</el-button>
        <el-button type="primary" @click="submit1('form2')">确 定</el-button>
      </div>
    </el-dialog>

    <el-dialog :visible.sync="dialogFormVisible4" title="编辑工艺" center style="min-height: 300px" width="400px">
      <el-form ref="form2" :model="form2" :rules="rules2" label-position="left" label-width="110px" >
        <el-form-item label="工艺名称" prop="Process">
          <el-input v-model="form2.Process" size="small" clearable style="width:200px"/>
        </el-form-item>
        <el-form-item label="工艺名称简称" prop="ProcessShort">
          <el-input v-model="form2.ProcessShort" size="small" clearable style="width:200px"/>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="calloff">取 消</el-button>
        <el-button type="primary" @click="submit1('form2')">确 定</el-button>
      </div>
    </el-dialog>

    <el-dialog :visible.sync="dialogFormVisible5" title="新增工艺要求" center style="min-height: 300px" width="400px">
      <el-form ref="form3" :model="form3" :rules="rules3" label-position="left" label-width="110px" >
        <el-form-item label="工艺要求" prop="Process">
          <el-input v-model="form3.TechnologicalRequirements" size="small" clearable style="width:200px"/>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="calloff">取 消</el-button>
        <el-button type="primary" @click="submit1('form3')">确 定</el-button>
      </div>
    </el-dialog>

    <el-dialog :visible.sync="dialogFormVisible6" title="编辑工艺要求" center style="min-height: 300px" width="400px">
      <el-form ref="form3" :model="form3" :rules="rules3" label-position="left" label-width="110px" >
        <el-form-item label="工艺要求" prop="TechnologicalRequirements">
          <el-input v-model="form3.TechnologicalRequirements" size="small" clearable style="width:200px"/>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="calloff">取 消</el-button>
        <el-button type="primary" @click="submit1('form3')">确 定</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { searchProcessClassification, searchProcessNameOfProcessClassification, searchTechnologicalRequirementsOfProcessNum, searchProcessClassificationOfProcessClassificationNum, addProcessClassification, editProcessClassification, delProcessClassification, addProcess, delProcess, editProcess, addTechnologicalRequirements, editTechnologicalRequirements, delTechnologicalRequirements } from '@/api/BasicData/WorkshopProcessMaintenance'
export default {
  data() {
    return {
      // 工艺分类所需变量
      a: '',
      expands: [],
      tableData1: [],
      ProcessNum: '',
      loading1: false,
      loading2: false,
      loading3: false,
      ProcessClassificationNum: '',
      radio: null,
      dialogFormVisible1: false,
      dialogFormVisible2: false,
      dialogFormVisible3: false,
      dialogFormVisible4: false,
      dialogFormVisible5: false,
      dialogFormVisible6: false,
      temp: null,
      ProcessClassificationNumS: '',
      form1: {
        ProcessClassification: ''
      },
      form2: {
        Process: '',
        ProcessShort: ''
      },
      form3: {
        TechnologicalRequirements: ''
      },
      rules1: {
        ProcessClassification: [{ required: true, message: '请输入工艺分类名称', trigger: 'blur' }]
      },
      rules2: {
        Process: [{ required: true, message: '请输入工艺名称', trigger: 'blur' }],
        ProcessShort: [{ required: true, message: '请输入工艺名称简称', trigger: 'blur' }]
      },
      rules3: {
        TechnologicalRequirements: [{ required: true, message: '请输入工艺要求', trigger: 'blur' }]
      },
      tableData2: [],
      tableData3: [],
      Num: [],
      maxNum: '',
      max: [],
      code: ''
    }
  },
  created() {
    this.searchProcessClassification()
  },
  methods: {
    searchProcessClassification() {
      this.loading1 = true
      searchProcessClassification().then(response => {
        this.loading1 = false
        this.tableData1 = response.data
        this.total1 = response.data
        return this.tableData1
      }).then(data => {
        this.Num = data
        this.max = []
        for (let i = 0; i < this.Num.length; i++) {
          this.max.push(this.Num[i].工艺分类流水号)
        }
        return this.max
      }).then(data => {
        const a = data
        this.maxNum = a[0]
        for (let j = 0; j < a.length; j++) {
          if (this.maxNum < a[j]) {
            this.maxNum = a[j]
          }
        }
        return this.maxNum
      }).then(data => {
        const b = data
        this.radio = b
        return this.radio
      }).then(data => {
        const c = data
        this.loading2 = true
        searchProcessNameOfProcessClassification(c).then(response => {
          this.loading2 = false
          this.tableData2 = response.data
        })
      })
    },
    addProcessClassification() {
      if (this.$refs['form1'] !== undefined) {
        this.$refs['form1'].resetFields()
      }
      this.form1 = {}
      this.dialogFormVisible1 = true
      this.temp = 1
    },
    editProcessClassification() {
      if (this.radio !== '') {
        this.temp = 2
        this.dialogFormVisible2 = true
        searchProcessClassificationOfProcessClassificationNum(this.radio).then(response => {
          this.form1.ProcessClassification = response.data[0].工艺分类名称
        })
      } else {
        this.$message.warning('请选择工艺分类')
      }
    },
    submitProcessClassification() {
      editProcessClassification(this.radio, this.form1.ProcessClassification).then(response => {
        if (response.data[0].result === '1') {
          this.$message.success('信息更新成功')
          this.dialogFormVisible2 = false
          this.searchProcessClassification()
        } else {
          this.$message.error('信息更新失败')
        }
      })
    },
    submitaddProcessClassification() {
      addProcessClassification(this.form1.ProcessClassification).then(response => {
        if (response.data[0].result === '1') {
          this.$message.success('增加成功')
          this.searchProcessClassification()
          this.dialogFormVisible1 = false
        } else { this.$message.error('增加失败') }
      })
    },
    submit1(formName) {
      this.$refs[formName].validate((valid) => {
        if (valid) {
          if (this.temp === 1) {
            this.submitaddProcessClassification()
            this.form1 = {
              ProcessClassification: ''
            }
            this.temp = null
          } else if (this.temp === 2) {
            this.submitProcessClassification()
            this.form1 = {
              ProcessClassification: ''
            }
            this.temp = null
          } else if (this.temp === 3) {
            this.submitaddProcess()
            this.form2 = {
              Process: '',
              ProcessShort: ''
            }
            this.temp = null
          } else if (this.temp === 4) {
            this.submiteditProcess()
            this.form2 = {
              Process: '',
              ProcessShort: ''
            }
            this.temp = null
          } else if (this.temp === 5) {
            this.submitTechnologicalRequirements()
            this.form3 = {
              TechnologicalRequirements: ''
            }
            this.temp = null
          } else if (this.temp === 6) {
            this.submiteditTechnologicalRequirements()
            this.form3 = {
              TechnologicalRequirements: ''
            }
            this.temp = null
          }
        }
      })
    },
    delProcessClassification() {
      if (this.radio !== '') {
        this.$confirm('此操作将永久删除该行, 是否继续?', '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }).then(() => {
          delProcessClassification(this.radio).then(response => {
            if (response.data[0].result === '1') {
              this.searchProcessClassification()
              this.$message.success('删除成功')
            } else {
              this.$message.error('删除失败')
            }
          })
        })
      } else {
        this.$message.warning('请选择工艺分类')
      }
    },
    calloff() {
      this.dialogFormVisible1 = false
      this.dialogFormVisible2 = false
      this.dialogFormVisible3 = false
      this.dialogFormVisible4 = false
      this.dialogFormVisible5 = false
      this.dialogFormVisible6 = false
      this.form1 = {
        ProcessClassification: ''
      }
      this.form2 = {
        Process: '',
        ProcessShort: ''
      }
      this.form3 = {
        TechnologicalRequirements: ''
      }
    },
    getCurrentRow(row) {
      searchProcessClassificationOfProcessClassificationNum(this.radio).then(response => {
        this.form1.ProcessClassification = response.data[0].工艺分类名称
      })
      this.loading2 = true
      searchProcessNameOfProcessClassification(row).then(response => {
        this.loading2 = false
        this.tableData2 = response.data
      })
    },
    searchTechnologicalRequirementsOfProcessNum(row, expandedRows) {
      // this.expands = []
      // this.expands.push(row.工艺编号)
      if (expandedRows.length > 1) {
        expandedRows.shift()
      }
      this.loading3 = true
      searchTechnologicalRequirementsOfProcessNum(row.工艺编号).then(response => {
        this.loading3 = false
        this.tableData3 = response.data
      })
    },
    addProcess() {
      if (this.$refs['form2'] !== undefined) {
        this.$refs['form2'].resetFields()
      }
      if (this.radio !== '') {
        this.form2.Process = ''
        this.form2.ProcessShort = ''
        this.dialogFormVisible3 = true
        this.temp = 3
      } else {
        this.$message.warning('请选择一个工艺分类')
      }
    },
    submitaddProcess() {
      addProcess(this.radio, this.form2.Process, this.form2.ProcessShort).then(response => {
        if (response.data[0].result === '1') {
          this.$message.success('增加成功')
          this.loading2 = true
          searchProcessNameOfProcessClassification(this.radio).then(response => {
            this.loading2 = false
            this.tableData2 = response.data
          })
          this.dialogFormVisible3 = false
        } else { this.$message.error('增加失败') }
      })
    },
    editProcess(row) {
      this.dialogFormVisible4 = true
      this.temp = 4
      this.form2.Process = row.工艺名称
      this.form2.ProcessShort = row.工艺名称简称
      this.ProcessNum = row.工艺编号
    },
    submiteditProcess() {
      editProcess(this.ProcessNum, this.form2.Process, this.form2.ProcessShort).then(response => {
        if (response.data[0].result === '1') {
          this.$message.success('信息更新成功')
          this.dialogFormVisible4 = true
          searchProcessNameOfProcessClassification(this.radio).then(response => {
            this.loading2 = false
            this.tableData2 = response.data
          })
          this.dialogFormVisible4 = false
        } else {
          this.$message.error('信息更新失败')
        }
      })
    },
    getRowKeys(row) {
      return row.工艺编号
    },
    delProcess(row) {
      this.$confirm('此操作将永久删除该行, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        delProcess(row.工艺编号).then(response => {
          if (response.data[0].result === '1') {
            this.loading2 = true
            searchProcessNameOfProcessClassification(this.radio).then(response => {
              this.loading2 = false
              this.tableData2 = response.data
            })
            this.$message.success('删除成功')
          } else {
            this.$message.error('删除失败')
          }
        })
      })
    },
    addTechnologicalRequirements(row) {
      if (this.$refs['form3'] !== undefined) {
        this.$refs['form3'].resetFields()
      }
      this.ProcessNum = row.工艺编号
      this.form3.TechnologicalRequirements = ''
      this.dialogFormVisible5 = true
      this.temp = 5
    },
    submitTechnologicalRequirements() {
      addTechnologicalRequirements(this.ProcessNum, this.form3.TechnologicalRequirements).then(response => {
        if (response.data[0].result === '1') {
          this.$message.success('增加成功')
          this.loading3 = true
          searchTechnologicalRequirementsOfProcessNum(this.ProcessNum).then(response => {
            this.loading3 = false
            this.tableData3 = response.data
          })
          this.dialogFormVisible5 = false
        } else { this.$message.error('增加失败') }
      })
    },
    editTechnologicalRequirements(row) {
      this.temp = 6
      this.dialogFormVisible6 = true
      this.form3.TechnologicalRequirements = row.工艺要求
      this.code = row.工艺要求流水号
      this.ProcessNum = row.工艺编号
    },
    submiteditTechnologicalRequirements() {
      editTechnologicalRequirements(this.code, this.form3.TechnologicalRequirements).then(response => {
        if (response.data[0].result === '1') {
          this.$message.success('信息更新成功')
          this.loading3 = true
          searchTechnologicalRequirementsOfProcessNum(this.ProcessNum).then(response => {
            this.loading3 = false
            this.tableData3 = response.data
          })
          this.dialogFormVisible6 = false
        } else {
          this.$message.error('信息更新失败')
        }
      })
    },
    delTechnologicalRequirements(row) {
      this.$confirm('此操作将永久删除该行, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        delTechnologicalRequirements(row.工艺要求流水号).then(response => {
          if (response.data[0].result === '1') {
            this.loading3 = true
            searchTechnologicalRequirementsOfProcessNum(row.工艺编号).then(response => {
              this.loading3 = false
              this.tableData3 = response.data
            })
            this.$message.success('删除成功')
          } else {
            this.$message.error('删除失败')
          }
        })
      })
    }
  }
}
</script>

<style scoped>
  .el-card {
    margin-top: 15px;
    margin-left: 15px;
  }

</style>
