<template>
  <div class="app-container">

    <el-card>

      <span>人员名称:</span>
      <el-input v-model="personnelNumber" placeholder="请选择人员名称" class="input-with-select" readonly clearable size="small" style="width:200px;margin:10px;">
        <el-button slot="append" icon="el-icon-search" size="small" @click="dialogTableVisible"/>
      </el-input>
      <el-button type="success" class="el-icon-search" size="small" @click="pageCurrent=1;pageSize=10;searchTable()">查询</el-button>
      <el-button type="primary" class="el-icon-circle-plus-outline" size="small" @click="editType1('form');param = 0">新增</el-button>

    </el-card>

    <el-card>
      <el-table v-loading="loading0" :data="tableData" height="650" style="width: 100%;" border>
        <el-table-column
          label="序号"
          type="index"
          align="center"
          width="50"/>
        <el-table-column label="零件图号" align="center" width="280px">
          <template slot-scope="scope">
            {{ scope.row.零件图号_零件工艺计划 }}
          </template>
        </el-table-column>
        <el-table-column label="修补工时" align="center" width="110px">
          <template slot-scope="scope">
            {{ scope.row.修补工时 }}
          </template>
        </el-table-column>
        <el-table-column label="年" align="center" width="100px">
          <template slot-scope="scope">
            {{ scope.row.年 }}
          </template>
        </el-table-column>
        <el-table-column label="月" align="center" width="100px">
          <template slot-scope="scope">
            {{ scope.row.月 }}
          </template>
        </el-table-column>
        <el-table-column label="人员" align="center" width="140px">
          <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="250px" fixed="right">
          <template slot-scope="scope">
            <el-button
              size="mini"
              type="primary"
              class="el-icon-edit"
              @click="handleChange1(scope.$index, scope.row, 'form');param = 1">编辑</el-button>
            <el-button
              size="mini"
              type="danger"
              class="el-icon-delete"
              @click="handleDelete(scope.$index, scope.row)">删除</el-button>
          </template>
        </el-table-column>
      </el-table>
      <div class="block">
        <el-pagination
          v-if="!loading0"
          :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-dialog :title="title1" :visible.sync="dialogFormVisible" center style="min-height: 300px" width="700px">
      <el-form ref="form" :model="form" :rules="rules" label-position="left" label-width="90px" class="demo-ruleForm">
        <el-row>
          <el-col :span="12">
            <el-form-item label="项目" prop="project">
              <el-select v-model="form.project" filterable size="small" @change="getMachine">
                <el-option
                  v-for="item in projects"
                  :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="Machine">
              <el-select v-model="form.Machine" filterable size="small" @change="getGroup">
                <el-option
                  v-for="item in Machines"
                  :key="item.value"
                  :label="item.label"
                  :value="item.value"/>
              </el-select>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="12">
            <el-form-item label="分组" prop="Group">
              <el-select v-model="form.Group" filterable size="small" @change="getParts">
                <el-option
                  v-for="item in Groups"
                  :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-select v-model="form.零件" filterable size="small">
                <el-option
                  v-for="item in Parts"
                  :key="item.value"
                  :label="item.label"
                  :value="item.value"/>
              </el-select>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="12">
            <el-form-item label="年" prop="yearNumberValue">
              <el-select v-model="form.yearNumberValue" size="small">
                <el-option
                  v-for="item in yearNumber"
                  :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="monthNumberValue">
              <el-select v-model="form.monthNumberValue" size="small">
                <el-option
                  v-for="item in monthNumber"
                  :key="item.value"
                  :label="item.label"
                  :value="item.value"/>
              </el-select>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="12">
            <el-form-item label="修补工时" prop="timeNumber">
              <el-input v-model="form.timeNumber" placeholder="修补工时" clearable size="small" style="width: 200px"/>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="修补工艺" prop="修补工艺">
              <el-input v-model="form.修补工艺" placeholder="修补工艺" clearable size="small" style="width: 200px"/>
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="callOff('form')">取消</el-button>
        <el-button type="primary" @click="submit('form')" >确定</el-button>
      </div>
    </el-dialog>

    <el-dialog :visible.sync="dialogFormVisible1" title="人员信息" center width="750px">
      <div style="height: 500px">
        <div style="float: left;margin-top: 10px;margin-left: 20px;overflow-y: scroll;height: 400px">
          <el-tree
            :data="data2"
            node-key="id"
            style="width: 150px;float: left"
            height="400"
            accordion
            @node-click="handleNodeClick"
          />
        </div>
        <el-table :data="List1" highlight-current-row height="400" style="width: 400px;float: left;margin-left: 30px" @row-click="handleCurrentChange2">
          <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>
      </div>
      <el-button style="margin-left: 260px" @click="dialogFormVisible1 = false">取消</el-button>
      <el-button type="primary" style="margin-top: 20px" @click="getName">确认</el-button>
    </el-dialog>

  </div>
</template>

<script>
import { delList, getTable8, getNames, getMachines, getGroups, getParts, getTree, fn, dialogtablevisible, searchTechnology, searchperson, searchtable, edittype, handlechange } from '@/api/ManufacturingCenter/RepairTimeEntry'
import elInput from 'element-ui/packages/input'
export default {
  components: {
    elInput
  },
  data() {
    return {
      codes2: '',
      Number2: '',
      codes: '',
      Number1: '',
      Name: '',
      ssks: '',
      List1: [],
      data2: [],
      dialogFormVisible1: false,
      a: '', // 初始化赋值中间变量
      liushuihao: '', // 编辑流水号
      renyuanbianhao: '', // 编辑人员编号
      personnelNumber: '', // 人员编号
      projects: [],
      Machines: [],
      Groups: [],
      Parts: [],
      form: {
        零件: '',
        project: '',
        Machine: '',
        Group: '',
        TechnologyValue: '',
        yearNumberValue: '',
        monthNumberValue: '',
        timeNumber: '',
        修补工艺: ''
      },
      Technology: [], // 工艺
      yearNumber: [], // 年
      monthNumber: [], // 月
      loading0: false,
      count1: 0, // 清表单验证计数器
      count2: 0, // 清表单验证计数器
      tableData: [], // 表格数据
      pageSize: 10,
      pageCurrent: 1,
      total: 0,
      dialogFormVisible: false,
      title1: '',
      rules: {
        零件: [{ required: true, message: '请选择零件', trigger: 'change' }],
        timeNumber: [{ required: true, message: '请输入修补工时', trigger: 'blur' }],
        yearNumberValue: [{ required: true, message: '请选择年份', trigger: 'blur' }],
        monthNumberValue: [{ required: true, message: '请选择月份', trigger: 'blur' }],
        修补工艺: [{ required: true, message: '请输入修补工艺', trigger: 'blur' }]
      }
    }
  },
  created() {
    this.fetchData()
    this.fetchData3()
    this.searchTable()
    this.fetchData2()
    this.getParts()
  },
  methods: {
    fetchData2() {
      this.projects = []
      getNames().then(response => {
        for (let i = 0; i < response.data.length; i++) {
          this.projects.push({
            label: response.data[i].项目名称,
            value: response.data[i].项目流水号
          })
        }
      })
    },
    getMachine() {
      this.form.Machine = ''
      this.Machines = []
      this.form.Group = ''
      this.Groups = []
      if (this.form.project !== '') {
        getMachines(this.form.project).then(response => {
          for (let i = 0; i < response.data.length; i++) {
            this.Machines.push({
              label: response.data[i].机床图号,
              value: response.data[i].机床流水号
            })
          }
        })
      }
    },
    getGroup() {
      this.form.Group = ''
      this.Groups = []
      getGroups(this.form.Machine).then(response => {
        for (let i = 0; i < response.data.length; i++) {
          this.Groups.push({
            label: response.data[i].组号,
            value: response.data[i].组件流水号
          })
        }
      })
    },
    getParts() {
      if (this.project === '') {
        this.checkbox_project = false
      } else {
        this.checkbox_project = true
      }
      if (this.Machine === '') {
        this.checkbox_Machine = false
      } else {
        this.checkbox_Machine = true
      }
      if (this.Group === '') {
        this.checkbox_Group = false
      } else {
        this.checkbox_Group = true
      }
      if (this.number === '') {
        this.checkbox_number = false
      } else {
        this.checkbox_number = true
      }
      this.form.零件 = ''
      this.Parts = []
      getParts(this.checkbox_project, this.form.project, this.checkbox_Machine, this.form.Machine, this.checkbox_Group, this.form.Group).then(response => {
        for (let i = 0; i < response.data.length; i++) {
          this.Parts.push({
            label: response.data[i].零件图号,
            value: response.data[i].工艺计划流水号
          })
        }
      })
    },
    handleCurrentChange2(row) { // 获取当前行人员信息
      this.ssks = row.节点名称
      this.Number1 = row.人员编号
      this.codes = row.序号
      this.Name = row.姓名
    },
    getName() { // 提交人员
      this.dialogFormVisible1 = false
      this.personnelNumber = this.Name
      this.codes2 = this.codes
      this.Number2 = this.Number1
    },
    handleNodeClick(data) { // 树节点点击
      this.ssks = data.label
      getTable8(data.id).then(response => {
        this.List1 = response.data
      })
    },
    fetchData3() {
      getTree().then(response => { // 获取人员信息树
        const data = response.data
        this.data2 = fn(data, 0)
      })
    },
    dialogTableVisible() {
      this.dialogFormVisible1 = true
      dialogtablevisible()
    },
    // 初始化工艺
    fetchData() {
      var time = new Date()
      this.form.TechnologyValue = ''
      this.Technology = []
      searchTechnology().then(response => {
        for (let i = 0; i < response.data.length; i++) {
          this.Technology.push({
            label: response.data[i].工艺名称,
            value: response.data[i].工艺编号
          })
        }
      })
      for (let i = time.getFullYear() - 2; i <= time.getFullYear() + 2; i++) {
        if (i === time.getFullYear()) {
          this.a = i
        }
        this.yearNumber.push({
          label: i,
          value: i
        })
      }
      for (let i = 0; i < 12; i++) {
        this.monthNumber.push({
          label: i + 1,
          value: i + 1
        })
      }
      this.form.yearNumberValue = this.a
      this.form.monthNumberValue = 1
    },
    // 查询
    searchTable() {
      this.tableData = []
      this.loading0 = true
      if (this.personnelNumber !== '' && this.personnelNumber !== null) {
        searchtable(1, this.Number2, this.pageCurrent, this.pageSize).then(response => {
          this.tableData = response.data.rows
          this.total = response.data.total
          this.loading0 = false
        })
      } else {
        searchtable(0, this.Number2, this.pageCurrent, this.pageSize).then(response => {
          this.tableData = response.data.rows
          this.total = response.data.total
          this.loading0 = false
        })
      }
    },
    // 删除
    handleDelete(index, row) {
      this.$confirm('此操作将永久删除该数据, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        delList(row.员工月修补工时流水号).then(response => {
          if (response.data[0].result === '1') {
            this.searchTable()
            this.$message({
              message: '已被成功删除',
              type: 'success'
            })
          }
        })
      }).catch(() => {
        this.$message({
          type: 'info',
          message: '已取消删除'
        })
      })
    },
    // 增加打开
    editType1(formName) {
      if (this.personnelNumber === '' || this.personnelNumber === null) {
        this.$message.error('请输入人员编号')
      } else {
        this.form.TechnologyValue = ''
        this.form.timeNumber = ''
        this.form.零件 = ''
        this.form.修补工艺 = ''
        this.count1 = this.count1 + 1
        if (this.count1 !== 1) {
          this.$refs[formName].resetFields()
        }
        this.title1 = '增加'
        this.dialogFormVisible = true
      }
    },
    // 增加确定
    editType() {
      searchperson(this.codes2).then(response => {
        if (response.data.length !== 0) {
          edittype(this.form.零件, response.data[0].人员编号, this.form.yearNumberValue, this.form.monthNumberValue, this.form.timeNumber, this.form.修补工艺).then(response => {
            if (response.data[0].result === '1') {
              this.$message.success('增加成功')
              this.pageCurrent = 1
              this.pageSize = 10
              this.searchTable()
              this.dialogFormVisible = false
            } else {
              this.$message.error('增加失败')
              this.dialogFormVisible = false
            }
          })
        } else {
          this.$message.error('人员编号无效')
          this.dialogFormVisible = false
        }
      })
    },
    // 编辑打开
    handleChange1(index, row, formName) {
      this.count2 = this.count2 + 1
      if (this.count2 !== 1) {
        this.$refs[formName].resetFields()
      }
      this.renyuanbianhao = row.人员编号
      this.liushuihao = row.员工月修补工时流水号
      this.form.零件 = parseInt(row.工艺计划流水号)
      this.form.yearNumberValue = row.年
      this.form.monthNumberValue = row.月
      this.form.timeNumber = row.修补工时
      this.form.修补工艺 = row.修补工艺
      this.dialogFormVisible = true
      this.title1 = '编辑'
    },
    // 编辑确定
    handleChange() {
      handlechange(this.liushuihao, this.form.零件, this.renyuanbianhao, this.form.yearNumberValue, this.form.monthNumberValue, this.form.timeNumber, this.form.修补工艺).then(response => {
        if (response.data[0].result === '1') {
          this.$message.success('修改成功')
          this.searchTable()
        } else {
          this.$message.error('修改失败')
        }
      })
      this.dialogFormVisible = false
    },
    // 提交添加或者修改
    submit(formName) {
      this.$refs[formName].validate((valid) => {
        if (valid) {
          if (this.param === 0) {
            this.editType()
          } else {
            this.handleChange()
          }
        } else {
          return false
        }
      })
    },
    // 重置表单
    callOff(formName) {
      this.dialogFormVisible = false
      this.$refs[formName].resetFields()
    },
    // 分页
    handleSizeChange(val) {
      this.pageCurrent = 1
      this.pageSize = val
      this.searchTable()
    },
    handleCurrentChange(val) {
      this.pageCurrent = val
      this.searchTable()
    }
  }
}
</script>

<style scoped>
  .el-card{
    margin-bottom: 15px;
  }
</style>
