<template>
       <x-search-bar class="mb-8-2">
        <template #default="{ gutter, colSpan }">
            <a-form :label-col="{ style: { width: '40px' } }" :model="searchFormData" layout="inline">
                <a-row :gutter="gutter">
                    <a-col v-bind="colSpan">
                        <a-form-item label="状态" name="status">
                              <a-select v-model:value="searchFormData.applyType" 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 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">
                <a-table :columns="columns" :data-source="listData" bordered="true" :loading="loading"
                    :pagination="paginationState" @change="onTableChange">
                    <template #bodyCell="{ column, record }">
                        <template v-if="'startAt' === column.dataIndex">
                            <span>{{ dayjs(record.startAt).format('YYYY-MM-DD HH:mm:ss') + ' - ' +
                                dayjs(record.endAt).format('YYYY-MM-DD HH:mm:ss') }}</span>
                        </template>
                        <template v-if="'startTime' === column.dataIndex">
                            <span>{{ record.startTime + ' - ' + record.endTime }}</span>
                        </template>
                         <template v-if="'applyType' === column.dataIndex">
                            <span v-if="record.applyType === 1">个人申请</span>
                            <span v-if="record.applyType === 2">企业申请</span>
                        </template>
                         <template v-if="'status' === column.dataIndex">
                             <a-tag color="#87d068" v-if="record.status === 3">审核通过</a-tag>
                             <a-tag color="#666666" v-if="record.status === 2">待审核</a-tag>
                             <a-tag color="#f50" v-if="record.status === 99">审核不通过</a-tag>
                             <!-- <a-tag color="#87d068" v-if="record.status === 3">审核通过</a-tag> -->
                        </template>
                        <template v-if="'applyTheme' === column.dataIndex">
                            <span>{{ applyThemeMap[record.applyTheme] || '未知' }}</span>
                        </template>

                        <template v-if="column.dataIndex === 'applySign'">
                            <a-image :width="60"
                                :src="record.applySign ? config('http.apiBasic') + record.applySign : errImg" />
                        </template>
                        <template v-if="column.dataIndex === 'rebackApplyPdf'">
                            <a v-if="record.rebackApplyPdf" :href="config('http.apiBasic') + record.rebackApplyPdf"
                                 rel="noopener noreferrer">
                                查看承诺报表
                            </a>
                            <span v-else>无</span>
                        </template>
                        <template v-if="column.dataIndex === 'rebackCovenantPdf'">
                            <a v-if="record.rebackCovenantPdf"
                                :href="config('http.apiBasic') + record.rebackCovenantPdf" 
                                rel="noopener noreferrer">
                                查看申请报表
                            </a>
                            <span v-else>无</span>
                        </template>
                        <template v-if="'action' === column.key">
                            <x-action-button @click="auditHandleEdit(record)" v-if="record.status === 2">
                                <a-tooltip>
                                    <template #title>审核</template>
                                    <i class='iconfont icon-shenhe' style='font-size:14px;color:#faad14'></i>
                                </a-tooltip>
                            </x-action-button>
                        </template>
                    </template>
                </a-table>
            </a-card>
        </a-col>
    </a-row>
    <a-modal :open="open" :title="'审核'" :width="640" title="审核" ok-text="确认" cancel-text="取消" @ok="handleAuditEdit"
        @cancel="open = false">
        <a-card class="mb-8-2">
            <a-form-item :label="'审核'" name="">
                <a-radio-group v-model:value="auditStatus"
                    :options="[{ value: 3, label: '通过' }, { value: 99, label: '驳回' }]"></a-radio-group>
            </a-form-item>
            <a-form-item :label="'备注'" name="">
                <a-textarea v-model:value="remark"></a-textarea>
            </a-form-item>
        </a-card>
    </a-modal>
</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, DownloadOutlined } from '@ant-design/icons-vue'
import { useI18n } from 'vue-i18n'
import { orderStatus } from '@/enums/index.js'
import { status } from 'nprogress'
import dayjs from 'dayjs'
defineOptions({
    name: 'mettingYuYue',
})
const { t } = useI18n() // 解构出t方法
const columns = [
    { title: '会议室', dataIndex: 'roomName' },
    { title: '申请状态', dataIndex: 'status' },
    // { title: '负责人', dataIndex: 'concatName' },
    // { title: '负责人手机号', dataIndex: 'concatPhone' },
    { title: '申请类型', dataIndex: 'applyType', width: 100 },
    { title: '申请主题', dataIndex: 'applyTheme', width: 100  },
    { title: '申请人', dataIndex: 'userName', width: 100  },
    // { title: '申请人手机号', dataIndex: 'userPhone' },
    // { title: '申请人身份证', dataIndex: 'userCardId', width: 120 },
    { title: '申请人地址', dataIndex: 'userAddress' },
    // { title: '公司名字', dataIndex: 'companyName' },
    { title: '申请理由', dataIndex: 'reason' },
    { title: '场次', dataIndex: 'counter' },
    { title: '人数', dataIndex: 'num' },
    { title: '预约时间', dataIndex: 'startTime' },
    // { title: '结束时间', dataIndex: 'endTime' },
    { title: '申请签名', dataIndex: 'applySign' },
    // { title: '备注', dataIndex: 'remark' },
    { title: '申请报表', dataIndex: 'rebackCovenantPdf' },
    { title: '承诺报表', dataIndex: 'rebackApplyPdf' },
    { title: t('button.action'), key: 'action', fixed: 'right', width: 80, align: 'center' },
]
const { listData, loading, showLoading, hideLoading, paginationState, resetPagination, searchFormData } = usePagination()
searchFormData.value = {
    applyType: 1,
}
const remark = ref('')
const currentInfo = ref({})
const auditStatus = ref(3)
const editDialogRef = ref()
const open = ref(false)
const currentForm = ref({})
const applyThemeMap = {
    1: '开会',
    2: '学术报告',
    3: '活动',
    4: '其他',
}
getPageList()
/**
 * 获取用户列表
 * @returns {Promise<void>}
 */
async function getPageList() {
    try {
        showLoading()
        const { pageSize, current } = paginationState
        const { success, data, total } = await apis.mettingYuYue
            .getDataList({
                applyType: searchFormData.value.applyType,
                pageSize,
                current: current,
            })
            .catch(() => {
                throw new Error()
            })
        hideLoading()
        if (config('http.code.success') === success) {
            listData.value = data.map(item => ({
                ...item,
                key: item.value
            }))
            paginationState.total = total
        }
    } catch (error) {
        hideLoading()
    }
}
const auditHandleEdit = (obj = {}) => {
    currentForm.value = obj
    open.value = true
    
}
const handleClick = async () => {
    try {
        const params = {
            ...currentInfo.value,
            status: auditStatus.value,
            remark: remark.value
        }
        const result = await apis.mettingYuYue.updateMenu(formData.value.id, params).catch(() => {
            throw new Error(error)
        })
        if (config('http.code.success') === result?.success) {
            message.success('已审核')
            open.value = false
        }
    } catch (error) {
        message.error({ content: error.message })
        hideLoading()
    }
}
/**
 * 删除
 */
const handleAuditEdit = async () => {
    try {
        const params = {
            ...currentForm.value,
            status: auditStatus.value,
            remark: remark.value,
            // num: currentForm.value.num || 0,
        }
        const result = await apis.mettingYuYue.updateMenu(currentForm.value.id, params).catch(() => {
            throw new Error()
        })
        if (config('http.code.success') === result?.success) {
            getPageList()
            open.value = false
            message.success('审核成功')
        }
    } catch (error) {
        message.error(error.message)
    }
}

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