import apiTools from "@/api";
import serviceDriveAuth from "@/mock/serviceDriveAuth";
//先接入假資料



export default {
  namespaced: true,
  state: {
    //假設一開始未定，等待DB資料進來，從action非同步，然後操作mutation，改變State

    schoolCode: "hbcn", //HBCN 學校代碼 (參考hbcn先寫死)

    //接mockData 使用
    serviceList: undefined,
    hiteachAuthList: undefined,
    spaceStatus: undefined,
    serviceIntroIsOpen: [],
    hiteachListItemIsOpen: [], //存放每個開關
    currentTab:0,//0 服務清單， 1 hiteach 清單

    //接實際api使用，一開始序號為空

    //第一區 服務清單
    service: [],
    seviceNameList: [
      "ON6MBDOP",
      "6BOPC6MD",
      "IPALYEIY", //智慧教學服務空間
      "RYGVCPLY",
      "AEGMCPLY",
    ],

    //第二區hiteach序號資料
    serial: [],
    serialInfo: {
      totalNum: 0,
      multiAuthNum: 0, //大量授權
      singleAuthNum: 0, //單一授權
      couldActiveNum: 0, //可啟用裝置數
      activeNum: 0, //已啟用裝置數
    },

    //第三區Space，由第一區的智慧教學服務空間的IPALYEIY
    space: {},
  },
  getters: {
    getServiceIntroIsOpen: (state) => {
      return state.serviceIntroIsOpen;
    },
    getHiteachListItemIsOpen: (state) => {
      return state.hiteachListItemIsOpen;
    },
    getServiceList: (state) => {
      if (state.service !== undefined) {
        return state.service;
      } else {
        return {};
      }
    },
    //取用產品服務名稱清單
    getServiceNameList: (state) => {
      return state.seviceNameList;
    },
    getHiteachAuthListInfo: (state) => {
      return state.serialInfo;
    },
    getHiteachAuthList: (state) => {
      /*if (state.hiteachAuthList !== undefined) {
        return state.hiteachAuthList;
      } else {
        return {};
      }*/
      if (state.serial !== undefined) {
        return state.serial;
      } else {
        return {};
      }
    },
    getSpaceStatus: (state) => {
      if (state.space !== undefined) {
        return state.space;
      } else {
        return {};
      }
    },
    getTab(state){
      return state.currentTab
    }
  },
  mutations: {
    setTab(state,page){
      state.currentTab = page;

    },
    setServiceIntroIsOpen(state, data) {
      state.serviceIntroIsOpen = data;
    },
    setHiteachListItemIsOpen(state, data) {
      state.hiteachListItemIsOpen = data;
    },
    setServiceList(state, data) {
      //state.serviceList = data; 接mock
      state.service = data;
    },
    setHiteachAuthList(state, data) {
      //state.hiteachAuthList = data;  //接mock
      state.serial = data;
      state.serialInfo.totalNum = data.length;

      //計算大量授權數
      let multiAuthNum = 0;
      data.forEach((item) => {
        if (item.deviceMax > 1) {
          multiAuthNum += 1;
        }
      });
      state.serialInfo.multiAuthNum = multiAuthNum;

      //計算單一授權數
      let singleAuthNum = 0;
      data.forEach((item) => {
        if (item.deviceMax == 1) {
          singleAuthNum += 1;
        }
      });
      state.serialInfo.singleAuthNum = singleAuthNum;

      //計算可啟用裝置數
      let couldActiveNum = 0;
      data.forEach((item) => {
        if (item.expireStatus != "F") {
          couldActiveNum += item.deviceMax;
        }
      });
      state.serialInfo.couldActiveNum = couldActiveNum;
      //計算已啟用裝置數
      let activeNum = 0;
      data.forEach((item) => {
        if (item.expireStatus == "A" || item.expireStatus == "S") {
          //console.log(item.deviceBound.length)
          activeNum += item.deviceBound.length;
        }
      });
      state.serialInfo.activeNum = activeNum;
    },
    setSpaceStatus(state, data) {
      state.space = data;
    },

    
  },
  actions: {
    //模擬從DB(ＭockData)取得所有資料 && commit to state.serviceDriveAuth

    getServiceListDataAsyc(context) {
      return new Promise((resolve) => {
        //先暫時模擬等待幾秒，直接取得資料

        let servicedata = serviceDriveAuth.serviceDriveAuthData.serviceList;
        let hiteachAuthdata =
          serviceDriveAuth.serviceDriveAuthData.hiteachClassroomAuth;

        let spaceStatusData = serviceDriveAuth.serviceDriveAuthData.spaceStatus;
        let temp = [],
          temp2 = [];

        let hiteachlistlength = hiteachAuthdata[0].serials.auth.length;
        //根據服務的長度生出開關，只要服務數量變動就必須重置
        for (let i = 0; i < servicedata.length; i++) {
          temp.push({
            index: i,
            isOpen: false,
          });
        }

        for (let i = 0; i < hiteachlistlength; i++) {
          temp2.push({
            index: i,
            isOpen: false,
          });
        }
        //context.commit("setServiceIntroIsOpen", temp);
        //context.commit("setHiteachListItemIsOpen", temp2);
        //context.commit("setServiceList", servicedata);
        //context.commit("setHiteachAuthList", hiteachAuthdata);
        //context.commit("setSpaceStatus", spaceStatusData);

        resolve({});
      });
    },



    //
    getSchoolProduct(context, schoolCode) {
      return new Promise((resolve, reject) => {
        apiTools.serviceDriveAuth.getSchoolProduct(schoolCode).then(
          (res) => {
            let temp = [];
            for (let i = 0; i < res.serial.length; i++) {
              temp.push({
                index: i,
                isOpen: false,
              });
            }
            context.commit("setHiteachListItemIsOpen", temp);
            console.log(res);
            context.commit("setHiteachAuthList", res.serial);

            //資料一進來，先比對一下沒有買的品項，加在表尾
            let serviceNameList = context.getters.getServiceNameList;
            let nowbuyList = [];
            for (let i = 0; i < res.service.length; i++) {
              nowbuyList.push(res.service[i].prodCode);
            }
            let notbuyList = serviceNameList.filter(
              (item) => nowbuyList.includes(item) == false
            );
            console.log(notbuyList);

            notbuyList.forEach((item) =>
              res.service.push({
                prodCode: item,
                notbuy: true,
                startDate: 0,
                endDate: 0,
              })
            );

            let temp2 = [];
            for (let i = 0; i < res.service.length; i++) {
              //資料一進來，將AclassOne排在第一個
              if (res.service[i].prodCode == "RYGVCPLY") {
                res.service.unshift(res.service[i]);
                res.service.splice(i + 1, 1);
              }

              temp2.push({
                index: i,
                isOpen: false,
              });
              temp2[0].isOpen = true; //預設把AclassOne細節打開
            }

            context.commit("setServiceIntroIsOpen", temp2);
            context.commit("setServiceList", res.service);

            //提取空間資料
            let space = res.service.filter(
              (item) => item.prodCode == "IPALYEIY"
            );

            Object.defineProperty(space[0], "totalused", {
              value:
                space[0].doc +
                space[0].image +
                space[0].item +
                space[0].other +
                space[0].paper +
                space[0].student +
                space[0].teacher +
                space[0].video,
              writable: true,
            });
            Object.defineProperty(space[0], "unUsed", {
              value: space[0].avaliable-space[0].totalused,
              writable: true,
            });

            Object.defineProperty(space[0], "unUsedPercent", {
              value: ((space[0].unUsed/space[0].avaliable)*100).toFixed(2),
              writable: true,
            });

            console.log(space);
            context.commit("setSpaceStatus", space[0]);

            resolve({
              code: 1,
              message: "Get service and hiteachList successful",
            });
          },
          (err) => {
            reject({
              code: 0,
              data: [],
              message: "Get service and hiteachList API error!",
            });
          }
        );
      });
    },
  },
};
