/*
 * @Author: lixg lixg@dongwu-inc.com
 * @Date: 2023-01-17 19:47:07
 * @LastEditors: lixg lixg@dongwu-inc.com
 * @LastEditTime: 2023-03-04 19:01:36
 * @FilePath: /stu_h5/src/store/index.js
 * @Description: 这是默认设置,请设置`customMade`, 打开koroFileHeader查看配置 进行设置: https://github.com/OBKoro1/koro1FileHeader/wiki/%E9%85%8D%E7%BD%AE
 */
import { createStore } from 'vuex'
import { PROJECT_PROCESS, ROUTER_PROCESS } from "@/api/api";
import { request } from "@/api/request";
import { TASK_TYPES } from "@/api/CONST";

export default createStore({
  state: {
    userInfo: {},
    projectInfo: {},
    routerInfo: {}
  },
  getters: {

  },
  mutations: {
    SET_USER(state, userInfo) {
      state.userInfo = userInfo
    },
    SET_PROJECT_INFO(state, info) {
      state.projectInfo = info
    },
    INIT_PROJECT_INFO(state) {
      if (state.projectInfo.status === -1) {
        state.projectInfo.stageProcessList.forEach((t) => {
          t.statusName = '已结束';
          t.taskProcessList?.forEach((s) => s.statusName = '已结束')
        })
        return
      }
      if (state.projectInfo.unlockMode === 1) {
        state.projectInfo.stageProcessList.forEach((t) => {
          if(t.studyModel == 1){
            state.projectInfo.stageProcessList?.forEach((t1) => {
              t1.statusName = "已完成";
              const stageState = t1.taskProcessList?.some((s) => {
                s.unlock = true;
                s.statusName = "已完成";
                s.status !== 1 && (s.statusName = TASK_TYPES.toName[s.type])
                return state.projectInfo.unlockMode === 1 ? s.status !== 1 : (s.status !== 1 && s.flag)
              });
              stageState && (t1.statusName = "进行中");
              return stageState;
            });
          }
          else{
            t.statusName = "进行中";
            t.taskProcessList?.forEach((s) => s.statusName = (s.status === 1) ? "已完成" : s.status === 2 ? (s.statusName = "进行中") : TASK_TYPES.toName[s.type]);
            t.taskProcessList?.every((s) => s.status === 1) && (t.statusName = "已完成");
          }
        });
        return;
      }
      state.projectInfo.stageProcessList.forEach((item,i)=>{
        if(item.studyModel == 1){
          state.projectInfo.stageProcessList?.some((t) => {
            t.statusName = "已完成";
            const stageState = t.taskProcessList?.some((s) => {
              s.unlock = true;
              s.statusName = "已完成";
              s.status !== 1 && (s.statusName = s.status==2? '进行中' : TASK_TYPES.toName[s.type])
              return state.projectInfo.unlockMode === 2 ? s.status !== 1 : (s.status !== 1 && s.flag)
            });
            stageState && (t.statusName = "进行中");
            return stageState;
          });
        }else{
          if(i == 0){
            item.statusName = "进行中";
            item.taskProcessList?.forEach((s) => s.statusName = (s.status === 1) ? "已完成" : s.status === 2 ? (s.statusName = "进行中") : TASK_TYPES.toName[s.type]);
            item.taskProcessList?.every((s) => s.status === 1) && (item.statusName = "已完成");
          }
          if(i > 0){
            if(state.projectInfo.stageProcessList[i-1].statusName == "已完成"){
              item.statusName = "进行中";
              item.taskProcessList?.forEach((s) => s.statusName = (s.status === 1) ? "已完成" : s.status === 2 ? (s.statusName = "进行中") : TASK_TYPES.toName[s.type]);
              item.taskProcessList?.every((s) => s.status === 1) && (item.statusName = "已完成");
            }else{
              item.statusName = "未解锁";
            }
          }
        }
      })
    },
    SET_ROUTER_INFO(state, info) {
      state.routerInfo = info
    },
    INIT_ROUTER_INFO(state) {
      // state.routerInfo.unlockMode 1自由模式 2闯关模式 3 闯关模式 必修 flag true
      if (state.routerInfo.status === -1) {
        state.routerInfo.statusName = '已结束'
        state.routerInfo.taskBoList.forEach((t) => t.statusName = '已结束')
        return
      }
      state.routerInfo.statusName = '进行中'
      if (state.routerInfo.unlockMode === 1) {
        state.routerInfo.taskBoList?.forEach((s) => s.statusName = (s.status === 1) ? '已完成' : TASK_TYPES.toName[s.type])
        state.routerInfo.taskBoList?.every((s) => s.status === 1) && (state.routerInfo.statusName = '已完成')
        return
      }
      state.routerInfo.statusName = '已完成'
      state.routerInfo.taskBoList?.some((s) => {
        s.unlock = true
        s.statusName = '已完成'
        s.status !== 1 && (s.statusName = TASK_TYPES.toName[s.type])
        return state.routerInfo.unlockMode === 2 ? s.status !== 1 : (s.status !== 1 && s.flag)
      }) && (state.routerInfo.statusName = '进行中')
    },
  },
  actions: {
    getProjectInfo(content, { projectId, type }) {
      request(PROJECT_PROCESS, { projectId, type }).then(res => {
        content.commit('SET_PROJECT_INFO', res.data)
        content.commit('INIT_PROJECT_INFO')
      })
    },
    getRouterInfo(content, { routerId, chapterId }) {
      request(ROUTER_PROCESS, { routerId, chapterId }).then(res => {
        content.commit('SET_ROUTER_INFO', res.data)
        content.commit('INIT_ROUTER_INFO')
      })
    },
  },
  modules: {}
})
