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

export default {
  async assign(model, addressType) {
    var apiAddress = "";
    const params = JSON.parse(JSON.stringify(model));

    if (addressType === 1) {
      apiAddress = apiPath.deploy.assignPickup;
    } else if (addressType === 2) {
      apiAddress = apiPath.deploy.assignDelivery;
    } else {
      apiAddress = apiPath.deploy.assignTransit;
    }
    var assignResult = await apiClient
      .post(apiAddress, params)
      .then((r) => {
        return r.data;
      })
      .catch((e) => {
        console.log(e);
        return {};
      });
    return assignResult;
  },
  async unassign(model, addressType) {
    var apiAddress = "";
    const params = JSON.parse(JSON.stringify(model));

    if (addressType === 1) {
      apiAddress = apiPath.deploy.unAssignPickup;
    } else {
      apiAddress = apiPath.deploy.unAssignPickup;
    }
    var assignResult = await apiClient
      .post(apiAddress, params)
      .then((r) => {
        return r.data;
      })
      .catch((e) => {
        console.log(e);
        return {};
      });
    return assignResult;
  },
  async getLocations(addressType) {
    var apiAddress = "";

    if (addressType === 1) {
      apiAddress = apiPath.deploy.getPickupLocations;
    } else if (addressType === 2) {
      apiAddress = apiPath.deploy.getDeliveryLocations;
    } else {
      apiAddress = apiPath.deploy.getTransitLocations;
    }
    var searchResult = await apiClient
      .get(apiAddress)
      .then((r) => {
        return r.data;
      })
      .catch((e) => {
        console.log(e);
        return {};
      });
    return searchResult;
  },
  async getDeployOrder(addressType, locationId) {
    var apiAddress = "";
    if (addressType === 1) {
      apiAddress = apiPath.deploy.getPickupOrders;
    } else if (addressType === 2) {
      apiAddress = apiPath.deploy.getDeliveryOrders;
    } else {
      apiAddress = apiPath.deploy.getDeliveryOrders;
    }

    const searchModel = {
      Pagination: {
        Total: 0,
        PageIndex: 1,
        PageSize: 25,
      },
      Sort: {
        Predicate: "",
        Reverse: true,
      },
      Search: {
        PredicateObject: {
          AddressType: addressType,
          LocationId: locationId,
        },
      },
    };
    var searchResult = await apiClient
      .post(apiAddress, searchModel)
      .then((r) => {
        return r.data;
      })
      .catch((e) => {
        console.log(e);
        return {};
      });
    return searchResult;
  },
  async getDeployMonitor(pageNumber = 1, pageSize = 10) {
    var apiAddress = `${apiPath.deploy.getDeployMonitor}?pageNumber=${pageNumber}&&pageSize=${pageSize}`;
    var searchResult = await apiClient
      .get(apiAddress)
      .then((r) => {
        return r.data;
      })
      .catch((e) => {
        console.log(e);
        return {};
      });
    return searchResult;
  },
  async getDeployDetail(searchModel) {
    var apiAddress = apiPath.deploy.getDeployDetail;
    var searchResult = await apiClient
      .post(apiAddress, searchModel)
      .then((r) => {
        return r.data;
      })
      .catch((e) => {
        console.log(e);
        return {};
      });
    return searchResult;
  },
  async reorderOrders(items) {
    var apiAddress = apiPath.deploy.reorderOrders;

    // Wrap the items array into the expected { Items: [...] } payload structure
    const payload = { Items: items };

    var result = await apiClient
      .post(apiAddress, payload)
      .then((r) => {
        return r.data;
      })
      .catch((e) => {
        console.log(e);
        return { Result: -1, Message: e.message || 'Lỗi hệ thống' };
      });
    return result;
  },
};
