<template>
  <div class="manage-container">
    <div class="manage-button">
      <el-button type="primary" size="small" @click="handleAdd">
        添加角色
      </el-button>
    </div>
    <el-table :data="roleData" border stripe style="width: 100%">
      <el-table-column
        prop="id"
        label="ID"
        width="60"
        align="center"
      ></el-table-column>
      <el-table-column
        prop="name"
        label="角色名"
        width="150"
        align="center"
      ></el-table-column>
      <el-table-column
        prop="key"
        label="key"
        width="150"
        align="center"
      ></el-table-column>
      <el-table-column
        prop="status"
        :label="$t('dataEnquiry.zt')"
        align="center"
        width="120"
      >
        <template #default="scope">
          <el-switch
            v-model="scope.row.status"
            active-value="0"
            inactive-value="1"
            @click="soreStatus(scope.row.status, scope.row.id)"
          ></el-switch>
        </template>
      </el-table-column>
      <el-table-column
        prop="createTime"
        :label="$t('grouping.cjsj')"
        width="220"
        align="center"
      ></el-table-column>
      <el-table-column
        prop="updateTime"
        :label="$t('grouping.gxsj')"
        width="220"
        align="center"
      ></el-table-column>
      <el-table-column fixed="right" :label="$t('defalult.cz')" align="center">
        <template #default="{ row }">
          <el-button type="text" @click="handleEdit(row)">
            {{ $t('defalult.bj') }}
          </el-button>
          <el-button type="text" @click="handleDelete(row)">
            {{ $t('defalult.sc') }}
          </el-button>
          <el-button type="text" @click="handleauthorization(row)">
            角色授权
          </el-button>
        </template>
      </el-table-column>
    </el-table>
    <el-pagination
      :background="background"
      :current-page="queryForm.page"
      :layout="layout"
      :page-size="queryForm.size"
      :total="total"
      style="text-align: right"
      @current-change="handleCurrentChange"
      @size-change="handleSizeChange"
    ></el-pagination>
    <role-edit ref="edit"></role-edit>
    <role-grant ref="grant"></role-grant>
  </div>
</template>

<script>
import { doDelete, doEdit, getList } from '@/api/role';
import roleGrant from './roleGrant';
import roleEdit from './roleEdit';
export default {
  name: 'RoleIndex',
  components: {
    roleGrant,
    roleEdit,
  },
  data() {
    return {
      roleData: [],
      lazy: true,
      activeName: 'first',
      layout: 'total, sizes, prev, pager, next, jumper',
      total: 0,
      input: '',
      textarea: '',
      list: [],
      background: true,
      listLoading: true,
      elementLoadingText: '正在加载...',
      queryForm: {
        page: 1,
        size: 20,
        name: '',
      },
    };
  },
  computed: {
    height() {
      return 500;
    },
  },
  created() {
    this.fetchData();
  },
  methods: {
    roleIndex() {
      this.fetchData();
    },
    async soreStatus(status, id) {
      const { code, msg } = await doEdit({ status: status, id: id });
      if (code == 0) {
        this.$notify({
          title: msg,
          type: 'success',
        });
      } else {
        this.$notify.error({
          title: msg,
        });
      }
      this.fetchData();
    },
    async fetchData() {
      this.listLoading = true;
      const { data } = await getList(this.queryForm);
      this.roleData = data.items;
      this.total = data.total;
      setTimeout(() => {
        this.listLoading = false;
      }, 500);
    },
    handleAdd() {
      this.$refs['edit'].showEdit();
    },
    handleEdit(row) {
      this.$refs['edit'].showEdit(row);
    },
    handleauthorization(row) {
      this.$refs['grant'].roleGrant(row);
    },
    handleQuery() {
      this.queryForm.page = 1;
      this.fetchData();
    },
    handleSizeChange(val) {
      this.queryForm.size = val;
      this.fetchData();
    },
    handleCurrentChange(val) {
      this.queryForm.page = val;
      this.fetchData();
    },
    handleDelete(row) {
      const that = this;
      if (row.id) {
        this.$confirm('你确定要删除当前项吗?', '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning',
        })
          .then(() => {
            const { msg } = doDelete({ roleId: row.id });
            this.$message({
              type: 'success',
              message: msg == undefined ? '删除成功' : msg,
            });
            setTimeout(function () {
              that.fetchData();
            }, 1000);
          })
          .catch(() => {
            this.$message({
              type: 'info',
              message: '已取消删除',
            });
          });
      }
    },
  },
};
</script>

<style lang="scss" scoped>
.manage-button {
  margin-bottom: 30px;
}
</style>
