import { AxiosRequestData } from "@/data/axios_request";
import cache from "@/utils/cache";

/**
 * 电子发票列表查询
 * @param {String} invoiceCode ：发票代码
 * @param {String} invoiceNumber : 发票号码
 * @param {Number} invoiceCategory : 发票种类，1.增值税普通电子发票   2.增值税电子专用发票  3.增值税普通纸质发票  4 增值税专用纸质发票
 * @param {String} belongEntName : 销方名称
 * @param {Number} status ：发票状态   1:开票中  2：开具失败  3：正数开具成功 4：负数开具成功 5：正数已作废 6：负数已作废 7：正数已红冲
 * @param {String} invoiceDateBegin : 开票日期范围查询-开始时间 格式yyyy-MM-dd HH:mm:ss
 * @param {String} invoiceDateEnd : 开票日期范围查询-结束时间 格式yyyy-MM-dd HH:mm:ss
 * @param {Number} pageNum :  当前页码  默认为1
 * @param {Number} pageSize : 每页数量  默认为10
 */
function invoiceQuery({
  invoiceCode = "",
  invoiceNumber = "",
  invoiceCategory = -1,
  belongEntName = "",
  belongEntTaxId = "",
  status = -1,
  invoiceDateBegin = "",
  invoiceDateEnd = "",
  pageNum = 1,
  pageSize = 10,
}) {
  const url = "/sys/invoice/findPage";
  let token = cache.getToken();
  let mobile = JSON.parse(cache.getLocal("user")).account;
  let args = {
    mobile: mobile,
    reqChannel: 4,
    pageNum: pageNum,
    pageSize: pageSize,
  };
  if (invoiceCode != "") {
    args.invoiceCode = invoiceCode;
  }
  if (invoiceNumber != "") {
    args.invoiceNumber = invoiceNumber;
  }
  if (invoiceCategory != -1 && invoiceCategory != "") {
    args.invoiceCategory = invoiceCategory;
  }
  if (belongEntName != "") {
    args.belongEntName = belongEntName;
  }
  if (belongEntTaxId !== "") {
    args.belongEntTaxId = belongEntTaxId;
  }
  if (status != -1 && status != "") {
    args.status = status;
  }
  if (invoiceDateBegin != "") {
    args.invoiceDateBegin = invoiceDateBegin;
  }
  if (invoiceDateEnd != "") {
    args.invoiceDateEnd = invoiceDateEnd;
  }
  console.log("发票查询 参数：");
  console.log(args);
  return new Promise(function(resolve, reject) {
    AxiosRequestData(url, token, args, false, 2).then((val) => {
      console.log("发票查询 结果：");
      console.log(val);
      if (val.code == 0) {
        resolve(val);
      } else {
        reject(val.msg);
      }
    });
  });
}

/**
 * 刷新开票结果
 * @param {String} invoiceReqFlowNo : 发票请求流水号
 */
function invoiceRefresh(invoiceReqFlowNo) {
  const url = "/sys/invoice/refreshResult";
  let token = cache.getToken();
  let mobile = JSON.parse(cache.getLocal("user")).account;
  let args = {
    invoiceReqFlowNo: invoiceReqFlowNo,
    mobile: mobile,
    reqChannel: 4,
  };
  console.log("刷新开票结果 参数：");
  console.log(args);
  return new Promise(function(resolve, reject) {
    AxiosRequestData(url, token, args, false, 1).then((v) => {
      console.log("刷新开票结果 结果：");
      console.log(v);
      if (v.code === 0) {
        resolve(v);
      } else {
        reject(v.msg);
      }
    });
  });
}

/**
 * 开票失败重试
 * @param {String} invoiceReqFlowNo : 发票请求流水号
 * @param {Number} status 开票状态 1： 开票中  2：失败  默认值2：失败
 */
function invoiceRetry(invoiceReqFlowNo, status) {
  const url = "/sys/invoice/retry";
  let token = cache.getToken();
  let mobile = JSON.parse(cache.getLocal("user")).account;
  let args = {
    invoiceReqFlowNo: invoiceReqFlowNo,
    status: status,
    mobile: mobile,
    reqChannel: 4,
  };
  console.log("开票失败重试 参数：");
  console.log(args);
  return new Promise(function(resolve, reject) {
    AxiosRequestData(url, token, args, false, 1).then((v) => {
      console.log("开票失败重试 结果：");
      console.log(v);
      if (v.code === 0) {
        resolve(v);
      } else {
        reject(v.msg);
      }
    });
  });
}

/**
 * 异常发票查询
 * @param {Number} timeOut 开票超时时间  单位分钟
 * @param {Number} invoiceType 发票类型 0-蓝字发票；1-红字发票
 * @param {String} invoicePerson 开票人
 * @param {Number} invoiceCategory 发票分类 1.增值税普通电子发票   2.增值税电子专用发票  3.增值税普通纸质发票  4 增值税专用纸质发票
 * @param {String} belongEntTaxId 开发票的企业税号
 * @param {String} belongEntName 开发票的企业名
 * @param {String} customerEntTaxId 客户企业税号
 * @param {String} customerName 客户名
 * @param {String} invoiceDateBegin : 开票日期范围查询-开始时间 格式yyyy-MM-dd HH:mm:ss
 * @param {String} invoiceDateEnd : 开票日期范围查询-结束时间 格式yyyy-MM-dd HH:mm:ss
 * @param {Number} pageNum :  当前页码  默认为1
 * @param {Number} pageSize : 每页数量  默认为10
 */
function invoiceAbnormalQuery({
  timeOut = "",
  invoiceType = "",
  invoicePerson = "",
  invoiceCategory = -1,
  belongEntTaxId = "",
  belongEntName = "",
  customerEntTaxId = "",
  customerName = "",
  invoiceDateBegin = "",
  invoiceDateEnd = "",
  pageNum = 1,
  pageSize = 10,
}) {
  const url = "/sys/invoice/findAbnormalPage";
  let token = cache.getToken();
  let mobile = JSON.parse(cache.getLocal("user")).account;
  let args = {
    mobile: mobile,
    pageNum: pageNum,
    pageSize: pageSize,
  };
  if (timeOut != "") {
    args.timeOut = timeOut;
  }
  if (invoiceType !== "") {
    args.invoiceType = invoiceType;
  }
  if (invoicePerson != "") {
    args.invoicePerson = invoicePerson;
  }
  if (invoiceCategory != -1 && invoiceCategory != "") {
    args.invoiceCategory = invoiceCategory;
  }
  if (belongEntTaxId != "") {
    args.belongEntTaxId = belongEntTaxId;
  }
  if (belongEntName != "") {
    args.belongEntName = belongEntName;
  }
  if (customerEntTaxId != "") {
    args.customerEntTaxId = customerEntTaxId;
  }
  if (customerName != "") {
    args.customerName = customerName;
  }
  if (invoiceDateBegin != "") {
    args.invoiceDateBegin = invoiceDateBegin;
  }
  if (invoiceDateEnd != "") {
    args.invoiceDateEnd = invoiceDateEnd;
  }
  console.log("异常发票查询 参数：");
  console.log(args);
  return new Promise(function(resolve, reject) {
    AxiosRequestData(url, token, args, false, 2).then((val) => {
      console.log("异常发票查询 结果：");
      console.log(val);
      if (val.code == 0) {
        resolve(val);
      } else {
        reject(val.msg);
      }
    });
  });
}

/**
 * 开具失败发票查询
 * @param {Number} invoiceCategory : 发票种类，1.增值税普通电子发票   2.增值税电子专用发票  3.增值税普通纸质发票  4 增值税专用纸质发票
 * @param {String} belongEntTaxId : 销方税号
 * @param {String} belongEntName : 销方名称
 * @param {String} customerName : 客户名称
 * @param {String} invoiceDateBegin : 开票日期范围查询-开始时间 格式yyyy-MM-dd HH:mm:ss
 * @param {String} invoiceDateEnd : 开票日期范围查询-结束时间 格式yyyy-MM-dd HH:mm:ss
 * @param {Number} pageNum :  当前页码  默认为1
 * @param {Number} pageSize : 每页数量  默认为10
 */
function invoiceFindFailed({
  invoiceCategory = -1,
  belongEntTaxId = "",
  belongEntName = "",
  customerName = "",
  invoicePerson = "",
  invoiceDateBegin = "",
  invoiceDateEnd = "",
  pageNum = 1,
  pageSize = 10,
}) {
  const url = "/sys/invoice/findFailedPage";
  let token = cache.getToken();
  let mobile = JSON.parse(cache.getLocal("user")).account;
  let args = {
    mobile: mobile,
    reqChannel: 4,
    pageNum: pageNum,
    pageSize: pageSize,
  };
  if (invoiceCategory != -1 && invoiceCategory != "") {
    args.invoiceCategory = invoiceCategory;
  }
  if (belongEntTaxId != "") {
    args.belongEntTaxId = belongEntTaxId;
  }
  if (belongEntName != "") {
    args.belongEntName = belongEntName;
  }
  if (customerName != "") {
    args.customerName = customerName;
  }
  if (invoicePerson != "") {
    args.invoicePerson = invoicePerson;
  }
  if (invoiceDateBegin != "") {
    args.invoiceDateBegin = invoiceDateBegin;
  }
  if (invoiceDateEnd != "") {
    args.invoiceDateEnd = invoiceDateEnd;
  }
  console.log("开具失败发票查询 参数：");
  console.log(args);
  return new Promise(function(resolve, reject) {
    AxiosRequestData(url, token, args, false, 2).then((val) => {
      console.log("开具失败发票查询 结果：");
      console.log(val);
      if (val.code == 0) {
        resolve(val);
      } else {
        reject(val.msg);
      }
    });
  });
}

/**
 * 获取开票失败信息
 * @param {String} invoiceId 发票主键id
 */
function invoiceGetFailedMsg(invoiceId) {
  const url = "/sys/invoice/failedMsg";
  let token = cache.getToken();
  let args = {
    invoiceId: invoiceId,
  };
  console.log("获取开票失败信息 参数：");
  console.log(args);
  return new Promise(function(resolve, reject) {
    AxiosRequestData(url, token, args, false, 2).then((val) => {
      console.log("获取开票失败信息 结果：");
      console.log(val);
      if (val.code === 0) {
        resolve(val);
      } else {
        reject(val.msg);
      }
    });
  });
}

/**
 * 开票中发票置为失败
 * @param {String} invoiceReqFlowNo 发票请求流水号
 * @param {String} entTaxId 销方企业税号
 * @param {String} reason 置为失败的原因
 */
function invoiceSetFailed(invoiceReqFlowNo, entTaxId, reason = "") {
  const url = "/sys/invoice/setFailedStatus";
  let token = cache.getToken();
  let mobile = JSON.parse(cache.getLocal("user")).account;
  let args = {
    invoiceReqFlowNo: invoiceReqFlowNo,
    entTaxId: entTaxId,
    mobile: mobile,
    reqChannel: 4,
  };
  if (reason != "") {
    args.reason = reason;
  }
  console.log("开票中发票置为失败 参数：");
  console.log(args);
  return new Promise(function(resolve, reject) {
    AxiosRequestData(url, token, args, false, 1).then((val) => {
      console.log("开票中发票置为失败 结果：");
      console.log(val);
      if (val.code === 0) {
        resolve(val);
      } else {
        reject(val.msg);
      }
    });
  });
}

/**
 * 获取接口开票请求参数
 * @param {String} cmdId 指令Id
 */
function invoiceGetReqParams(cmdId) {
  const url = "/sys/invoice/getReqParams";
  let token = cache.getToken();
  let args = {
    cmdId: cmdId,
  };
  console.log("获取接口开票请求参数 参数：");
  console.log(args);
  return new Promise(function(resolve, reject) {
    AxiosRequestData(url, token, args, false, 2).then((val) => {
      console.log("获取接口开票请求参数 结果：");
      console.log(val);
      if (val.code === 0) {
        resolve(val);
      } else {
        reject(val.msg);
      }
    });
  });
}

/**
 * 修改发票请求参数
 * @param {String} cmdId 指令Id
 */
function invoiceUpdReqParams(cmdId, changeContent, fpkjCmdParam) {
  const url = "/sys/invoice/updReqParams";
  let token = cache.getToken();
  let args = {
    cmdId,
    changeContent,
    fpkjCmdParam,
  };
  console.log("修改发票请求参数 参数：");
  console.log(args);
  return new Promise(function(resolve, reject) {
    AxiosRequestData(url, token, args, false, 1).then((val) => {
      console.log("修改发票请求参数 结果：");
      console.log(val);
      if (val.code === 0) {
        resolve(val);
      } else {
        reject(val.msg);
      }
    });
  });
}

/**
 * 获取开票特殊字符集合
 */
function getSpeChars() {
  const url = "/sys/invoice/getSpeChars";
  let token = cache.getToken();
  let args = {};
  console.log("获取开票特殊字符集合 参数：");
  console.log(args);
  return new Promise(function(resolve, reject) {
    AxiosRequestData(url, token, args, false, 2).then((val) => {
      console.log("获取开票特殊字符集合 结果：");
      console.log(val);
      if (val.code === 0) {
        resolve(val);
      } else {
        reject(val.msg);
      }
    });
  });
}

export {
  invoiceQuery,
  invoiceRefresh,
  invoiceRetry,
  invoiceAbnormalQuery,
  invoiceFindFailed,
  invoiceGetFailedMsg,
  invoiceSetFailed,
  invoiceGetReqParams,
  invoiceUpdReqParams,
  getSpeChars,
};
