import myMockData from "@/api/studentWeb";
import Vue from "vue";
import i18n from '@/locale'
var getfinishedtime = function () {
    var currentdate = new Date();
    var datetime =
        "今天 " +
        currentdate.getHours() +
        ":" +
        currentdate.getMinutes() +
        ":" +
        currentdate.getSeconds();

    return datetime;
};
export default {
    //定義狀態
    state: {
        lang: "zh-TW", //存放
        sideBarIsOpen: true, //存放側邊欄是否開合
        userInfo: {
            name: '',
            studentId: '',
            roles: []
        },
        paperInfo: {},
        Item: {}, //存放当前活動相關的狀態
        examInfo: {},
        tempfinishedItem: [], //用來暫存目前完成的項目ID
        tempfinishedItemtime: [],
        CountedIsDonePercent: myMockData.initialfinishResult, //存放活動完成度值，初始值先從Api抓資料，後續透過vuex抓狀態變化
        filterType: "all", //存放篩選活動類型
        isSelectedNow: false, //是否选中活动

        CurrentSelectedReadingName: " ", //存放課前預習活動目前所選中的教材名稱
        isSelectedReadingNow: false,
        filterClassName: "所有課程", //存放目前篩選活動的來源課程類型

        //存放目前預習活動所選到的單元階段
        CurrentPhase: 0,

        //存放目前預習活動所通過的階段
        PassPhaseTest: 0,

        //存放自學相關的狀態
        StudyItem: "",
        isStudySelectedNow: false,
        Project: "所有課程",
        TestPhase: "", //存放目前測驗階段
        OpenTest: false, //存放是否打開測驗組件
        OpenTestSubjectNow: {},
        finishedSubject: [], //存放完成的科目

        scrollvalue: 0, //所選的項目是否超過表格範圍

        //存放是否打開同學互評組件
        OpenComment: false,

        //存放目前選中點評的學生資料
        CurrentCommentStudent: "",
        CurrentCommentStudentID: "",

        //存放當前播放hiteach回傳影片的時間戳記所回傳之指定pdf的頁數
        CurrentTVMarkerReturnPage: 1,

        /*課前預習多題闖關模組狀態*/
        sentCurrentphase: false, //是否送出目前單元測驗的答案
        trytestCount: [0, 0, 0],
        currentQuestionNo: 0,

        /*課前預習多題闖關模組狀態End*/

        isFromInfoPoptoScroll: false, //現在是不是從彈窗連續在同一頁點擊

        isVoteResulthover: false, //滑過清單上的投票結果
        currentVoteResultItemY: 0, //存放目前滑過元素的高度

        hintNextItem: true, //設定活動完成時自動顯示下一個

        isOpenCommentList: false,
        nowPageName: "", //暫存是哪一頁(判斷自學或課前預習)
        isResetSurvey: false, //重置問卷選項

        //假設學校庫中有課程ID
        courseIDs: ["T10000", "T11000", "T11101"],
        //暫存我剛才成功加入的ID
        myCourseID: [],
        addSuccess: false,
        coureseMessageOpen: false, //是否打開加入課程的提示訊息
    },

    //定義方法
    mutations: {
        //保存学生基本信息
        setUserInfo(state, data) {
            if (data.name !== "") {
                state.userInfo.name = data.name // 学生姓名
                state.userInfo.studentId = data.sub // 学生ID
                state.userInfo.roles = data.roles // 學生是否拥有不同权限
            }
        },
        searchCourseIDforAdd(state, cIDadd) {
            if (
                state.courseIDs.includes(cIDadd) == true &&
                state.myCourseID.includes(cIDadd) == false
            ) {
                state.myCourseID.push(cIDadd);
                console.log("加入成功");
                state.addSuccess = true;
            } else if (state.myCourseID.includes(cIDadd) == true) {
                console.log("課程已經加入");
                state.addSuccess = "exist";
            } else {
                console.log("加入失敗");
                state.addSuccess = false;
            }
            //打開提示
            state.coureseMessageOpen = true;
            setTimeout(() => {
                state.coureseMessageOpen = false;
            }, 3000);
        },
        SaveNowPage(state, pageName) {
            state.nowPageName = pageName;
        },
        resetSurvey(state, reset) {
            state.isResetSurvey = reset;
        },
        voteResulthover(state, hoverItem) {
            state.isVoteResulthover = hoverItem.status;
            state.currentVoteResultItemY = hoverItem.itemIDHeignt;
            //console.log('滑過投票結果:'+ state.isVoteResulthover, state.currentVoteResultItemY)
        },
        SetisFromInfoPoptpScroll(state, status) {
            state.isFromInfoPoptoScroll = status;
            //console.log('從通知彈窗切換項目')
        },
        SetCurrentQuestionNo(state, no) {
            state.currentQuestionNo = no;
        },
        SetPaperInfo(state, data) {
            state.paperInfo = {}
            state.paperInfo = data
        },
        SetExamInfo(state, data) {
            //console.log('存入数据')
            //console.log(data)
            state.examInfo = {}
            state.examInfo = data
        },
        SetTrytestCount(state, nowCountArray) {
            state.trytestCount = nowCountArray;
            //console.log("目前單元嘗試次數" + state.trytestCount);
        },
        ToggleSentCurrentphase(state, sentCurrentphase) {
            state.sentCurrentphase = sentCurrentphase;
        },
        ChangeCurrentPdfPage(state, page) {
            state.CurrentTVMarkerReturnPage = page;
            console.log("目前pdf的頁數" + state.CurrentTVMarkerReturnPage);
            //抓取pdf的iframe，進行強制更新
            var iframe = document.getElementById("hiteachPdf");
            iframe.src = `./PlayerAPI_v1.0.6.pdf#page=${
                state.CurrentTVMarkerReturnPage
                }?+${new Date().getTime()}`;
            //刷新iframe的方法
            iframe.contentWindow.location.reload();
        },

        //存放從APP中控的navbarToggle目前的開關狀態，用以控制旗下的每個分頁sidebar
        ToggleSidebar(state, sideBarIsOpen) {
            if (sideBarIsOpen == true) {
                state.sideBarIsOpen = true;
            } else if (sideBarIsOpen == false) {
                state.sideBarIsOpen = false;
            } else {
                state.sideBarIsOpen = !state.sideBarIsOpen;
            }
        },

        //＊＊存放活動相關的函式
        //物件當前物件名

        SavefinishedSubject(state, subject) {
            if (state.finishedSubject.includes(subject) == false) {
                state.finishedSubject.push(subject);
            }
            console.log(state.finishedSubject);
        },
        SavefinishedItemID(state, itemID) {
            if (state.tempfinishedItem.includes(itemID) == false) {
                state.tempfinishedItem.push(itemID);
                state.tempfinishedItemtime.push(getfinishedtime());
            }

            console.log(state.tempfinishedItem, state.tempfinishedItemtime);
        },
        ChangeItemName(state, item) {
            state.Item = item;
            //state.Item.isDone = true;
            state.isSelectedNow = true;
            if (item == null) {
                state.isSelectedNow = false;
            }
        },
        ShowCountedIsDone(state, mockdata) {
            var count = 0;

            for (var i = 0; i < mockdata.length; i++) {
                if (mockdata[i].isDone == true) {
                    count++;
                }
            }
            var Result = (count / mockdata.length) * 100;
            state.CountedIsDonePercent = Result.toFixed(2);
            //console.log("from Vuex mutation:你目前完成了 " + state.CountedIsDonePercent + " ％活動")
        },
        //動作是使用者點擊下拉選單篩選類型，回傳目前顯示的類型狀態
        ChangeFilterType(state, evenType) {
            state.filterType = evenType;
            state.isSelectedNow = false;
        },
        ChangeFilterClassName(state, className) {
            state.filterClassName = className;
        },
        //＊＊存放活動相關的函式
        ChangeCurrentSelectedReadingName(state, item) {
            //變更當前課前預習活動的閱讀教材
            //console.log(item);
            state.CurrentSelectedReadingName = item;
            //console.log("vuex-store's state - itemName: " + state.Item.title);
            state.isSelectedReadingNow = true;
            //console.log(state.CurrentSelectedReadingName)
        },
        ChangeCurrentPhase(state, phase) {
            state.CurrentPhase = phase;
            //console.log(state.CurrentPhase);
        },
        //＊＊存放自學相關的函式
        //物件當前物件名
        ChangeStudyItemName(state, item) {
            //console.log(item);
            state.StudyItem = item;
            //console.log("vuex-store's state - itemName: " + state.Item.title);
            state.isStudySelectedNow = true;
        },

        ChangeProjectType(state, project) {
            state.Project = project;
            console.log(state.Project);
        },
        ChangeTestPhase(state, phase) {
            //變更測驗階段
            state.TestPhase = phase;
            console.log(state.TestPhase);
        },
        PassPhaseTest(state, phase) {
            state.PassPhaseTest = phase;
        },

        initStore(state) {
            state.isSelectedNow = false;
            state.isStudySelectedNow = false;
            state.filterType == "all";
        },
        ToggleLessonTestPop(state) {
            state.OpenTest = !state.OpenTest;
        },
        ToggleLessonTestPopWithSubject(state, subject) {
            console.log('修改后数据')
            console.log(subject)
            state.OpenTestSubjectNow = {}
            state.OpenTest = !state.OpenTest;
            state.OpenTestSubjectNow = subject;
            //console.log(state.OpenTest, state.OpenTestSubjectNow);
        },
        ChangeClassmatesCommentID(state, ID) {
            state.CurrentCommentStudentID = ID;
        },
        ChangeClassmatesComment(state, item) {
            state.CurrentCommentStudent = item;
        },
        ToggleClassmatesComment(state, item) {
            state.CurrentCommentStudent = item;
            state.OpenComment = !state.OpenComment;
        },
        //修改表單的位置
        scrollList(state, scrollvalue) {
            state.scrollvalue = scrollvalue;
            console.log(state.scrollvalue);
        },

        setLanguage(state, setlang) {
            state.lang = setlang;
            Vue.config.lang = state.lang;
            //console.log(state.lang, Vue.config.lang);
            i18n.locale = state.lang; //對i18n物件進行直接的語系切換
            localStorage.setItem("local", state.lang);
        },
        //是否隱藏下一個活動提示
        sethintNextItem(state, hidehint) {
            state.hintNextItem = hidehint;
            localStorage.setItem("hintNextItem", state.hintNextItem);
        },
        OpenCommentList(state, open) {
            state.isOpenCommentList = open;
        },
    },
    getters: {
        getUserInfo: (state) => {
            return state.userInfo;
        },
        getPaperInfo: (state) => {
            return state.paperInfo;
        },
        getExamInfo: (state) => {
            return state.examInfo;
        },
        getSidebarisOpen: (state) => {
            return state.sideBarIsOpen;
        },
        getItemTitle: (state) => {
            return state.Item;
        },
        getStudyItemTitle: (state) => {
            return state.StudyItem;
        },
        getCountedIsDonePercent: (state) => {
            return state.CountedIsDonePercent;
        },
        getFilterType: (state) => {
            return state.filterType;
        },
        getFilterClassName: (state) => {
            return state.filterClassName;
        },

        getIsSelectedNow: (state) => {
            return state.isSelectedNow;
        },
        getIsStudySelectedNow: (state) => {
            return state.isStudySelectedNow;
        },
        getProjectType: (state) => {
            return state.Project;
        },
        getTestPhase: (state) => {
            return state.TestPhase;
        },
        getCurrentSelectedReadingName: (state) => {
            return state.CurrentSelectedReadingName;
        },
        getisSelectedReadingNow: (state) => {
            return state.isSelectedReadingNow;
        },
        getCurrentPhase: (state) => {
            return state.CurrentPhase;
        },
        getCurrentPassPhaseTest: (state) => {
            return state.PassPhaseTest;
        },
        getisOpenLessonTestPopNow: (state) => {
            return state.OpenTest;
        },
        getisOpenClassmatesCommentNow: (state) => {
            return state.OpenComment;
        },
        getCurrentClassmatesComment: (state) => {
            return state.CurrentCommentStudent;
        },
        getCurrentClassmatesCommentID: (state) => {
            return state.CurrentCommentStudentID;
        },
        getScrollvalue: (state) => {
            return state.scrollvalue;
        },
        getFinishedItem: (state) => {
            return state.tempfinishedItem;
        },
        getFinishedItemTime: (state) => {
            return state.tempfinishedItemtime;
        },
        getCurrentPdfpage: (state) => {
            return state.CurrentTVMarkerReturnPage;
        },
        getIsSentCurrentphase: (state) => {
            return state.sentCurrentphase;
        },
        getCurrentQuestionNo: (state) => {
            return state.currentQuestionNo;
        },
        getTrytestCount: (state) => {
            return state.trytestCount;
        },
        getCurrentSubject: (state) => {
            return state.OpenTestSubjectNow;
        },
        getCurrentfinishedSubject: (state) => {
            return state.finishedSubject;
        },
        getisFromInfoPoptoScroll: (state) => {
            return state.isFromInfoPoptoScroll;
        },
        getisVoteResulthover: (state) => {
            return state.isVoteResulthover;
        },
        gethoverVoteItemY: (state) => {
            return state.currentVoteResultItemY;
        },
        getCurrentLaguage: (state) => {
            return state.lang;
        },
        getNextItemHint: (state) => {
            return state.hintNextItem;
        },
        getOpenCommentList: (state) => {
            return state.isOpenCommentList;
        },
        getNowPage: (state) => {
            return state.nowPageName;
        },
        getResetSurvey: (state) => {
            return state.isResetSurvey;
        },
        //抓我加入的課程ID
        getMycID: (state) => {
            return {
                myCourseID: state.myCourseID,
                addSuccess: state.addSuccess,
                coureseMessageOpen: state.coureseMessageOpen,
            };
        },
    },
}
