<template>
    <div>
        <div class="mt-4" v-loading="pager.loading">
            <el-table :data="pager.lists" @selection-change="handleSelectionChange" :height="300">
                <el-table-column type="selection" width="55" />
                <el-table-column label="商品ID" prop="id" show-overflow-tooltip />
                <el-table-column label="商品图片" prop="image" min-width="80">
                    <template #default="{ row }">
                        <el-image
                            style="width: 50px; height: 50px"
                            :src="row.image"
                            :preview-teleported="true"
                        />
                    </template>
                </el-table-column>
                <el-table-column
                    label="商品名称"
                    prop="store_name"
                    min-width="200"
                    show-overflow-tooltip
                />
                <el-table-column
                    label="单位"
                    prop="unit_name"
                    min-width="80"
                    show-overflow-tooltip
                />
                <el-table-column
                    label="数量"
                    prop="cart_num"
                    min-width="100"
                    show-overflow-tooltip
                />
                <el-table-column
                    label="供货价"
                    prop="purchase"
                    min-width="100"
                    show-overflow-tooltip
                />
                <el-table-column label="售价" prop="price" min-width="100" show-overflow-tooltip />
                <el-table-column
                    label="总价"
                    prop="total_price"
                    min-width="100"
                    show-overflow-tooltip
                />
            </el-table>
        </div>
        <div class="flex mt-4 justify-end">
            <pagination v-model="pager" @change="getLists" />
        </div>
        <el-button class="w-full mt-4" type="primary" @click="onBindRefundProduct"
            >添加商品</el-button
        >
    </div>
</template>

<script lang="ts" setup>
import { usePaging } from '@/hooks/usePaging'
import { apiStoreOrderCartInfo } from '@/api/store_order'
import { ElMessage } from 'element-plus'
const props = defineProps<{
    oid: number | string
}>()
// 查询条件
const queryParams = reactive({
    oid: props.oid
})

// 选中数据
const selectData = ref<any[]>([])

// 表格选择后回调事件
const handleSelectionChange = (val: any[]) => {
    selectData.value = val.map(({ id }) => id)
}

const { pager, getLists, resetParams, resetPage } = usePaging({
    fetchFun: apiStoreOrderCartInfo,
    params: queryParams
})
getLists()

const emits = defineEmits(['onBindRefundProduct'])
// 绑定用户
const onBindRefundProduct = () => {
    const list = pager.lists.filter((item) => selectData.value.includes(item.id))
    if (!list.length) return ElMessage.error('请先选择门店')
    emits('onBindRefundProduct', list)
}
</script>
