<!-- 集采交易-我的集采 -->
<template>
    <app-view>
        <template #header>
            <app-filter :options="filterOptons" />
        </template>
        <!-- 表格数据 -->
        <app-table :data="dataList" v-model:columns="tableColumns" :loading="loading">
            <template #header>
                <app-auth-operation :menus="['centralize_mine_add', 'centralize_mine_apply']" @closed="onRefresh" />
            </template>
            <!-- 状态 -->
            <template #presalestatus="{ value }">
                {{ getWRPresaleStatusName(value) }}
            </template>
            <!-- 操作 -->
            <template #operate="{ row }">
                <app-auth-operation type="dropdown" :menus="['centralize_mine_details']" :options="{ selectedRow: row }"
                    @closed="onRefresh" />
            </template>
            <template #footer>
                <app-pagination :total="total" v-model:page-size="pageSize" v-model:page-index="pageIndex"
                    @change="onRefresh" />
            </template>
        </app-table>
    </app-view>
</template>

<script lang="ts" setup>
import { shallowRef } from 'vue'
import { ElMessage } from 'element-plus'
import { useDataFilter } from '@/hooks/datatable'
import { useRequest } from '@/hooks/request'
import { queryGZPreSell } from '@/services/api/presale'
import { getWRPresaleStatusName } from '@/constants/presale'
import { useLoginStore } from '@/stores'
import AppTable from '@pc/components/base/table/index.vue'
import AppPagination from '@pc/components/base/pagination/index.vue'
import AppFilter from '@pc/components/base/table-filter/index.vue'
import AppAuthOperation from '@pc/components/modules/auth-operation/index.vue'

const loginStore = useLoginStore()
const { filterOptons, getQueryParams } = useDataFilter<Ermcp.GZPreSellReq>()
const { loading, dataList, total, pageIndex, pageSize, run } = useRequest(queryGZPreSell, {
    params: {
        pagesize: 20,
        marketid: 62201,
        presalestatuses: '1,2,3',
        selluserid: loginStore.userId
    },
    onError: (err) => {
        ElMessage.error(err)
    }
})

const tableColumns = shallowRef<Ermcp.TableColumn[]>([
    { prop: 'wrstandardname', label: '商品' },
    { prop: 'presaleqty', label: '集采数量(克拉)' },
    { prop: 'startdate', label: '开始日期' },
    { prop: 'enddate', label: '结束日期' },
    { prop: 'tradeqty', label: '已采购数量(克拉)', width: 140 },
    { prop: 'createtime', label: '申请时间' },
    { prop: 'presalestatus', label: '状态' },
    { prop: 'operate', label: '操作', fixed: 'right' },
])

filterOptons.selectList = [
    {
        label: '状态',
        key: 'presalestatuses',
        options: [
            { label: '未开始', value: '1' },
            { label: '进行中', value: '2' },
            { label: '已结束', value: '3' }
        ],
    },
]

filterOptons.inputList = [
    { label: '商品', keys: ['wrstandardname'] },
]

filterOptons.buttonList = [
    { lable: '重置', onClick: () => onSearch(true) },
    { lable: '查询', className: 'el-button--primary', onClick: () => onSearch() }
]

const onSearch = (clear = false) => {
    getQueryParams((qs) => {
        pageIndex.value = 1
        run(qs)
    }, clear)
}

const onRefresh = () => {
    getQueryParams((qs) => run(qs))
}
</script>