<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="bar_code">
                    <el-input
                        class="w-[280px]"
                        v-model="queryParams.bar_code"
                        clearable
                        placeholder="请输入商品条码"
                    />
                </el-form-item>
                <el-form-item label="商品分类" prop="store_name">
                    <el-cascader
                        class="w-[280px]"
                        v-model="queryParams.class_all"
                        filterable
                        clearable
                        :before-filter="beforeFilter"
                        :props="props"
                        placeholder="请选择商品分类"
                        @change="resetPage"
                    />
                </el-form-item>
                <el-form-item>
                    <el-button type="primary" @click="resetPage">查询</el-button>
                    <el-button @click="resetParams">重置</el-button>
                    <export-data
                        class="ml-2.5"
                        :fetch-fun="apiStoreProductLists"
                        :params="queryParams"
                        :page-size="pager.size"
                    />
                </el-form-item>
            </el-form>
        </el-card>
        <el-card class="!border-none" v-loading="pager.loading" shadow="never">
            <router-link
                :to="{
                    path: 'store_product_edit',
                    query: { id: 0, mode: 'add' }
                }"
                class="mr-4"
                v-perms="['store_product.store_product/add']"
            >
                <el-button type="primary" icon="Plus"> 新增 </el-button>
            </router-link>
            <el-button
                v-perms="['store_product.store_product/delete']"
                :disabled="!selectData.length"
                @click="handleDelete(selectData)"
            >
                删除
            </el-button>
            <!-- <router-link
                :to="{
                    path: 'product_import'
                }"
                class="ml-4"
                v-perms="['store_product.store_product/add']"
            >
                <el-button type="primary"> 导入商品到门店 </el-button>
            </router-link> -->
            <div class="mt-4">
                <el-table :data="pager.lists" @selection-change="handleSelectionChange">
                    <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="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="purchase"
                        min-width="100"
                        show-overflow-tooltip
                    />
                    <el-table-column
                        label="商户价"
                        prop="cost"
                        min-width="120"
                        show-overflow-tooltip
                    />
                    <el-table-column
                        label="会员价"
                        prop="vip_price"
                        min-width="120"
                        show-overflow-tooltip
                    />
                    <el-table-column
                        label="终端零售价"
                        prop="price"
                        min-width="120"
                        show-overflow-tooltip
                    />
                    <el-table-column
                        label="起批量"
                        prop="batch"
                        min-width="100"
                        show-overflow-tooltip
                    />
                    <el-table-column
                        label="毛利率"
                        prop="rose"
                        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
                    />
                    <el-table-column
                        label="销量"
                        prop="sales"
                        min-width="100"
                        show-overflow-tooltip
                    />
                    <!-- <el-table-column label="商品来源" prop="system_store_name" min-width="200" show-overflow-tooltip /> -->
                    <el-table-column label="状态" prop="is_show">
                        <template #default="{ row }">
                            <el-tag type="success" v-if="row.is_show"> 上架 </el-tag>
                            <el-tag type="info" v-else> 下架 </el-tag>
                        </template>
                    </el-table-column>
                    <el-table-column
                        label="条码"
                        prop="bar_code"
                        min-width="200"
                        show-overflow-tooltip
                    />
                    <el-table-column label="操作" width="180" fixed="right">
                        <template #default="{ row }">
                            <router-link
                                :to="{
                                    path: 'store_product_edit',
                                    query: { id: row.id, mode: 'edit', page: pager.page }
                                }"
                                class="mr-4"
                                v-perms="['store_product.store_product/edit']"
                            >
                                <el-button type="primary" link> 编辑 </el-button>
                            </router-link>
                            <el-button
                                v-perms="['store_product.store_product/edit']"
                                type="primary"
                                link
                                @click="handleDetail(row)"
                            >
                                详情
                            </el-button>
                            <el-button
                                v-perms="['store_product.store_product/delete']"
                                type="danger"
                                link
                                @click="handleDelete(row.id)"
                            >
                                删除
                            </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>
        <Details ref="detailsRef"></Details>
    </div>
</template>

<script lang="ts" setup name="storeProductLists">
import { usePaging } from '@/hooks/usePaging'
import { useDictData } from '@/hooks/useDictOptions'
import { apiStoreProductLists, apiStoreProductDelete } from '@/api/store_product'
import { timeFormat } from '@/utils/util'
import feedback from '@/utils/feedback'
import Details from './details.vue'
import { apiStoreCategoryLists } from '@/api/store_category'
import { useRoute } from 'vue-router'

const route = useRoute()

const detailsRef = ref(null)
// 查询条件
const queryParams = reactive({
    store_name: '',
    class_all: '',
    bar_code: ''
})

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

// 表格选择后回调事件
const handleSelectionChange = (val: any[]) => {
    selectData.value = val.map(({ id }) => id)
}
// 获取字典数据
const { dictData } = useDictData('')
// 分页相关
let page_no = 1
if (route.query.page_no) {
    page_no = parseInt(route.query.page_no)
}

const { pager, getLists, resetParams, resetPage } = usePaging({
    page: page_no,
    fetchFun: apiStoreProductLists,
    params: queryParams
})
// 添加
const handleAdd = async () => {}

// 详情
const handleDetail = async (data: any) => {
    detailsRef.value?.open()
    detailsRef.value?.setFormData(data)
}

// 编辑
const handleEdit = async (data: any) => {}

let resolves
let nodes
const flag = ref(false)
let name
const beforeFilter = (value) => {
    name = value
    flag.value = true
    props.lazyLoad(nodes, resolves)
    return false
}
const props = {
    value: 'id',
    label: 'name',
    emitPath: false,
    lazy: true,
    lazyLoad(node, resolve) {
        if (!resolves) resolves = resolve
        if (!nodes) nodes = node
        const pid = node.value || 0
        let data = {}
        if (flag.value) {
            data = { name: name }
            if (!name) {
                data = {
                    pid: pid,
                    page_size: 10000
                }
            }
        } else {
            data = {
                pid: pid,
                page_size: 10000
            }
        }
        apiStoreCategoryLists({
            ...data
        }).then((res) => {
            resolve(
                res.lists.map((item: any) => {
                    // item.leaf = item.is_children == 0;
                    item.leaf = true
                    flag.value = false
                    return item
                })
            )
        })
    }
}

// 删除
const handleDelete = async (id: number | any[]) => {
    await feedback.confirm('确定要删除？')
    await apiStoreProductDelete({ id })
    getLists()
}

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