<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
                    v-if="is_warehouse == 0"
                />
                <el-table-column label="商品ID" prop="product_id" show-overflow-tooltip v-else />
                <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="cate_name"
                    min-width="120"
                    show-overflow-tooltip
                />
                <el-table-column
                    label="单位"
                    prop="unit_name"
                    min-width="80"
                    show-overflow-tooltip
                />
                <el-table-column
                    label="商品价格"
                    prop="price"
                    min-width="100"
                    show-overflow-tooltip
                />
                <el-table-column label="成本价" prop="cost" min-width="100" show-overflow-tooltip />
                <el-table-column
                    label="供货价"
                    prop="purchase"
                    min-width="100"
                    show-overflow-tooltip
                />
                <el-table-column
                    label="售卖库存"
                    prop="stock"
                    min-width="100"
                    show-overflow-tooltip
                />
                <el-table-column
                    label="兑换库存"
                    prop="swap"
                    min-width="100"
                    show-overflow-tooltip
                    v-if="props.is_warehouse != 1"
                />
                <el-table-column
                    label="销量"
                    prop="sales"
                    min-width="100"
                    show-overflow-tooltip
                    v-if="props.is_warehouse != 1"
                />
            </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 { useDictData } from '@/hooks/useDictOptions'
import { apiStoreProductLists } from '@/api/store_product'
import { apiStoreBranchProductLists } from '@/api/store_branch_product'
import { apiWarehouseProductStoregeLists } from '@/api/warehouse_product_storege'
import { timeFormat } from '@/utils/util'
import feedback from '@/utils/feedback'
import { ElMessage } from 'element-plus'
const props = defineProps<{
    is_warehouse: number
    is_branch: number
    store_id: string
    code: any[]
    warehouse_id: string
}>()

// 查询条件
const queryParams = reactive({
    store_name: '',
    warehouse_id: '',
    store_id: '',
    product_type: ''
})

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

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

// 获取字典数据
const { dictData } = useDictData('')

// 分页相关
if (props.is_warehouse == 1) {
    if (props.warehouse_id == '' || props.warehouse_id == null) {
        ElMessage.error('请先选择仓库')
    } else {
        queryParams.warehouse_id = props.warehouse_id
        datas.value = {
            fetchFun: apiWarehouseProductStoregeLists,
            params: queryParams
        }
    }
} else {
    if (props.is_branch == 1) {
        if (props.store_id == '' || props.store_id == null) {
            ElMessage.error('请先选择门店')
        } else {
            queryParams.store_id = props.store_id
        }
        datas.value = {
            fetchFun: apiStoreBranchProductLists,
            params: queryParams
        }
    } else {
        queryParams.product_type = '0'
        datas.value = {
            fetchFun: apiStoreProductLists,
            params: queryParams
        }
    }
}
const productList = () => {
    getLists()
}
const { pager, getLists, resetParams, resetPage } = usePaging(datas.value)
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>
