const URL = "/one-api/mockup/itemout/";

export async function search(payload) {
  try {
    var response = await axios.post(URL + "RequestItemOut/search", payload);
    if (response.status !== 200) {
      return {
        status: "ERR",
        message: response.statusText,
      };
    }

    let data = response.data;
    return data;
  } catch (e) {
    return {
      status: "ERR",
      message: e.message,
    };
  }
}

export async function searchDetail(payload) {
  try {
    var response = await axios.post(
      URL + "RequestItemOut/searchDetail",
      payload
    );
    if (response.status !== 200) {
      return {
        status: "ERR",
        message: response.statusText,
      };
    }

    let data = response.data;
    return data;
  } catch (e) {
    return {
      status: "ERR",
      message: e.message,
    };
  }
}

export async function getRegional(payload) {
  try {
    var response = await axios.post(URL + "RequestItemOut/getRegionalBranchByUser", payload);
    if (response.status !== 200) {
      return {
        status: "ERR",
        message: response.statusText,
      };
    }

    let data = response.data;
    return data;
  } catch (e) {
    return {
      status: "ERR",
      message: e.message,
    };
  }
}

export async function getBranch(payload) {
  try {
    var response = await axios.post(URL + "RequestItemOut/getBranch", payload);
    if (response.status !== 200) {
      return {
        status: "ERR",
        message: response.statusText,
      };
    }

    let data = response.data;
    return data;
  } catch (e) {
    return {
      status: "ERR",
      message: e.message,
    };
  }
}

export async function deleteRequest(payload) {
  try {
    var response = await axios.post(
      URL + "RequestItemOut/deleteRequest",
      payload
    );
    if (response.status !== 200) {
      return {
        status: "ERR",
        message: response.statusText,
      };
    }

    let data = response.data;
    return data;
  } catch (e) {
    return {
      status: "ERR",
      message: e.message,
    };
  }
}

export async function saveDetail(payload) {
  try {
    var response = await axios.post(
      URL + "RequestItemOut/saveDetail",
      payload
    );
    if (response.status !== 200) {
      return {
        status: "ERR",
        message: response.statusText,
      };
    }

    let data = response.data;
    return data;
  } catch (e) {
    return {
      status: "ERR",
      message: e.message,
    };
  }
}
export async function updateDetail(payload) {
  try {
    var response = await axios.post(
      URL + "RequestItemOut/updateDetail",
      payload
    );
    if (response.status !== 200) {
      return {
        status: "ERR",
        message: response.statusText,
      };
    }

    let data = response.data;
    return data;
  } catch (e) {
    return {
      status: "ERR",
      message: e.message,
    };
  }
}
export async function deleteDetail(payload) {
  try {
    var response = await axios.post(
      URL + "RequestItemOut/deleteDetail",
      payload
    );
    if (response.status !== 200) {
      return {
        status: "ERR",
        message: response.statusText,
      };
    }

    let data = response.data;
    return data;
  } catch (e) {
    return {
      status: "ERR",
      message: e.message,
    };
  }
}

export async function orderRequest(payload) {
  try {
    var response = await axios.post(
      URL + "RequestItemOut/orderRequest",
      payload
    );
    if (response.status !== 200) {
      return {
        status: "ERR",
        message: response.statusText,
      };
    }

    let data = response.data;
    return data;
  } catch (e) {
    return {
      status: "ERR",
      message: e.message,
    };
  }
}

export async function getUnit(token, prm) {
  try {
      var resp = await axios.post(URL + 'RequestItemOut/getUnit', {
          token: token,
          search: prm
      });
      if (resp.status != 200) {
          return {
              status: "ERR",
              message: resp.statusText
          };
      }
      let data = resp.data;
      return data;
  } catch (e) {
      return {
          status: "ERR",
          message: e.message
      };
  }
}


export async function getDivisionByUser(payload) {
  try {
      var resp = await axios.post(URL + 'RequestItemOut/getDivisionByUser', payload);
      if (resp.status != 200) {
          return {
              status: "ERR",
              message: resp.statusText
          };
      }
      let data = resp.data;
      return data;
  } catch (e) {
      return {
          status: "ERR",
          message: e.message
      };
  }
}


export async function getItems(payload) {
  try {
      var resp = await axios.post(URL + 'RequestItemOut/getItems', payload);
      if (resp.status != 200) {
          return {
              status: "ERR",
              message: resp.statusText
          };
      }
      let data = resp.data;
      return data;
  } catch (e) {
      return {
          status: "ERR",
          message: e.message
      };
  }
}

export async function getItemGroupSubGroup(payload) {
  try {
    var resp = await axios.post(URL + 'RequestItemOut/getItemGroupSubGroup', payload);
    if (resp.status != 200) {
      return {  
        status: "ERR",
        message: resp.statusText
      };
    }
    let data = resp.data;
    return data;
  } catch (e) {
    return {
      status: "ERR",
      message: e.message
    };
  }
}

export async function getUnitByItemId(payload) {
  try {
    var resp = await axios.post(URL + 'RequestItemOut/getUnitByItemId', payload);
    if (resp.status != 200) {
      return {
        status: "ERR",
        message: resp.statusText
      };
    }
    let data = resp.data;
    return data;
  } catch (e) {
    return {
      status: "ERR",
      message: e.message
    };
  }
}

export async function saveRequest(payload) {
  try {
    var response = await axios.post(URL + 'RequestItemOut/saveRequest', payload);
    if (response.status != 200) {
      return {
        status: "ERR",
        message: response.statusText
      };
    }
    let data = response.data;
    return data;
  } catch (e) {
    return {
      status: "ERR",
      message: e.message
    };
  }
} 

export async function getAPI(fn,payload) {
  try {
    let urx = URL + 'RequestItemOut/'+fn;
    console.log(urx);
    var response = await axios.post(urx, payload);
    if (response.status != 200) {
      return {
        status: "ERR",
        message: response.statusText
      };
    }
    let data = response.data;
    return data;
  } catch (e) {
    return {
      status: "ERR",
      message: e.message
    };
  }
} 

export async function getWarehouse(payload) {
  try {
    var response = await axios.post(
      URL + "RequestItemOut/getWarehouse",
      payload
    );
    if (response.status !== 200) {
      return {
        status: "ERR",
        message: response.statusText,
      };
    }

    let data = response.data;
    return data;
  } catch (e) {
    return {
      status: "ERR",
      message: e.message,
    };
  }
}

export async function getlevel(payload) {
  try {
    var response = await axios.post(URL + "RequestItemOut/getlevel", payload);
    if (response.status !== 200) {
      return {
        status: "ERR",
        message: response.statusText,
      };
    }

    let data = response.data;
    return data;
  } catch (e) {
    return {
      status: "ERR",
      message: e.message,
    };
  }
}

export async function getDivisionUser(payload) {
  try {
    var response = await axios.post(URL + "RequestItemOut/getDivisionUser", payload);
    if (response.status !== 200) {
      return {
        status: "ERR",
        message: response.statusText,
      };
    }

    let data = response.data;
    return data;
  } catch (e) {
    return {
      status: "ERR",
      message: e.message,
    };
  }
}

export async function approveRequest(payload) {
  try {
    var response = await axios.post(URL + 'RequestItemOut/approveRequest', payload);
    if (response.status != 200) {
      return {
        status: "ERR",
        message: response.statusText
      };
    }
    let data = response.data;
    return data;
  } catch (e) {
    return {
      status: "ERR",
      message: e.message
    };
  }
} 

export async function rejectRequest(payload) {
  try {
    var response = await axios.post(URL + 'RequestItemOut/rejectRequest', payload);
    if (response.status != 200) {
      return {
        status: "ERR",
        message: response.statusText
      };
    }
    let data = response.data;
    return data;
  } catch (e) {
    return {
      status: "ERR",
      message: e.message
    };
  }
} 
