import axios from "@/utils/AxiosService";

function getConfig(config = {}) {
  return Object.assign({ loading: true, encrypt: true }, config || {});
}

/**
 * 新增
 * @param url
 * @param params
 * @param config
 * @returns {Promise<unknown>}
 */
const insert = function (url, params, config = { loading: true }) {
  return new Promise((resolve, reject) => {
    axios.request(Object.assign({
      url: url,
      data: params,
      method: "POST"
    }, getConfig(config))).then(data => {
      resolve(data);
    }).catch(err => {
      reject(err);
    });
  });
};

const post = function (url, params, config = { loading: true }) {
  return new Promise((resolve, reject) => {
    axios.request(Object.assign({
      url: url,
      data: params,
      method: "POST"
    }, getConfig(config))).then(data => {
      resolve(data);
    }).catch(err => {
      reject(err);
    });
  });
};

const put = function (url, params, config = { loading: true }) {
  return new Promise((resolve, reject) => {
    axios.request(Object.assign({
      url: url,
      data: params,
      method: "PUT"
    }, getConfig(config))).then(data => {
      resolve(data);
    }).catch(err => {
      reject(err);
    });
  });
};

/**
 * 修改
 * @param url
 * @param params
 * @param config
 */
const update = function (url, params, config = { loading: true }) {
  return new Promise((resolve, reject) => {
    axios.request(Object.assign({
      url: url,
      data: params,
      method: "PUT"
    }, getConfig(config))).then(data => {
      resolve(data);
    }).catch(err => {
      reject(err);
    });
  });
};

/**
 * 查询一条记录
 * @param url
 * @param params
 * @param config
 */
const get = function (url, params, config = { loading: true }) {
  return new Promise((resolve, reject) => {
    axios.request(Object.assign({
      url: url,
      params,
      method: "GET"
    }, getConfig(config))).then(data => {
      resolve(data);
    }).catch(err => {
      reject(err);
    });
  });
};
/**
 * 查询一条记录
 * @param url
 * @param params
 * @param config
 */
const deletes = function (url, params, config = { loading: true }) {
  return new Promise((resolve, reject) => {
    axios.request(Object.assign({
      url: url,
      data: params,
      method: "DELETE"
    }, getConfig(config))).then(data => {
      resolve(data);
    }).catch(err => {
      reject(err);
    });
  });
};

/**
 * 分页
 * @param url
 * @param params
 * @param config
 */
const page = function (url, params, config = { loading: true }) {
  return new Promise((resolve, reject) => {
    axios.request(Object.assign({
      url: url,
      data: params,
      method: "POST"
    }, getConfig(config))).then(data => {
      resolve(data);
    }).catch(err => {
      reject(err);
    });
  });
};
const uploadFile = function (url, params, config = { loading: true }) {
  return new Promise((resolve, reject) => {
    axios.request(Object.assign({
      url: url,
      data: params,
      method: "POST"
    }, getConfig(config))).then(data => {
      resolve(data);
    }).catch(err => {
      reject(err);
    });
  });
};

const downloadFile = function (url, params, config = { loading: true }) {
  return new Promise((resolve, reject) => {
    axios.request(Object.assign({
      url: url,
      data: params,
      method: "POST",
      responseType: 'blob'
    }, config || {})).then(data => {
      resolve(data);
    }).catch(err => {
      reject(err);
    });
  });
};

export default {
  insert,
  update,
  get,
  deletes,
  post,
  page,
  put,
  uploadFile,
  downloadFile,
};
