import apiClient from "@/infrastructure/apiClient";
import apiPath from "@/infrastructure/apiPath";

export default {
  async RandomCode(prefix) {
    let dateNow = Date.now();
    let randonStr = Math.random().toString(36).slice(2, 7).toUpperCase();
    return `${prefix}${dateNow}${randonStr}`;
  },
  async CreateOrder(model) {
    const params = JSON.parse(JSON.stringify(model));
    var result = await apiClient
      .post(apiPath.order.createOrder, params)
      .then((r) => {
        if (r.data) return r.data;
      })
      .catch((e) => {
        console.log(e);
        return {};
      });
    return result;
  },
  async Search(model) {
    const params = JSON.parse(JSON.stringify(model));
    var result = await apiClient
      .post(apiPath.order.searchOrder, params)
      .then((r) => {
        if (r.data) return r.data;
      })
      .catch((e) => {
        console.log(e);
        return {};
      });
    return result;
  },
  async Tracking(code) {
    var result = await apiClient
      .get(apiPath.order.tracking + `${code}`)
      .then((r) => {
        if (r.data) {
          return r.data;
        }
      })
      .catch((e) => {
        return {};
      });
    return result;
  },
  async UpdateAddress(value) {
    const params = JSON.parse(JSON.stringify(value));
    var result = await apiClient
      .post(apiPath.order.updateAddress, params)
      .then((r) => {
        if (r.data) {
          return r.data;
        }
      })
      .catch((e) => {
        return {};
      });
    return result;
  },
  async Cancel(model) {
    const params = JSON.parse(JSON.stringify(model));
    console.log("vao day roi");
    console.log(model);
    console.log(apiPath.order.cancel);
    var result = await apiClient
      .put(apiPath.order.cancel, params)
      .then((r) => {
        if (r.data) {
          return r.data;
        }
      })
      .catch((e) => {
        return {};
      });
    return result;
  },
  async RemoveCancel(model) {
    const params = JSON.parse(JSON.stringify(model));
    var result = await apiClient
      .put(apiPath.order.removeCancel, params)
      .then((r) => {
        if (r.data) {
          return r.data;
        }
      })
      .catch((e) => {
        return {};
      });
    return result;
  },
  async getPaymentMethods() {
    var result = await apiClient
      .get(apiPath.order.paymentMethods)
      .then((r) => {
        if (r.data) {
          return r.data;
        }
      })
      .catch((e) => {
        console.error("Error getting payment methods:", e);
        return [];
      });
    return result;
  },
  async getTimeFilterTypes() {
    var result = await apiClient
      .get(apiPath.order.timeFilterTypes)
      .then((r) => {
        if (r.data) {
          return r.data;
        }
      })
      .catch((e) => {
        console.error("Error getting time filter types:", e);
        return [];
      });
    return result;
  },
  async prepareOrderListFilter(customerId = 0) {
    const payload = {
      CustomerId: Number(customerId) > 0 ? Number(customerId) : 0,
    };
    var result = await apiClient
      .post(apiPath.order.prepareOrderListFilter, payload)
      .then((r) => {
        if (r.data) {
          return r.data;
        }
      })
      .catch((e) => {
        console.error("Error preparing order list filter:", e);
        return null;
      });
    return result;
  },
  async exportExcel(exportModel) {
    const params = JSON.parse(JSON.stringify(exportModel));
    var result = await apiClient
      .post(apiPath.order.exportExcel, params)
      .then((r) => {
        if (r.data) {
          return r.data;
        }
      })
      .catch((e) => {
        console.error("Error exporting Excel:", e);
        return null;
      });
    return result;
  },
  async downloadExcel(fileName) {
    var result = await apiClient
      .get(`${apiPath.order.downloadExcel}/${fileName}`, {
        responseType: "blob",
      })
      .then((r) => {
        return r.data;
      })
      .catch((e) => {
        console.error("Error downloading Excel:", e);
        return null;
      });
    return result;
  },
};
