<template>
    <div>
        <el-card class="!border-none mb-4" shadow="never">
            <el-form class="mb-[-16px]" :model="queryParams" inline>
                <el-form-item label="商品名称" prop="store_name">
                    <el-input
                        class="w-[280px]"
                        @keydown.enter="resetPage"
                        v-model="queryParams.store_name"
                        clearable
                        placeholder="请输入商品名称"
                    />
                </el-form-item>
                <el-form-item label="类型" prop="type">
                    <el-select
                        v-model="queryParams.type"
                        placeholder="请选择类型"
                        style="width: 240px"
                        @change="resetPage"
                    >
                        <el-option
                            v-for="item in [
                                { id: 1, name: '默认' },
                                { id: 2, name: '门店' },
                                { id: 3, name: '仓库' }
                            ]"
                            :key="item.id"
                            :label="item.name"
                            :value="item.id"
                        />
                    </el-select>
                </el-form-item>
                <el-form-item label="门店" prop="store_id" v-if="queryParams.type == 2">
                    <el-select
                        v-model="queryParams.store_id"
                        filterable
                        remote
                        reserve-keyword
                        placeholder="输入门店名称搜索"
                        remote-show-suffix
                        :remote-method="remoteMethod"
                        :loading="storeloading"
                        style="width: 240px"
                        @change="resetPage"
                    >
                        <el-option
                            v-for="item in storeList"
                            :key="item.id"
                            :label="item.name"
                            :value="item.id"
                        />
                    </el-select>
                </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>
        </el-card>
        <el-card class="!border-none" v-loading="pager.loading" shadow="never">
            <div class="mt-4">
                <el-table :data="pager.lists">
                    <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="stock"
                        min-width="100"
                        show-overflow-tooltip
                    />
                    <el-table-column
                        label="备注"
                        prop="remark"
                        min-width="200"
                        show-overflow-tooltip
                    />
                    <el-table-column label="操作" width="180" fixed="right">
                        <template #default="{ row }">
                            <el-button
                                v-perms="['statistics.negative/update_negative_zero']"
                                type="primary"
                                link
                                @click="handleDetail(row)"
                            >
                                归零
                            </el-button>
                        </template>
                    </el-table-column>
                </el-table>
            </div>
            <div class="flex mt-4 justify-end">
                <pagination v-model="pager" @change="getLists" />
            </div>
        </el-card>
    </div>
</template>

<script lang="ts" setup name="storeProductLists">
import { usePaging } from '@/hooks/usePaging'
import { useDictData } from '@/hooks/useDictOptions'
import { negative_inventory, update_negative_zero } from '@/api/workbench'
import { useRoute } from 'vue-router'
import { apiSystemStoreLists } from '@/api/system_store'

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

const storeloading = ref(false)
const storeList = ref([])
const remoteMethod = (e = '') => {
    storeloading.value = true
    apiSystemStoreLists({
        name: e,
        page_size: 50
    })
        .then((res) => {
            storeList.value = res.lists
            setTimeout(() => {
                storeloading.value = false
            }, 300)
        })
        .catch((err) => {
            setTimeout(() => {
                storeloading.value = false
            }, 300)
        })
}

// 详情
const handleDetail = async (data: any) => {
    update_negative_zero({ type: queryParams.type, id: data.id })
        .then((res) => {
            ElMessage.success('操作成功')
            getLists()
        })
        .catch((err) => {
            ElMessage.error('操作失败')
        })
}
// 获取字典数据
const { dictData } = useDictData('')
// 分页相关

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

onMounted(() => {
    getLists()
})
</script>
