<template>
    <a-card>
        <a-tabs v-model:activeKey="activeKey">
            <a-tab-pane key="1" tab="项目分类">
                <a-row :gutter="8" :wrap="false">
                    <a-col flex="auto">
                        <a-card title="服务项目分类列表">
                            <template #extra>
                                <a-space>
                                    <a-button type="primary"
                                        @click="$refs.editDialogRef.handleCreate(record)">新建</a-button>
                                </a-space>
                            </template>
                            <a-table :columns="columns" :data-source="listData" bordered="true" :loading="loading"
                                :pagination="paginationState" :scroll="{ x: 'max-content' }" @change="onTableChange">
                                <template #bodyCell="{ index, column, record }">
                                    <template v-if="column.key === 'serialNumber'">
                                        <span>{{ index + 1 }}</span>
                                    </template>
                                    <template v-if="column.key === 'categoryType'">
                                        <span>{{ dicsStore.getDictLabel('PROJECT_TYPE', record.categoryType) }}</span>
                                    </template>
                                    <template v-if="column.key === 'status'">
                                        <a-tag v-if="record.status === 'enabled'" color="green">启用</a-tag>
                                        <a-tag v-else>停用</a-tag>
                                    </template>
                                    <template v-if="'action' === column.key">
                                        <x-action-button @click="$refs.editDialogRef.handleEdit(record)">
                                            <span>编辑</span>
                                        </x-action-button>
                                        <x-action-button @click="handleDelete(record)">
                                            <span style="color: #ff4d4f;">删除</span>
                                        </x-action-button>
                                    </template>
                                </template>
                            </a-table>
                        </a-card>
                    </a-col>
                </a-row>
            </a-tab-pane>
            <a-tab-pane key="2" tab="服务项目">
               <ServerProject />
            </a-tab-pane>
        </a-tabs>

    </a-card>

    <edit-dialog ref="editDialogRef" @ok="onOk"></edit-dialog>
</template>

<script setup>
import { message, Modal } from 'ant-design-vue'
import { ref } from 'vue'
import apis from '@/apis'
import { config } from '@/config'
import { usePagination } from '@/hooks'
import { useI18n } from 'vue-i18n'
import EditDialog from './components/EditDialog.vue'
import { useDicsStore } from '@/store'
import AreaCascader from '@/components/AreaCascader/index.vue'
import dayjs from 'dayjs'
import NodeTree from '@/components/NodeTree/index.vue'
import ServerProject from './components/serverProject.vue'
defineOptions({
    name: 'serverProjectManage',
})
const dicsStore = useDicsStore()
const activeKey = ref('1')
const columns = [
    {
        title: '序号',
        dataIndex: 'serialNumber',
        key: 'serialNumber',
        align: 'center',
        width: 80,
    },
    {
        title: '类型',
        dataIndex: 'categoryType',
        key: 'categoryType',
        align: 'center',
        width: 100,
    },
    {
        title: '分类名称',
        dataIndex: 'name',
        key: 'name',
        align: 'center',
        width: 180,
    },
    {
        title: '分类描述',
        dataIndex: 'remark',
        key: 'remark',
        align: 'center',

    },
    {
        title: '状态',
        dataIndex: 'status',
        key: 'status',
        align: 'center',
        width: 80,
    },

    {
        title: '操作',
        dataIndex: 'action',
        key: 'action',
        align: 'center',
        width: 120,
        fixed: 'right',
    }
];
const { t } = useI18n() // 解构出t方法
const { listData, loading, showLoading, hideLoading, paginationState, resetPagination, searchFormData } = usePagination()
const editDialogRef = ref()

getPageList()

async function getPageList() {
    try {
        const { pageSize, current } = paginationState
        const { success, data, total } = await apis.projectType
            .getProjectList({
                pageSize,
                current: current,
                ...searchFormData.value,
            })
            .catch(() => {
                throw new Error()
            })

        if (config('http.code.success') === success) {
            listData.value = data
            paginationState.total = total
        }
    } catch (error) {

    }
}

/**
 * 删除
 */
function handleDelete({ id }) {
    Modal.confirm({
        title: t('pages.system.user.delTip'),
        content: t('button.confirm'),
        okText: t('button.confirm'),
        onOk: () => {
            return new Promise((resolve, reject) => {
                ; (async () => {
                    try {
                        const { success } = await apis.projectType.delItem(id).catch(() => {
                            throw new Error()
                        })
                        if (config('http.code.success') === success) {
                            resolve()
                            message.success(t('component.message.success.delete'))
                            await getPageList()
                        }
                    } catch (error) {
                        reject()
                    }
                })()
            })
        },
    })
}

/**
 * 分页
 */
function onTableChange({ current, pageSize }) {
    paginationState.current = current
    paginationState.pageSize = pageSize
    getPageList()
}

/**
 * 搜索
 */
function handleSearch() {
    resetPagination()
    getPageList()
}
/**
 * 重置
 */
function handleResetSearch() {
    searchFormData.value = {}
    resetPagination()
    getPageList()
}
/**
 * 编辑完成
 */
async function onOk() {
    await getPageList()
}

</script>

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