import store from '@/store/index'

export default {
  install (Vue) {
    //业务
    store.registerModule('enrol', {
      state: {
        //基础信息
        StdReg:{std_name:"Ghost"},//默认值
        StdMajor:null,
        dict:{},
        StdImageArray:[],
        StdImageMap:{},
        FrameParam:{},

        //公共业务
        isLoading: false,
        isUserRegisted: false,
        isEnroled: false,
        //报考
        curStep: "major",//报考当前步骤
        step:{
          categoryId: "",
          category:{},
          major:{},
          majorId: "",
          aspect:{},
          aspectId: "",
          subject:{},
          subjectId: "",
          subjectMap: {},
          subjectInited: false,
          agentId:"",
          agent:{},
          currentSubject:{},
          variety:{},
          varietyId: "",
          // tracks:[],
          varietyTracksMap:{},
          tracksArray:[],
          currentTracks:{},
          enrolId: ""
        },
      },
      mutations: {
        updateLoadingStatus (state, payload) {
          state.isLoading = payload.isLoading
        },

        updateIsUserRegisted (state, payload){
          state.isUserRegisted = payload.isUserRegisted
        },

        updateEnrolCurStep (state, payload){
          state.curStep = payload.curStep
        },

        updateEnrolStepData (state, payload){
          let stateProp = payload.stateProp;
          let value = payload.value;
          let obj = payload.obj;
          switch (stateProp) {
            case 'category':
              state.step.categoryId = value;
              if(obj) state.step.category = obj;

              // init data
              state.step.major={};
              state.step.majorId= "";
              state.step.aspect={};
              state.step.aspectId= "";
              state.step.subject={};
              state.step.subjectId= "";
              state.step.variety={};
              state.step.varietyId= "";
              state.step.varietyTracksMap={};
              state.step.tracksArray=[];
              state.step.currentTracks={};
              state.step.enrolId="";

              break;
            case 'major':
              state.step.majorId = value;
              if(obj) state.step.major = obj;
              break;
            case 'aspect':
              state.step.aspectId = value;
              if(obj){
                state.step.aspect = obj.aspect;
                if(obj.variety){
                  state.step.variety = obj.variety;
                  state.step.varietyId = obj.variety.variety_id;
                }
              } 
              break;
            case 'subject':
              state.step.subjectId = value;
              if(obj){
                if(obj.varietyTracksMap)
                  state.step.varietyTracksMap = obj.varietyTracksMap;
                if(obj.subjectMap){
                  state.step.subjectMap = obj.subjectMap;
                }
                if(obj.currentSubject)
                  state.step.currentSubject = obj.currentSubject; 
                  
                if(obj.subjectInited == true || obj.subjectInited == false)
                  state.step.subjectInited = obj.subjectInited;
              } 
              break;
            case 'tracks':
              if(obj){
                state.step.tracksArray = obj;
              } 
              if(payload.obj.currentTracks){
                state.step.currentTracks = payload.obj.currentTracks;
              }
              
              break;
            case 'agent':
              state.step.agentId = value;
              if(obj) state.step.agent = obj;
              break;
            case 'confirm':
              state.step.enrolId = value;
              break;
            default:
              break;
          }

        },

        updateStdReg (state, payload){
          state.StdReg = payload.StdReg;
          if(payload.StdMajor)
            state.StdMajor = payload.StdMajor;
        },

        updateStdMajor (state, payload){
          state.StdMajor = payload.StdMajor;
          let majorLevelObj = _.find(state.dict['majorLevelArray'],{"dict_value":payload.StdMajor.major_level+ ''})
          if(majorLevelObj)
            payload.StdMajor.major_level_name = majorLevelObj.dict_text;
          state.StdMajor = payload.StdMajor;
        },


        updateInitData (state, payload){
          state.StdReg = payload.StdReg;
          state.dict = payload.dict;
          if(payload.StdMajor){
            let majorLevelObj = _.find(payload.dict['majorLevelArray'],{"dict_value":payload.StdMajor.major_level+ ''})
            payload.StdMajor.major_level_name = majorLevelObj.dict_text;
            state.StdMajor = payload.StdMajor;
          }

          // state.StdImageArray = payload.StdImageArray;
          // payload.StdImageArray.forEach((item)=>{
          //   state.StdImageMap[item.material_type] = item;
          // })
          // payload.FrameParamArray.forEach((item)=>{
          //   state.FrameParam[item.param_name] = item.param_value;
          // })
          // state.FrameParamArray = payload.FrameParamArray;
        },

        updateIsEnroled (state, payload){
          state.isEnroled = payload.isEnroled;
        },

        initCertNoMustUpdate (state, payload){
          
          state.StdReg.cert_no_must_update = payload.cert_no_must_update;
        }

        
      },
      actions: {
        // updateDemoPosition ({commit}, top) {
        //   commit({type: 'updateDemoPosition', top: top})
        // }
      },
    })
    
  }
}
