import DGTMessage from '@/utils/message'
import { signUp } from '@/api/apply.js'
import { ElMessageBox } from 'element-plus'
import { createOrder } from '@/api/order.js'
export function downloadFile(url, fileName) {
  if (!url) {
    DGTMessage.error('文件地址不存在');
    return;
  }
  const link = document.createElement('a');
  link.href = url;
  link.download = fileName || '';
  document.body.appendChild(link);
  link.click();
  document.body.removeChild(link);
}

export async function submitSignUp(formData,t,callback) {
  try {
    const response = await signUp(formData);
    if (response.code === 200) {
      DGTMessage.success(t('workflowTrade.signUpSuccess'));
      if(callback && typeof callback === 'function'){
        callback();
      }
      return response.data;
    } else {
      // DGTMessage.error(response.msg || t('workflowTrade.signUpFailed'));
      return null;
    }
  } catch (error) {
    return null;
  }   
}
//截止日期转化为天数和小时
export function formatDeadline(deadline) {
  if (!deadline || isNaN(Date.parse(deadline))) {
    return {diffDays:0,diffHours:0};
  }
  const now = new Date();
  const deadlineDate = new Date(deadline);
  const diffTime = deadlineDate - now;
  //负数直接返回0
  if(diffTime <=0){
    return {diffDays:0,diffHours:0};
  }
  const diffDays = Math.ceil(diffTime / (1000 * 60 * 60 * 24));
  const diffHours = Math.ceil(diffTime / (1000 * 60 * 60)) % 24;
  return {diffDays,diffHours};
}
export function confirmBuy({type='baoMiBalance',price=0,callback,appStore,router,t,payMethod,orderType,productId}){
  const balance = appStore.userInfo?.[type] || 0;
  if(balance < price ){
    ElMessageBox.confirm('您的余额不足，是否前往充值？', t('common.tip'), {
      confirmButtonText: t('common.confirm'),
      cancelButtonText: t('common.cancel'),
      type: 'warning'
    }).then(() => {
      // 确认购买
      router.push({
        path: `/order-confirm`,
      })
    }).catch(() => {
      // 取消购买
    });
  }else{
    const unit = type === 'baoMiBalance' ? t('common.baomibi') : t('common.mibi');
    const ms = `${t('common.payNow')} ${price} ${unit}？`
    ElMessageBox.confirm(ms, t('common.tip'), {
      confirmButtonText: t('common.confirm'),
      cancelButtonText: t('common.cancel'),
      type: 'warning'
    }).then(async () => {
      const res = await createOrder({
        payMethod,
        orderType,
        orderAmt:price,
        productId,
        orderNum:1
      })
      if(res.code === 200){
        DGTMessage.success(t('common.buy')+t('common.success'));
        if(callback && typeof callback === 'function'){
          callback();
        }
      }else{
        // DGTMessage.error(res.msg || t('common.buy')+t('common.fail'));
      }
      
    }).catch(() => {
      // 取消购买
    });
  }
}
//判断是否登录
export function isLogin({callback,t}){
  let token = localStorage.getItem('token');
  if(!token){
    //确认弹框
    ElMessageBox.confirm('您未登录，是否前往登录？', t('common.tip'), {
      confirmButtonText: t('common.confirm'),
      cancelButtonText: t('common.cancel'),
      type: 'warning'
    }).then(() => {
      // 确认购买
      if(callback && typeof callback === 'function'){
        callback();
      }
    }).catch(() => {
      // 取消购买
    });
    return false;
  }
  return true;
}
//复制功能
export function copyText(text, t) {
  if (!text) {
    DGTMessage.error(t('common.copyError'));
    return;
  }
  const input = document.createElement('input');
  input.value = text;
  document.body.appendChild(input);
  input.select();
  document.execCommand('copy');
  document.body.removeChild(input);
  DGTMessage.success(t('common.copySuccess'));
}
