import Http from "../utils/http";
import { host } from "../utils/constant";

const userHttp = new Http(host);

export function fetchProfile() {
  return userHttp.get("/user/me").then((res) => {
    getApp<any>().globalData.user = res;
    return res;
  });
}

export function fetchCollectList() {
  if (getApp<any>().globalData.collectIds) {
    return Promise.resolve(getApp<any>().globalData.collectIds);
  }
  return userHttp
    .get<{
      data: {
        status: number;
        station_id: number;
      }[];
    }>("/user/collectList?page=1&page_size=999")
    .then((res) => {
      getApp<any>().globalData.collectIds = res.data
        ? res.data
            .filter((item) => Number(item.status) === 1)
            .map((item) => {
              return Number(item.station_id);
            })
        : [];
      return getApp<any>().globalData.collectIds;
    });
}

export function addCollectList(sid: number) {
  let ids = getApp<any>().globalData.collectIds
    ? (getApp<any>().globalData.collectIds as number[])
    : [];
  const status = ids.includes(sid) ? 2 : 1;
  return userHttp
    .post("/user/addCollect", {
      data: {
        station_id: sid,
        status,
      },
    })
    .then(() => {
      if (status === 1) {
        ids.push(sid);
      } else {
        ids = ids.filter((id) => id !== sid);
      }
      getApp<any>().globalData.collectIds = ids;
      return status === 1;
    });
}

export function updateProfile(data: {
  nick_name?: string;
  avatar?: string;
  vin?: string;
  license_plate?: string;
  card_no?: string;
}) {
  return userHttp.post("/user/update", {
    data,
  });
}

declare const wx: any;

export function insertMoney(amount: number) {
  return userHttp
    .post<{
      appId: string;
      nonceStr: string;
      package: string;
      paySign: string;
      signType: string;
      timestamp: string;
    }>("/payment/pay", {
      data: {
        amount: parseInt(`${amount * 100}`),
        openid: getApp<any>().globalData.user.openid,
      },
    })
    .then((res: any) => {
      return new Promise((resolve, reject) => {
        // #ifdef MP-WEIXIN
        wx.requestPayment({
          timeStamp: res.timestamp,
          nonceStr: res.nonceStr,
          package: res.package,
          signType: "MD5",
          paySign: res.paySign,
          success(res: any) {
            resolve(res);
          },
          fail: reject,
        });
        // #endif
        // #ifndef MP-WEIXIN
        reject({
          errMsg: "目前仅支持微信支付",
        });
        // #endif
      });
    });
}

export function logout() {
  return userHttp.get("/user/logout");
}

export function fetchWallet(type: number, page: number, pageSize: number) {
  return userHttp
    .get<{
      data: any[];
    }>(`/account/wallet?page=${page}&page_size=${pageSize}&type=${type}`)
    .then((res) => {
      return res.data;
    });
}

export function fetchOrder(orderid: string) {
  return userHttp.get(`/charge/orderInfo?order_id=${orderid}`);
}
