<template>
    <div>
        <el-card class="!border-none mb-4" shadow="never">
            <el-form class="mb-[-16px]" :model="queryParams" inline>
                <el-form-item label="" prop="create_time">
                    <el-date-picker
                        v-model="startEndTime"
                        type="daterange"
                        range-separator="至"
                        start-placeholder="开始日期"
                        end-placeholder="结束日期"
                        unlink-panels
                        @change="changeStartEndTime"
                    />
                </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="product_order"
                        :params="queryParams"
                        :page-size="pager.size"
                    />
                </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" @selection-change="handleSelectionChange">
                    <el-table-column type="selection" width="55" />
                    <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"
                        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="store_info"
                        min-width="100"
                        show-overflow-tooltip
                    />
                    <el-table-column
                        label="数量"
                        prop="cart_num"
                        min-width="100"
                        show-overflow-tooltip
                    />
                    <el-table-column
                        label="单价"
                        prop="price"
                        min-width="120"
                        show-overflow-tooltip
                    />
                    <el-table-column
                        label="总价"
                        prop="total_price"
                        min-width="120"
                        show-overflow-tooltip
                    />
                </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 { product_order } from '@/api/workbench'
import { timeFormat } from '@/utils/util'
import feedback from '@/utils/feedback'
import { apiStoreCategoryLists } from '@/api/store_category'
import { useRoute } from 'vue-router'
import { apiSystemStoreLists } from '@/api/system_store'
import moment from 'moment'

const route = useRoute()

const detailsRef = ref(null)
const startEndTime = ref([new Date(), new Date()])

// 查询条件
const queryParams = reactive({
    start_time: moment(startEndTime.value[0]).format('YYYY-MM-DD') + ' 00:00:00',
    end_time: moment(startEndTime.value[1]).format('YYYY-MM-DD') + ' 23:59:59',
    is_all: '',
    store_id: ''
})

const changeStartEndTime = () => {
    if (startEndTime.value[0] && startEndTime.value[1]) {
        queryParams.start_time = moment(startEndTime.value[0]).format('YYYY-MM-DD') + ' 00:00:00'
        queryParams.end_time = moment(startEndTime.value[1]).format('YYYY-MM-DD') + ' 23:59:59'
    } else {
        queryParams.start_time = ''
        queryParams.end_time = ''
    }
    getLists()
}
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 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 url = ref(product_order)
const { pager, getLists, resetParams, resetPage } = usePaging({
    page: page_no,
    fetchFun: url.value,
    params: queryParams
})

// 编辑

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 primary = ref('primary')
const primary2 = ref('')
const handleGroup = () => {
    queryParams.is_all = ''
    primary.value = 'primary'
    primary2.value = ''
    url.value = product_order
    getLists()
}
const handleList = () => {
    queryParams.is_all = '1'
    primary.value = ''
    primary2.value = 'primary'
    url.value = product_order
    resetPage()
    getLists()
}
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
                })
            )
        })
    }
}

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