<script lang="ts" setup name="roleIndex">
import { ElMessage, ElMessageBox } from 'element-plus'
import Role from './components/Role.vue'
import { deleteRoleById, getRoleByList } from '@/api/system/roleApi'

const state = reactive({
  loading: false,
  // 表格是否自适应高度
  tableAutoHeight: false,
  // 搜索
  search: {
    keywords: '',
  },
  // 列表数据
  dataList: [],
})

const roleRef = ref()

const getDataList = async () => {
  state.loading = true
  const { data } = await getRoleByList({})
  state.dataList = data
  state.loading = false
}

onMounted(() => {
  getDataList()
})

function onCreate() {}

function onRoleAdd() {
  roleRef.value.showAddModel()
}

function onRoleEdit(row: any) {
  const roleId = row.roleId
  roleRef.value.showEditModel(roleId)
}

function onRoleDel(row: any) {
  ElMessageBox.confirm(`确认删除「${row.roleName}」吗？`, '确认信息').then(async () => {
    const { msg } = await deleteRoleById(row.roleId)
    ElMessage.success(msg)
    await getDataList()
  }).catch(() => {})
}
</script>

<template>
  <div :class="{ 'absolute-container': state.tableAutoHeight }">
    <page-header title="角色管理">
      <template #content>
        <div>
          系统支持基于角色的权限设定
        </div>
      </template>

      <el-button-group>
        <el-button type="primary" size="large" @click="onRoleAdd">
          <template #icon>
            <el-icon>
              <svg-icon name="ep:plus" />
            </el-icon>
          </template>
          新增
        </el-button>
      </el-button-group>
    </page-header>

    <page-main>
      <el-table v-loading="state.loading" class="list-table" :data="state.dataList" border stripe highlight-current-row height="100%">
        <el-table-column type="index" label="序号" width="60" header-align="center" align="center" />

        <el-table-column prop="roleName" label="名称" header-align="center" align="center" width="200">
          <template #default="scope">
            <span>{{ scope.row.roleName }}</span>
          </template>
        </el-table-column>

        <el-table-column prop="roleDesc" label="说明">
          <template #default="scope">
            <span>{{ scope.row.roleDesc }}</span>
          </template>
        </el-table-column>

        <el-table-column label="操作" width="150" align="center" fixed="right">
          <template #default="scope">
            <el-button type="primary" size="small" plain @click="onRoleEdit(scope.row)">
              编辑
            </el-button>
            <el-button v-if="!scope.row.isFixed" type="danger" size="small" plain @click="onRoleDel(scope.row)">
              删除
            </el-button>
          </template>
        </el-table-column>
      </el-table>
    </page-main>
    <Role ref="roleRef" @success="getDataList" />
  </div>
</template>

<style lang="scss" scoped>
.absolute-container {
  position: absolute;
  width: 100%;
  height: 100%;
  display: flex;
  flex-direction: column;

  .page-header {
    margin-bottom: 0;
  }

  .page-main {
    // 让 page-main 的高度自适应
    flex: 1;
    overflow: auto;
    display: flex;
    flex-direction: column;

    .search-container {
      margin-bottom: 0;
    }
  }
}
</style>
