import { $get, $post, getDeviceInfo, getEnterOptions } from '@/utils'
import manifest from "@/manifest.json";
import { version } from 'uuid';

export const LOGIN = (params) => new Promise(async (resolve, reject) => {
  try {
    const res = await $post('/users/login', {
      device_id: getDeviceInfo().deviceId,
      ...params
    });
    const { ret } = res.data
    if (ret == 1) {
      resolve(res.data)
    }
    reject(res.data)
  } catch (error) { }
})


export const DEL_ACCOUNT = (params = {}) => new Promise(async (resolve, reject) => {
  try {
    const res = await $post('/users/delaccount', { ...params });
    const { ret } = res.data
    if (ret == 1) {
      resolve(res.data)
    }
    reject(res.data)
  } catch (error) { }
})

export const GET_USERINFO = (params = {}, other = {}) => new Promise(async (resolve, reject) => {
  try {
    const res = await $post('/users/info', params, other);
    const { ret } = res.data
    if (ret == 1) {
      resolve(res.data)
    }
    reject(res.data)
  } catch (error) { }
})

export const USER_STATICS = () => new Promise(async (resolve, reject) => {
  try {
    const res = await $post('/users/statics');
    const { ret } = res.data
    if (ret == 1) {
      resolve(res.data)
    }
    reject(res.data)
  } catch (error) { }
})

export const USER_EDIT_USERPASS = (params) => new Promise(async (resolve, reject) => {
  try {
    const res = await $post('/users/edit/userpass', params);
    const { ret } = res.data
    if (ret == 1) {
      resolve(res.data)
    }
    reject(res.data)
  } catch (error) { }
})

export const USER_EDIT_PAYPASS = (params) => new Promise(async (resolve, reject) => {
  try {
    const res = await $post('/users/edit/paypass', params);
    const { ret } = res.data
    if (ret == 1) {
      resolve(res.data)
    }
    reject(res.data)
  } catch (error) { }
})

export const USERS_EMAIL_GET = (params) => new Promise(async (resolve, reject) => {
  try {
    const res = await $post('/users/email/get', params, { isLoading: true });
    const { ret } = res.data
    if (ret == 1) {
      resolve(res.data)
    }
    reject(res.data)
  } catch (error) { }
})

export const USERS_EDIT_AVATAR = (params) => new Promise(async (resolve, reject) => {
  try {
    const res = await $post('/users/edit/avatar', params);
    const { ret } = res.data
    if (ret == 1) {
      resolve(res.data)
    }
    reject(res.data)
  } catch (error) { }
})

export const USERS_LEVELAUTH = (params) => new Promise(async (resolve, reject) => {
  try {
    const res = await $post('/users/levelauth', params);
    const { ret } = res.data
    if (ret == 1) {
      resolve(res.data)
    }
    reject(res.data)
  } catch (error) { }
})


export const GET_USERS_LEVELAUTH = () => new Promise(async (resolve, reject) => {
  try {
    const res = await $get('/users/levelauth');
    const { ret } = res.data
    if (ret == 1) {
      resolve(res.data)
    }
    reject(res.data)
  } catch (error) { }
})


export const USERS_THIRD_LOGIN = (params) => new Promise(async (resolve, reject) => {
  try {
    const res = await $post('/users/third/login', params);
    const { ret } = res.data
    if (ret == 1) {
      resolve(res.data)
    }
    reject(res.data)
  } catch (error) { }
})

export const CONTENT_COUPON = (params = {}, other = {}) => new Promise(async (resolve, reject) => {
  try {
    const res = await $post('/content/coupon', params, other);
    const { ret } = res.data
    if (ret == 1) {
      resolve(res.data)
    }
    reject(res.data)
  } catch (error) { }
})

export const SHOP_GET_COUPON = (id = '') => new Promise(async (resolve, reject) => {
  try {
    const res = await $post('/shop/get/coupon', { id });
    const { ret } = res.data
    if (ret == 1) {
      resolve(res.data)
    }
    reject(res.data)
  } catch (error) { }
})

export const SHOP_COUPON_LIST = (status = 0) => new Promise(async (resolve, reject) => {
  try {
    const res = await $post('/shop/coupon/list', { status });
    const { ret } = res.data
    if (ret == 1) {
      resolve(res.data)
    }
    reject(res.data)
  } catch (error) { }
})

export const USER_FORGET = (params) => new Promise(async (resolve, reject) => {
  try {
    const res = await $post('/users/forget', params, { isLoading: true });
    const { ret } = res.data
    if (ret == 1) {
      resolve(res.data)
    }
    reject(res.data)
  } catch (error) { }
})


export const USER_INIT = (params = {}) => new Promise(async (resolve, reject) => {
  try {
    const res = await $post('/users/init', {
      device: getDeviceInfo(),
      option: getEnterOptions(),
      version: manifest.versionCode,
      ...params
    }, { isLoading: true });
    const { ret } = res.data
    if (ret == 1) {
      resolve(res.data)
    }
    reject(res.data)
  } catch (error) {
    console.error(error);

  }
})

export const USER_LOGOUT = () => new Promise(async (resolve, reject) => {
  try {
    const res = await $post('/users/logout');
    const { ret } = res.data
    if (ret == 1) {
      resolve(res.data)
    }
    reject(res.data)
  } catch (error) { }
})

export const USER_MESSAGE = (params = {}) => new Promise(async (resolve, reject) => {
  try {
    const res = await $post('/users/message', { ...params });
    const { ret } = res.data
    if (ret == 1) {
      resolve(res.data)
    }
    reject(res.data)
  } catch (error) {
    console.error(error);

  }
})

export const USER_ORDER_COUNT = () => new Promise(async (resolve, reject) => {
  try {
    const res = await $post('/users/order/count');
    const { ret } = res.data
    if (ret == 1) {
      resolve(res.data)
    }
    reject(res.data)
  } catch (error) { }
})

export const USERS_MESSAGE_READER = (ids = '') => new Promise(async (resolve, reject) => {
  try {
    const res = await $post('/users/message/reader', { ids });
    const { ret } = res.data
    if (ret == 1) {
      resolve(res.data)
    }
    reject(res.data)
  } catch (error) { }
})

export const USERS_MESSAGE_UNREAD = () => new Promise(async (resolve, reject) => {
  try {
    const res = await $post('/users/message/unread');
    const { ret } = res.data
    if (ret == 1) {
      resolve(res.data)
    }
    reject(res.data)
  } catch (error) { }
})

export const USERS_MESSAGE_DETAIL = (id = '') => new Promise(async (resolve, reject) => {
  try {
    const res = await $post('/users/message/detail', { id });
    const { ret } = res.data
    if (ret == 1) {
      resolve(res.data)
    }
    reject(res.data)
  } catch (error) { }
})

export const USER_SMS = (params = {}) => new Promise(async (resolve, reject) => {
  try {
    const res = await $post('/users/sms', { ...params });
    const { ret } = res.data
    if (ret == 1) {
      resolve(res.data)
    }
    reject(res.data)
  } catch (error) {
    console.error(error);

  }
})

export const CONTENT_BOXES = (params = {}) => new Promise(async (resolve, reject) => {
  try {
    const res = await $post('/content/boxes', params, { isLoading: true });
    const { ret } = res.data
    if (ret == 1) {
      resolve(res.data)
    }
    reject(res.data)
  } catch (error) { }
})

export const CONTENT_RECEIVE_BOX = (params = {}) => new Promise(async (resolve, reject) => {
  try {
    const res = await $post('/content/receive/box', { ...params }, { isLoading: true });
    const { ret } = res.data
    if (ret == 1) {
      resolve(res.data)
    }
    reject(res.data)
  } catch (error) {
    console.error(error);
  }
})


export const CONTENT_BOX_LISTS = (params = {}) => new Promise(async (resolve, reject) => {
  try {
    const res = await $post('/content/box/lists', { ...params });
    const { ret } = res.data
    if (ret == 1) {
      resolve(res.data)
    }
    reject(res.data)
  } catch (error) {
    console.error(error);
  }
})

export const SHOP_STORE_LISTS = (params = {}) => new Promise(async (resolve, reject) => {
  try {
    const res = await $post('/shop/store/lists', { ...params });
    const { ret } = res.data
    if (ret == 1) {
      resolve(res.data)
    }
    reject(res.data)
  } catch (error) {
    console.error(error);
  }
})

export const USER_CHECK_PAYPASS = (oldpass = '') => new Promise(async (resolve, reject) => {
  try {
    const res = await $post('/users/check/paypass', { oldpass });
    const { ret } = res.data
    if (ret == 1) {
      resolve(res.data)
    }
    reject(res.data)
  } catch (error) { }
})

export const RONGIMCALL_NEED_FROMS = (params = {}) => new Promise(async (resolve, reject) => {
  try {
    const res = await $post('/rongimcall/need/froms', { ...params });
    const { ret } = res.data
    if (ret == 1) {
      resolve(res.data)
    }
    reject(res.data)
  } catch (error) {
    console.error(error);
  }
})

export const RONGIMCALL_FROMS_LISTS = (params = {}) => new Promise(async (resolve, reject) => {
  try {
    const res = await $post('/rongimcall/froms/lists', { ...params });
    const { ret } = res.data
    if (ret == 1) {
      resolve(res.data)
    }
    reject(res.data)
  } catch (error) {
    console.error(error);
  }
})



