import { URLS } from "../api/apiConfig";
import { TAxios } from "../api/TAxios";
import { types } from "./actionTypes";
import { changeAppStatus, showError } from "./commonActions";

// 회원 주문 취소/반품/교환 사유 조회 (IF-LGSP-347)
export const getMyinfoOrderCancelColumnsSearch =
  (params, callback) => (dispatch, getState) => {
    const { reasonTpCd } = params;

    const onSuccess = (response) => {
      console.log(
        "getMyinfoOrderCancelColumnsSearch onSuccess ",
        response.data
      );

      if (response.data.retCode === 0) {
        dispatch({
          type: types.GET_MY_INFO_ORDER_CANCEL_COLUMNS_SEARCH,
          payload: response.data.data,
        });

        if (callback) callback();
      } else {
        dispatch(
          showError(
            response.data.retCode,
            response.data.retMsg,
            false,
            response.data.retDetailCode
          )
        );
      }
    };

    const onFail = (error) => {
      console.error("getMyinfoOrderCancelColumnsSearch onFail ", error);
    };

    TAxios(
      dispatch,
      getState,
      "get",
      URLS.GET_MY_INFO_ORDER_CANCEL_COLUMNS_SEARCH,
      { reasonTpCd },
      {},
      onSuccess,
      onFail
    );
  };

// 회원 주문 취소/반품/교환 조회 (IF-LGSP-366)
export const getMyinfoOrderCancelSearch = (params) => (dispatch, getState) => {
  const { mbrNo, ordNo, patnrId, prdtId, prodSno, shptmChngRsnCd } = params;

  const onSuccess = (response) => {
    console.log("getMyinfoOrderCancelSearch onSuccess ", response.data);

    dispatch({
      type: types.GET_MY_INFO_ORDER_CANCEL_SEARCH,
      payload: response.data.data,
    });
  };

  const onFail = (error) => {
    console.error("getMyinfoOrderCancelSearch onFail ", error);
  };

  TAxios(
    dispatch,
    getState,
    "get",
    URLS.GET_MY_INFO_ORDER_CANCEL_SEARCH,
    { mbrNo, ordNo, patnrId, prdtId, prodSno, shptmChngRsnCd },
    {},
    onSuccess,
    onFail
  );
};

// 주문 부분 결제 취소 (IF-LGSP-351)
export const updateOrderPartialCancel = (params) => (dispatch, getState) => {
  const {
    mbrNo,
    ordNo,
    prodSno,
    reqChngRsn,
    reqChngRsnCd,
    reqMbrId,
    reqMbrNo,
  } = params;

  const onSuccess = (response) => {
    console.log("updateOrderPartialCancel onSuccess ", response.data);

    if (response.data.retCode === 0) {
      dispatch({
        type: types.UPDATE_ORDER_PARTIAL_CANCEL,
        payload: response.data.data,
      });
    } else {
      dispatch(
        showError(
          response.data.retCode,
          response.data.retMsg,
          false,
          response.data.retDetailCode
        )
      );
    }
  };

  const onFail = (error) => {
    console.error("updateOrderPartialCancel onFail ", error);
  };

  TAxios(
    dispatch,
    getState,
    "post",
    URLS.UPDATE_ORDER_PARTIAL_CANCEL,
    { mbrNo, ordNo, prodSno, reqChngRsn, reqChngRsnCd, reqMbrId, reqMbrNo },
    {},
    onSuccess,
    onFail
  );
};

// 결제전체취소 (IF-LGSP-367)
export const paymentTotalCancel =
  (params, callback) => (dispatch, getState) => {
    const { mbrNo, ordNo, reqChngRsn, reqChngRsnCd } = params;

    dispatch(
      changeAppStatus({ showLoadingPanel: { show: true, type: "wait" } })
    );

    const onSuccess = (response) => {
      console.log("paymentTotalCancel onSuccess ", response.data);

      if (response.data.retCode === 0) {
        dispatch({
          type: types.PAYMENT_TOTAL_CANCEL,
          payload: response.data.data,
        });

        if (callback) callback(response.data);
      } else {
        dispatch(
          showError(
            response.data.retCode,
            response.data.retMsg,
            false,
            response.data.retDetailCode
          )
        );
      }

      dispatch(changeAppStatus({ showLoadingPanel: { show: false } }));
    };

    const onFail = (error) => {
      console.error("paymentTotalCancel onFail ", error);
      dispatch(changeAppStatus({ showLoadingPanel: { show: false } }));
    };

    TAxios(
      dispatch,
      getState,
      "post",
      URLS.PAYMENT_TOTAL_CANCEL,
      {},
      { mbrNo, ordNo, reqChngRsn, reqChngRsnCd },
      onSuccess,
      onFail
    );
  };
