import merchant from '@/api/https/merchant'
import toast from '@/utils/toast'
import { getStorage, setStorage } from '@/utils/storage'

/**
 * storeInfoStore 模块的 actions
 */
export default {
    /**
     * 获取单门店信息
     */
    getOneStore: async (store: any, storeID: number) => {
        let stateRes = await merchant.get_stores({
            storeID: storeID
        });
        if (stateRes.code == 0) {
            let stateData = stateRes.data.stores[0];
           
            store.commit('setCreateStoreTimer', stateData.createdAt)  // 门店创建时间(存vueX)
            
            store.commit('setStoreStatus', stateData.status)   // 门店营业状态(存vueX) 本地
            
            store.commit('setAllStoreInfo', stateData)    // 门店所有数据(存vueX)

            // 三方平台门店信息
            if(stateData.StoreMaps && stateData.StoreMaps.length>0){
                // 取出所有第三方门店(美团/饿了么)
                let vendorStoreIDS: any = {}
                let platformStore:any = {}
                for(let i =0;i<stateData.StoreMaps.length;i++){
                    const element = stateData.StoreMaps[i]
                    let obj = {
                        vendorStoreID:element.vendorStoreID,
                        vendorID:element.vendorID,
                        vendorOrgCode:element.vendorOrgCode
                    }
                    platformStore[element.vendorID] = {...element}
                    // 美团
                    if (element.vendorID == 1) {
                        vendorStoreIDS.MT = element.vendorStoreID
                        vendorStoreIDS.appID = element.vendorOrgCode
                        let res = await merchant.get_vendor_store(obj)
                        if(res.code == '0' ) platformStore[element.vendorID].status = res.data.bussinessStatus
                    }
                    // 饿了么
                    if (element.vendorID == 3) {
                        vendorStoreIDS.ELM = element.vendorStoreID
                        let res = await merchant.get_vendor_store(obj)
                        if(res.code == '0' ) platformStore[element.vendorID].status = res.data.bussinessStatus
                    }
                }
                store.commit('setPlatformInfo', platformStore) // 三方门店的所有信息
                store.commit('setVendorStoreIDS', vendorStoreIDS) // 三方门店ID(存vueX)[美团/饿了么]    
            }

            // 保存门店经纬度地址
            setStorage("lat", stateData.lat)
            setStorage("lng", stateData.lng)
        } else {
            toast("系统异常", 2);
        }
    }
}