/**
 *@description: 切换门店
 *@return {*}
 *@param {}-
*/
import login from "@/api/https/login"
import { onBeforeUnmount, Ref, ref } from "vue"
import toast from "@/utils/toast"
import configCms1 from "@/utils/configCms"
import { setStorage } from "@/utils/storage"
import { onLoad, onPullDownRefresh } from '@dcloudio/uni-app'
import useGlobalFunc from "@/composables/useGlobalFunc"

const configCms:AnyObject = configCms1

function switchStoreFn() {
    const { logOutFn } = useGlobalFunc()

    onLoad(async () => {
        await objTarrau()
        await getStore()
    })

    /*************************************************
     * 下拉刷新
    */
    let onPullDownRefreshTimer: any = null
    onPullDownRefresh(() => {
        clearTimeout(onPullDownRefreshTimer)
        onPullDownRefreshTimer = setTimeout(async () => {
            uni.stopPullDownRefresh()
            await getStore()
        }, 1000)
    })

    /**
     * 获取门店信息
     */
    const storeDataList = ref<AnyObject[]>()
    async function getStore() {
        let data = {
            version: '1.3.5'
        }
        let storeRes = await login.get_my_store_list(data)
        if (storeRes.code == 0) {
            let data: Array<any> = storeRes.data || []
            if (data.length > 0) {
                storeDataList.value = data
                filterStoreDataList.value = data
            } else {
                uni.jxAlert({
                    title: '提示',
                    content: '您还没有绑定门店，请联系运营！',
                    success: () => {
                        logOutFn()
                        uni.reLaunch({
                            url: '/subPages/login/wxLogin/wxLogin',
                        })
                    }
                })
            }
        } else {
            storeDataList.value = []
            toast("获取门店信息失败")
            uni.jxAlert({
                title: '警告',
                content: '获取门店信息失败，重新登录试试！',
                success: () => {
                    logOutFn()
                    uni.reLaunch({
                        url: '/subPages/login/wxLogin/wxLogin',
                    })
                }
            })
        }
    }

    /**
     * 进入店铺
     */
    function switchStore(id: string, name: string) {
        uni.jxConfirm({
            title: '提示',
            content: `是否进入【${name}】`,
            success: () => {
                setStorage("storeID", id)
                setStorage('storeName', name)
                uni.switchTab({ url: '/pages/merchant/index' })
            },
        })
    }

    /**
     * 筛选方法
     */
    const status: Ref<string> = ref("0")
    function changeStatus(e: AnyObject) {
        keyword.value = ""
        status.value = e.detail.value
        if (e.detail.value == 0) {
            getStore()
        }
        if (e.detail.value == 1) {
            // 临时休息
            storeDataList.value = storeDataList.value?.filter((item) => {
                return item.status == "0"
            })
        }
        if (e.detail.value == 2) {
            getStore()
            // 营业中
            storeDataList.value = storeDataList.value?.filter((item) => {
                return item.status == "1"
            })
        }
        if (e.detail.value == 3) {
            // 休息
            storeDataList.value = storeDataList.value?.filter((item) => {
                return item.status == "-1"
            })
        }
        if (e.detail.value == 4) {
            // 禁用
            storeDataList.value = storeDataList.value?.filter((item) => {
                return item.status == "-2"
            })
        }
    }

    /**
     * 通过搜索筛选
     */
    const keyword: Ref<string> = ref("")
    const filterStoreDataList = ref<AnyObject[]>()
    function searchStore(keyword: string) {
        if (keyword == "") {
            status.value = "0"
            return getStore()
        }
        filterStoreDataList.value = storeDataList.value?.filter((item) => {
            if (JSON.stringify(item).indexOf(keyword) > -1) {
                return true
            }
        })
    }

    /**
     * 清空搜索框
     */
    function clearInput() {
        keyword.value = ""
        status.value = "0"
        getStore()
    }

    /**
     * 获取筛选数据列表
     */
    const pickerDataList: Ref<string[]> = ref([])
    function objTarrau() {
        pickerDataList.value.push("全部")
        for (let key in configCms.storeStatus) {
            pickerDataList.value.push(configCms.storeStatus[key])
        }
    }

    /*************************************************
     * 收尾工作
    */
    onBeforeUnmount(() => {
        clearTimeout(onPullDownRefreshTimer)
    })

    return {
        filterStoreDataList,    // 门店信息 存储
        switchStore,            // 进入门店
        status,                 // 门店状态
        changeStatus,           // 选择框过滤门店
        keyword,                // 搜索框关键词
        searchStore,            // 搜索框搜索门店
        clearInput,             // 清空文本框操作
        pickerDataList,         // 筛选框 存储
    }
}

export default switchStoreFn