import {createAction} from '../utils';
import * as customer from '../services/customer';
import ResponseError from '../components/ResponseError';
import TurnPage from '../components/TurnPage';
import {REFRESH_STATE} from 'react-native-refresh-flatlist';

let reportCustomerData = [];
export default {
    namespace: 'customer',
    state: {
        loading: false,
        reportCustomerDataRState: REFRESH_STATE.Ready,
        reportCustomerData: [],
        projectDict: [],
    },
    reducers: {
        updateState(state, {payload}) {
            return {...state, ...payload};
        },
        queryList(state, {payload}) {
            return {...state, ...payload};
        },
    },
    effects: {
        * fetchReportCustomerData({payload}, {call, put}) {
            if (payload.page > 1) {
                yield put(createAction('queryList')({reportCustomerDataRState: REFRESH_STATE.FooterRefreshing}));
            } else {
                yield put(createAction('queryList')({reportCustomerDataRState: REFRESH_STATE.HeaderRefreshing}));
            }
            const response = yield call(customer.queryReportCustomerData, payload);
            if (!response.code) {
                const result = TurnPage(response, payload, reportCustomerData);
                reportCustomerData = result.data;
                yield put(createAction('queryList')({
                    reportCustomerData: reportCustomerData, reportCustomerDataRState: result.state,
                    total: response.totalResult, page: payload.page,
                }));
            } else {
                ResponseError(response);
                yield put(createAction('queryList')({reportCustomerDataRState: REFRESH_STATE.Ready}));
            }
        },
        * fetchReportDict({payload, callback}, {call, put}) {
            const response = yield call(customer.queryReportDict);
            if (!response.code) {
                if (callback) {
                    callback(response.data);
                }
                yield put(createAction('queryList')({projectDict: response.data.project}));
            } else {
                ResponseError(response);
            }
        },
        * saveReportCustomer({payload, callback}, {call, put}) {
            const response = yield call(customer.saveReportCustomer, payload);
            if (!response.code) {
                if (callback) {
                    callback();
                }
            } else {
                ResponseError(response);
            }
        },
        * signRepeat({payload, callback}, {call, put}) {
            const response = yield call(customer.signRepeat, payload);
            if (!response.code) {
                if (callback) {
                    callback();
                }
            } else {
                ResponseError(response);
            }
        },
        * getUser({payload, callback}, {call, put}) {
            const response = yield call(customer.getUser, payload);
            if (!response.code) {
                if (callback) {
                    callback(response.data);
                }
            } else {
                ResponseError(response);
            }
        },
        * dispatchUser({payload, callback}, {call, put}) {
            const response = yield call(customer.dispatchUser, payload);
            if (!response.code) {
                if (callback) {
                    callback(response.data);
                }
            } else {
                ResponseError(response);
            }
        },
    },
    subscriptions: {
        setup({dispatch}) {
            //dispatch({ type: 'loadStorage' })
        },
    },
};
