<template>
  <ATablePage
    ref="tablePage"
    v-bind="tablePageProps">
    <template #simpleFormItem="formData">
      <ElFormItem>
        <ElInput
          v-model="formData.roleCode"
          placeholder="角色代码"/>
      </ElFormItem>
      <ElFormItem>
        <ElInput
          v-model="formData.roleName"
          placeholder="角色名称"/>
      </ElFormItem>
    </template>
    <template #columns>
      <ElTableColumn label="角色代码" prop="roleCode"/>
      <ElTableColumn label="角色名称" prop="roleName"/>
      <ElTableColumn label="备注" prop="memo"/>
    </template>
    <RoleForm ref="roleForm" :research="research"/>
    <BindRes ref="bindRes"/>
  </ATablePage>
</template>
<script lang="ts" setup>
import RoleApi from '@/pages/sys/role/role-api.ts'
import RoleForm from '@/pages/sys/role/RoleForm.vue'
import { ElMessage } from 'element-plus'
import BindRes from '@/pages/sys/role/BindRes.vue'
import ATablePage, {
  type ATablePageInstance,
  buildTablePageProps,
} from '@/components/a-page/a-table-page/ATablePage.tsx'

const roleFormIns = useTemplateRef<InstanceType<typeof RoleForm>>('roleForm')
const bindResIns = useTemplateRef<InstanceType<typeof BindRes>>('bindRes')
const tablePageIns = useTemplateRef<ATablePageInstance>('tablePage')
const tablePageProps = buildTablePageProps<RoleTypes.SearchRoleParam, RoleTypes.SearchRoleResult>({
  pageLayout: {
    enableHighForm: false,
  },
  searchForm: {
    simpleForm: {colCount: 2},
    paging: RoleApi.paging,
  },
  toolBar: {
    leftTools: [
      {
        icon: 'Plus',
        label: '新建',
        action() {
          roleFormIns.value?.open()
        },
      },
    ],
  },
  table: {
    actionColumn: {
      tableActions: [

        {
          tooltip: '编辑',
          icon: 'Edit',
          type: 'success',
          show({row}) {
            return row.id != '1'
          },
          action({row}: { row: RoleTypes.SearchRoleResult }) {
            if (row.id == '1') {
              ElMessage.error('不能操作管理员')
              return
            }
            roleFormIns.value?.open(row)
          },
        },
        {
          tooltip: '资源',
          type: 'warning',
          icon: 'caidan',
          action({row}: { row: RoleTypes.SearchRoleResult }) {
            bindResIns.value?.open(row.id!)
          },
        },
        {
          icon: 'Delete',
          loading: false,
          type: 'danger',
          tooltip: '删除',
          show({row}) {
            return row.id != '1'
          },
          confirm: {
            title: '是否删除当前数据',
          },
          action({row}: { row: RoleTypes.SearchRoleResult }) {
            if (row.id == '1') {
              ElMessage.error('不能操作管理员')
              return Promise.resolve(false)
            }
            return RoleApi.del([ row.id! ])
              .then(() => {
                ElMessage.success('删除成功')
                return true
              })
          },
        },
      ],
    },
  },
})

function research() {
  tablePageIns.value?.doSearch()
}
</script>
