import { defineStore } from "pinia";

// localStorage.setItem(
//   "userInfo",
//   JSON.stringify(
    //     {"user_id":"1168","user_name":"62ac52a163fae82d7e4076a2","user_realname":"侯晓倩","user_password":"b923bc037bffebe673963ddd046c7306990ea7aa","user_group_id":"1","user_role_id":"7","user_level_id":"1","user_detail_id":"0","user_score":"0","create_dateline":"1658201572","modify_dateline":"1670568745","isdelete":"0","area_id1":"0","area_id2":"0","area_id3":"0","area_id4":"0","dept_id":"333","class_id":null,"user_phone":"","user_cardno":"","phone_validation":"0","parent_user_id":"0","user_nickname":"","user_avatar":"","user_email":"","email_validation":"0","email_token":"","email_token_exptime":"0","ischeck":"1","address":"","gender":"1","about":null,"qq":"","openid":"","user_birthday":"0000-00-00","webchatopenid":"","user_level":"1","user_abalance":"0.00","user_dcash":"0.00","user_dqsj":"","user_alipay_account":"","cubeUserId":"62ac52a163fae82d7e4076a2","outUserId":"rfO9ObSSRBgjHcJ1oCxhBQiEiE","cubeOrgId":"testorg001","cubeinfo":"{\"sessionId\":\"0d26267b325a48ca9a8cc8495c963a70\",\"cubeUserId\":null,\"cubeCommonUserId\":\"62ac52a163fae82d7e4076a2\",\"cubeCorpId\":\"1\",\"cubeCorpName\":\"浙江省教育厅\",\"unionid\":\"rfO9ObSSRBgjHcJ1oCxhBQiEiE\",\"mobile\":null,\"name\":\"侯晓倩\",\"userId\":\"126527261720436005\",\"corpId\":\"ding47729af11dc37275a1320dcb25e91351\",\"corpName\":null,\"roleStatus\":null,\"sessionSource\":\"\",\"isStudent\":null,\"platform\":\"dingtalk\",\"outUserId\":\"rfO9ObSSRBgjHcJ1oCxhBQiEiE\",\"appId\":null,\"developerRegisterDTO\":null,\"developerCorpInstallAuthored\":null,\"corpChooseRight\":null,\"userOneId\":null,\"orgId\":null,\"avatar\":null,\"authCode\":null}","user_no":"1168","token":"e671QRCtGG7Tt3gj_bjQUgUY55_b_aCEcjCW2GdiZba8Kgx91788IAk_bnDIxaDpsiMZNaCrudZ51VWl_a8dLna9kw3XyaVOt4pM7ifkLUk_axRlo","setting":{"sitename":"景宁教育魔方","sitedomain":"http:\/\/jnjymf_api.bozedu.top\/","site_icp":"浙ICP备05048509号-1","sitelogo":"http:\/\/jnjymf_api.bozedu.top\/src\/assets\/image\/public\/logo3.png","siteqrcode":"http:\/\/jnjymf_api.bozedu.top\/dev.qr.png","sitefavicon":"","user_failedlogin_max":"5"},"user_org":[{"uo_id":"323","uo_name":"景宁畲族自治县教育局","uo_master_username":"625cce3e63fae8144b719348,6268f6def6e7a89284f6d9d8","uo_type":"1"},{"uo_id":"1036","uo_name":"学校","uo_master_username":"","uo_type":"1"},{"uo_id":"1045","uo_name":"小学","uo_master_username":"","uo_type":"1"},{"uo_id":"333","uo_name":"景宁畲族自治县红星小学","uo_master_username":"","uo_type":"2"}],"uo_id":"333","uo_type":"2","uo_name":"景宁畲族自治县红星小学","dept_ids":["323","1036","1045","333"],"dept_master":[]}

//     {
//         user_id: "1181",
//         user_name: "6360acc1f70a665a9821ef87",
//         user_realname: "调试 赵静",
//         user_password: "b923bc037bffebe673963ddd046c7306990ea7aa",
//         user_group_id: "1",
//         user_role_id: "7",
//         user_level_id: "1",
//         user_detail_id: "0",
//         user_score: "130",
//         create_dateline: "1667969311",
//         modify_dateline: "1692239663",
//         isdelete: "0",
//         area_id1: "0",
//         area_id2: "0",
//         area_id3: "0",
//         area_id4: "0",
//         dept_id: "333",
//         dept_id_all: "333",
//         class_id: null,
//         user_phone: "",
//         user_cardno: "",
//         phone_validation: "0",
//         parent_user_id: "0",
//         user_nickname: "",
//         user_avatar: "",
//         user_email: "",
//         email_validation: "0",
//         email_token: "",
//         email_token_exptime: "0",
//         ischeck: "1",
//         address: "",
//         gender: "1",
//         about: null,
//         qq: "",
//         openid: "",
//         user_birthday: "0000-00-00",
//         webchatopenid: "",
//         user_level: "1",
//         user_abalance: "0.00",
//         user_dcash: "0.00",
//         user_dqsj: "",
//         user_alipay_account: "",
//         cubeUserId: "6360acc1f70a665a9821ef87",
//         outUserId: "U6Qo1v6VBMNBWNZlRmZ7MQiEiE",
//         cubeOrgId: "6221b69ff70a668a695e52af",
//         cubeinfo:
//           '{"sessionId":"77dc1486071044abbdc90ed4141aed90","cubeUserId":null,"cubeCommonUserId":"6360acc1f70a665a9821ef87","cubeCorpId":"6221b69ff70a668a695e52af","cubeCorpName":"景宁畲族自治县教育局","unionid":"U6Qo1v6VBMNBWNZlRmZ7MQiEiE","mobile":null,"name":"调试 赵静","userId":"4713535222-190354638","corpId":"ding5e1e3f9760fb4e5435c2f4657eb6378f","corpName":null,"roleStatus":null,"sessionSource":"","isStudent":null,"platform":"dingtalk","outUserId":"U6Qo1v6VBMNBWNZlRmZ7MQiEiE","appId":"app_2022070623603","developerRegisterDTO":null,"developerCorpInstallAuthored":null,"corpChooseRight":null,"userOneId":null,"orgId":null,"avatar":null,"authCode":null}',
//         user_no: "1181",
//         wx_user_role_id_ext: null,
//         wx_user_role_id_ext_now: "0",
//         wx_cm_id: "0",
//         wx_grade_id: "0",
//         wx_user_grade_num: "0",
//         wx_user_class_num: "0",
//         wx_last_token: null,
//         wx_last_login: null,
//         wx_graduation: "0",
//         power_set: '{"cjlr":"2","cjsh":"1"}',
//         token:
//           "4993GxFlApL_aPkUZuzlLVWtfD4wf8Yrz76EN0oRQ_a96Z2qPbkiDfa_aryvBQjFd1Y7mFEz2MjAe3je5vH7rIDR11Lsn8EQs3pkC1ziKQjOnc",
//         setting: {
//           sitename: "景宁教育魔方",
//           sitedomain: "http://60.188.226.44:8090/",
//           site_icp: "浙ICP备05048509号-1",
//           sitelogo:
//             "http://60.188.226.44:8090/src/assets/image/public/logo3.png",
//           siteqrcode: "http://60.188.226.44:8090/dev.qr.png",
//           sitefavicon: "",
//           user_failedlogin_max: "5",
//         },
//         user_org: [
//           {
//             uo_id: "323",
//             uo_name: "景宁畲族自治县教育局",
//             uo_master_username:
//               "625cce3e63fae8144b719348,6268f6def6e7a89284f6d9d8",
//             uo_type: "1",
//           },
//           {
//             uo_id: "1036",
//             uo_name: "学校人员组织",
//             uo_master_username: "",
//             uo_type: "1",
//           },
//           {
//             uo_id: "1045",
//             uo_name: "小学",
//             uo_master_username: "",
//             uo_type: "1",
//           },
//           {
//             uo_id: "333",
//             uo_name: "景宁畲族自治县红星小学",
//             uo_master_username: "6360ae4ef70a665a9821efc3",
//             uo_type: "2",
//           },
//         ],
//         uo_id: "333",
//         uo_type: "2",
//         uo_name: "景宁畲族自治县红星小学",
//         dept_ids: ["323", "1036", "1045", "333"],
//         dept_master: [],
//       },
//   )
// );

const RoleMap = {
  JLD: [1, 2, 3],
  ZJ: [4, 5, 6],
  JYZ: [7, 8],
};
const userInfo = JSON.parse(localStorage.getItem("userInfo"));
// const { token, org_info, user_realname, user_avatar, power,user_org, } = userInfo

export const user = {
  userInfo: userInfo,
  token: userInfo?.token,
  // role: org_info.dept_role.role_id * 1,
  real_name: userInfo?.user_realname,
  avatar: userInfo?.user_avatar,
  // roleName:
  //   Object.keys(RoleMap).find((k) =>
  //     RoleMap[k].includes(org_info.dept_role.role_id * 1)
  //   ) || 'None',
  power: userInfo?.power,
  user_org: userInfo?.user_org,
};
export const useUserStore = defineStore("user", {
  state: () => user,
  actions: {
    logout() {
      localStorage.removeItem("userInfo");
      window.open(window.globalVariables.login, "_self");
    },
  },
});
