import { createStore } from 'vuex'
import { getMenu, getPageContent } from '@/api/index'
export const store = createStore({
    state:{
        menuList:[],//菜单列表
        pageContent:[],//页面主体内容（不包含banner）
        recomMendation:[],//推荐内容
        currentPagePath:'',//当前页名称
        acitveId:0,//客户案例类型切换活动id
        sub:0,//当前页下标
        columnTypes:[],//页面下属类型
        scrollUp:{ //返回顶部
            opacity:0,
            visibility:"hidden",
            scrollTo:0
        },
        route:{},//跳转数据
        //分页数据
        pagination:{
            total:0,
            size:10,
            count:1
        },
        meta:{
            title:undefined,keywords:undefined,description:undefined
        },
        dialogPersonalStatus:false//弹框状态
    },
    mutations:{
        //菜单列表
        setMenuList(state:any,data:any){
            state.menuList = data
            return state.menuList
        },
        //页面主体内容
        setPageData(state:any,data:any){
            state.pageContent = data.records
            state.pagination.total = data.total
            return state.pageContent,state.pagination
        },
        //
        setCurrentPagePathOrSub(state:any,data:any){
            state.currentPagePath = data.index
            state.sub = data.sub
            return  state.index,state.sub 
        },
        //子页面活动状态
        setAcitveId(state:any,data:object){
            state.acitveId = data
            return state.acitveId
        },
        //推荐内容
        setRecomMendation(state:any,data:object){
            state.recomMendation = data
            return state.recomMendation
        },
        //设置弹框状态
        setDialogPersonalStatus(state:any,data:string){
            state.dialogPersonalStatus = data
            return state.dialogPersonalStatus
        },
        //设置页面下属类型
        setColumnTypes(state:any,data:string){
            state.columnTypes = data
            return state.columnTypes
        },
        //设置跳转路由参数
        setRoute(state:any,data:string){
            state.route = data
            return state.route
        },
        //设置meta
        setMeta(state:any,data:string){
            state.meta = data
            return state.meta
        },
        
    },
    actions:{
        getMenuList({ commit } :any,params:any){
            return new Promise(resolve=>{
                getMenu().then((res:any)=>{
                    let data = []
                    //映射路由地址兼容旧版本
                    for(let i=0;i<res.data.length;i++){
                        data.push(res.data[i])
                        if(data[i].categoryStyle == 1){
                            data[i].path = "/"
                            if(data[i].children.length>0){
                                for(let ii=0;ii<data[i].children.length;ii++){
                                    data[i].children[ii].path = "/?categoryid=" + data[i].children[ii].id
                                }
                            }
                        }
                        if(data[i].categoryStyle == 2){
                            data[i].path = "/cases"
                            if(data[i].children.length>0){
                                for(let ii=0;ii<data[i].children.length;ii++){
                                    data[i].children[ii].path = "/cases?categoryid=" + data[i].children[ii].id
                                }
                            }
                        }
                        if(data[i].categoryStyle == 3){
                            data[i].path = "/services"
                            if(data[i].children.length>0){
                                for(let ii=0;ii<data[i].children.length;ii++){
                                    data[i].children[ii].path = "/services?categoryid=" + data[i].children[ii].id
                                }
                            }
                        }
                        if(data[i].categoryStyle == 4){
                            data[i].path = "/news"
                            if(data[i].children.length>0){
                                for(let ii=0;ii<data[i].children.length;ii++){
                                    data[i].children[ii].path = "/news?categoryid=" + data[i].children[ii].id
                                }
                            }
                        }
                        if(data[i].categoryStyle == 5){
                            data[i].path = "/about"
                            if(data[i].children.length>0){
                                for(let ii=0;ii<data[i].children.length;ii++){
                                    data[i].children[ii].path = "/about?categoryid=" + data[i].children[ii].id
                                }
                            }
                        }
                    }
                    const menuList = res.data
                        for(let i=0;i<menuList.length;i++){
                            if((menuList[i].path == params.path) && !params.query.categoryid){//一级菜单
                                if(!params.query.dup && !menuList[i].sname){
                                    commit("setMeta",{title:menuList[i].categoryName,keywords:menuList[i].metakeywords,description:menuList[i].metadescription})
                                }else if(params.query.dup && menuList[i].sname){
                                    commit("setMeta",{title:menuList[i].categoryName,keywords:menuList[i].metakeywords,description:menuList[i].metadescription})
                                }
                            }else if(menuList[i].children.length>0){//二级菜单
                                for(let ii=0;ii<menuList[i].children.length;ii++){
                                    if(!params.query.dup){
                                        if(`${menuList[i].children[ii].path}` == `${params.path}?categoryid=${params.query.categoryid}`){ 
                                            commit("setMeta",{title:menuList[i].children[ii].categoryName,keywords:menuList[i].children[ii].metakeywords,description:menuList[i].children[ii].metadescription})
                                        }
                                    }
                                    if(!params.query.dup){
                                        let path = menuList[i].children[ii].path.split("?")
                                        if(`${path[0]}?dup=${menuList[i].children[ii].sname}` == `${params.path}?dup=${params.query.dup}`){ 
                                            commit("setMeta",{title:menuList[i].children[ii].categoryName,keywords:menuList[i].children[ii].metakeywords,description:menuList[i].children[ii].metadescription})
                                        }
                                    }
                                }
                            }
                        }
                    commit('setMenuList',res.data)
                    resolve(true)
                })
            })
        },
        /**
         * 获取页面数据
         * @param data.params 传参
         * @param data.index 页面 
         * @param data.sub 页面下标
         * @returns 
         */
        getPageData({ commit }:any,data:any){
            return new Promise(resolve=>{
                //当前页
                if(data.index || data.sub){
                    commit('setCurrentPagePathOrSub',{index:data.index,sub:data.sub})
                }
                //cases页类型活动id
                if(
                    data.params?.categoryid && (data.index == "/cases"  || 
                    data.index == "/services") || data.index == "/news" || 
                    data.index == "/news_Read" && data.params.acitve ||
                    data.index == "/services_Read" && data.params.acitve
                    ){
                    commit('setAcitveId',data.params.categoryid)
                }
                getPageContent(data.params).then((res:any)=>{
                    //当页主体数据
                    commit('setPageData',res.data) 
                    resolve(true)
                })
                //推荐列表查询
                if(data.params?.details){
                    let params =  JSON.parse(JSON.stringify(data.params))
                    delete params.articleid
                    params.pageSize = 100
                    delete params.id
                    delete params.details
                    getPageContent(params).then((res:any)=>{
                        commit('setRecomMendation',res.data.records)
                        resolve(true)
                    })
                }
            })
        },
    }
})