<!-- 
    TODO 系统管理 / 角色管理
 -->
<template>
  <el-main>
    <!-- 搜索栏 -->
    <el-form
      :model="listParm"
      label-width="80px"
      :inline="true"
    >
      <el-form-item class="item-search">
        <el-input
          v-model="listParm.name"
          placeholder="请输入角色名称"
        />
      </el-form-item>
      <el-form-item>
        <el-button
          :icon="Search"
          @click="searchBtn"
        >
          搜索
        </el-button>
        <el-button
          style="color: #ff7670"
          :icon="RefreshRight"
          @click="resetBtn"
        >
          重置
        </el-button>
        <el-button
          v-permission="['sys:role:add']"
          type="primary"
          :icon="Plus"
          @click="addBtn"
        >
          新增
        </el-button>
      </el-form-item>
    </el-form>
    <!-- 表格 -->
    <el-table
      :height="tableHeight"
      :data="roleTable.list"
      stripe
    >
      <el-table-column
        prop="name"
        label="角色名称"
        :min-width="160"
      />
      <el-table-column
        prop="remark"
        label="角色备注"
        :min-width="160"
      />
      <el-table-column
        label="操作"
        width="440"
        fixed="right"
      >
        <template #default="scope">
          <el-button
            v-permission="['sys:role:edit']"
            type="primary"
            size="mini"
            :icon="Edit"
            @click="editBtn(scope.row)"
          >
            编辑
          </el-button>
          <el-button
            v-permission="['sys:roleDataPermission:save']"
            type="success"
            size="mini"
            :icon="Setting"
            @click="openPermisitonSet(scope.row)"
          >
            数据权限
          </el-button>
          <el-button
            v-permission="['sys:role:assign']"
            type="primary"
            size="mini"
            :icon="Setting"
            @click="assignPermission(scope.row.id, scope.row.name)"
          >
            分配权限
          </el-button>
          <el-button
            v-permission="['sys:role:delete']"
            type="danger"
            size="mini"
            :icon="Delete"
            @click="deleteBtn(scope.row.id)"
          >
            删除
          </el-button>
        </template>
      </el-table-column>
    </el-table>
    <!-- 分页 -->
    <el-pagination
      v-model:current-page="listParm.pageNum"
      :page-sizes="[10, 20, 40, 80, 100]"
      :page-size="listParm.pageSize"
      layout="total, sizes, prev, pager, next, jumper"
      :total="listParm.total"
      background
      @size-change="sizeChange"
      @current-change="currentChange"
    />
  </el-main>
  <!-- 新增、编辑弹框 -->
  <AddRole
    ref="addRoleRef"
    @save="save"
  />
  <!-- 分配权限弹框 -->
  <AssignMenu ref="assignMenuRef" />
  <!-- 数据权限弹窗 -->
  <DataPermisitonSet
    ref="dataPermisitonSetRef"
    :data-type="DataPermissionType.ROLE"
    @save-confirm="hanelDataPermisitonSave"
  />
</template>
<script setup lang="ts" name="roleList">
import { ref, onMounted, nextTick } from "vue";
import {
  Search,
  Edit,
  Plus,
  Setting,
  RefreshRight,
  Delete
} from "@element-plus/icons-vue";
import useRoleTable from "@/composables/role/useRoleTable";
import useRole from "@/composables/role/useRole";
import AddRole from "./AddRole.vue";
import AssignMenu from "./AssignMenu.vue";
import useResizeHeight from "@/hooks/useResizeHeight";
import DataPermisitonSet from "@/views/system/dataPermission/dataPermisitonSet.vue";
import { DataPermissionType } from "@/composables/dataPermission/usedataPermissionSet";

//表格列表
const {
  listParm,
  roleTable,
  getRoleList,
  sizeChange,
  currentChange,
  searchBtn,
  resetBtn
} = useRoleTable();

//新增、编辑
const {
  addBtn,
  editBtn,
  deleteBtn,
  assignPermission,
  save,
  addRoleRef,
  assignMenuRef,
  dataPermisitonSetRef,
  openPermisitonSet,
  hanelDataPermisitonSave
} = useRole(getRoleList);
// 表格自适应屏幕高度
const { tableHeight } = useResizeHeight();
</script>
