import { useRoute } from 'vue-router'

import { createStore } from 'vuex'

// import { ActionContext } from 'vuex'
import type { App } from 'vue'

import {
    getEvaluationTaskDetailApi,
} from '@/api/evaluationDataComponent'
import { ElMessage } from 'element-plus'
 

interface HorizontalMenuItem {
  // 当前活跃的步骤 名称
  currentActiveStepName:string, 
  evaluationManageDetailName:string,  // 评价管理任务名称
  evaluationType:string |number,   // 评价方式  0-线上匿名，1-线下答卷，2-线上导入，3-线上实名

  isHeadTeacherInvolved:boolean,    //班主任是否参与评价
  isGenerateAccountByGender:boolean,  // 是否按男女生成评价账号
  isGenerateReviewCode:boolean,  //是否已生成评价码
  // 各个步骤的完成状态
  isFinishEvaluationData:boolean,  // 评价数据是否完成
  isFinishTeachingRelationship:boolean, // 授课关系是否完成
  isFinishQuestionnaireTemplate:boolean,  //问卷模板是否完成
  isFinishReviewAccount:boolean,  //评价账号是否完成
  isFinishParameterSetting:boolean,   //参数设置是否完成
  isFinishEvaluationProgress:boolean, //评价进度是否完成
  isFinsihGenerateAnalysis:boolean,  //生成分析是否完成

  isAllowDeleteOrEdit:boolean,  //任务是否允许修改或删除



}
// 定义 RootState 接口（可扩展）
interface RootState {
  count: number,
  ordinaryPeopleHorizontalMenuItems:any[],
  managerHorizontalMenuItems:any[],
  evaluationManageStepData:HorizontalMenuItem

}

const route = useRoute()


// type StoreActionContext = ActionContext<RootState, RootState>
// 创建 store 实例
const store = createStore<RootState>({
  state: () => ({
    count: 0,
    // 管理员 评教评学 的访问菜单
    managerHorizontalMenuItems:[
      {
        title:'问卷管理',
        route:`/main`,
        childrenRoute:[
          '/main/createQuestionnaire',
          '/main/previewQuestionnaire'
        ]
      },
      {
        title:'评价管理',
        route:`/main/reviewManagement`,
        childrenRoute:[
          '/main/reviewManagementDetail'
        ]
      },
      {
        title:'评价分析',
        route:`/main/reviewAnalysis`,
        childrenRoute:[]
      },
      {
        title:'个人中心',
        route:`/main/personalCenter`,
        childrenRoute:[]
      },
    ],

    // 普通教师 评教评学 的访问菜单
    ordinaryPeopleHorizontalMenuItems: [
      {
        title:'评价分析',
        route:`/main/reviewAnalysis`,
        childrenRoute:[]
      },
      {
        title:'个人中心',
        route:`/main/personalCenter`,
        childrenRoute:[]
      },
    ],
    evaluationManageStepData:{}  //评价管理详情 全局数据

  }),
  mutations: {
    increment(state: RootState) {
      state.count++
    },
    updateEvaluationManageStepData(state,newData){  //更新评价管理详情的数据
      state.evaluationManageStepData = {
        ...state.evaluationManageStepData,
        ...newData
      }
    }
  },
  actions: {
    increment({ commit }) {
      commit('increment')
    },
    //更新评价管理详情的数据
    async updateEvaluationManageStepData({commit},evaluationManagementId){
      console.log('路由参数',evaluationManagementId)
      if(!evaluationManagementId){
        ElMessage.warning('请传参')
        return
      }
      let res = await getEvaluationTaskDetailApi(evaluationManagementId)
      if(res?.code === 200){
        let newData = {
          evaluationManageDetailName:res.data.evaluationName,
          evaluationType:res.data.evaluationType,
          isHeadTeacherInvolved:Boolean(res.data.classMasterStatus),
          isGenerateAccountByGender:Boolean(res.data.genderStatus),
          isGenerateReviewCode:Boolean(res.data.evaluationCodeStatus),
          isFinishEvaluationData:Boolean(res.data.evaluationDataStatus),
          isFinishTeachingRelationship:Boolean(res.data.teachingRelationStatus),
          isFinishQuestionnaireTemplate:Boolean(res.data.assessmentTemplateStatus),
          isFinishReviewAccount:Boolean(res.data.evaluationAccountStatus),
          isFinishParameterSetting:Boolean(res.data.paramSetupStatus),
          isFinishEvaluationProgress:Boolean(res.data.evaluationProgressStatus),
          isFinsihGenerateAnalysis:Boolean(res.data.analysisStatus),
          isAllowDeleteOrEdit:!Boolean(res.data.status),   // status  0 可以  1 不可以


        }
        commit('updateEvaluationManageStepData',newData)
      }
    }
  },
  getters: {
    count: (state: RootState) => state.count,
    getDemo:(state)=>()=>{
      return  state.managerHorizontalMenuItems.map(item=>{
          return item
      })
    },
    getEvaluationManageStepData:(state)=>state.evaluationManageStepData
  }
})

// 提供一个方法用于注册到 Vue 应用中
export function setupStore(app: App) {
  app.use(store)
}

export default store