<template>
  <GiPageLayout>
    <GiTable
      row-key="postId"
      :data="dataList"
      :columns="tableColumns"
      :loading="loading"
      :scroll="{ x: '100%', y: '100%', minWidth: 1200 }"
      :pagination="pagination"
      :disabled-tools="['size']"
      @refresh="search"
    >
      <template #top>
        <GiForm v-model="queryForm" search :columns="queryFormColumns" size="medium" @search="search" @reset="reset"></GiForm>
      </template>
      <template #toolbar-left>
        <a-button v-permission="['system:post:create']" type="primary" @click="onAdd">
          <template #icon><icon-plus /></template>
          <template #default>新增</template>
        </a-button>
      </template>
      <template #status="{ record }">
        <a-tag :color="Number(record.status) === 1 ? 'green' : 'red'" size="small">
          {{ Number(record.status) === 1 ? '正常' : '停用' }}
        </a-tag>
      </template>
      <template #action="{ record }">
        <a-space>
          <a-link v-permission="['system:post:update']" title="修改" @click="onUpdate(record)">修改</a-link>
          <a-link
            v-permission="['system:post:delete']"
            status="danger"
            title="删除"
            @click="onDelete(record)"
          >
            删除
          </a-link>
        </a-space>
      </template>
    </GiTable>

    <PostAddModal ref="PostAddModalRef" @save-success="search" />
    <PostDetailDrawer ref="PostDetailDrawerRef" />
  </GiPageLayout>
</template>

<script setup lang="ts">
import type { TableColumnData, TableInstance } from '@arco-design/web-vue'
import PostAddModal from './PostAddModal.vue'
import PostDetailDrawer from './PostDetailDrawer.vue'
import { deletePost, pagePost,listPost } from '@/apis/system/post'
import type { PostVO, PostPageQuery } from '@/apis/system/type'
import { useResetReactive, useTable } from '@/hooks'
import { isMobile } from '@/utils'
import has from '@/utils/has'
import type { ColumnItem } from '@/components/GiForm'

defineOptions({ name: 'SystemPost' })

const [queryForm, resetForm] = useResetReactive({
  postName: undefined,
  status: undefined,
})

const queryFormColumns: ColumnItem[] = reactive([
  {
    type: 'input',
    label: '岗位名称',
    field: 'postName',
    span: { xs: 24, sm: 12, xxl: 8 },
    props: {
      placeholder: '请输入岗位名称',
    },
  },
  {
    type: 'select',
    label: '状态',
    field: 'status',
    span: { xs: 24, sm: 12, xxl: 8 },
    props: {
      options: [
        { label: '正常', value: 1 },
        { label: '停用', value: 0 }
      ],
      placeholder: '请选择状态',
    },
  },
])

const {
  tableData: dataList,
  loading,
  pagination,
  search,
  handleDelete,
} = useTable((params) => listPost({ 
  ...queryForm, 
}), { immediate: true })

const tableColumns = ref<TableColumnData[]>([
  {
    title: '序号',
    width: 66,
    align: 'center',
    render: ({ rowIndex }) => h('span', {}, rowIndex + 1 + (pagination.current - 1) * pagination.pageSize),
    fixed: !isMobile() ? 'left' : undefined,
  },
  { 
    title: '岗位名称', 
    dataIndex: 'postName', 
    minWidth: 140, 
    ellipsis: true, 
    tooltip: true,
    fixed: !isMobile() ? 'left' : undefined,
  },
  { 
    title: '岗位排序', 
    dataIndex: 'postSort', 
    minWidth: 100, 
    ellipsis: true, 
    tooltip: true 
  },
  { 
    title: '状态', 
    dataIndex: 'status', 
    slotName: 'status', 
    align: 'center', 
    width: 100 
  },
  { 
    title: '备注', 
    dataIndex: 'remark', 
    minWidth: 180, 
    ellipsis: true, 
    tooltip: true 
  },
  { 
    title: '创建时间', 
    dataIndex: 'createTime', 
    minWidth: 180, 
    ellipsis: true, 
    tooltip: true,
    sortable: {
      sortDirections: ['ascend', 'descend'],
    }
  },
  {
    title: '操作',
    dataIndex: 'action',
    slotName: 'action',
    width: 160,
    fixed: !isMobile() ? 'right' : undefined,
    show: has.hasPermOr(['system:post:update', 'system:post:delete']),
  },
])

// 重置
const reset = () => {
  resetForm()
  search()
}

// 删除
const onDelete = (record: PostVO) => {
  return handleDelete(() => deletePost(record.postId), {
    content: `确定删除岗位「${record.postName}」吗？`,
    showModal: true,
  })
}

const PostAddModalRef = ref<InstanceType<typeof PostAddModal>>()
const PostDetailDrawerRef = ref<InstanceType<typeof PostDetailDrawer>>()

// 新增
const onAdd = () => {
  PostAddModalRef.value?.onAdd()
}

// 修改
const onUpdate = (record: PostVO) => {
  PostAddModalRef.value?.onUpdate(record.postId)
}

// 详情
const onDetail = (record: PostVO) => {
  PostDetailDrawerRef.value?.onDetail(record.postId)
}
</script>

<style scoped lang="scss"></style> 