<template>
    <a-row :gutter="16">
        <a-col :span="24">
            <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="name">
                                    <a-input :placeholder="'请输入用户姓名'" v-model:value="searchFormData.name"></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 v-bind="colSpan">
                                <a-form-item label="主叫号码" name="callerNumber">
                                    <a-input :placeholder="'请输入主叫号码'"
                                        v-model:value="searchFormData.callerNumber"></a-input>
                                </a-form-item>
                            </a-col>
                            <a-col v-bind="colSpan">
                                <a-form-item label="被叫号码" name="calleeNumber">
                                    <a-input :placeholder="'请输入被叫号码'"
                                        v-model:value="searchFormData.calleeNumber"></a-input>
                                </a-form-item>
                            </a-col>
                            <a-col v-bind="colSpan">
                                <a-form-item label="电话方向" name="callDirection">
                                    <a-select v-model:value="searchFormData.callDirection" allowClear>
                                        <a-select-option v-for="item in dicsStore.dictOptions.CALL_DIRECTION"
                                            :key="item.dval" :value="item.dval">{{
                                                item.introduction }}</a-select-option>
                                    </a-select>
                                </a-form-item>
                            </a-col>
                            <a-col class="align-left" 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-card>
                <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>

    <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 './compontent/EditDialog.vue'
import { useDicsStore } from '@/store'
defineOptions({
    name: 'serverProjectManage',
})
const dicsStore = useDicsStore()
const activeKey = ref('1')
const columns = [
    {
        title: '话务员ID',
        dataIndex: 'name',
        key: 'name',
        align: 'center',
        width: 100,
    },
    {
        title: '坐席姓名',
        dataIndex: 'name',
        key: 'name',
        align: 'center',
        width: 180,
    },
    {
        title: '性别',
        dataIndex: 'callerNumber',
        key: 'callerNumber',
        align: 'center',

    },
    {
        title: '身份证号',
        dataIndex: 'calledNumber',
        key: 'calledNumber',
        align: 'center',
    },
    {
        title: '联系电话',
        dataIndex: 'idCardNumber',
        key: 'idCardNumber',
        align: 'center',
    },
    {
        title: '话务工号',
        dataIndex: 'region',
        key: 'region',
        align: 'center',
    },
    {
        title: '常用分机',
        dataIndex: 'callSource',
        key: 'callSource',
        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()

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>
