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

export default {
  async search(searchModel) {
    const params = JSON.parse(JSON.stringify(searchModel));
    var searchResult = await apiClient
      .post(apiPath.customer.search, params)
      .then((r) => {
        return r.data;
      })
      .catch((e) => {
        console.log(e);
        return {};
      });
    return searchResult;
  },
  async suggest(keyword) {
    var searchResult = await apiClient
      .get(apiPath.customer.suggest + keyword)
      .then((r) => {
        return r.data;
      })
      .catch((e) => {
        console.log(e);
        return {};
      });
    return searchResult;
  },
  async createOrUpdate(customerModel) {
    const params = JSON.parse(JSON.stringify(customerModel));
    var customerResult = await apiClient
      .post(apiPath.customer.createOrUpdate, params)
      .then((r) => {
        return r.data;
      })
      .catch((e) => {
        console.log(e);
        return {};
      });
    return customerResult;
  },
  async updateProp(customerModel) {
    const params = JSON.parse(JSON.stringify(customerModel));
    var customerResult = await apiClient
      .put(apiPath.customer.updateProp, params)
      .then((r) => {
        return r.data;
      })
      .catch((e) => {
        console.log(e);
        return {};
      });
    return customerResult;
  },
  async getProp(customerId, propType) {
    var customerPropModel = await apiClient
      .get(apiPath.customer.getProp + customerId + "&propType=" + propType)
      .then((r) => {
        return r.data;
      })
      .catch((e) => {
        console.log(e);
        return {};
      });
    return customerPropModel;
  },
  async getById(id) {
    var customerModel = await apiClient
      .get(apiPath.customer.getById + id)
      .then((r) => {
        return r.data;
      })
      .catch((e) => {
        console.log(e);
        return {};
      });
    return customerModel;
  },
  async lock(customerId) {
    const params = JSON.parse(JSON.stringify({ Id: customerId }));

    var roleResult = await apiClient
      .put(apiPath.customer.lock, params)
      .then((r) => {
        return r.data;
      })
      .catch((e) => {
        console.log(e);
        throw new Error(e);
      });
    return roleResult;
  },
  async unlock(customerId) {
    const params = JSON.parse(JSON.stringify({ Id: customerId }));

    var roleResult = await apiClient
      .put(apiPath.customer.unLock, params)
      .then((r) => {
        return r.data;
      })
      .catch((e) => {
        console.log(e);
        throw new Error(e);
      });
    return roleResult;
  },
  async registerAccount(createOrUpdateModel) {
    const params = JSON.parse(JSON.stringify(createOrUpdateModel));

    var searchResult = await apiClient
      .post(apiPath.customer.registerAccount, params)
      .then((r) => {
        return r.data;
      })
      .catch((e) => {
        console.log(e);
        return {};
      });
    return searchResult;
  },
  async getManagerById(id) {
    var customerModel = await apiClient
      .get(apiPath.customer.getManagerById + id)
      .then((r) => {
        return r.data;
      })
      .catch((e) => {
        console.log(e);
        return {};
      });
    return customerModel;
  },
  async assignManagerById(model) {
    const params = JSON.parse(JSON.stringify(model));

    var roleResult = await apiClient
      .put(apiPath.customer.assignManager, params)
      .then((r) => {
        return r.data;
      })
      .catch((e) => {
        console.log(e);
        throw new Error(e);
      });
    return roleResult;
  },
  async setPassword(setPasswordModel) {
    const params = JSON.parse(JSON.stringify(setPasswordModel));

    var roleResult = await apiClient
      .put(apiPath.customer.setPassword, params)
      .then((r) => {
        return r.data;
      })
      .catch((e) => {
        console.log(e);
        return {};
      });
    return roleResult;
  },
};
