<template>
  <div class="app-container">
    <table style="width: 100%">

      <el-card>
        <span>员工姓名:</span>
        <el-input v-model="staffName" placeholder="员工姓名" size="small" clearable style="width:200px"/>
        <span style="margin-left: 15px">角色名称:</span>
        <el-select v-model="roleName" placeholder="角色名称" clearable size="small" style="width:200px">
          <el-option
            v-for="item in roleNames"
            :key="item.value"
            :label="item.label"
            :value="item.value"/>
        </el-select>
        <el-button type="success" icon="el-icon-search" size="small" style="margin-left: 15px" @click="pageCurrent1=1;searchData()">查询</el-button>
        <el-button type="primary" icon="el-icon-circle-plus-outline" size="small" @click="handelAdd">增加</el-button>
      </el-card>

      <el-card>
        <el-table :data="tableData" highlight-current-row>
          <el-table-column label="员工姓名">
            <template slot-scope="scope">
              {{ scope.row.姓名 }}
            </template>
          </el-table-column>
          <el-table-column disabled label="所属部门" >
            <template slot-scope="scope">
              {{ scope.row.部门名称 }}
            </template>
          </el-table-column>
          <el-table-column label="岗位名称">
            <template slot-scope="scope">
              {{ scope.row.岗位名称 }}
            </template>
          </el-table-column>
          <el-table-column label="角色名称">
            <template slot-scope="scope">
              {{ scope.row.角色功能 }}
            </template>
          </el-table-column>
          <el-table-column label="考勤编号">
            <template slot-scope="scope">
              {{ scope.row.考勤编号 }}
            </template>
          </el-table-column>
          <el-table-column label="操作">
            <template slot-scope="scope">
              <el-button
                size="mini"
                type="primary"
                icon="el-icon-edit"
                @click="submitEdit(scope.$index, scope.row)">修改</el-button>
              <el-button
                size="mini"
                type="danger"
                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="pageCurrent1"
            :page-sizes="[10, 20, 30, 40, 50]"
            :page-size="10"
            :total="total1"
            layout="total, sizes, prev, pager, next, jumper"
            @size-change="handleSizeChange1"
            @current-change="handleCurrentChange1"/>
        </div>
      </el-card>
    </table>

    <el-dialog :visible.sync="dialogFormVisible" 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="handleCurrentChange">
          <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="dialogFormVisible = false">取消</el-button>
      <el-button type="primary" style="margin-top: 20px" @click="getName">确认</el-button>
    </el-dialog>

    <el-dialog :visible.sync="dialogFormVisible1" title="增加" center style="min-height: 270px" width="800px">
      <el-form ref="form1" :model="form1" :rules="rules" label-position="left" label-width="110px" class="demo-ruleForm">
        <el-row>
          <el-col :span="12">
            <el-form-item label="员工姓名" prop="staffName">
              <el-input v-model="form1.staffName" placeholder="请双击选择" size="small" clearable style="width:200px" @dblclick.native="dialogTableVisible()"/>
            </el-form-item>
            <el-form-item label="角色名称" prop="roleName">
              <el-select v-model="form1.roleName" placeholder="角色名称" size="small" clearable style="width:200px">
                <el-option
                  v-for="item in roleNames"
                  :key="item.value"
                  :label="item.label"
                  :value="item.value"/>
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="考勤编号" >
              <el-input
                v-model="cjscgy"
                disabled
                size="small"
                style="width:200px"/>
            </el-form-item>
            <el-form-item label="所属部门" >
              <el-input v-model="ssks" size="small" style="width:200px"/>
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="callOFF">取 消</el-button>
        <el-button type="primary" @click="submitAdd1()">确 定</el-button>
      </div>
    </el-dialog>

    <el-dialog :visible.sync="dialogFormVisible2" title="编辑" center style="min-height: 270px" width="800px">
      <el-form ref="form2" :model="form2" :rules="rules1" label-position="left" label-width="110px" class="demo-ruleForm">
        <el-row>
          <el-col :span="12">
            <el-form-item label="员工姓名" >
              <el-input v-model="form2.staffName" disabled size="small" clearable style="width:200px"/>
            </el-form-item>
            <el-form-item label="角色名称" prop="roleName">
              <el-select v-model="form2.roleName" placeholder="角色名称" clearable size="small" style="width:200px">
                <el-option
                  v-for="item in roleNames"
                  :key="item.value"
                  :label="item.label"
                  :value="item.value"/>
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="考勤编号" >
              <el-input
                v-model="cjscgy"
                disabled
                size="small"
                style="width:200px"/>
            </el-form-item>
            <el-form-item disabled="true" label="所属部门" >
              <el-input v-model="ssks" disabled size="small" style="width:200px"/>
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="callOFF">取消</el-button>
        <el-button type="primary" @click="submitAdd2()">确定</el-button>
      </div>
    </el-dialog>

  </div>
</template>

<script>
import { dialogtablevisible, deleteData, getTree, fn, getTable8, initOrderType, addData1, addData2, searchTable } from '@/api/BasicData/WorkshopPersonnelRoleMana'
export default {
  data() {
    return {
      number: '',
      form1: {
        staffName: '',
        roleName: ''
      },
      form2: {
        staffName: '',
        roleName: ''
      },
      rules: {
        staffName: [{ required: true, message: '请选择员工姓名', trigger: 'change' }],
        roleName: [{ required: true, message: '请选择员工角色', trigger: 'change' }]
      },
      rules1: {
        roleName: [{ required: true, message: '请选择员工角色', trigger: 'change' }]
      },
      staffName: '',
      cjscgy: '',
      List1: [],
      ssks: '',
      data2: [],
      dialogFormVisible1: false,
      dialogFormVisible2: false,
      tableData: [],
      check1: '',
      check2: '',
      name: '', // 中间变量
      check3: '',
      check4: '',
      staffNum: '',
      personnelNumber: '',
      personnelStream: '',
      pageCurrent1: 1,
      pageSize1: 10,
      total1: null,
      roleName: null,
      roleNames: [],
      rybh: '',
      jsbh: '',
      dialogFormVisible: false
    }
  },
  created() {
    this.fetchData()
  },
  methods: {
    handelAdd(row) {
      this.dialogFormVisible1 = true
      this.ssks = ''
      this.cjscgy = ''
      if (this.$refs['form1'] !== undefined) {
        this.$refs['form1'].resetFields()
      }
    },
    submitEdit(index, row) {
      if (this.$refs['form2'] !== undefined) {
        this.$refs['form2'].resetFields()
      }
      this.dialogFormVisible2 = true
      this.personnelStream = row.人员角色流水号
      this.form2.staffName = row.姓名
      this.ssks = row.部门名称
      this.personnelNumber = row.人员编号
      this.form2.roleName = parseInt(row.角色编号)
      this.cjscgy = row.考勤编号
    },
    callOFF() {
      this.dialogFormVisible1 = false
      this.dialogFormVisible2 = false
      this.ssks = ''
      this.cjscgy = ''
    },
    searchData() {
      if (this.staffName !== '' && this.staffName !== null) {
        this.check1 = 1
      } else {
        this.check1 = 0
      }
      if (this.roleName !== '' && this.roleName !== null) {
        this.check2 = 1
      } else {
        this.check2 = 0
      }
      searchTable(this.check1, this.staffName, this.check2, this.roleName, this.pageCurrent1, this.pageSize1).then(response => {
        console.log(response)
        this.tableData = response.data.rows
        this.total1 = response.data.total
      })
    },
    fetchData() {
      getTree().then(response => { // 获取人员信息树
        const data = response.data
        this.data2 = fn(data, 0)
      })
      initOrderType().then(response => { // 角色名称
        for (let i = 0; i < response.data.length; i++) {
          this.roleNames.push({
            label: response.data[i].角色功能,
            value: response.data[i].角色编号
          })
        }
      })
      this.searchData()
    },
    handleSizeChange1(val) {
      this.pageCurrent1 = 1
      this.pageSize1 = val
      this.searchData()
    },
    handleCurrentChange1(val) {
      this.pageCurrent1 = val
      this.searchData()
    },
    submitAdd1() {
      this.$refs['form1'].validate((valid) => {
        if (valid) {
          addData1(this.personnelNumber, this.form1.roleName).then(response => {
            if (response.data[0].result === '1') {
              this.$message.success('增加成功')
              this.ssks = ''
              this.form1.staffName = ''
              this.form1.roleName = ''
              this.searchData()
              this.dialogFormVisible1 = false
            } else { this.$message.error('增加失败') }
          })
        } else {
          console.log('error submit!!')
          return false
        }
      })
    },
    submitAdd2() {
      this.$refs['form2'].validate((valid) => {
        if (valid) {
          addData2(this.personnelStream, this.personnelNumber, this.form2.roleName).then(response => {
            if (response.data[0].result === '1') {
              this.$message.success('修改成功')
              this.ssks = ''
              this.searchData()
              this.dialogFormVisible2 = false
            } else { this.$message.error('修改失败') }
          })
        } else {
          console.log('error submit!!')
          return false
        }
      })
    },
    handleCurrentChange3(val) {
      this.searchgroup()
    },
    handleNodeClick(data) { // 树节点点击
      this.ssks = data.label
      getTable8(data.id).then(response => {
        this.List1 = response.data
      })
    },
    handleDelete(row) { // 删除
      this.$confirm('此操作将永久删除该行, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        deleteData(row.人员角色流水号).then(response => {
          if (response.data[0].result === '1') {
            this.$message.success('删除成功')
          } else { this.$message.error('删除失败') }
          this.searchData()
        })
      }).catch(() => {
        this.$message({
          type: 'info',
          message: '已取消删除'
        })
      })
    },
    dialogTableVisible() {
      this.dialogFormVisible = true
      dialogtablevisible().then(response => {
      })
    },
    handleCurrentChange(row) { // 获取当前行人员信息
      this.ssks = row.节点名称
      this.number = row.考勤编号
      this.name = row.姓名
      this.personnelNumber = row.人员编号
    },
    getName() { // 提交人员
      this.cjscgy = this.number
      this.dialogFormVisible = false
      this.form1.staffName = this.name
      this.form2.staffName = this.name
    }
  }
}
</script>

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