/**
 * 订单木块方法
 */
import { Ref, ref } from "vue";
import order from "@/api/https/order"
import { getStorage } from '@/utils/storage'
import { onShow } from "@dcloudio/uni-app";


function tabFn() {
    onShow(async () => {
        await getOrderCount()
    })


    /**
     * tab 菜单栏数据
     */
    const currentStatus: Ref<number> = ref(2);
    interface OrderStatusType {
        id: number;
        status: number | Array<number>;
        name: string;
        count: number;
    }
    const orderStatus = ref<OrderStatusType[]>([
        {
            id: 1,
            status: [3],
            name: "待接单",
            count: 0,
        },
        {
            id: 2,
            status: [10],
            name: "待拣货",
            count: 0,
        },
        {
            id: 3,
            status: [15],
            name: "待配送",
            count: 0,
        },
        {
            id: 4,
            status: [20],
            name: "配送中",
            count: 0,
        },
        {
            id: 5,
            status: [5, 10, 15, 20, 17, 18, 22],
            name: "异常单",
            count: 0,
        },
        {
            id: 6,
            status: [155, 160, 165, 167, 180, 190],
            name: "售后单",
            count: 0,
        },
        {
            id: 7,
            status: [110, 115, 120],
            name: "已完成",
            count: 0,
        },
    ]);


    /**
     * tab 点击事件
     */
    const scrollLeft: Ref<number> = ref(0); // 滚动到左边距离
    const record: Ref<number> = ref(1); // 记录值
    function clickTab(id: number, recordID: number, e: AnyObject) {
        currentStatus.value = id;
        // 判断左右滑动
        let { offsetLeft } = e.target;
        if (id > recordID) {
            scrollLeft.value = offsetLeft - 75;
        } else {
            scrollLeft.value = offsetLeft - 75 * 3;
        }
        record.value = id; // 赋值记录纸
        // 更新条数
        getOrderCount()
    }


    /**
     * 请求订单数量信息
     */
    async function getOrderCount() {
        let orderData = {
            storeID: getStorage('storeID'),
            lastHours: new Date().getHours() * 1 + 48
        }
        // 订单数量
        let orderCount = await order.Get_store_rder_count_info(orderData)
        // 售后单量
        let afsCount = await order.Get_store_afs_order_countinfo(orderData)
        if (orderCount.code == 0 && afsCount.code == 0) {
            // 没有数据默认一个空数组
            let data = orderCount.data || []
            let data2 = afsCount.data || []

            // 判断显示条数
            if (data.length > 0) {
                // 3 待接单
                orderStatus.value[0].count = data.filter((item: any) => item.status === 3 && item.lockStatus === 0).length > 0 ? data.filter((item: any) => item.status === 3 && item.lockStatus === 0)[0].count : 0
                orderStatus.value[0].count += data.filter((item: any) => item.status === 3 && item.lockStatus === -20).length > 0 ? data.filter((item: any) => item.status === 3 && item.lockStatus === 0)[0].count : 0
                // 10 待拣货
                orderStatus.value[1].count = data.filter((item: any) => item.status === 10 && item.lockStatus === 0).length > 0 ? data.filter((item: any) => item.status === 10 && item.lockStatus === 0)[0].count : 0
                orderStatus.value[1].count += data.filter((item: any) => item.status === 10 && item.lockStatus === -20).length > 0 ? data.filter((item: any) => item.status === 10 && item.lockStatus === 0)[0].count : 0
                // 15 待配送
                orderStatus.value[2].count = data.filter((item: any) => item.status === 15 && item.lockStatus === 0).length > 0 ? data.filter((item: any) => item.status === 15 && item.lockStatus === 0)[0].count : 0
                orderStatus.value[2].count += data.filter((item: any) => item.status === 15 && item.lockStatus === -20).length > 0 ? data.filter((item: any) => item.status === 15 && item.lockStatus === -20)[0].count : 0
                // 20 配送中
                orderStatus.value[3].count = data.filter((item: any) => item.status === 20 && item.lockStatus === 0).length > 0 ? data.filter((item: any) => item.status === 20 && item.lockStatus === 0)[0].count : 0
                orderStatus.value[3].count += data.filter((item: any) => item.status === 20 && item.lockStatus === -20).length > 0 ? data.filter((item: any) => item.status === 20 && item.lockStatus === -20)[0].count : 0
                // 17 异常单
                orderStatus.value[4].count = data.filter((item: any) => item.status === 17 && item.lockStatus === 0).length > 0 ? data.filter((item: any) => item.status === 17 && item.lockStatus === 0)[0].count : 0
                // 18 已完成
                orderStatus.value[4].count += data.filter((item: any) => item.status === 18 && item.lockStatus === 0).length > 0 ? data.filter((item: any) => item.status === 18 && item.lockStatus === 0)[0].count : 0
                // 22 已完成
                orderStatus.value[4].count += data.filter((item: any) => item.status === 22 && item.lockStatus === 0).length > 0 ? data.filter((item: any) => item.status === 22 && item.lockStatus === 0)[0].count : 0
                orderStatus.value[4].count += data.filter((item: any) => item.lockStatus === -5 && item.status < 100).length > 0 ? data.filter((item: any) => item.lockStatus === -5 && item.status < 100)[0].count : 0
            } else {
                orderStatus.value[0].count = 0
                orderStatus.value[1].count = 0
                orderStatus.value[2].count = 0
                orderStatus.value[3].count = 0
                orderStatus.value[4].count = 0
            }
            // 售后单
            if (data2.length > 0) {
                orderStatus.value[5].count = data2.find((item: any) => item.status === 155) ? data2.find((item: any) => item.status === 155).count : 0
                orderStatus.value[5].count += data2.find((item: any) => item.status === 165) ? data2.find((item: any) => item.status === 155).count : 0
            } else {
                orderStatus.value[5].count = 0
            }
        }
    }


    /**
     * 导出数据
     */
    return {
        currentStatus,          // 高亮初始值
        orderStatus,            // tab 菜单栏数据
        scrollLeft,             // 滚动到左边距离
        record,                 // 记录值
        clickTab,               // tab 点击事件
        getOrderCount,          // 获取数据
    }
}


export default tabFn