<template>
    <div>
        <el-form class="mb-[-16px]" :model="queryParams" inline>
            <el-form-item label="商品名称" prop="store_name">
                <el-input
                    class="w-[200px]"
                    v-model="queryParams.store_name"
                    @keydown.enter="productList()"
                    clearable
                    placeholder="请输入商品名称"
                />
            </el-form-item>
            <el-form-item>
                <el-button type="primary" @click="resetPage">查询</el-button>
                <el-button @click="resetParams">重置</el-button>
            </el-form-item>
        </el-form>
        <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="订单id" prop="order_id" show-overflow-tooltip />
                <el-table-column label="商品ID" prop="product_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" show-overflow-tooltip />
                <el-table-column label="分类" prop="cate_name" show-overflow-tooltip />
                <el-table-column label="单位" prop="unit_name" show-overflow-tooltip />
                <el-table-column label="供应商" prop="supplier_name" show-overflow-tooltip />
                <el-table-column label="需求数量" prop="need_num" show-overflow-tooltip />

                <el-table-column label="采购数量" prop="buyer_nums" show-overflow-tooltip />
                <el-table-column label="采购价" prop="price" show-overflow-tooltip />
                <el-table-column label="采购总价" prop="total_price" show-overflow-tooltip />
                <el-table-column label="采购人员" prop="buyer_name" show-overflow-tooltip />
                <el-table-column label="采购状态" prop="buyer_confirm_name" 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="bindStore">添加商品</el-button>
    </div>
</template>

<script lang="ts" setup name="systemStoreLists">
import { usePaging } from '@/hooks/usePaging'
import { apiPurchaseProductOfferLists } from '@/api/purchase_product_offer'
import { ElMessage } from 'element-plus'

// 查询条件
const queryParams = reactive({
    buyer_confirm: 0
})

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

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

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

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