import axios from "axios";
import { ResPage, StoreUser } from "@/api/interface/index";
import { PORT_NONE } from "@/api/config/servicePort";
import http from "@/api";
import { ResultEnum } from "@/enums/httpEnum";
import { useUserStore } from "@/stores/modules/user";
import { ElMessage } from "element-plus";
import { LOGIN_URL } from "@/config";
import router from "@/routers";

const httpStore = axios.create({
  baseURL: import.meta.env.VITE_API_URL_STORE as string,
  timeout: ResultEnum.TIMEOUT as number,
  withCredentials: true
});
httpStore.interceptors.request.use(
  config => {
    const userStore = useUserStore();
    if (config.headers) {
      (config.headers as any).Authorization = userStore.token;
    }
    return config;
  },
  error => Promise.reject(error)
);
httpStore.interceptors.response.use(
  response => {
    const data = response.data;
    const userStore = useUserStore();
    if (data.code == ResultEnum.OVERDUE) {
      userStore.setToken("");
      router.replace(LOGIN_URL);
      ElMessage.error(data.msg);
      return Promise.reject(data);
    }
    if (data.code && data.code !== ResultEnum.SUCCESS) {
      ElMessage.error(data.msg);
      return Promise.reject(data);
    }
    return data;
  },
  error => {
    ElMessage.error("请求失败，请稍后重试");
    return Promise.reject(error);
  }
);

//个人实名
export const verifyIdInfo = params => {
  return http.get<StoreUser.ResStoreUserList>(PORT_NONE + `/merchantAuth/verifyIdInfo`, params);
};

// 信息提交（店铺入驻，使用 alienStore 前缀）
export const applyStore = params => {
  return httpStore.post(PORT_NONE + `/store/info/saveStoreInfo`, params);
};

//用户信息查询
export const getMerchantByPhone = params => {
  return http.get<StoreUser.ResStoreUserList>(PORT_NONE + `/merchantUser/getMerchantByPhone`, params);
};

//通知列表
export const getNoticeList = params => {
  return http.get<StoreUser.ResStoreUserList>(PORT_NONE + `/notice/getNoticeList`, params);
};
//通知查看详情标记已读
export const markNoticeAsRead = params => {
  return http.get<StoreUser.ResStoreUserList>(PORT_NONE + `/notice/markNoticeAsRead`, params);
};
//通知列表  一键已读
export const markAllNoticesAsRead = params => {
  return http.post(PORT_NONE + `/notice/markAllNoticesAsRead`, params);
};
//今日订单量
export const getTodayOrderCount = params => {
  return http.get<StoreUser.ResStoreUserList>(PORT_NONE + `/storeManage/getTodayOrderCount`, params);
};
//今日预计收益
export const getTodayIncome = params => {
  return http.get<StoreUser.ResStoreUserList>(PORT_NONE + `/storeManage/getTodayIncome`, params);
};

//验券
export const verifyOrder = params => {
  return http.get<StoreUser.ResStoreUserList>(PORT_NONE + `/couponManage/verifyOrder`, params);
};
export const verifyCoupon = params => {
  return http.get<StoreUser.ResStoreUserList>(PORT_NONE + `/couponManage/verifyCoupon`, params);
};
//保存用户信息
export const updateMerchantUserInfo = params => {
  return http.post(PORT_NONE + `/merchantUser/updateMerchantUserInfo`, params);
};

//财务   可提现金额
export const getAccountBalance = params => {
  return http.get<StoreUser.ResStoreUserList>(PORT_NONE + `/incomeManage/getAccountBalance`, params);
};
//已到账金额/未到账金额
export const getPaymentCycle = params => {
  return http.get<StoreUser.ResStoreUserList>(PORT_NONE + `/incomeManage/getPaymentCycle`, params);
};
//今日可收益
export const getGroupIncome = params => {
  return http.get<StoreUser.ResStoreUserList>(PORT_NONE + `/incomeManage/getGroupIncome`, params);
};
//是否有支付密码
export const checkPayPassword = params => {
  return http.get<StoreUser.ResStoreUserList>(PORT_NONE + `/merchantUser/checkPayPassword`, params);
};
//设置提现密码
export const setPayPassword = params => {
  return http.post(PORT_NONE + `/merchantUser/setPayPassword`, params);
};
//提现记录列表
export const getCashOutRecordList = params => {
  return http.get<StoreUser.ResStoreUserList>(PORT_NONE + `/incomeManage/getCashOutRecordList`, params);
};
export const getUserByPhone = params => {
  return http.get(PORT_NONE + `/merchantUser/getMerchantByPhone`, params);
};
export const getDetail = params => {
  return http.get(PORT_NONE + `/storeManage/getStoreDetail`, params);
};
//重置商户到刚注册状态
export const resetToInitialStatus = () => {
  return http.post(PORT_NONE + `/merchantUser/resetToInitialStatus`);
};
//添加支付宝账号
export const addAlipayAccount = params => {
  return http.post(PORT_NONE + `/merchantUser/addAlipayAccount`, params);
};

//删除支付宝账号
export const deleteAlipayAccount = () => {
  return http.post(PORT_NONE + `/merchantUser/deleteAlipayAccount`, {});
};
//提现申请-提交
export const cashOut = params => {
  return http.post(PORT_NONE + `/incomeManage/cashOut`, params);
};

export const getCheckPayPasswordTwo = params => {
  return http.get(PORT_NONE + `/merchantUser/checkPayPassword`, params);
};

// 检查菜单访问权限（返回合同管理和食品经营许可证两个字段）
export const checkMenuPermissions = params => {
  return http.get(PORT_NONE + `/storePlatformLogin/getExpirationTime`, params);
};
// 经营板块三级分类接口 - 一级分类
export const getFirstLevelList = params => {
  return http.get(PORT_NONE + `/businessSection/getFirstLevelList`, params);
};
// 经营板块三级分类接口 - 二级分类
export const getSecondLevelList = (params: { parentDictId: string }) => {
  return http.get(PORT_NONE + `/businessSection/getSecondLevelList`, params, { loading: false });
};

// 经营板块三级分类接口 - 三级分类
export const getThirdLevelList = (parentDictId: string) => {
  return http.get(PORT_NONE + `/businessSection/getThirdLevelList`, { parentDictId }, { loading: false });
};
