<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 label="排序" prop="order_by">
                    <el-select
                        v-model="queryParams.order_by"
                        placeholder="请选择排序"
                        style="width: 240px"
                        @change="resetPage"
                    >
                        <el-option
                            v-for="item in [
                                { id: 'asc', name: '升序' },
                                { id: 'desc', name: '降序' }
                            ]"
                            :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-button type="primary" @click="stockProductPrice">重新统计数据</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 mb-4" shadow="never">
            <div class="flex flex-wrap">
                <div
                    class="w-1/5 flex mb-6"
                    v-for="(item, index) in visitorOption.series"
                    :key="index"
                >
                    <div class="mr-2">
                        <div
                            class="rounded-full p-2"
                            :style="{ 'background-color': colorList[index % 4] }"
                        >
                            <iconfont :iconName="iconList[index]" white className="text-6xl" />
                        </div>
                    </div>
                    <div>
                        <div class="text-info">
                            {{ item.name }}
                            <el-tooltip
                                class="box-item"
                                effect="dark"
                                :content="item.desc"
                                placement="top-start"
                            >
                                <el-icon>
                                    <Warning />
                                </el-icon>
                            </el-tooltip>
                        </div>
                        <div v-if="item.total_money != ''">
                            <span class="text-2xl mr-2">库存:{{ item.total_value }}</span>

                            <span class="text-2xl mr-2">金额:{{ item.total_money }}</span>
                        </div>
                        <div v-else>
                            <span class="text-6xl mr-2">{{ item.total_value }}</span>
                        </div>
                    </div>
                </div>
            </div>
        </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="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="sales"
                        min-width="100"
                        show-overflow-tooltip
                    >
                        <template #default="{ row }">
                            <el-button type="primary" link @click="handleDetai(row)">{{
                                row.sales || 0
                            }}</el-button>
                        </template>
                    </el-table-column>
                    <el-table-column
                        label="门店库存"
                        prop="store_stock"
                        min-width="100"
                        show-overflow-tooltip
                    >
                        <template #default="{ row }">
                            <el-button type="primary" link @click="handleStockDetai(row, 1)">{{
                                row.store_stock || 0
                            }}</el-button>
                        </template>
                    </el-table-column>
                    <el-table-column
                        label="仓库库存"
                        prop="warehouse_stock"
                        min-width="100"
                        show-overflow-tooltip
                    >
                        <template #default="{ row }">
                            <el-button type="primary" link @click="handleStockDetai(row, 0)">{{
                                row.warehouse_stock || 0
                            }}</el-button>
                        </template>
                    </el-table-column>
                    <el-table-column
                        label="库存"
                        prop="total_stock"
                        min-width="100"
                        show-overflow-tooltip
                    >
                    </el-table-column>
                    <el-table-column
                        label="价值"
                        prop="total_price"
                        min-width="100"
                        show-overflow-tooltip
                    >
                    </el-table-column>
                    <el-table-column
                        label="采购总价"
                        prop="total_purchase"
                        min-width="100"
                        show-overflow-tooltip
                    >
                        <template #default="{ row }">
                            <el-button
                                type="primary"
                                link
                                @click="handleWarehouseProduct(row, 1)"
                                >{{ row.total_purchase || 0 }}</el-button
                            >
                        </template>
                    </el-table-column>
                    <el-table-column
                        label="已结"
                        prop="total_completed_amount"
                        min-width="100"
                        show-overflow-tooltip
                    >
                        <template #default="{ row }">
                            <el-button
                                type="primary"
                                link
                                @click="handleWarehouseProduct(row, 2)"
                                >{{ row.total_completed_amount || 0 }}</el-button
                            >
                        </template>
                    </el-table-column>
                    <el-table-column
                        label="未结"
                        prop="total_outstanding_amount"
                        min-width="100"
                        show-overflow-tooltip
                    >
                        <template #default="{ row }">
                            <el-button
                                type="primary"
                                link
                                @click="handleWarehouseProduct(row, 3)"
                                >{{ row.total_outstanding_amount || 0 }}</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>
        <stockDetails ref="stockDetailsRef"></stockDetails>
        <warehouseProduct ref="warehouseProductRef"></warehouseProduct>
    </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 { total_warehouse, warehouse_list, stock_product_price } from '@/api/workbench'
import Details from './details.vue'
import stockDetails from './stockDetails.vue'
import warehouseProduct from './warehouseProduct.vue'
import { apiStoreCategoryLists } from '@/api/store_category'
import { useRoute } from 'vue-router'
const showDetails = ref(false)
const showStockDetails = ref(false)
const showWarehouseProduct = ref(false)
const route = useRoute()

const detailsRef = ref(null)
const stockDetailsRef = ref(null)
const warehouseProductRef = ref(null)
// 查询条件
const queryParams = reactive({
    store_name: '',
    class_all: '',
    bar_code: '',
    field: 'stock',
    order_by: ''
})
const stockProductPrice = () => {
    stock_product_price({}).then((res) => {})
}
// 销量详情
const handleDetai = async (data: any) => {
    showDetails.value = true
    await nextTick()
    detailsRef.value?.open()
    detailsRef.value?.getDetail(data)
}
// 库存详情
const handleStockDetai = async (data: any, type = 1) => {
    showStockDetails.value = true
    await nextTick()
    stockDetailsRef.value?.open()
    stockDetailsRef.value?.getDetail(data, type)
}
// 商品出入库详情
const handleWarehouseProduct = async (data: any, type = 1) => {
    showWarehouseProduct.value = true
    await nextTick()
    warehouseProductRef.value?.open()
    warehouseProductRef.value?.getDetail(data, type)
}
// 选中数据
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: warehouse_list,
    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 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 colorList = [
    '#5DB1FF',
    '#4CD384',
    '#FFC46A',
    '#CAA5F1',
    '#FFC46A',
    '#4CD384',
    '#5DB1FF',
    '#CAA5F1'
]
// 商品浏览量, 商品访客数, 加购件数, 下单件数, 支付件数, 支付金额, 成本金额, 退款金额, 退款件数, 访客-支付转化率
const iconList = ref([
    'RectangleCopy58',
    'RectangleCopy48',
    'RectangleCopy65',
    'RectangleCopy62',
    'RectangleCopy56',
    'RectangleCopy55',
    'RectangleCopy57',
    'RectangleCopy65',
    'RectangleCopy61',
    'RectangleCopy52',
    'RectangleCopy14',
    'RectangleCopy32'
])
// 表单数据
const visitorOption: any = reactive({
    xAxis: {
        type: 'category',
        data: [0],
        axisLabel: {
            rotate: 45,
            color: '#333'
        }
    },
    yAxis: {
        type: 'value',
        name: '金额',
        position: 'left',
        axisLabel: {
            formatter: '{value}'
        }
    },
    grid: {
        left: '3%',
        right: '4%',
        bottom: '3%',
        containLabel: true
    },
    legend: {
        data: ['访问量']
    },
    itemStyle: {
        // 点的颜色。
        color: 'red'
    },
    tooltip: {
        trigger: 'axis'
    },
    series: [
        {
            name: '访问量',
            data: [0],
            type: 'line',
            smooth: true
        }
    ]
})
const totalWarehouse = () => {
    total_warehouse({}).then((res) => {
        // 清空echarts 数据
        visitorOption.xAxis.data = []
        visitorOption.series = []
        console.log(res)

        visitorOption.legend.data = Object.values(res.series).map((item: any) => item.name)

        // 写入从后台拿来的数据
        visitorOption.xAxis.data = res.x
        visitorOption.series = Object.values(res.series).map((item: any) => {
            return {
                type: 'line',
                smooth: true,
                data: item.value,
                name: item.name,
                total_value: item.total_value,
                rate: item.rate,
                desc: item.desc,
                total_money: item.total_money
            }
        })
    })
}

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