// import { fetchList, fetchColumns } from '@main/api/portal'
import { getPortal as fetchList, addPortal,updatePortal , getSysPortal } from "../../api/portal";
import { getPortalMenus as fetchColumns } from '../../api/menu'
const portal = {
  namespaced: true,
  state: {
    record:null,
    list: [],
    sysList:[],
    columns: []
  },
  mutations: {
    SET_record: (state, list) => {
      state.record = list
    },
    SET_list: (state, list) => {
      state.list = list
    },
    SET_sysList: (state, list) => {
      state.sysList = list
    },
    SET_columns: (state, columns) => {
      state.columns = columns
    }
  },
  actions: {
    setList({ commit, state }, data) {
      commit('SET_list', data);
    },
    //创建门户,功能选择 
    async loadColumns({ commit, state }) {

      // console.log('\n portal 加载门户  相关栏目功能列表')

      await fetchColumns().then(res => {
        const data = res.data.dataPack.rows;
        commit('SET_columns', data);
        return data;
      });
    },
    //取已配好的门户
    loadList({ commit, state }) {

      // console.log('\n portal 加载用户自定义 门户 ')
      
      commit('SET_list', []);
      return new Promise((resolve, reject)=>{
        fetchList().then(res => {
          const records = res.data.dataPack.rows;
          if(records.length==0){
            return;
          }
          const item = records[records.length-1];
          let data=[];
          if(item){
            if(typeof item.package == 'object'){
              data = item.package;
            }else{
              data = JSON.parse(item.package);
            }
            
          }
          commit('SET_record', item);
          commit('SET_list', data);
          resolve(data)
          return data;
        });

      });


      // await fetchList().then(res => {
      //   const records = res.data.dataPack.rows;
      //   if(records.length==0){
      //     return;
      //   }
      //   const item = records[records.length-1];
      //   let data=[];
      //   if(item){
      //     data = JSON.parse(item.package);
      //   }
      //   commit('SET_record', item);
      //   commit('SET_list', data);
      //   return data;
      // });
    },
    //取已配好的系统 门户
    async loadSysList({ commit, state }) {
      // console.log('\n portal 加载系统 门户 ')
      
      commit('SET_sysList', []);
      await getSysPortal().then(res => {
        const records = res.data.dataPack.rows;
        if(records.length==0){
          return;
        }
        const item = records[records.length-1];
        let data=[];
        if(item){
          if(typeof item.package =="object"){
            data = item.package;
          }else{
            data = JSON.parse(item.package);
          }
        }
        commit('SET_sysList', data);
        return data;
      });
    },
    //保存门户
    async savePortal({ commit, state },data=[]){ 
      let me =this;
      if(state.record){
        await updatePortal(state.record, {
          fuser: me.state.user.id, //id
          userBz:me.state.user.bzId,  //bzId
          package: JSON.stringify(data)
        }).then(res => {
          console.info('\n 更新门户 updatePortal callback:',res);
        });
      }else{
        let record = {
          FID: new Date().valueOf(),
          package:JSON.stringify(data),
          fuser: me.state.user.id, //id
          userBz:me.state.user.bzId  //bzId
        };        
        await addPortal(record).then(res => {
          console.info('保存 addPortal callback:',res);
        });
      };
    }
    
  }
}

export default portal


