<template>
  <el-container>
    <el-main>
      <el-card class="box-card">
     
        <p>添加角色</p>
        <el-form ref="roleForm" style="margin-top:10px" :inline="true" :model="role" status-icon :rules="rules" label-width="100px" class="demo-ruleForm">

          <el-form-item label="名字" prop="description">
            <el-input v-model="role.description" />
          </el-form-item>

          <el-form-item label="角色名称" prop="name">
            <el-input v-model="role.name" autocomplete="off" />
          </el-form-item>

          <el-form-item>
            <el-button type="primary" @click="addRole('roleForm')">添加</el-button>
            <el-button @click="resetForm('roleForm')">取消</el-button>
          </el-form-item>
        </el-form>
      </el-card>

      <template>
        <el-card style="margin-top:10px" class="box-card">
          <p>角色列表</p>
          <el-table
            :data="roles"
            style="width: 100%"
          >
            <el-table-column
              label="ID"
              width="80"
            >
              <template slot-scope="scope">
                <span>{{ scope.row.id }}</span>
              </template>
            </el-table-column>
            <el-table-column
              label="名字"
              width="280"
            >
              <template slot-scope="scope">
                <span>{{ scope.row.description }}</span>
              </template>
            </el-table-column>
            <el-table-column
              label="角色名称"
              width="180"
            >
              <template slot-scope="scope">
                <span>{{ scope.row.name }}</span>
              </template>
            </el-table-column>

            <el-table-column
              label="权限"
              width="380"
            >
              <template slot-scope="scope">
                <el-badge v-for="auth in scope.row.authorities" :key="auth.id">  <el-tag size="small">{{ auth.name }}</el-tag></el-badge>
              </template>
            </el-table-column>

            <el-table-column label="操作">
              <template slot-scope="scope">
                <el-button
                  size="mini"
                  @click="handleEdit(scope.$index, scope.row)"
                >编辑</el-button>
                <el-button
                  size="mini"
                  type="danger"
                  @click="handleDelete(scope.$index, scope.row)"
                >删除</el-button>
              </template>
            </el-table-column>
          </el-table>
          <el-dialog
            title="修改角色"
            :visible.sync="dialogVisible" :close-on-click-modal="false" :show-close="false"
            width="70%"
          >
            <el-container>
              <el-main>
                <el-form ref="updateRoleForm" style="margin-top:10px" :model="editRole" status-icon :rules="rules" label-width="100px" class="demo-ruleForm">

                  <el-form-item label="名字" prop="description">
                    <el-input v-model="editRole.description" />
                  </el-form-item>

                  <el-form-item label="角色名称" prop="name">
                    <el-input v-model="editRole.name" autocomplete="off" />
                  </el-form-item>

                  <el-form-item>
                    <el-alert title="权限" type="info" :closable="false">
                      <el-checkbox-group v-model="editRole.authorities" size="mini">
                        <el-checkbox v-for="auth in authorities" :key="auth.id" border="" :label="auth.name">{{ auth.name }}</el-checkbox>
                      </el-checkbox-group>
                    </el-alert>

                  </el-form-item>
                  <el-form-item>
                    <el-button type="primary" @click="updateRole('updateRoleForm')">确认</el-button>
                    <el-button @click="cancel()">取消</el-button>
                  </el-form-item>
                </el-form>
              </el-main>
            </el-container>
          </el-dialog>
        </el-card>
      </template>

    </el-main>
  </el-container>

</template>


<script>

export default {
  data() {
    return {
      dialogVisible: false,
      role: {
        description: '',
        name: '',
        authorities: []
      },
      editRole: {
        description: '',
        name: '',
        authorities: []
      },
      rules: {
        name: [
          { required: true, message: '请输入角色名称', trigger: 'blur' },
          { min: 2, max: 30, message: '长度在 2 到 30 个字符', trigger: 'blur' }
        ],
        description: [
          { required: true, message: '请输入名称', trigger: 'blur' },
          { min: 2, max: 100, message: '长度在 2到 100 个字符', trigger: 'blur' }
        ]
      }
    }
  },
  computed: {
    roles() {
      return this.$store.getters.allRoles
    },
    authorities() {
      return this.$store.getters.allAuthorities
    }
  },
  created() {
    console.log('created')
    this.$store.dispatch('role/getAllRoles')
    this.$store.dispatch('authority/getAllAuthorities')
  },
  methods: {
    handleEdit(index, row) {
      console.log(index, row)
      this.editRole = row

      var authList = new Array()
      for (var i in row.authorities) {
        console.log('auth', row.authorities[i])
        authList.push(row.authorities[i].name)
      }
      console.log('authList', authList)
      this.editRole.authorities = authList
      this.dialogVisible = true
      console.log('editRole',this.editRole)
    },
    updateRole(formName) {
      console.log('start to update role-1', this.editRole)
      this.$refs[formName].validate((valid) => {
        if (valid) {
          console.log('start to update role0', this.editRole)

          this.$store.dispatch('role/updateRole', this.editRole).then(res => {
            if (res.data.success) {
              this.$message({ message: '修改权限成功', type: 'success' })
              this.editRole=res.data.data
              var authList = new Array()
              for (var i in this.editRole.authorities) {
                console.log('auth', this.editRole.authorities[i])
                authList.push(this.editRole.authorities[i].name)
              }
              
              this.editRole.authorities = authList
              this.$store.dispatch('role/getAllRoles')
            } else {
              console.log('修改权限失败!', res)
              this.$message({ message: '修改权限失败!' + res.data.info, type: 'error' })
            }
          }
          )
        } else {
          console.log('error submit!!')
          return false
        }
      })
    },
    cancel() {
      this.$store.dispatch('role/getAllRoles')
      this.$store.dispatch('authority/getAllAuthorities')
      this.dialogVisible = false
    },
    handleDelete(index, row) {
      console.log(index, row)
      this.$confirm('即将永久删除角色:' + row.name + ', 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        this.$store.dispatch('role/deleteRole', row).then(res => {
          if (res.data.success) {
            this.$message({
              type: 'success',
              message: '删除成功!'
            })
            this.$store.dispatch('role/getAllRoles')
          } else {
            this.$message({ message: '删除失败!' + res.data.info, type: 'error' })
          }
        }).catch(error => {
          this.$message({ message: '删除失败!' + res.data.info, type: 'error' })
        })
      }).catch(() => {
        this.$message({
          type: 'info',
          message: '已取消删除'
        })
      })
    },
    addRole(formName) {
      this.$refs[formName].validate((valid) => {
        if (valid) {
          console.log('start to add role0', this.role)
          this.$store.dispatch('role/addRole', this.role).then(res => {
            if (res.data.success) {
              this.$message({ message: '添加权限成功', type: 'success' })
              this.$store.dispatch('role/getAllRoles')
            } else {
              console.log('添加权限失败!', res)
              this.$message({ message: '添加权限失败!' + res.data.info, type: 'error' })
            }
          }
          )
        } else {
          console.log('error submit!!')
          return false
        }
      })
    },
    resetForm(formName) {
      this.$refs[formName].resetFields()
    }
  }
}
</script>
