import apiClient from "@/infrastructure/apiClient";
import apiPath from "@/infrastructure/apiPath";
export default {
  // Truck Management
  async truckCreateOrUpdate(formData) {
    var searchResult = await apiClient
      .post(apiPath.truck.truckCreateOrUpdate, formData, {
        headers: {
          "Content-Type": "multipart/form-data",
        },
      })
      .then((r) => {
        if (r.data) return r.data;
      })
      .catch((e) => {
        console.log(e);
        return {};
      });
    return searchResult;
  },
  async getById(id) {
    var result = await apiClient
      .get(apiPath.truck.getById + id)
      .then((r) => {
        if (r.data) return r.data;
      })
      .catch((e) => {
        console.log(e);
        return {};
      });
    return result;
  },
  async getTruckByIds(model) {
    const params = JSON.parse(JSON.stringify(model));
    var searchResult = await apiClient
      .post(apiPath.truck.truckGetByIds, params)
      .then((r) => {
        if (r.data) return r.data;
      })
      .catch((e) => {
        console.log(e);
        return {};
      });
    return searchResult;
  },
  async getTruckByAgencyId(model) {
    var searchResult = await apiClient
      .get(apiPath.truck.truckGetByAgencyId + "?id=" + model)
      .then((r) => {
        if (r.data) return r.data;
      })
      .catch((e) => {
        console.log(e);
        return {};
      });
    return searchResult;
  },
  async truckSearch(model) {
    const params = JSON.parse(JSON.stringify(model));
    var searchResult = await apiClient
      .post(apiPath.truck.truckSearch, params)
      .then((r) => {
        if (r.data) return r.data;
      })
      .catch((e) => {
        console.log(e);
        return {};
      });
    return searchResult;
  },
  async truckDelete(id) {
    var searchResult = await apiClient
      .post(apiPath.truck.truckDelete + '?id=' + id)
      .then((r) => {
        if (r.data) return r.data;
      })
      .catch((e) => {
        console.log(e);
        return {};
      });
    return searchResult;
  },
  // Truck Specification
  async truckSpecGetAll(id) {
    var searchResult = await apiClient
      .get(apiPath.truck.truckSpecGetAll + "?truckId=" + id)
      .then((r) => {
        if (r.data) return r.data;
      })
      .catch((e) => {
        console.log(e);
        return {};
      });
    return searchResult;
  },
  async truckSpecCreateOrUpdate(model) {
    const params = JSON.parse(JSON.stringify(model));
    var searchResult = await apiClient
      .post(apiPath.truck.truckSpecCreateOrUpdate, params)
      .then((r) => {
        if (r.data) return r.data;
      })
      .catch((e) => {
        console.log(e);
        return {};
      });
    return searchResult;
  },
  async truckSpecDelete(model) {
    const params = JSON.parse(JSON.stringify(model));
    var searchResult = await apiClient
      .post(apiPath.truck.truckSpecDelete, params)
      .then((r) => {
        if (r.data) return r.data;
      })
      .catch((e) => {
        console.log(e);
        return {};
      });
    return searchResult;
  },
  // Truck Specification Attribute
  async truckSpecAttrCreateOrUpdate(model) {
    const params = JSON.parse(JSON.stringify(model));
    var searchResult = await apiClient
      .post(apiPath.truck.truckSpecAttrCreateOrUpdate, params)
      .then((r) => {
        if (r.data) return r.data;
      })
      .catch((e) => {
        console.log(e);
        return {};
      });
    return searchResult;
  },
  async truckSpecAttrDelete(model) {
    const params = JSON.parse(JSON.stringify(model));
    var searchResult = await apiClient
      .post(apiPath.truck.truckSpecAttrDelete, params)
      .then((r) => {
        if (r.data) return r.data;
      })
      .catch((e) => {
        console.log(e);
        return {};
      });
    return searchResult;
  },
  async truckSpecAttrSearch(model) {
    const params = JSON.parse(JSON.stringify(model));
    var searchResult = await apiClient
      .post(apiPath.truck.truckSpecAttrSearch, params)
      .then((r) => {
        if (r.data) return r.data;
      })
      .catch((e) => {
        console.log(e);
        return {};
      });
    return searchResult;
  },
  async truckSpecAttrGetAll() {
    var searchResult = await apiClient
      .get(apiPath.truck.truckSpecAttrGetAll)
      .then((r) => {
        if (r.data) return r.data;
      })
      .catch((e) => {
        console.log(e);
        return {};
      });
    return searchResult;
  },
  // Truck Maintenance Attribute
  async truckMaintAttrCreateOrUpdate(model) {
    const params = JSON.parse(JSON.stringify(model));
    var searchResult = await apiClient
      .post(apiPath.truck.truckMaintAttrCreateOrUpdate, params)
      .then((r) => {
        if (r.data) return r.data;
      })
      .catch((e) => {
        console.log(e);
        return {};
      });
    return searchResult;
  },
  async truckMaintAttrDelete(model) {
    const params = JSON.parse(JSON.stringify(model));
    var searchResult = await apiClient
      .post(apiPath.truck.truckMaintAttrDelete, params)
      .then((r) => {
        if (r.data) return r.data;
      })
      .catch((e) => {
        console.log(e);
        return {};
      });
    return searchResult;
  },
  async truckMaintAttrSearch(model) {
    const params = JSON.parse(JSON.stringify(model));
    var searchResult = await apiClient
      .post(apiPath.truck.truckMaintAttrSearch, params)
      .then((r) => {
        if (r.data) return r.data;
      })
      .catch((e) => {
        console.log(e);
        return {};
      });
    return searchResult;
  },
  async truckMaintAttrGetAll() {
    var searchResult = await apiClient
      .get(apiPath.truck.truckMaintAttrGetAll)
      .then((r) => {
        if (r.data) return r.data;
      })
      .catch((e) => {
        console.log(e);
        return {};
      });
    return searchResult;
  },
  // Truck Maintenance Link
  async truckMaintLinkGetAll(id) {
    var searchResult = await apiClient
      .get(apiPath.truck.truckMaintLinkGetAll + "?truckMaintId=" + id)
      .then((r) => {
        if (r.data) return r.data;
      })
      .catch((e) => {
        console.log(e);
        return {};
      });
    return searchResult;
  },

  //Truck Maintenance
  async truckMaintCreateOrUpdate(model) {
    const params = JSON.parse(JSON.stringify(model));
    var searchResult = await apiClient
      .post(apiPath.truck.truckMaintCreateOrUpdate, params)
      .then((r) => {
        if (r.data) return r.data;
      })
      .catch((e) => {
        console.log(e);
        return {};
      });
    return searchResult;
  },
  async truckMaintDelete(model) {
    const params = JSON.parse(JSON.stringify(model));
    var searchResult = await apiClient
      .post(apiPath.truck.truckMaintDelete, params)
      .then((r) => {
        if (r.data) return r.data;
      })
      .catch((e) => {
        console.log(e);
        return {};
      });
    return searchResult;
  },
  async truckMaintSearch(model) {
    const params = JSON.parse(JSON.stringify(model));
    var searchResult = await apiClient
      .post(apiPath.truck.truckMaintSearch, params)
      .then((r) => {
        if (r.data) return r.data;
      })
      .catch((e) => {
        console.log(e);
        return {};
      });
    return searchResult;
  },
  async truckMaintByIds(model) {
    const params = JSON.parse(JSON.stringify(model));
    var searchResult = await apiClient
      .post(apiPath.truck.truckMaintByIds, params)
      .then((r) => {
        if (r.data) return r.data;
      })
      .catch((e) => {
        console.log(e);
        return {};
      });
    return searchResult;
  },
  //Truck Activity
  async truckCheckIn(model) {
    var searchResult = await apiClient
      .get(
        apiPath.truck.truckCheckIn +
        `?agencyId=${model.AgencyId}&truckId=${model.TruckId}`
      )
      .then((r) => {
        if (r.data) return r.data;
      })
      .catch((e) => {
        console.log(e);
        return {};
      });
    return searchResult;
  },
  async truckCheckOut(model) {
    var searchResult = await apiClient
      .get(
        apiPath.truck.truckCheckOut +
        `?agencyId=${model.AgencyId}&truckId=${model.TruckId}`
      )
      .then((r) => {
        if (r.data) return r.data;
      })
      .catch((e) => {
        console.log(e);
        return {};
      });
    return searchResult;
  },
  // Get available trucks for driver utils
  async getAvailableTrucks() {
    var searchResult = await apiClient
      .get(apiPath.truck.getAvailableTrucks)
      .then((r) => {
        if (r.data) return r.data;
      })
      .catch((e) => {
        console.log(e);
        return [];
      });
    return searchResult;
  },
  // Get all trucks by agency
  async getTrucksByAgency() {
    var searchResult = await apiClient
      .get(apiPath.truck.getTrucksByAgency)
      .then((r) => {
        if (r.data) return r.data;
      })
      .catch((e) => {
        console.log(e);
        return [];
      });
    return searchResult;
  },
  // Check in truck with images
  async checkInTruck(formData) {
    var searchResult = await apiClient
      .post(apiPath.truck.checkInTruck, formData, {
        headers: {
          "Content-Type": "multipart/form-data",
        },
      })
      .then((r) => {
        if (r.data) return r.data;
      })
      .catch((e) => {
        console.log(e);
        return { Result: 0, Message: "Có lỗi xảy ra khi gửi yêu cầu" };
      });
    return searchResult;
  },
  // Get my current truck
  async getMyCurrentTruck() {
    var searchResult = await apiClient
      .get(apiPath.truck.getMyCurrentTruck)
      .then((r) => {
        if (r.data) return r.data;
      })
      .catch((e) => {
        console.log(e);
        return {
          Result: 0,
          Message: "Có lỗi xảy ra khi lấy thông tin xe",
          Items: [],
        };
      });
    return searchResult;
  },
  // Check out truck with images
  async checkOutTruck(formData) {
    var searchResult = await apiClient
      .post(apiPath.truck.checkOutTruck, formData, {
        headers: {
          "Content-Type": "multipart/form-data",
        },
      })
      .then((r) => {
        if (r.data) return r.data;
      })
      .catch((e) => {
        console.log(e);
        return { Result: 0, Message: "Có lỗi xảy ra khi gửi yêu cầu" };
      });
    return searchResult;
  },
  // Activity History
  async getActivityHistory(truckId) {
    var searchResult = await apiClient
      .get(apiPath.truck.getActivityHistory + "/" + truckId)
      .then((r) => {
        if (r.data) return r.data;
      })
      .catch((e) => {
        console.log(e);
        return {};
      });
    return searchResult;
  },
  // Update Last Refill ODO
  async updateLastRefillOdo(truckId, odoValue) {
    var searchResult = await apiClient
      .post(apiPath.truck.updateLastRefillOdo + `?truckId=${truckId}&odoValue=${odoValue}`)
      .then((r) => {
        if (r.data) return r.data;
      })
      .catch((e) => {
        console.log(e);
        return { Result: 2, Message: "Có lỗi xảy ra khi cập nhật ODO" };
      });
    return searchResult;
  },
  // Get Last Refill ODO
  async getLastRefillOdo(truckId) {
    var searchResult = await apiClient
      .get(apiPath.truck.getLastRefillOdo + `?truckId=${truckId}`)
      .then((r) => {
        if (r.data) return r.data;
      })
      .catch((e) => {
        console.log(e);
        return null;
      });
    return searchResult;
  },
};
