<template>
    <a-form :model="searchFormData" labelAlign="left">
        <a-row :gutter="24">
            <a-col :span="6">
                <a-form-item label="类型" name="categoryType">
                    <a-select v-model:value="searchFormData.categoryType" allowClear>
                        <a-select-option v-for="item in dicsStore.dictOptions.PROJECT_TYPE" :key="item.dval"
                            :value="item.dval">{{
                                item.introduction }}</a-select-option>
                    </a-select>
                </a-form-item>
            </a-col>
            <!-- 所在区域 -->
            <a-col :span="6">
                <a-form-item label="项目分类" name="categoryId">
                    <a-select v-model:value="searchFormData.categoryId" allowClear>
                        <a-select-option v-for="item in categoryList" :key="item.id" :value="item.name">{{
                            item.name }}</a-select-option>
                    </a-select>
                </a-form-item>
            </a-col>
            <a-col :span="6">
                <a-form-item label="项目名称" name="name">
                    <a-input v-model:value="searchFormData.name"></a-input>
                </a-form-item>
            </a-col>
            <!-- 操作按钮 -->
            <a-col class="align-left" :span="6">
                <a-space>
                    <a-button @click="handleResetSearch">{{ $t('button.reset') }}</a-button>
                    <a-button ghost type="primary" @click="handleSearch">
                        {{ $t('button.search') }}
                    </a-button>
                </a-space>
            </a-col>
        </a-row>
    </a-form>
    <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 === 'price'">
                    <span>{{ record.price + record.frequencyUnit }}</span>
                </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>
    <edit-dialog ref="editDialogRef" @ok="onOk" :categoryList="categoryList"></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 './projectEditDialog.vue'
import { useDicsStore } from '@/store'
defineOptions({
    name: 'serverProject',
})
const dicsStore = useDicsStore()
const categoryList=ref([])
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: 'name', key: 'name', align: 'center', },
    { title: '价格', dataIndex: 'price', key: 'price', align: 'center', },
    { title: '简介', dataIndex: 'content', key: 'content', align: 'center', },
   
    { 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()
getCategoriesAll()
async function getCategoriesAll() {
    try {
        console.log(111)
        const { success, data, total } = await apis.projectType
            .getProjectListAll({
                pageSize:100, current:1,
            })
            .catch(() => {
                throw new Error()
            })
        if (config('http.code.success') === success) {
            categoryList.value = data.map(item=>({id:item.id,name:item.name}))
           
        }
    } catch (error) {

    }

}
async function getPageList() {
    try {
        const { pageSize, current } = paginationState
        const { success, data, total } = await apis.serviceProject
            .getProjectList({
                pageSize, 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.serviceProject.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>
