import { POST_LOGIN } from "./api";
import instance from "@/util/request";
import apiHandler from "@/util/apiHandler";

export async function Login({ account, password }) {
  const res = await instance.post(POST_LOGIN, {
    account,
    password,
  });

  if (res.code === "0000") {
    console.log(res.data);
    document.cookie = `JWT-Authorization=${res.data.token}; Max-Age=${
      24 * 60 * 60 * 1000
    }`;
    // 設定 user_name Cookie
    document.cookie = `user_name=${res.data.user_name}; Max-Age=${
      24 * 60 * 60 * 1000
    }`;
  }

  return apiHandler(res.code, res.data, {
    msg: res.msg,
    code: res.code,
    isSuccess: false,
  });
}
