<template>
    <div class="container">
        <Breadcrumb :items="['网站管理', '商品管理']" />

        <a-card title="商品列表">
            <a-row>
                <a-col :flex="1">
                    <a-form :model="queryData" :label-col-props="{ span: 6 }" :wrapper-col-props="{ span: 18 }"
                        label-align="left">
                        <a-row :gutter="16">
                            <a-col :span="8">
                                <a-form-item field="id" label="商品名称">
                                    <a-input-number v-model="queryData.id" placeholder="请输入商品名称关键词" :step="1"
                                        :precision="0" />
                                </a-form-item>
                            </a-col>
                        </a-row>
                    </a-form>
                </a-col>
                <a-divider style="height: 84px" direction="vertical" />
                <a-col :flex="'86px'" style="text-align: right">
                    <a-space direction="vertical" :size="18">
                        <a-button type="primary" @click="search">
                            <template #icon>
                                <icon-search />
                            </template>
                            搜索
                        </a-button>
                        <a-button @click="reset">
                            <template #icon>
                                <icon-refresh />
                            </template>
                            重置
                        </a-button>
                    </a-space>
                </a-col>
            </a-row>

            <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 #username="{ record }">
                    <a-avatar :size="30">
                        <IconUser v-if="!record.avatar" />
                        <img :alt="record.username ?? ''" :src="record.avatar" v-else />
                    </a-avatar>
                    {{ record.username }}
                </template>
                <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">
                        已下架
                    </a-tag>
                    <a-tag color="green" v-else-if="record.state === 1">
                        正常
                    </a-tag>
                </template>
                <template #optional="{ record }">
                    <a-button @click="$router.push(`/admin/goods/addGoods/${record.id}`)">编辑商品</a-button>
                </template>
            </a-table>
        </a-card>
    </div>
</template>

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

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

const queryData = reactive({
    keyword: '',
})

const search = () => {
    pagination.current = 1
    getList()
}

const reset = () => {
    pagination.current = 1
    queryData.id = ''
    queryData.state = -1
    getList()
}

const columns = [
    {
        title: '商品ID',
        dataIndex: 'id',
    },
    {
        title: '商品名称',
        dataIndex: 'name',
    }, {
        title: '商品价格',
        dataIndex: 'price',
    },
    {
        title: '库存数量',
        dataIndex: 'num',
    }, {
        title: '乐跑次数',
        dataIndex: 'lepao_count',
    }, {
        title: '浏览量',
        dataIndex: 'views',
    }, {
        title: '创建时间',
        slotName: 'create_time'
    }, 
    {
        title: '创建人',
        dataIndex: 'create_user',
    },{
        title: '最后更新时间',
        slotName: 'update_time'
    }, {
        title: '更新人',
        dataIndex: 'update_user',
    },{
        title: '状态',
        slotName: 'state'
    }, {
        title: '操作',
        slotName: 'optional'
    }
]

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

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

        const reqData = {
            ...queryData,
            pagesize: pagination.pagesize,
            current: pagination.current
        }

        const res = await adminGetGoodsList(reqData)
        if (!res || res.code !== 0)
            return Notification.error({
                title: '获取商品失败！',
                content: res?.msg ?? '请稍后再试'
            })

        data.value = res.data
        console.log(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 // 页大小变化后回到第一页
    getList()
}

onMounted(() => {
    getList()
})

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>