import {
  axios,
  userLogin,
  uploadFile as uploadFileUrl,
  
  setLoginHook,
  newupload,
  userInfo,
} from "@/request";
import { getCaseInfo } from "@/store/case";
import { encodePwd } from "@/util";
import { user } from "./user";
import { refreshRole } from "./role";
import { changSaveLocal } from "@/util/localUtil";
import { appConstant } from "@/app";
import { router } from "@/router";
import { ref, watchEffect } from "vue";
function randomWord(randomFlag, min, max?) {
  let str = '';
  let range = min;
  const arr = [
    '0',
    '1',
    '2',
    '3',
    '4',
    '5',
    '6',
    '7',
    '8',
    '9',
    'a',
    'b',
    'c',
    'd',
    'e',
    'f',
    'g',
    'h',
    'i',
    'j',
    'k',
    'l',
    'm',
    'n',
    'o',
    'p',
    'q',
    'r',
    's',
    't',
    'u',
    'v',
    'w',
    'x',
    'y',
    'z',
    'A',
    'B',
    'C',
    'D',
    'E',
    'F',
    'G',
    'H',
    'I',
    'J',
    'K',
    'L',
    'M',
    'N',
    'O',
    'P',
    'Q',
    'R',
    'S',
    'T',
    'U',
    'V',
    'W',
    'X',
    'Y',
    'Z',
  ];
  // 随机产生
  if (randomFlag) {
    range = Math.round(Math.random() * (max - min)) + min;
  }
  for (let i = 0; i < range; i++) {
    const pos = Math.round(Math.random() * (arr.length - 1));
    str += arr[pos];
  }
  return str;
}

function encodeStr(str, strv = ''): string {
  const NUM = 2;
  const front = randomWord(false, 8);
  const middle = randomWord(false, 8);
  const end = randomWord(false, 8);

  const str1 = str.substring(0, NUM);
  const str2 = str.substring(NUM);

  if (strv) {
    const strv1 = strv.substring(0, NUM);
    const strv2 = strv.substring(NUM);
    return [front + str2 + middle + str1 + end, front + strv2 + middle + strv1 + end];
  }

  return front + str2 + middle + str1 + end;
}
export type LoginProps = {
  phoneNum?: string;
  userName?: string;
  code: string;
  password: string;
};

export const title = ref(appConstant.title);
export const loginShow = ref(false);
export const desc = ref(appConstant.desc);

watchEffect(
  () => (document.title = title.value + (desc.value ? " | " + desc.value : ""))
);

const refreshUserInfo = async (data: any) => {
  user.value.info = data;
  await refreshRole();
};

export const setLoginShow = (show: boolean) => {
  loginShow.value = show;
}


export const login = async (props) => {
  console.log("login", props, encodeStr(window.btoa(props.password)));
  const res = await axios.post(userLogin, {
    userName: props.username,
    username: props.username,
    password: encodeStr(window.btoa(props.password))
  });
  user.value.token = res.data.token;
  await getCaseInfo(props.caseId);
  localStorage.setItem("token", user.value.token);
  loginShow.value = false;
  // refreshUserInfo(res.data);
};

if (user.value.token) {
  // axios.get(userInfo).then((res) => {
  //   refreshUserInfo(res.data);
  // });
}

export const uploadFile = async (file: File) => {
  const route = router.currentRoute.value;
  const url = route.name != 'drawShareFile'?uploadFileUrl:newupload
  return (await axios.post<string>(url, { file })).data;
};
export const uploadOldFile = async (file: File) => {
  return (await axios.post<string>(uploadFileUrl, { file })).data;
};

// 设置全局请求hook
setLoginHook(() => {
  return {
    loginShow: loginShow.value,
    setLoginShow: (value) => {
      loginShow.value = value;
    },
  };
});
