import { URLS } from "../api/apiConfig";
import { TAxios } from "../api/TAxios";
import { types } from "./actionTypes";

// IF-LGSP-339 : 회원 다운로드 쿠폰 정보 조회
export const getProductCouponInfo = (props) => (dispatch, getState) => {
  const { mbrNo, patnrId, prdtId } = props;

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

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

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

  TAxios(
    dispatch,
    getState,
    "get",
    URLS.GET_PRODUCT_COUPON_INFO,
    { mbrNo, patnrId, prdtId },
    {},
    onSuccess,
    onFail
  );
};
// IF-LGSP-364 : 상품 쿠폰 전체 다운로드
export const getProductCouponTotDownload = (props) => (dispatch, getState) => {
  // 쿠폰 아이디 여러개 일 경우 쉼표로 구분  ex)80,81,82
  const { mbrNo, cpnSnoAll } = props;

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

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

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

  TAxios(
    dispatch,
    getState,
    "post",
    URLS.GET_PRODUCT_COUPON_TOTDOWNLOAD,
    {},
    { mbrNo, cpnSnoAll },
    onSuccess,
    onFail
  );
};
// IF-LGSP-318 : 상품 쿠폰 다운로드
export const getProductCouponDownload = (props) => (dispatch, getState) => {
  const { mbrNo, cpnSno } = props;

  console.log("#mbrNo , cpnSno", mbrNo, cpnSno);
  const onSuccess = (response) => {
    console.log("getProductCouponDownload onSuccess ", response.data);

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

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

  TAxios(
    dispatch,
    getState,
    "post",
    URLS.GET_PRODUCT_COUPON_DOWNLOAD,
    {},
    { mbrNo, cpnSno },
    onSuccess,
    onFail
  );
};
// IF-LGSP-317 : 상품별 다운로드 가능 쿠폰정보 조회
export const getProductCouponSearch = (props) => (dispatch, getState) => {
  const { mbrNo, patnrId, prdtId, catCd } = props;

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

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

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

  TAxios(
    dispatch,
    getState,
    "get",
    URLS.GET_PRODUCT_COUPON_SEARCH,
    { mbrNo, patnrId, prdtId, catCd },
    {},
    onSuccess,
    onFail
  );
};

export const clearCouponInfo = () => ({
  type: types.CLEAR_COUPON_INFO,
});

export const clearGetProductCouponDownload = () => ({
  type: types.CLEAR_GET_PRODUCT_COUPON_DOWNLOAD,
});
