import { defineStore } from "pinia"
import { ref } from "vue"
import areaList from "@/utils/areaList.js"

export const useAppStore = defineStore('app', () => {
    const map_info = ref(localStorage.getItem('map_info') || 'luxian');

    const setMapInfo = (e) => {
        map_info.value = e;
        localStorage.setItem('map_info', e);
    }

    const address = ref(JSON.parse(localStorage.getItem('address') || '{}'));
    if (!address.value.areaCode) {
        address.value = { areaCode: 510524, streetCode: 510524100 };
    }

    // 测试使用的初始化配置
    // address.value = { areaCode: 510524, streetCode: 510524100 };

    const setAddress = (e) => {
        console.log('更新', e);
        address.value.areaCode = e.areaCode;
        address.value.streetCode = e.streetCode;
        localStorage.setItem('address', JSON.stringify(e));
    }

    const delivery = ref({
        pending_order_count: 0,
        delivering_order_count: 0,
        finished_order_count: 0,
        order_count: 0
    })

    const setDelivery = (e) => {
        e.pending_order_count ? delivery.value.pending_order_count = e.pending_order_count : null;
        e.delivering_order_count ? delivery.value.delivering_order_count = e.delivering_order_count : null;
        e.finished_order_count ? delivery.value.finished_order_count = e.finished_order_count : null;
        e.order_count ? delivery.value.order_count = e.order_count : null;
    }

    const delivery_address = ref(JSON.parse(localStorage.getItem('delivery_address') || '{}'));

    // if (!delivery_address.value.areaCode) {
    //     delivery_address.value = { areaCode: 510524, streetCode: 510524100 };
    // }

    const setDeliveryAddress = (e) => {
        delivery_address.value = e;
        localStorage.setItem('delivery_address', JSON.stringify(e));
    }

    const area = ref(areaList);
    const setArea = (data) => {
        area.value = data;
    }

    const street = ref([]);
    const setStreet = (data) => {
        street.value = data;
    }

    return {
        map_info,
        address,
        delivery,
        delivery_address,
        area,
        street,
        setMapInfo,
        setAddress,
        setDelivery,
        setDeliveryAddress,
        setArea,
        setStreet
    }
})