<template>
  <div>
    <el-card>
      <span class="show-text">姓名:</span>
      <el-input id="inputPersonalName" v-model="PersonalName" clearable style="width: 280px;margin-right: 26px" class="personal-name" placeholder="请输入姓名"/>
      <span class="show-text">角色:</span>
      <el-select id="selectPersonalRoleValue" v-model="PersonalroleValue" class="personal-role-value" style="width:280px;margin-right: 14px" clearable placeholder="请选择角色">
        <el-option
          v-for="item in Personalrole"
          :key="item.value"
          :label="item.label"
          :value="item.value"/>
      </el-select>
      <el-button :disabled="loading" icon="el-icon-search" type="primary" @click="searchTable()">查询</el-button>
      <el-button style="margin-left: 14px" type="success" icon="el-icon-circle-plus-outline" @click="addType('form')">新增</el-button>
      <div style="margin-top: 26px">
        <el-table
          v-loading="loading"
          :data="tableData"
          :header-row-style= "{fontSize: '18px',fontFamily:'MiSans Demibold',height: '64px',backgroundColor:'#fff'}"
          :row-style="{fontSize: '16px',fontFamily:'MiSans Regular',height: '64px'}"
          element-loading-text="数据加载中"
          element-loading-spinner="el-icon-loading"
          element-loading-background="rgba(0, 0, 0, 0.2)"
          stripe
          tooltip-effect="dark"
          style="width: 1580px"
          height="694"
          border
          size="mini">
          <el-table-column type="index" label=" " align="center" width="100"/>
          <el-table-column align="left" label="姓名" width="320" show-overflow-tooltip>
            <template slot-scope="scope">
              <template v-if="scope.row.edit">
                <el-input v-model="scope.row.personnel_name" style="width:200px;margin-right:3px" clearable size="mini" width="120"/>
              </template>
              <span v-else>{{ scope.row.personnel_name }}</span>
            </template>
          </el-table-column>
          <el-table-column align="center" label="工号" min-width="180" show-overflow-tooltip>
            <template slot-scope="scope">
              <template v-if="scope.row.edit">
                <el-input v-model="scope.row.account" style="width:200px;margin-right:3px" clearable size="mini" width="120"/>
              </template>
              <span v-else>{{ scope.row.account }}</span>
            </template>
          </el-table-column>
          <el-table-column align="center" label="密码" min-width="180" show-overflow-tooltip>
            <template slot-scope="scope">
              <template v-if="scope.row.edit">
                <el-input v-model="scope.row.password" style="width:200px;margin-right:3px" clearable size="mini" width="120"/>
              </template>
              <span v-else>{{ scope.row.password }}</span>
            </template>
          </el-table-column>
          <el-table-column align="center" label="角色" min-width="180" show-overflow-tooltip>
            <template slot-scope="scope">
              <span type="success">{{ scope.row.roles_function }}</span>
            </template>
          </el-table-column>
          <el-table-column align="center" label="编辑" min-width="150">
            <template slot-scope="scope">
              <el-button type="text" style="font-size: 16px;font-family: MiSans Regular,serif;color: #4d77d6" icon="el-icon-edit" @click="editType('form1', scope.row)">编辑</el-button>
              <el-button type="text" style="font-size: 16px;font-family: MiSans Regular,serif;color: #c61a1a" icon="el-icon-delete" @click="openDelete(scope.row)">删除</el-button>
            </template>
          </el-table-column>
        </el-table>
        <div class="block" style="margin-top: 32px;">
          <el-pagination
            v-if="!loading"
            :current-page="pageList"
            :page-sizes="[10, 20, 30, 40]"
            :page-size="pageSize"
            :total="total"
            background
            layout="total, sizes, prev, pager, next, jumper"
            @size-change="handleSizeChange"
            @current-change="handleCurrentChange"/>
        </div>
      </div>
    </el-card>
    <el-dialog :title="title" :visible.sync="dialogFormVisible" :show-close="false" center width="440px">
      <el-divider style="margin: 0"/>
      <el-form ref="form" :model="form" :rules="rules" label-position="right" label-width="100px">
        <el-form-item label="姓名" prop="PersonalName">
          <el-input v-model="form.PersonalName" style="width: 240px;border-radius: 4px" clearable placeholder="请输入姓名"/>
        </el-form-item>
        <el-form-item label="工号" prop="account">
          <el-input v-model="form.account" style="width: 240px;border-radius: 4px" clearable placeholder="请输入工号"/>
        </el-form-item>
        <el-form-item label="密码" prop="password">
          <el-input v-model="form.password" style="width: 240px;border-radius: 4px" clearable placeholder="请输入密码"/>
        </el-form-item>
        <el-form-item label="角色" prop="PersonalroleValue">
          <el-select v-model="form.PersonalroleValue" style="width:240px;border-radius: 4px" filterable clearable placeholder="请选择角色">
            <el-option
              v-for="item in Personalrole"
              :key="item.value"
              :label="item.label"
              :value="item.value"/>
          </el-select>
        </el-form-item>
        <el-row>
          <el-col :span="14"/>
          <el-form-item >
            <el-button style="width: 80px;height: 40px" type="primary" @click="submit('form')">确 定</el-button>
            <el-button style="width: 80px;height: 40px" @click="callOff('form')">取消</el-button>
          </el-form-item>
        </el-row>
      </el-form>
    </el-dialog>
    <el-dialog :title="title1" :visible.sync="dialogFormVisible1" :show-close="false" center width="440px">
      <el-divider style="margin: 0"/>
      <el-form ref="form1" :model="form1" :rules="rules1" label-position="right" label-width="100px">
        <el-form-item label="姓名" prop="PersonalName">
          <el-input v-model="form1.PersonalName" style="width: 240px;border-radius: 4px" clearable placeholder="请输入姓名"/>
        </el-form-item>
        <el-form-item label="工号" prop="account">
          <el-input v-model="form1.account" style="width: 240px;border-radius: 4px" readonly placeholder="请输入工号"/>
        </el-form-item>
        <el-form-item label="密码" prop="password">
          <el-input v-model="form1.password" style="width: 240px;border-radius: 4px" clearable placeholder="请输入密码"/>
        </el-form-item>
        <el-form-item label="角色" prop="PersonalroleValue">
          <el-select v-model="form1.PersonalroleValue" style="width:240px;border-radius: 4px" filterable clearable placeholder="请选择角色">
            <el-option
              v-for="item in Personalrole"
              :key="item.value"
              :label="item.label"
              :value="item.value"/>
          </el-select>
        </el-form-item>
        <el-row>
          <el-col :span="14"/>
          <el-form-item >
            <el-button style="width: 80px;height: 40px" type="primary" @click="confirmEdit('form1')">确 定</el-button>
            <el-button style="width: 80px;height: 40px" @click="callOff1('form1')">取消</el-button>
          </el-form-item>
        </el-row>
      </el-form>
    </el-dialog>
  </div>
</template>

<script>
export default {
  data() {
    return {
      PersonalName: '', // 姓名
      PersonalroleValue: '', // 角色
      Personalrole: [],
      title: '',
      title1: '',
      dialogFormVisible: false,
      dialogFormVisible1: false,
      form: {
        PersonalName: '',
        account: '',
        password: '',
        PersonalroleValue: ''
      },
      rules: {
        PersonalName: [{ required: true, message: '请输入姓名', trigger: 'blur' }],
        account: [{ required: true, message: '请输入账号', trigger: 'change' }],
        password: [{ required: true, message: '请输入密码', trigger: 'change' }],
        PersonalroleValue: [{ required: true, message: '请选择角色', trigger: 'blur' }]
      },
      form1: {
        PersonalName: '',
        account: '',
        password: '',
        PersonalroleValue: ''
      },
      rules1: {
        PersonalName: [{ required: true, message: '请输入姓名', trigger: 'blur' }],
        account: [{ required: true, message: '请输入账号', trigger: 'change' }],
        password: [{ required: true, message: '请输入密码', trigger: 'change' }],
        PersonalroleValue: [{ required: true, message: '请选择角色', trigger: 'blur' }]
      },
      loading: false,
      tableData: [],
      total: 0, // 总条数
      pageSize: 10, // 每页显示条数
      pageList: 1 // 后台获取页
    }
  },
  created() {
    this.getPersonalrole()
    this.searchTable()
  },
  methods: {
    // 初始化获取角色信息
    getPersonalrole() {
      this.Personalrole = []
      var Data = this.CreateData('11', '人员管理_角色信息初始化查询_查询')
      this.ExecDatabase(Data).then(response => {
        for (let i = 0; i < response.data.length; i++) {
          this.Personalrole.push({
            label: response.data[i].Roles_Function,
            value: response.data[i].Roles_number
          })
        }
      })
    },

    // 查询
    searchTable() {
      if (this.PersonalName === '' || this.PersonalName === null) { this.PersonalName_check = 0 } else { this.PersonalName_check = 1 }
      if (this.PersonalroleValue === '' || this.PersonalroleValue === null) { this.PersonalroleValue_check = 0 } else { this.PersonalroleValue_check = 1 }
      this.loading = true
      this.tableData = []
      var param = []
      param[0] = ['姓名_check', this.PersonalName_check]
      param[1] = ['姓名', this.PersonalName]
      param[2] = ['角色代码_check', this.PersonalroleValue_check]
      param[3] = ['角色代码', this.PersonalroleValue]
      param[4] = ['PageCurrent', this.pageList]
      param[5] = ['PageSize', this.pageSize]
      param[6] = ['PageCount', '1111', 'int', '1']
      param[7] = ['ItemCount', '1111', 'int', '1']
      var Data = this.CreateData('11', '人员管理_人员信息查询_查询', param)
      this.ExecDatabase(Data).then(response => {
        if (response.data.result.length !== 0) {
          for (let i = 0; i < response.data.result.length; i++) {
            this.tableData.push({
              personnel_number: response.data.result[i].Personnel_Number,
              personnel_name: response.data.result[i].Personnel_Name,
              account: response.data.result[i].account,
              password: response.data.result[i].password,
              roles_number: parseInt(response.data.result[i].Roles_number),
              roles_function: response.data.result[i].Roles_Function
            })
          }
        }
        this.total = parseInt(response.data.output[0].ItemCount)
        this.loading = false
      })
    },

    // 点击编辑
    editType(formName, row) {
      if (this.$refs[formName] !== undefined) {
        this.$refs[formName].resetFields()
      }
      this.form1.PersonalName = row.personnel_name
      this.form1.personnel_number = row.personnel_number
      this.form1.account = row.account
      this.form1.password = row.password
      this.form1.PersonalroleValue = row.roles_number
      this.title1 = '编辑人员'
      this.dialogFormVisible1 = true
    },

    // 确定编辑
    confirmEdit(formName) {
      this.$refs[formName].validate((valid) => {
        if (valid) {
          var param = []
          param[0] = ['Personnel_Number', this.form1.personnel_number]
          param[1] = ['account', this.form1.account]
          param[2] = ['password', this.form1.password]
          param[3] = ['Personnel_Name', this.form1.PersonalName]
          param[4] = ['Roles_number', this.form1.PersonalroleValue]
          if (this.form1.account === '' || this.form1.account === null) {
            this.$message.error('请输入工号')
          } else if (this.form1.password === '' || this.form1.password === null) {
            this.$message.error('请输入密码')
          } else if (this.form1.personnel_name === '' || this.form1.personnel_name === null) {
            this.$message.error('请输入姓名')
          } else if (this.form1.PersonalroleValue === '' || this.form1.PersonalroleValue === null) {
            this.$message.error('请选择角色')
          } else {
            var Data = this.CreateData('12', '人员管理_人员信息修改_编辑', param)
            this.ExecDatabase(Data).then(response => {
              if (response.data[0].result === '1') {
                this.$message({
                  message: '信息编辑成功',
                  type: 'success'
                })
                this.searchTable()
                this.dialogFormVisible1 = false
              } else {
                this.$message.error('信息编辑失败')
              }
            })
          }
        }
      })
    },

    // 删除
    openDelete(row) {
      this.$confirm('当前人员将永久删除, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        confirmButtonClass: 'confirm-confirm-button',
        cancelButtonClass: 'confirm-cancel-button',
        type: 'warning'
      }).then(() => {
        var param = []
        param[0] = ['Personnel_Number', row.personnel_number]
        var Data = this.CreateData('12', '人员管理_人员信息修改_删除', param)
        this.ExecDatabase(Data).then(response => {
          if (response.data[0].result === '1') {
            this.$message({
              message: '人员离职成功',
              type: 'success'
            })
            this.searchTable()
          } else {
            this.$message.error('人员离职失败')
          }
        })
      }).catch(() => {
        this.$message({
          type: 'info',
          message: '已取消删除'
        })
      })
    },

    // 点击增加
    addType(formName) {
      if (this.$refs[formName] !== undefined) {
        this.$refs[formName].resetFields()
      }
      this.form.PersonalName = ''
      this.form.account = ''
      this.form.password = ''
      this.form.PersonalroleValue = ''
      this.title = '增加人员'
      this.dialogFormVisible = true
    },

    // 增加确定
    submit(formName) {
      this.$refs[formName].validate((valid) => {
        if (valid) {
          var param = []
          param[0] = ['Personnel_Name', this.form.PersonalName]
          param[1] = ['account', this.form.account]
          param[2] = ['password', this.form.password]
          param[3] = ['Roles_number', this.form.PersonalroleValue]
          var Data = this.CreateData('12', '人员管理_人员信息新增_新增', param)
          this.ExecDatabase(Data).then(response => {
            if (response.data[0].result === '1') {
              this.$message({
                message: '信息增加成功',
                type: 'success'
              })
              this.pageList = 1
              this.pageSize = 10
              this.searchTable()
            } else {
              this.$message.error('信息增加失败')
            }
          })
          this.dialogFormVisible = false
        } else {
          return false
        }
      })
    },

    // 重置表单
    callOff(formName) {
      this.dialogFormVisible = false
      this.$refs[formName].resetFields()
    },

    // 重置表单
    callOff1(formName) {
      this.dialogFormVisible1 = false
      this.$refs[formName].resetFields()
    },
    // 分页
    handleSizeChange(val) {
      this.pageList = 1
      this.pageSize = val
      this.searchTable()
    },
    handleCurrentChange(val) {
      this.pageList = val
      this.searchTable()
    }
  }
}
</script>

<style scoped>
  ::v-deep .personal-name #inputPersonalName {
    height: 44px;
  }
  ::v-deep .personal-role-value #selectPersonalRoleValue {
    height: 44px;
  }
</style>
