<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="150px" 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>
          </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>
      <el-form ref="form" :model="form" :rules="rules" label-position="left" label-width="110px" class="demo-ruleForm" style="width: 48%;margin: auto">
        <el-form-item label="工艺" prop="TechnologyValue">
          <el-select v-model="form.TechnologyValue" filterable size="small">
            <el-option
              v-for="item in Technology"
              :key="item.value"
              :label="item.label"
              :value="item.value"/>
          </el-select>
        </el-form-item>

        <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-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-form-item label="修补工时" prop="timeNumber">
          <el-input v-model="form.timeNumber" placeholder="修补工时" clearable size="small" style="width: 200px"/>
        </el-form-item>

        <el-row>
          <el-col :span="14"/>
          <el-form-item >
            <el-button size="small" @click="callOff('form')">取消</el-button>
            <el-button size="small" type="primary" @click="submit('form')">确 定</el-button>
          </el-form-item>
        </el-row>

      </el-form>
    </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 { getTable8, getTree, fn, dialogtablevisible, searchTechnology, searchperson, searchtable, edittype, handlechange } from '@/api/ManufacturingCenter/RepairTimeEntry'
export default {
  data() {
    return {
      codes2: '',
      Number2: '',
      codes: '',
      Number1: '',
      Name: '',
      ssks: '',
      List1: [],
      data2: [],
      dialogFormVisible1: false,
      a: '', // 初始化赋值中间变量
      liushuihao: '', // 编辑流水号
      renyuanbianhao: '', // 编辑人员编号
      personnelNumber: '', // 人员编号
      form: {
        TechnologyValue: '',
        yearNumberValue: '',
        monthNumberValue: '',
        timeNumber: ''
      },
      Technology: [], // 工艺
      yearNumber: [], // 年
      monthNumber: [], // 月
      loading0: false,
      count1: 0, // 清表单验证计数器
      count2: 0, // 清表单验证计数器
      tableData: [], // 表格数据
      pageSize: 10,
      pageCurrent: 1,
      total: 0,
      dialogFormVisible: false,
      title1: '',
      rules: {
        TechnologyValue: [{ required: true, message: '请选择工艺', trigger: 'change' }],
        timeNumber: [{ required: true, message: '请输入修补工时', trigger: 'blur' }]
      }
    }
  },
  created() {
    this.fetchData()
    this.fetchData3()
    this.searchTable()
  },
  methods: {
    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
        })
      }
    },
    // 增加打开
    editType1(formName) {
      if (this.personnelNumber === '' || this.personnelNumber === null) {
        this.$message.error('请输入人员编号')
      } else {
        this.count1 = this.count1 + 1
        if (this.count1 !== 1) {
          this.$refs[formName].resetFields()
        }
        this.title1 = '增加'
        this.form.TechnologyValue = ''
        this.form.timeNumber = ''
        this.dialogFormVisible = true
      }
    },
    // 增加确定
    editType() {
      searchperson(this.codes2).then(response => {
        if (response.data.length !== 0) {
          edittype(response.data[0].人员编号, this.form.TechnologyValue, this.form.yearNumberValue, this.form.monthNumberValue, this.form.timeNumber).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.TechnologyValue = parseInt(row.工艺编号)
      this.form.yearNumberValue = row.年
      this.form.monthNumberValue = row.月
      this.form.timeNumber = row.修补工时
      this.dialogFormVisible = true
      this.title1 = '编辑'
    },
    // 编辑确定
    handleChange() {
      handlechange(this.liushuihao, this.renyuanbianhao, this.form.TechnologyValue, this.form.yearNumberValue, this.form.monthNumberValue, this.form.timeNumber).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>
