<template>
    <div class="container">
        <Breadcrumb :items="['售后服务', '我的工单']" />

        <a-card title="工单列表">
            <a-button type="primary" size="large" @click="$router.push('/service/createOrder')">
                <template #icon>
                    <icon-plus />
                </template>
                提交工单
            </a-button>

            <a-table :data="data" stripe hoverable column-resizable class="table" :loading="loading" :columns="columns"
                :pagination="{
                    showPageSize: true,
                    showJumper: true,
                    showTotal: true,
                    pageSize: pagination.pagesize,
                    current: pagination.current,
                    total: pagination.total
                }" @page-change="handlePageChange" @page-size-change="handlePageSizeChange">
                <template #create_time="{ record }">
                    {{ stramptoTime(record.create_time) }}
                </template>
                <template #update_time="{ record }">
                    {{ stramptoTime(record.update_time) }}
                </template>
                <template #state="{ record }">
                    <a-tag color="orangered" v-if="record.state === 0">
                        <template #icon>
                            <icon-schedule />
                        </template>
                        待处理
                    </a-tag>
                    <a-tag color="arcoblue" v-else-if="record.state === 1">
                        <template #icon>
                            <icon-customer-service />
                        </template>
                        已回复
                    </a-tag>
                    <a-tag color="green" v-else-if="record.state === 2">
                        <template #icon>
                            <icon-check-circle />
                        </template>
                        已关闭
                    </a-tag>
                </template>
                <template #optional="{ record }">
                    <a-button @click="$router.push(`/service/orderDetail/${record.id}`)">查看详情</a-button>
                </template>
            </a-table>
        </a-card>
    </div>
</template>

<script setup>
import { ref, reactive, onMounted } from 'vue'
import { orderList } from '@/api/workOrder'
import { Notification } from '@arco-design/web-vue'

const pagination = reactive({
    total: 0,
    current: 1,
    pagesize: 20
})

const columns = [
    {
        title: '工单ID',
        dataIndex: 'id',
    }, 
    {
        title: '工单标题',
        dataIndex: 'title',
    }, {
        title: '提醒邮箱',
        dataIndex: 'email',
    }, {
        title: '创建时间',
        slotName: 'create_time'
    }, {
        title: '最后更新时间',
        slotName: 'update_time'
    }, {
        title: '状态',
        slotName: 'state'
    }, {
        title: '操作',
        slotName: 'optional'
    }
]

const loading = ref(false)
const data = ref([])

const getOrderList = async () => {
    try {
        loading.value = true

        const reqData = {
            pagesize: pagination.pagesize,
            current: pagination.current
        }
        const res = await orderList(reqData)
        if (!res || res.code !== 0)
            return Notification.error({
                title: '获取工单失败！',
                content: res?.msg ?? '请稍后再试'
            })

        data.value = res.data
        pagination.total = res.pagination.total
    } catch (error) {
        Notification.error({
            title: '获取工单失败！',
            content: error.message || '请稍后再试'
        })
    } finally {
        loading.value = false
    }
}

// 分页 - 页码变化
const handlePageChange = (page) => {
    pagination.current = page
    getOrderList()
}

// 分页 - 每页条数变化
const handlePageSizeChange = (size) => {
    pagination.pagesize = size
    pagination.current = 1 // 页大小变化后回到第一页
    getOrderList()
}

onMounted(() => {
    getOrderList()
})

const stramptoTime = (time) => {
    return new Date(time).toLocaleString('zh-CN', { year: 'numeric', month: '2-digit', day: '2-digit', hour: '2-digit', minute: '2-digit', second: '2-digit' })
}
</script>

<style scoped>
.container {
    padding: 0 20px 20px 20px;
}

.table {
    margin-top: 15px;
}
</style>