<template>
    <x-search-bar class="mb-8-2">
        <template #default="{ gutter, colSpan }">
            <a-form :model="searchFormData" layout="inline">
                <a-row :gutter="gutter">
                    <a-col v-bind="colSpan">
                        <a-form-item label="所属区域" name="areaId">
                            <a-select v-model:value="searchFormData.areaId" allowClear>
                                <a-select-option :value="1">南通</a-select-option>
                                <a-select-option :value="2">盐城</a-select-option>
                            </a-select>
                        </a-form-item>
                    </a-col>

                    <a-col v-bind="colSpan">
                        <a-form-item label="客户姓名" name="customerName">
                            <a-input placeholder="请输入客户姓名" v-model:value="searchFormData.customerName"></a-input>
                        </a-form-item>
                    </a-col>
                    <a-col v-bind="colSpan">
                        <a-form-item label="客户手机号" name="customerPhone">
                            <a-input placeholder="请输入客户手机号" v-model:value="searchFormData.customerPhone"></a-input>
                        </a-form-item>
                    </a-col>
                    <a-col v-bind="colSpan">
                        <a-form-item label="客户身份证号" name="idCard">
                            <a-input placeholder="请输入客户身份证号" v-model:value="searchFormData.idCard"></a-input>
                        </a-form-item>
                    </a-col>

                    <a-col class="align-right" v-bind="colSpan">
                        <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>
        </template>
    </x-search-bar>
    <a-row :gutter="8" :wrap="false">
        <a-col flex="auto">
            <a-card type="flex">
               <x-action-bar class="mb-8-2">
                    <a-button type="primary" @click="$refs.editDialogRef.handleCreate()">
                        <template #icon>
                            <plus-outlined></plus-outlined>
                        </template>
                         导入记录
                    </a-button>
                </x-action-bar>
                <a-table :columns="columns" :data-source="listData" bordered="true" :loading="loading"
                    :pagination="paginationState" :scroll="{ x: 'max-content' }" @change="onTableChange">
                    <template #bodyCell="{ column, record }">
                        <template v-if="column.dataIndex === 'areaId'">
                            <span>{{ record.areaId === 1 ? '南通' : '盐城' }}</span>
                        </template>
                        <template v-if="column.dataIndex === 'status'">
                            <span>{{ record.status == "uncheck" ? '未核销' : '已核销' }}</span>
                        </template>
                        <template v-if="'action' === column.key">
                            <!-- <x-action-button @click="$refs.editDialogRef.handleEdit(record)">
                                <a-tooltip>
                                    <template #title> {{ $t('pages.system.user.edit') }}</template>
                <edit-outlined /> </a-tooltip></x-action-button> -->
                            <x-action-button @click="handleDelete(record)">
                                <a-tooltip>
                                    <template #title>{{ $t('pages.system.delete') }}</template>
                                    <delete-outlined style="color: #ff4d4f" /> </a-tooltip></x-action-button>
                        </template>
                    </template>
                </a-table>
            </a-card>
        </a-col>
    </a-row>

    <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 { formatUtcDateTime } from '@/utils/util'
import { config } from '@/config'
import dayjs from 'dayjs'
import { usePagination } from '@/hooks'
import { customersEnum, areaEnum } from "@/enums/useEnum"
import EditDialog from './components/EditDialog.vue'
import { PlusOutlined, EditOutlined, DeleteOutlined } from '@ant-design/icons-vue'
import { useI18n } from 'vue-i18n'
import { render } from 'less'
defineOptions({
    name: 'owner',
})
const { t } = useI18n() // 解构出t方法
const columns = [
    { title: '所属城市', dataIndex: 'areaId', width: 120, align: 'center' },
    { title: '楼盘名称', dataIndex: 'productName', width: 120, align: 'center' },
    { title: '楼盘期数', dataIndex: 'productStages', width: 120, align: 'center' },
    { title: '房源信息', dataIndex: 'roomName', width: 120, align: 'center' },
    { title: '客户姓名', dataIndex: 'customerName', width: 120 },
    { title: '客户手机号', dataIndex: 'customerPhone', width: 120 },
    { title: '客户身份证号', dataIndex: 'idCard', width: 300, align: 'center' },
    { title: '来源信息', dataIndex: 'formInfo', width: 300, align: 'center' },
    { title: '积分', dataIndex: 'point', width: 60, align: 'center' },
    { title: '状态', dataIndex: 'status', width: 60, align: 'center' },
    { title: t('button.action'), key: 'action', fixed: 'right', width: 100, align: 'center' },
]

const { listData, loading, showLoading, hideLoading, paginationState, resetPagination, searchFormData } =
    usePagination()

const editDialogRef = ref()
getPageList()
/**
 * 获取表格数据
 * @returns {Promise<void>}
 */
async function getPageList() {
    try {
        showLoading()
        const { pageSize, current } = paginationState
        const { success, data, total } = await apis.customer
            .getHouseOwners({
                pageSize,
                current: current,
                ...searchFormData.value,
            })
            .catch(() => {
                throw new Error()
            })
        hideLoading()
        if (config('http.code.success') === success) {
            listData.value = data
            paginationState.total = total
        }
    } catch (error) {
        hideLoading()
    }
}

/**
 * 删除
 */
function handleDelete({ id }) {
    console.log(id);
    Modal.confirm({
        title: t('pages.system.user.delTip'),
        content: t('button.confirm'),
        okText: t('button.confirm'),
        onOk: async () => {
            try {
                const { success } = await apis.customer.delHouseOwner(id);
                if (success === config('http.code.success')) {
                    message.success('删除成功');
                    await getPageList();
                } else {
                    message.error('删除失败');
                    throw new Error('Delete API returned non-success status');
                }
            } catch (error) {
                message.error('删除失败');
                throw error; // 阻止 Modal 关闭，并避免 unhandled rejection
            }
        },
    });
}

/**
 * 分页
 */
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>
