<template>
  <div class="app-container">
    <el-card>
      <span class="show-text">角色功能:</span>
      <el-input
        v-model="RoleFunctioning"
        clearable
        placeholder="请输入角色功能"
        style="width: 280px;margin-right: 26px"/>
      <el-button icon="el-icon-search" plain type="primary" @click="searchData()">查询</el-button>
      <el-button
        icon="el-icon-circle-plus-outline"
        plain
        style="margin-left: 14px"
        type="success"
        @click="handleAdd('form')">
        新增
      </el-button>
      <div style="margin-top: 10px">
        <el-table
          v-loading="loading"
          :data="tableData"
          border
          element-loading-background="rgba(0, 0, 0, 0.2)"
          element-loading-spinner="el-icon-loading"
          element-loading-text="数据加载中"
          height="694"
          highlight-current-row
          size="mini"
          stripe
          style="width: 642px"
          tooltip-effect="dark">
          <el-table-column align="center" label=" " type="index" width="100"/>
          <el-table-column align="center" label="角色功能" width="341">
            <template slot-scope="scope">
              {{ scope.row.Roles_Function }}
            </template>
          </el-table-column>
          <el-table-column align="center" label="编辑" width="200">
            <template slot-scope="scope">
              <el-button
                icon="el-icon-edit"
                style="font-family: MiSans Regular,serif;color: #4d77d6"
                type="text"
                @click="handleEdit(scope.row)">编辑
              </el-button>
              <el-button
                icon="el-icon-delete"
                style="font-family: MiSans Regular,serif;color: #c61a1a"
                type="text"
                @click="handleDelete(scope.row)">删除
              </el-button>
            </template>
          </el-table-column>
        </el-table>
        <div class="block" style="margin-top: 10px;">
          <el-pagination
            v-if="!loading"
            :current-page="pageList"
            :page-size="pageSize"
            :page-sizes="[10, 20, 30, 40]"
            :total="total"
            background
            layout="total, sizes, prev, pager, next, jumper"
            @size-change="handleSizeChange"
            @current-change="handleCurrentChange"/>
        </div>
      </div>
    </el-card>

    <el-dialog :visible.sync="dialogFormVisible1" center title="新增角色" width="440px">
      <el-divider style="margin: 0"/>
      <el-form ref="form" :model="form" :rules="rules" label-position="left" label-width="100px">
        <el-form-item label="角色功能" prop="角色功能" style="display: inline-block">
          <el-input
            v-model="form.角色功能"
            clearable
            placeholder="角色功能"
            style="width: 240px;border-radius: 4px"/>
        </el-form-item>
        <el-row>
          <el-col :span="14"/>
          <el-form-item>
            <el-button style="width: 80px;height: 40px" type="primary" @click="submitAdd1('form')">确 定</el-button>
            <el-button style="width: 80px;height: 40px" @click="dialogFormVisible1 = false">取消</el-button>
          </el-form-item>
        </el-row>
      </el-form>
    </el-dialog>

    <el-dialog :visible.sync="dialogFormVisible2" center title="编辑角色" width="440px">
      <el-divider style="margin: 0"/>
      <el-form ref="form2" :model="form2" :rules="rules2" label-position="left" label-width="100px">
        <el-form-item label="角色名称" prop="角色功能" style="display: inline-block">
          <el-input
            v-model="form2.角色功能"
            clearable
            placeholder="角色功能"
            style="width: 240px;border-radius: 4px"/>
        </el-form-item>
        <el-row>
          <el-col :span="14"/>
          <el-form-item>
            <el-button style="width: 80px;height: 40px" type="primary" @click="submitAdd2('form2')">确 定</el-button>
            <el-button style="width: 80px;height: 40px" @click="dialogFormVisible2 = false">取消</el-button>
          </el-form-item>
        </el-row>
      </el-form>
    </el-dialog>
  </div>
</template>

<script>
export default {
  data() {
    return {
      RoleFunctioning: '',
      loading: false,
      tableData: [],
      dialogFormVisible1: false,
      dialogFormVisible2: false,
      name: '',
      num: '', // 角色编号
      form: {
        角色功能: ''
      },
      form2: {
        角色功能: '',
        num: '' // 角色编号
      },
      rules: {
        角色功能: [{ required: true, message: '请输入角色名称', trigger: 'blur' }]
      },
      rules2: {
        角色功能: [{ required: true, message: '请输入角色名称', trigger: 'blur' }]
      },
      total: 0, // 总条数
      pageSize: 10, // 每页显示条数
      pageList: 1 // 后台获取页
    }
  },
  created() {
    this.searchData()
  },
  methods: {
    // 查询
    searchData() {
      this.loading = true
      this.tableData = []
      var param = []
      param[0] = ['角色功能', this.RoleFunctioning]
      param[1] = ['PageCurrent', this.pageList]
      param[2] = ['PageSize', this.pageSize]
      param[3] = ['PageCount', '1111', 'int', '1']
      param[4] = ['ItemCount', '1111', 'int', '1']
      var Data = this.CreateData('11', '菜单系统模块_项目角色_查询数据_MESWORK', param)
      this.ExecDatabase(Data).then(response => {
        if (response.data.result.length !== 0) {
          this.tableData = response.data.result
        }
        this.total = parseInt(response.data.output[0].ItemCount)
        this.loading = false
      })
    },
    // 编辑角色
    handleEdit(row) {
      if (this.$refs['form2'] !== undefined) {
        this.$refs['form2'].resetFields()
      }
      this.form2.角色功能 = row.Roles_Function
      this.form2.num = row.Roles_number
      this.dialogFormVisible2 = true
    },
    // 编辑角色功能
    submitAdd2(formName) {
      this.$refs[formName].validate((valid) => {
        if (valid) {
          var param = []
          param[0] = ['角色功能', this.form2.角色功能]
          param[1] = ['角色编号', this.form2.num]
          var Data = this.CreateData('11', '菜单系统模块_项目角色_修改数据_MESWORK', param)
          this.ExecDatabase(Data).then(response => {
            if (response.data[0].result === 1) {
              this.$message({
                message: '信息编辑成功',
                type: 'success'
              })
              this.searchData()
              this.dialogFormVisible2 = false
            } else {
              this.$message.error('存在同名角色，信息编辑失败')
            }
          })
        }
      })
    },

    // 增加角色
    handleAdd() {
      if (this.$refs['form'] !== undefined) {
        this.$refs['form'].resetFields()
      }
      this.dialogFormVisible1 = true
    },
    // 确认增加角色
    submitAdd1(formName) { // 增加角色功能
      this.$refs[formName].validate((valid) => {
        if (valid) {
          var param = []
          param[0] = ['角色功能', this.form.角色功能]
          var Data = this.CreateData('11', '菜单系统模块_项目角色_增加_MESWORK', param)
          this.ExecDatabase(Data).then(response => {
            if (response.data[0].result === 1) {
              this.$message({
                message: '信息编辑成功',
                type: 'success'
              })
              this.searchData()
              this.dialogFormVisible1 = false
            } else {
              this.$message.error('存在同名角色，信息增加失败')
            }
          })
        }
      })
    },
    // 删除角色
    handleDelete(row) {
      this.$confirm('当前角色将永久删除, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        confirmButtonClass: 'confirm-confirm-button',
        cancelButtonClass: 'confirm-cancel-button',
        type: 'warning'
      }).then(() => {
        var param = []
        param[0] = ['角色编号', row.Roles_number]
        var Data = this.CreateData('11', '菜单系统模块_项目角色_删除_MESWORK', param)
        this.ExecDatabase(Data).then(response => {
          if (response.data[0].result === 1) {
            this.$message.success('删除成功')
          } else if (response.data[0].result === 0) {
            this.$message.error('删除失败,当前角色已经分配了菜单！')
          } else if (response.data[0].result === 2) {
            this.$message.error('删除失败,当前角色已经分配了人员！')
          } else {
            this.$message.error('删除失败')
          }
          this.searchData()
        })
      }).catch(() => {
        this.$message({
          type: 'info',
          message: '已取消删除'
        })
      })
    },
    // 分页
    handleSizeChange(val) {
      this.pageList = 1
      this.pageSize = val
      this.searchTable()
    },
    handleCurrentChange(val) {
      this.pageList = val
      this.searchTable()
    }
  }
}
</script>
<style scoped>
.el-card {
  margin: 0px;
}
</style>
