<template>
  <div class="gl-page-content">
    <div class="table">
      <BaseTable
        ref="baseTableRef"
        tableName="SYSTEM_ROLE"
        :api="roleListRolesApi"
        :actionsWidth="95"
      >
        <template #leftOptions>
          <el-button type="primary" :icon="Plus" @click="openDetailFun('')">新增角色</el-button>
        </template>
        <template #tableActions="{ row }">
          <div class="gl-table-actions">
            <el-link type="primary" @click="openDetailFun(row.id)">编辑</el-link>
            <el-popconfirm title="确定删除角色吗？" :width="200" placement="left" @confirm="delFun(row.id)">
              <template #reference>
                <el-link type="danger">删除</el-link>
              </template>
            </el-popconfirm>
          </div>
        </template>
      </BaseTable>
    </div>
    <Detail v-model="detailVisible" :id="roleId" tableName="SYSTEM_ROLE" @submit="submitFun" />
  </div>
</template>

<script setup lang="ts">
import { ref } from 'vue';
import { Plus } from '@element-plus/icons-vue';
import { ElMessage } from 'element-plus';
import BaseTable from '@/components/common/table/baseTable.vue';
import Detail from './components/detail.vue';
import { roleListRolesApi, roleDeleteRoleApi } from '@/api/system/role';

const detailVisible = ref(false);
const baseTableRef = ref<any>();
const roleId = ref<number | string>('');

const openDetailFun = (id: number | string) => {
  roleId.value = id;
  detailVisible.value = true;
};

const submitFun = () => {
  baseTableRef.value.resetFun();
};

const delFun = (id: number) => {
  const params = {
    id,
  };
  roleDeleteRoleApi(params).then((res: any) => {
    if (res.code === 200) {
      baseTableRef.value.resetFun();
      ElMessage.success('操作成功');
    }
  });
};
</script>
