import {cautionCreditDemandtUrl, getNanoCreditDemandUrl} from "../IlinkConstants";
import axios from "axios";
import I18n from 'react-native-i18n';
import {store} from "../../redux/store";
import {
    fetchCautionCreditDemandAgentError,
    fetchCautionCreditDemandAgentPending,
    fetchCautionCreditDemandAgentReset,
    fetchCautionCreditDemandAgentSuccess,
    fetchGetNanoCreditDemandDetailError,
    fetchGetNanoCreditDemandDetailPending,
    fetchGetNanoCreditDemandDetailReset,
    fetchGetNanoCreditDemandDetailSuccess
} from "../../redux/actions/NanoCreditAction";

export const cautionCreditDemandAgentAction = (data) => {

    const auth = store.getState().authKeyReducer;
    const authKey = auth !== null ? `${auth.authKey.token_type} ${auth.authKey.access_token}` : '';

    return dispatch => {
        dispatch(fetchCautionCreditDemandAgentPending());

        axios({
            url: cautionCreditDemandtUrl,
            method: 'POST',
            data,
            headers: {
                'Authorization': authKey,
                'X-Localization': I18n.currentLocale()
            },
        })
            .then(response => {
                console.log(response);
                dispatch(fetchCautionCreditDemandAgentSuccess(response));
            })
            .catch(error => {
                if (error.response)
                    dispatch(fetchCautionCreditDemandAgentError(error.response));
                else if (error.request)
                    dispatch(fetchCautionCreditDemandAgentError(error.request))
                else
                    dispatch(fetchCautionCreditDemandAgentError(error.message))
            });
    }
}

export const cautionCreditDemandAgentReset = () => {
    return dispatch => {
        dispatch(fetchCautionCreditDemandAgentReset());
    }
}

export const getNanoCreditDemandDetailAction = (idDemand) => {

    const auth = store.getState().authKeyReducer;
    const authKey = auth !== null ? `${auth.authKey.token_type} ${auth.authKey.access_token}` : '';

    return dispatch => {
        dispatch(fetchGetNanoCreditDemandDetailPending());

        axios({
            url: `${getNanoCreditDemandUrl}/${idDemand}`,
            method: 'GET',
            headers: {
                'Authorization': authKey,
                'X-Localization': I18n.currentLocale()
            },
        })
            .then(response => {
                console.log(response);
                dispatch(fetchGetNanoCreditDemandDetailSuccess(response));
            })
            .catch(error => {
                if (error.response)
                    dispatch(fetchGetNanoCreditDemandDetailError(error.response));
                else if (error.request)
                    dispatch(fetchGetNanoCreditDemandDetailError(error.request));
                else
                    dispatch(fetchGetNanoCreditDemandDetailError(error.message));
            });
    }
}

export const getNanoCreditDemandDetailReset = () => {
    return dispatch => {
        dispatch(fetchGetNanoCreditDemandDetailReset());
    }
}