<template>
    <x-search-bar class="mb-4">
        <template #default="{ gutter, colSpan }">
            <a-form :label-col="{ style: { width: '130px' } }" :model="searchFormData" layout="inline">
                <!-- 基础查询字段 -->
                <a-row :gutter="gutter">
                    <a-col v-bind="colSpan">
                        <a-form-item label="所在区域" name="area">
                            <AreaCascader v-model:value="searchFormData.areaCodes" @change="onAreaChange" />
                        </a-form-item>
                    </a-col>

                    <a-col v-bind="colSpan">
                        <a-form-item label="服务满意度" name="node">
                            <a-select v-model:value="searchFormData.node" placeholder="请选择满意度">
                                <a-select-option v-for="item in dicsStore.dictOptions.Satisfaction_Type"
                                    :key="item.dval" :value="item.dval">
                                    {{ item.introduction }}
                                </a-select-option>
                            </a-select>
                        </a-form-item>
                    </a-col>

                    <a-col v-bind="colSpan">
                        <a-form-item label="服务人员满意度" name="node">
                            <a-select v-model:value="searchFormData.node" placeholder="请选择服务人员满意度">
                                <a-select-option v-for="item in dicsStore.dictOptions.Satisfaction_Type"
                                    :key="item.dval" :value="item.dval">
                                    {{ item.introduction }}
                                </a-select-option>
                            </a-select>
                        </a-form-item>
                    </a-col>
                </a-row>

                <!-- 高级查询折叠面板 -->
                <a-collapse v-model:activeKey="advancedSearchVisible" ghost>
                    <a-collapse-panel key="1" :showArrow="false" style="padding: 0; border: none;">
                        <template #header></template>
                        <a-row :gutter="gutter">

                            <a-form-item label="回访时间" name="plannedDate" style="white-space: nowrap;">
                                <a-range-picker v-model:value="searchFormData.plannedDate" style="width: 220px;" />
                            </a-form-item>

                            <a-col v-bind="colSpan">
                                <a-form-item label="回访类型" name="node">
                                    <a-select v-model:value="searchFormData.node" placeholder="请选择所在节点">
                                        <a-select-option v-for="item in dicsStore.dictOptions.Visit_Type"
                                            :key="item.dval" :value="item.dval">
                                            {{ item.introduction }}
                                        </a-select-option>
                                    </a-select>
                                </a-form-item>
                            </a-col>

                            <a-col v-bind="colSpan">
                                <a-form-item label="回访人" name="serviceProject">
                                    <a-input v-model:value="searchFormData.serviceProject" placeholder="请输入服务项目" />
                                </a-form-item>
                            </a-col>
                        </a-row>
                    </a-collapse-panel>
                </a-collapse>

                <!-- 操作按钮 -->
                <a-row :gutter="gutter" style="margin-top: 16px;">
                    <a-col :span="24" style="text-align: right;">
                        <a-space>
                            <a-button @click="handleResetSearch">{{ $t('button.reset') }}</a-button>

                            <a-button ghost type="primary" @click="handleSearch">
                                {{ $t('button.search') }}
                            </a-button>
                            <a-button type="link" @click="toggleAdvancedSearch"
                                style="color: #1890ff; border: none; padding: 0; font-size: 14px;">
                                {{ advancedSearchVisible.length ? '收起' : '高级查询' }}
                                <template #icon>
                                    <down-outlined v-if="advancedSearchVisible.length" />
                                    <up-outlined v-else />
                                </template>
                            </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 title="服务人员列表">
                <div style="margin-bottom: 20px;">
                    <a-space>
                        <!-- <a-button type="primary" @click="$refs.editDialogRef.handleCreate(record)">新建</a-button> -->
                        <a-button type="primary">导出</a-button>
                        <a-button type="dashed">导出记录</a-button>
                    </a-space>
                </div>
                <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>
                </a-table>
            </a-card>
        </a-col>
    </a-row>
    <!-- <edit-dialog ref="editDialogRef" @ok="onOk"></edit-dialog>
    <detail ref="detailRef"></detail> -->
</template>

<script setup>
import { DownOutlined, UpOutlined } from '@ant-design/icons-vue'
import AreaCascader from '@/components/AreaCascader/index.vue'
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 { useDicsStore } from '@/store'


import dayjs from 'dayjs'
import NodeTree from '@/components/NodeTree/index.vue'
defineOptions({
    name: 'allocation',
})
const dicsStore = useDicsStore()

const columns = [
    {
        title: '序号',
        dataIndex: 'serialNumber',
        key: 'serialNumber',
        align: 'center',
        width: 80,
    },
    {
        title: '姓名',
        dataIndex: 'name',
        key: 'name',
        align: 'center',
        width: 100,
    },
    {
        title: '性别',
        dataIndex: 'gender',
        key: 'gender',
        align: 'center',
        width: 80,
    },
    {
        title: '电话',
        dataIndex: 'phone',
        key: 'phone',
        align: 'center',
        width: 80,
    },
    {
        title: '身份证号',
        dataIndex: 'idCard',
        key: 'idCard',
        align: 'center',
        width: 180,
    },
    {
        title: '护理人员类型',
        dataIndex: 'serviceType',
        key: 'serviceType',
        align: 'center',
        width: 120,
    },
    {
        title: '用工形式',
        dataIndex: 'workType',
        key: 'workType',
        align: 'center',
        width: 120,
    },
    {
        title: '状态',
        dataIndex: 'status',
        key: 'status',
        align: 'center',
        width: 120,
    },
    // --- 联系方式 ---
    {
        title: '护理人员类型',
        dataIndex: 'serviceType',
        key: 'serviceType',
        align: 'center',
        width: 130,
    },
    {
        title: '所属服务组织',
        dataIndex: 'stationName',
        key: 'stationName',
        align: 'center',
        width: 120,
    },
    // {
    //     title: '组织所在区域',
    //     dataIndex: 'areaLabels',
    //     key: 'areaLabels',
    //     align: 'center',
    //     width: 120,
    // },
];
const { t } = useI18n() // 解构出t方法
const { listData, loading, showLoading, hideLoading, paginationState, resetPagination, searchFormData } = usePagination()
const editDialogRef = ref()
const detailRef = ref()
// 控制高级查询面板是否展开
const advancedSearchVisible = ref([]) // 默认收起

getPageList()

async function getPageList() {
    console.log("===1")
    try {
        const { pageSize, current } = paginationState
        const { success, data, total } = await apis.workOrder
            .getBackRecordList({
                pageSize,
                current: current,
                ...searchFormData.value,
            })

            listData.value = data
            paginationState.total = total
            console.log("===",data)   
            .catch(() => {
                throw new Error()
            })

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

    }
}
/**核销 */
const checkHandler = (record) => {
    Modal.confirm({
        title: '即将核销是否继续',
        content: t('button.confirm'),
        okText: t('button.confirm'),
        onOk: async () => {
            const params = {
                ...record,
                status: 'success'
            }
            const { success } = await apis.productOrder.updateItem(params.id, params).catch(() => {
                // throw new Error()
            })
            if (config('http.code.success') === success) {
                // resolve()
                message.success('核销成功')
                await getPageList()
            }
        },
    })
}
/**
 * 删除
 */
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.productOrder.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()
}

// 切换高级查询
function toggleAdvancedSearch() {
    advancedSearchVisible.value = advancedSearchVisible.value.length ? [] : ['1']
}

</script>

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