<template>
    <div class="workbench">
        <el-card shadow="never" class="!border-none">
            <div class="flex">
                <div class="flex-1 h-[270px]">
                    <div class="text-2xl">今日订单金额</div>
                    <div>
                        <span class="text-[12px]">¥</span>
                        <span class="text-2xl">1352.36</span>
                    </div>
                    <v-charts style="height: 200px;" :option="leftOption" :autoresize="true"></v-charts>
                </div>
                <div class="w-[1px] bg-[#ccc] mr-4"></div>
                <div class="flex-1 flex">
                    <div class="flex-1">
                        <div class="text-1xl">今日订单金额</div>
                        <div class="text-[12px]">
                            <span class="text-info">昨日: 10</span>
                            <span class="text-info ml-2">日环比</span>
                            <span class="text-success">-55%</span>
                        </div>
                        <v-charts style="height: 180px;" :option="leftOption" :autoresize="true"></v-charts>
                        <div class="text-1xl mt-2">今日订单金额</div>
                        <div class="text-[12px]">
                            <span class="text-info">昨日: 10</span>
                            <span class="text-info ml-2">日环比</span>
                            <span class="text-success">-55%</span>
                        </div>
                    </div>
                    <div class="flex-1">
                        <div class="text-1xl">今日订单金额</div>
                        <div class="text-[12px]">
                            <span class="text-info">昨日: 10</span>
                            <span class="text-info m-2">日环比</span>
                            <span class="text-success">-55%</span>
                        </div>
                        <v-charts style="height: 180px;" :option="leftOption" :autoresize="true"></v-charts>
                        <div class="text-1xl mt-2">今日订单金额</div>
                        <div class="text-[12px]">
                            <span class="text-info">昨日: 10</span>
                            <span class="text-info ml-2">日环比</span>
                            <span class="text-success">-55%</span>
                        </div>
                    </div>
                </div>
            </div>
        </el-card>
        <el-card shadow="never" class="mt-4 !border-none">
            <div class="mb-6 flex justify-between items-center">
                <span class="text-2xl">用户概况</span>
                <div class="flex items-center text-sm">
                    <span class="mr-4">时间筛选: </span>
                    <el-date-picker v-model="startEndTime" type="daterange" range-separator="至" start-placeholder="开始日期"
                        end-placeholder="结束日期" unlink-panels :shortcuts="shortcuts" />
                    <el-button type="primary" class="ml-4" @click="getData">查询</el-button>
                </div>
            </div>
            <!-- <div class="flex flex-wrap">
                <div class="w-1/5 flex mb-6" v-for="(item, index) in basicList" :key="index">
                    <div class="mr-2">
                        <div class="rounded-full p-2" :style="{ 'background-color': colorList[index % 8] }">
                            <iconfont :iconName="item.icon" white className="text-6xl" />
                        </div>
                    </div>
                    <div>
                        <div class="text-info">{{ item.name }}</div>
                        <div class="text-6xl">{{ item.num }}</div>
                        <div class="text-info">环比增长: <span :class="item.percent > 0 ? 'text-success' : 'text-danger'">{{
                            item.percent }}%</span></div>
                    </div>
                </div>
            </div> -->
            <div>
                <v-charts style="height: 400px" :option="visitorOption" :autoresize="true" />
            </div>
        </el-card>
    </div>
</template>

<script lang="ts" setup name="statistics_user">
import { apiGetUserBasic, apiGetUserTrend, apiGetBottomTrade, apiGetTopTrade } from '@/api/workbench'
import moment from 'moment'
import vCharts from 'vue-echarts'

const shortcuts = [
    {
        text: '近一周',
        value: () => {
            const end = new Date()
            const start = new Date()
            start.setDate(start.getDate() - 7)
            return [start, end]
        },
    },
    {
        text: '近一月',
        value: () => {
            const end = new Date()
            const start = new Date()
            start.setMonth(start.getMonth() - 1)
            return [start, end]
        },
    },
    {
        text: '近三月',
        value: () => {
            const end = new Date()
            const start = new Date()
            start.setMonth(start.getMonth() - 3)
            return [start, end]
        },
    },
]

// 表单数据
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 leftInfo = ref({})
const leftOption: any = reactive({
    tooltip: {
        trigger: 'axis',
        axisPointer: {
            type: 'cross'
        },
        formatter: (params) => {
            let str = `<p><b>${params[0].name}点</b></p>`;
            params.forEach((item) => {
                str += `<p><span style="display:inline-block;width:8px;height:4px;background-color:${item.color};"></span> ${item.seriesName}: <strong>${item.value}</strong></p>`;
            });
            return str;
        }
    },
    grid: {
        left: '3%',
        right: '4%',
        bottom: '3%',
        containLabel: true
    },
    legend: {
        data: ['昨天', '今天'],
        left: '3%',
        top: '3%'
    },
    xAxis: [
        {
            type: 'category',
            boundaryGap: false,
            data: ['03', '04', '05', '06', '07', '08', '09', '10', '11', '12', '13', '14', '15', '16', '17', '18', '19', '20', '21', '22', '23', '00', '01', '02'],
            axisLabel: {
                show: false, // 不显示 x 轴标签
            },
            axisTick: {
                show: false, // 不显示 x 轴刻度线
            },
            axisLine: {
                show: false, // 不显示 x 轴线
            },
            grid: {
                show: false
            }
        }
    ],
    yAxis: [
        {
            type: 'value',
            show: false,
            spliteLine: {
                show: false
            },
            axisLabel: {
                show: false, // 不显示 x 轴标签
            },
            axisTick: {
                show: false, // 不显示 x 轴刻度线
            },
            axisLine: {
                show: false, // 不显示 x 轴线
            },
            grid: {
                show: false
            }
        },
    ],
    series: [
        {
            name: '昨天',
            type: 'line',
            data: [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 6, 0, 0, 0, 0, 0],
            smooth: true,
            symbol: 'none', // 不显示数据点
            itemStyle: {
                color: '#ccc',
            },
            areaStyle: {
                color: {
                    //线性渐变
                    type: 'linear',
                    x: 0,
                    y: 0,
                    x2: 0,
                    y2: 1,
                    colorStops: [{
                        offset: 0, color: '#ccc', // 0% 处的颜色
                    }, {
                        offset: 0.6, color: '#fff', // 100% 处的颜色
                    }],
                    global: false, // 缺省为 false
                },
            },
        },
        {
            name: '今天',
            type: 'line',
            data: [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 2, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
            smooth: true,
            symbol: 'none', // 不显示数据点
            areaStyle: {
                color: {
                    //线性渐变
                    type: 'linear',
                    x: 0,
                    y: 0,
                    x2: 0,
                    y2: 1,
                    colorStops: [{
                        offset: 0, color: '#5B8FF9', // 0% 处的颜色
                    }, {
                        offset: 0.6, color: '#fff', // 100% 处的颜色
                    }],
                    global: false, // 缺省为 false
                },
            },
        },
    ],
})

// 颜色
const colorList = ['#5DB1FF', '#4CD384', '#FFC46A', '#CAA5F1', '#FFC46A', '#4CD384', '#5DB1FF', '#CAA5F1']
// 商品浏览量, 商品访客数, 加购件数, 下单件数, 支付件数, 支付金额, 成本金额, 退款金额, 退款件数, 访客-支付转化率
const basicList = reactive([
    {
        name: '访客数',
        type: 'people',
        icon: 'RectangleCopy',
        num: 0,
        percent: 0
    },
    {
        name: '浏览量',
        type: 'browse',
        icon: 'RectangleCopy49',
        num: 0,
        percent: 0
    },
    {
        name: '新增用户数',
        type: 'newUser',
        icon: 'RectangleCopy53',
        num: 0,
        percent: 0
    },
    {
        name: '成交用户数',
        type: 'payPeople',
        icon: 'RectangleCopy5',
        num: 0,
        percent: 0
    },
    {
        name: '访客-支付转换率',
        type: 'payPercent',
        icon: 'RectangleCopy4',
        num: 0,
        percent: 0
    },
    {
        name: '激活付费会员数',
        type: 'payUser',
        icon: 'RectangleCopy7',
        num: 0,
        percent: 0
    },
    {
        name: '充值用户数',
        type: 'rechargePeople',
        icon: 'RectangleCopy59',
        num: 0,
        percent: 0
    },
    {
        name: '客单价',
        type: 'payPrice',
        icon: 'RectangleCopy15',
        num: 0,
        percent: 0
    },
    {
        name: '累计用户',
        type: 'cumulativeUser',
        icon: 'RectangleCopy54',
        num: 0,
        percent: 0
    },
    {
        name: '累计付费会员数',
        type: 'cumulativePayUser',
        icon: 'RectangleCopy7',
        num: 0,
        percent: 0
    },
    {
        name: '累计充值用户数',
        type: 'cumulativeRechargePeople',
        icon: 'RectangleCopy6',
        num: 0,
        percent: 0
    },
    {
        name: '累计成交用户数',
        type: 'cumulativePayPeople',
        icon: 'RectangleCopy9',
        num: 0,
        percent: 0
    }
])
const startEndTime = ref(['', '']);
// 获取数据
const getData = () => {
    let date = '';
    if (startEndTime.value[0] && startEndTime.value[1]) date = moment(startEndTime.value[0]).format('YYYY/MM/DD') + '-' + moment(startEndTime.value[1]).format('YYYY/MM/DD');
    apiGetUserBasic({
        date: date
    }).then(res => {
        basicList.forEach((item: any) => {
            item.num = res[item.type].num;
            item.percent = res[item.type].percent;
        })
    })
    apiGetTopTrade({}).then(res => {
        console.log(res);
        leftOption.series[0].data = res.left.series[1].value;
        leftOption.series[1].data = res.left.series[0].value;
    })
    apiGetBottomTrade({
        date: date
    }).then(res => {
        // 清空echarts 数据
        visitorOption.xAxis.data = []
        visitorOption.series = []

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

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

    })
}


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

<style lang="scss" scoped></style>
