import { computed, ref } from 'vue'
import { onLoad } from "@dcloudio/uni-app";
import { getStorage } from '@/utils/storage';
import merchant from '@/api/https/merchant';
import useGlobalFunc from '@/composables/useGlobalFunc';
import { store } from "@/store";

export default {
    setup() {
        // const { isTxd } = useGlobalFunc()

        // ******************** 时间选择组件操作 *****************************
        //#region 
        const dateTimePicker = ref<any>(null)   // 时间组件实例
        const timeActive = ref<number>(0) // 时间选择高亮
        const timeIng = ref<string>('00:00') // 选择器的当前时间
        // 确认选择时间
        function confirm(e: AnyObject): void {
            switch (timeActive.value) {
                case 1:
                    businessTime.value.timer1 = e.value
                    break
                case 2:
                    businessTime.value.timer2 = e.value
                    break
                case 3:
                    businessTime.value.timer3 = e.value
                    break
                case 4:
                    businessTime.value.timer4 = e.value
                    break
            }
        }
        // 打开时间设置
        function setTime(type: number): void {
            timeActive.value = type
            let time = businessTime.value
            switch (type) {
                case 1:
                    timeIng.value = time.timer1
                    break
                case 2:
                    timeIng.value = time.timer2
                    break
                case 3:
                    timeIng.value = time.timer3
                    break
                case 4:
                    timeIng.value = time.timer4
                    break
            }
            dateTimePicker.value.open()
        }
        // 时间标题
        const timeTitle = computed(() => {
            let type = timeActive.value
            if (type == 1 || type == 3) {
                return '选择开始时间'
            } else {
                return '选择结束时间'
            }
        })
        //#endregion 


        // ******************** 获取门店营业时间 *****************************
        //#region 
        const businessTime = ref<AnyObject>({})
		onLoad(async() => {
            store.commit('storeInfo/jxLoadingFn', true)
            if(!store.getters['storeInfo/businessHours']) await store.dispatch('storeInfo/getOneStore',getStorage("storeID"))
            businessTime.value = store.getters['storeInfo/businessHours']
            store.commit('storeInfo/jxLoadingFn', false)
        })
        //#endregion 


        // ******************** 保存营业时间 *****************************
        //#region 
        async function saveBusinessTime() {
            let time = businessTime.value
            // 一开始时间大于结束时间
            if (formatTime(time.timer1) >= formatTime(time.timer2)) {
                return uni.jxAlert({
                    title: '营业时间一',
                    content: `开始时间不能大于结束时间，您可以修改为【${time.timer2}至${time.timer1}】`
                })
            }
            // 第二段时间小于第一段时间
            if (time.timer3 != '00:00' && time.timer4 != '00:00') {
                if (formatTime(time.timer3) < formatTime(time.timer2)) {
                    return uni.jxAlert({
                        title: '提示',
                        content: `第二段营业时间不能小于第一段营业时间，您可以修改到【${time.timer2}】点之后`
                    })
                }
            }
            // 二开始时间大于结束时间
            if (formatTime(time.timer3) > formatTime(time.timer4)) {
                return uni.jxAlert({
                    title: '营业时间二',
                    content: `开始时间不能大于结束时间，您可以修改为【${time.timer3}至${time.timer4}】`
                })
            }

            let data = {
                storeID: getStorage("storeID"),
                payload: JSON.stringify({
                    openTime1: formatTime(time.timer1),
                    closeTime1: formatTime(time.timer2),
                    openTime2: formatTime(time.timer3),
                    closeTime2: formatTime(time.timer4),
                }),
            }
            // setTxdIngTime(time)
            let updateTimerRes = await merchant.update_store(data)
            if (updateTimerRes.code == 0) {
                uni.jxAlert({
                    title: '提示',
                    content: '修改成功，请返回下拉刷新',
                    success: () => {
                        uni.navigateBack()
                    }
                })
            } else {
                uni.jxAlert({
                    title: '操作提示',
                    content: `提示：${updateTimerRes.desc}`,
                    success: () => {
                        uni.navigateBack()
                    }
                })
            }
        }
        // 格式化数据
        function formatTime(time: string): number {
            let newTime = time.slice(0, 2) + time.slice(-2)
            return +newTime
        }
        //#endregion 


        // ******************** 修改淘鲜达线上时间 *****************************
        //#region 
        // async function setTxdIngTime(time: AnyObject) {
        //     // if (isTxd()) {
        //     //     let data = {
        //     //         vendorOrgCode: 34402634,
        //     //         txdStores: JSON.stringify({
        //     //             flag: [2],
        //     //             txdStoreID: `JX${getStorage('storeID')}`,
        //     //             startTime: time.timer1,
        //     //             endTime: time.timer2
        //     //         })
        //     //     }
        //     //     await merchant.update_txd_store(data)
        //     // } else {
        //     //     console.log('此店铺暂无淘鲜达店铺')
        //     // }
        // }
        //#endregion 


        return {
            dateTimePicker,             // 时间组件实例
            confirm,                    // 选择时间val
            businessTime,               // 营业时间
            timeActive,                 // 时间选择高亮 
            setTime,                    // 打开时间设置
            timeTitle,                  // 选择器标题
            saveBusinessTime,           // 确定修改时间
            timeIng,                    // 选择器的当前时间
        }
    }
}