<template>
  <el-container class="role-view panel-block">
    <el-header class="sa-header">
      <div class="sa-title sa-flex sa-row-between">
        <div class="label sa-flex">{{ t('modules.auth.roleList') }}</div>
        <div>
          <el-button class="sa-button-refresh" icon="RefreshRight" @click="getData()"></el-button>
          <el-button icon="Plus" type="primary" @click="addRow">{{ t('modules.auth.addRole') }}</el-button>
        </div>
      </div>
    </el-header>
    <el-main class="sa-p-0">
      <div class="sa-table-wrap panel-block panel-block--bottom" v-loading="loading">
        <el-table height="100%" class="sa-table" :data="table.data" @sort-change="fieldFilter" row-key="id" stripe>
          <template #empty>
            <sa-empty />
          </template>
          <el-table-column prop="id" label="ID" min-width="100" sortable="custom">
          </el-table-column>
          <el-table-column prop="name" :label="t('modules.auth.roleName')" min-width="200" sortable="custom">
            <template #default="scope">
              <span class="sa-table-line-1">{{ scope.row.name || '-' }}</span>
            </template>
          </el-table-column>
          <el-table-column fixed="right" :label="t('common.actions')" min-width="120">
            <template #default="scope">
              <template v-if="scope.row.id != 1">
                <el-button link type="primary" @click="editRow(scope.row)">{{ t('common.edit') }}</el-button>
                <el-popconfirm width="fit-content" :confirm-button-text="t('common.confirm')"
                  :cancel-button-text="t('common.cancel')" :title="t('message.confirmDelete')"
                  @confirm="deleteApi(scope.row.id)">
                  <template #reference>
                    <el-button link type="danger">{{ t('common.delete') }}</el-button>
                  </template>
                </el-popconfirm>
              </template>
            </template>
          </el-table-column>
        </el-table>
      </div>
    </el-main>
    <sa-view-bar>
      <template #right>
        <sa-pagination :pageData="pageData" @updateFn="getData" />
      </template>
    </sa-view-bar>
  </el-container>
</template>

<script setup>
import { onMounted, reactive, ref } from 'vue';
import { useI18n } from 'vue-i18n';
import admin from '@/app/admin/api';
import { useModal, usePagination } from '@/sheep/hooks';
import RoleEdit from './edit.vue';

const { t } = useI18n();
const { pageData } = usePagination();

// 列表
const table = reactive({
  data: [],
  order: '',
  sort: '',
});
const loading = ref(true);

// 获取数据
async function getData(page, searchParams = {}) {
  if (page) pageData.page = page;
  loading.value = true;

  try {
    const { code, data } = await admin.auth.role.roleList({
      page: pageData.page,
      size: pageData.size,
      ...searchParams,
    });

    if (code == 200) {
      table.data = data.list;
      pageData.page = data.pageNum;
      pageData.size = data.pageSize;
      pageData.total = data.total;
    }
  } catch (error) {
    console.error('获取数据失败:', error);
  } finally {
    loading.value = false;
  }
}

// table 字段排序
function fieldFilter({ prop, order }) {
  table.order = order == 'ascending' ? 'asc' : 'desc';
  table.sort = prop;
  getData();
}

// 新建角色
function addRow() {
  useModal(
    RoleEdit,
    { title: '新建角色', type: 'add' },
    {
      confirm: () => {
        getData();
      },
    },
  );
}

// 编辑角色
function editRow(row) {
  useModal(
    RoleEdit,
    {
      title: '编辑角色',
      type: 'edit',
      id: row.id,
    },
    {
      confirm: () => {
        getData();
      },
    },
  );
}

// 删除api 单独批量可以直接调用
async function deleteApi(id) {
  await admin.auth.role.delete({ id });
  getData();
}

onMounted(() => {
  getData();
});
</script>

<style lang="scss" scoped>
.role-view {
  .el-header {
    height: auto;
  }

  .el-main {
    .sa-table-wrap {
      height: 100%;
    }
  }
}
</style>
