<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="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="signDate">
                             <a-month-picker v-model:value="searchFormData.signDate" @change="handleDateChange"/>
                        </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">
                <div class="exportExcel" style="margin-bottom: 10px;">
                    <a-button type="primary">
                        <a :href='config("http.apiBasic") + `/api/v1/sign-in-dailies/excl?signYear=${signYear}&signMonth=${signMonth}`'>导出Excel</a>
                    </a-button>
                </div>
                <a-table :columns="columns" :data-source="listData" bordered="true" :loading="loading"
                    :pagination="paginationState" :scroll="{ x: 1000 }" @change="onTableChange">
                    <template #bodyCell="{ column, record }">
                        <template v-if="column.dataIndex === 'img'">
                            <a-image :width="60" :src="config('http.apiBasic') + record.img || $imageErr.imgErr" />
                        </template>
                        <template v-if="column.dataIndex === 'rewardName'">
                            <span>{{ record.rewardName }}</span>
                        </template>
                        <template v-if="column.dataIndex === 'customerName'">
                            <span>{{ record.customerName }}</span>
                        </template>
                        <template v-if="column.dataIndex === 'customerPhone'">
                            <span>{{ record.customerPhone }}</span>
                        </template>

                        <template v-if="column.dataIndex === 'rewardType'">
                            <span>{{ signInEnum.getName(record.rewardType) }}</span>
                        </template>
                        <template v-if="column.dataIndex === 'signMonth'">
                            <span>{{ record.signYear + '年' + record.signMonth + '月' }}</span>
                        </template>
                        <template v-if="'status' === column.dataIndex">
                            <a-tag v-if="record.status === 'checked'" :color="'orange'">待核销</a-tag>
                            <a-tag v-if="record.status === 'success'" :color="'green'">已核销</a-tag>
                        </template>
                        <template v-if="'action' === column.key">
                            <x-action-button v-if="record.status === 'checked'" @click="handleDelete(record)">
                                <a-tooltip>
                                    <template #title>核销</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 { config } from '@/config'
import { usePagination } from '@/hooks'
// import EditDialog from './components/EditDialog.vue'
import { PlusOutlined, EditOutlined, DeleteOutlined } from '@ant-design/icons-vue'
import { useI18n } from 'vue-i18n'
import { signInEnum } from "@/enums/useEnum"
const signYear=ref('')
const signMonth=ref('')
defineOptions({
    name: 'signInSet',
})
const { t } = useI18n() // 解构出t方法
const columns = [
    { title: '奖品名称', dataIndex: 'rewardName' },
    { title: '奖品类型', dataIndex: 'rewardType', width: 120, align: 'center' },
    { title: '客户名称', dataIndex: 'customerName', align: 'center' },
    { title: '客户电话', dataIndex: 'customerPhone', width: 130, align: 'center' },
    { title: '所属日期', dataIndex: 'signMonth', align: 'center' },
    { title: '状态', dataIndex: 'status', 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() {
    console.log(searchFormData.value)
    try {
        showLoading()
        const { pageSize, current } = paginationState
        const { success, data, total } = await apis.signIn
            .getSignInList({
                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 handleDateChange(date, dateString) {
    if (dateString) {
        const [year, month] = dateString.split('-')
        signYear.value = Number(year)
        signMonth.value = Number(month)
        searchFormData.value.signYear = signYear.value
        searchFormData.value.signMonth = signMonth.value
    } else {
        signYear.value = ''
        signMonth.value = ''
        searchFormData.value.signYear = ''
        searchFormData.value.signMonth = ''
    }
}
/**
 * 删除
 */
function handleDelete(params) {
    Modal.confirm({
        title: '即将核销,是否继续？',
        content: t('button.confirm'),
        okText: t('button.confirm'),
        onOk: () => {
            return new Promise((resolve, reject) => {
                ; (async () => {
                    try {
                        const form = {
                            ...params,
                            status: 'success'
                        }
                        const { success } = await apis.signIn.updateSingInItem(form.id, form).catch(() => {
                            throw new Error()
                        })
                        if (config('http.code.success') === success) {
                            resolve()
                            message.success('核销成功')
                            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>
