<template>
    <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" :scroll="{ x: 1000 }">
                    <template #bodyCell="{ column, record }">
                        <template v-if="'url' === column.dataIndex">
                            <a-button type="link" @click="auditHandleEdit(record, 'img')">点击查看</a-button>
                        </template>
                        <template v-if="'status' === column.dataIndex">
                            <a-tag :color="orderStatus.getColor(record.status)">{{
                                orderStatus.getName(record.status) }}</a-tag>
                        </template>
                        <template v-if="'createdAt' === column.dataIndex">
                            {{ dayjs(record.createdAt).format('YYYY-MM-DD') }}
                        </template>
                        <template v-if="'action' === column.key">
                            <x-action-button @click="auditHandleEdit(record, 'audit')" v-if="record.status === 1">
                                <a-tooltip>
                                    <template #title> {{ '审核' }}</template>
                                    <i class='iconfont icon-shenhe' style='font-size:14px;color:#faad14'></i>
                                </a-tooltip>
                            </x-action-button>
                            <x-action-button @click="auditHandleFinish(record)" v-if="record.status === 2">
                                <a-tooltip>
                                    <template #title> {{ '完成' }}</template>
                                    <CheckCircleOutlined />
                                </a-tooltip>
                            </x-action-button>
                            <x-action-button @click="handleDelete(record)" v-if="record.status !== 98">
                                <a-tooltip>
                                    <template #title>撤销</template>
                                    <delete-outlined style="color: #ff4d4f" /> </a-tooltip></x-action-button>
                        </template>
                    </template>
                </a-table>
            </a-card>
        </a-col>
    </a-row>
    <a-modal v-model:open="open" :title="'审核'" @ok="handleClick">
        <a-card class="mb-8-2">

            <a-form-item label="审核" name="">
                <a-radio-group v-model:value="auditStatus">
                    <a-radio :value="2">通过</a-radio>
                    <a-radio :value="99">不通过</a-radio>
                </a-radio-group>
            </a-form-item>

            <a-form-item label="备注" name="">
                <a-textarea :placeholder="'请输入审核备注'" v-model:value="remark"
                    :auto-size="{ minRows: 2, maxRows: 5 }"></a-textarea>
            </a-form-item>

        </a-card>
    </a-modal>
    <a-modal v-model:open="imgOpen" :title="'图片/视频'" @ok="imgOpen = false">
        <a-card class="mb-8-2">
            <a-form-item :label="'图片'" name="images">
                <template v-if="currentInfo.images && currentInfo.images.length > 0">
                    <a-image-preview-group>
                        <a-image v-for="item in currentInfo.images" :width="200"
                            :src="config('http.apiBasic') + item" />
                    </a-image-preview-group>
                </template>
                <template v-else>暂无</template>

            </a-form-item>
            <a-form-item label="视频" name="video">
                <template v-if="currentInfo.videos && currentInfo.videos.length > 0">
                    <video width="320" height="240" controls>
                        <source v-for="item in currentInfo.videos" :src="config('http.apiBasic') + item">
                    </video>
                </template>
                <template>暂无</template>
            </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, CheckCircleOutlined, DeleteOutlined } from '@ant-design/icons-vue'
import { useI18n } from 'vue-i18n'
import { orderStatus } from '@/enums/index.js'
import dayjs from 'dayjs'
defineOptions({
    name: 'orderList',
})
const { t } = useI18n() // 解构出t方法
const columns = [
    // { title: '用户名称', dataIndex: 'customerName', width: 100 },
    { title: '工单分类', dataIndex: 'orderType', width: 100 },
    { title: '工单区域', dataIndex: 'orderAreaName', width: 150 },
    // { title: '工单标题', dataIndex: 'content', ellipsis: true },
    { title: '工单内容', dataIndex: 'title', ellipsis: true, width: 200 },
    { title: '图片/视频', dataIndex: 'url', width: 100, align: 'center' },

    { title: '维修地点', dataIndex: 'address', ellipsis: true, width: 200 },
    { title: '报修人', dataIndex: 'concatName', width: 180 },
    { title: '联系电话', dataIndex: 'customerPhone', width: 180 },
    { title: '创建时间', dataIndex: 'createdAt', width: 180 },
    // { title: '上门时间', dataIndex: 'startAt' },
    { title: '状态', dataIndex: 'status', width: 120, align: 'center' },
    { title: t('button.action'), key: 'action', fixed: 'right', width: 100, align: 'center' },
]
const { listData, loading, showLoading, hideLoading, paginationState, resetPagination, searchFormData } = usePagination()
const remark = ref('')
const currentInfo = ref({})
const auditStatus = ref(2)
const editDialogRef = ref()
const open = ref(false)
const imgOpen = ref(false)
getPageList()
/**
 * 获取用户列表
 * @returns {Promise<void>}
 */
async function getPageList() {
    try {
        showLoading()
        const { pageSize, current } = paginationState
        const { success, data, total } = await apis.orderList
            .getDataList({
                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 = {}, type) => {
    currentInfo.value = obj
    if (type === 'audit') {
        open.value = true
    } else {
        imgOpen.value = true
    }

}
const handleClick = async () => {
    try {
        const params = {
            ...currentInfo.value,
            status: auditStatus.value,
            remark: remark.value
        }
        const result = await apis.orderList.updateMenu(currentInfo.value.id, params).catch(() => {
            throw new Error(error)
        })
        if (config('http.code.success') === result?.success) {
            message.success('已审核')
            open.value = false
            getPageList()
        }
    } catch (error) {
        message.error({ content: error.message })
        hideLoading()
    }
}
function auditHandleFinish(params){
    Modal.confirm({
        title: '确定完成该工单吗',
        content: t('button.confirm'),
        okText: t('button.confirm'),
        onOk: () => {
            return new Promise((resolve, reject) => {
                ; (async () => {
                    try {
                        const updateParams = {
                            ...params,
                            status: 3
                        }
                        console.log(updateParams)
                        const { success } = await apis.orderList.updateMenu(params.id, updateParams).catch(() => {
                            throw new Error()
                        })
                        if (config('http.code.success') === success) {
                            resolve()
                            message.success('操作成功')
                            await getPageList()
                        }
                    } catch (error) {
                        reject()
                    }
                })()
            })
        },
    })
}
/**
 * 删除
 */
function handleDelete(value) {
    Modal.confirm({
        title: '确定撤销该工单吗',
        content: t('button.confirm'),
        okText: t('button.confirm'),
        onOk: () => {
            return new Promise((resolve, reject) => {
                ; (async () => {
                    try {
                        const params = {
                            ...value,
                        }
                        console.log(params)
                        const { success } = await apis.orderList.delMenu(value.id, params).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>
