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 = [], reviewTodayData = [], reviewRecordData = [], processRecordData = [], orderListData = [], checkCustomerData = [];
export default {
    namespace: 'customer',
    state: {
        loading: false,
        reportCustomerDataRState: REFRESH_STATE.Ready,
        reportCustomerData: [],
        reviewTodayRState: REFRESH_STATE.Ready,
        reviewTodayData: [],
        reviewRecordRState: REFRESH_STATE.Ready,
        checkCustomerData: [],
        checkCustomerRState: REFRESH_STATE.Ready,
        orderListData: [],
        orderListRState: REFRESH_STATE.Ready,
        reviewRecordData: [],
        processRecordRState: REFRESH_STATE.Ready,
        processRecordData: [],
        projectDict: [],
        customerDetail: {},
        processDetail: {},
        customerReviewDetail: {},
    },
    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);
            }
        },
        * againDispatchUser({payload, callback}, {call, put}) {
            const response = yield call(customer.againDispatchUser, payload);
            if (!response.code) {
                if (callback) {
                    callback(response.data);
                }
            } else {
                ResponseError(response);
            }
        },
        * fetchReviewToday({payload}, {call, put}) {
            if (payload.page > 1) {
                yield put(createAction('queryList')({reviewTodayRState: REFRESH_STATE.FooterRefreshing}));
            } else {
                yield put(createAction('queryList')({reviewTodayRState: REFRESH_STATE.HeaderRefreshing}));
            }
            const response = yield call(customer.queryReviewToday, payload);
            if (!response.code) {
                const result = TurnPage(response, payload, reviewTodayData);
                reviewTodayData = result.data;
                yield put(createAction('queryList')({
                    reviewTodayData: reviewTodayData, reviewTodayRState: result.state,
                    total: response.totalResult, page: payload.page,
                }));
            } else {
                ResponseError(response);
                yield put(createAction('queryList')({reviewTodayRState: REFRESH_STATE.Ready}));
            }
        },
        * fetchReviewRecord({payload}, {call, put}) {
            if (payload.page > 1) {
                yield put(createAction('queryList')({reviewRecordRState: REFRESH_STATE.FooterRefreshing}));
            } else {
                yield put(createAction('queryList')({reviewRecordRState: REFRESH_STATE.HeaderRefreshing}));
            }
            const response = yield call(customer.queryReviewRecord, payload);
            if (!response.code) {
                const result = TurnPage(response, payload, reviewRecordData);
                reviewRecordData = result.data;
                yield put(createAction('queryList')({
                    reviewRecordData: reviewRecordData, reviewRecordRState: result.state,
                    totalRecord: response.totalResult, page: payload.page,
                }));
            } else {
                ResponseError(response);
                yield put(createAction('queryList')({reviewRecordRState: REFRESH_STATE.Ready}));
            }
        },
        * addReview({payload, callback}, {call, put}) {
            const response = yield call(customer.addReview, payload);
            if (!response.code) {
                if (callback) {
                    callback();
                }
            } else {
                ResponseError(response);
            }
        },
        * addInternalReview({payload, callback}, {call, put}) {
            const response = yield call(customer.addInternalReview, payload);
            if (!response.code) {
                if (callback) {
                    callback();
                }
            } else {
                ResponseError(response);
            }
        },
        * editCustomerInfo({payload, callback}, {call, put}) {
            const response = yield call(customer.editCustomerInfo, payload);
            if (!response.code) {
                if (callback) {
                    callback();
                }
            } else {
                ResponseError(response);
            }
        },
        * fetchCustomerDetail({payload, callback}, {call, put}) {
            const response = yield call(customer.queryCustomerDetail, payload);
            if (response.id) {
                yield put(createAction('queryList')({customerDetail: response}));
            } else {
                ResponseError(response);
            }
        },
        * fetchCustomerReviewDetail({payload, callback}, {call, put}) {
            const response = yield call(customer.fetchCustomerReviewDetail, payload);
            if (response.id) {
                yield put(createAction('queryList')({customerReviewDetail: response}));
            } else {
                ResponseError(response);
            }
        },
        * addCustomerProject({payload, callback}, {call, put}) {
            const response = yield call(customer.addCustomerProject, payload);
            if (!response.code) {
                callback()
            } else {
                ResponseError(response);
            }
        },
        * checkReview({payload, callback}, {call, put}) {
            const response = yield call(customer.checkReview, payload);
            if (!response.code) {
                callback()
            } else {
                ResponseError(response);
            }
        },
        * fetchCheckCustomer({payload}, {call, put}) {
            if (payload.page > 1) {
                yield put(createAction('queryList')({checkCustomerRState: REFRESH_STATE.FooterRefreshing}));
            } else {
                yield put(createAction('queryList')({checkCustomerRState: REFRESH_STATE.HeaderRefreshing}));
            }
            const response = yield call(customer.queryCheckCustomer, payload);
            if (!response.code) {
                const result = TurnPage(response, payload, checkCustomerData);
                checkCustomerData = result.data;
                yield put(createAction('queryList')({
                    checkCustomerData: checkCustomerData, checkCustomerRState: result.state,
                    total: response.totalResult, page: payload.page,
                }));
            } else {
                ResponseError(response);
                yield put(createAction('queryList')({checkCustomerRState: REFRESH_STATE.Ready}));
            }
        },
        * getProcess({payload, callback}, {call, put}) {
            const response = yield call(customer.getProcess, payload);
            if (!response.code) {
                callback(response.data)
            } else {
                ResponseError(response);
            }
        },
        * addOrder({payload, callback}, {call, put}) {
            const response = yield call(customer.addOrder, payload);
            if (!response.code) {
                callback()
            } else {
                ResponseError(response);
            }
        },
        * fetchOrderList({payload}, {call, put}) {
            if (payload.page > 1) {
                yield put(createAction('queryList')({orderListRState: REFRESH_STATE.FooterRefreshing}));
            } else {
                yield put(createAction('queryList')({orderListRState: REFRESH_STATE.HeaderRefreshing}));
            }
            const response = yield call(customer.queryOrderList, payload);
            if (!response.code) {
                const result = TurnPage(response, payload, orderListData);
                orderListData = result.data;
                yield put(createAction('queryList')({
                    orderListData: orderListData, orderListRState: result.state,
                    total: response.totalResult, page: payload.page,
                }));
            } else {
                ResponseError(response);
                yield put(createAction('queryList')({orderListRState: REFRESH_STATE.Ready}));
            }
        },
        * fetchProcessDetail({payload, callback}, {call, put}) {
            const response = yield call(customer.queryProcessDetail, payload);
            if (response.id) {
                yield put(createAction('queryList')({processDetail: response}));
            } else {
                ResponseError(response);
            }
        },
        * fetchProcessRecord({payload}, {call, put}) {
            if (payload.page > 1) {
                yield put(createAction('queryList')({ProcessRecordRState: REFRESH_STATE.FooterRefreshing}));
            } else {
                yield put(createAction('queryList')({ProcessRecordRState: REFRESH_STATE.HeaderRefreshing}));
            }
            const response = yield call(customer.queryProcessRecord, payload);
            if (!response.code) {
                const result = TurnPage(response, payload, processRecordData);
                processRecordData = result.data;
                yield put(createAction('queryList')({
                    processRecordData: processRecordData, processRecordRState: result.state,
                    totalRecord: response.totalResult, page: payload.page,
                }));
            } else {
                ResponseError(response);
                yield put(createAction('queryList')({processRecordRState: REFRESH_STATE.Ready}));
            }
        },
        * fetchProcessDict({payload, callback}, {call, put}) {
            const response = yield call(customer.queryProcessDict, payload);
            if (!response.code) {
                callback(response.data)
            } else {
                ResponseError(response);
            }
        },
        * dispatchService({payload, callback}, {call, put}) {
            const response = yield call(customer.dispatchService, payload);
            if (!response.code) {
                callback()
            } else {
                ResponseError(response);
            }
        },
    },
    subscriptions: {
        setup({dispatch}) {
            //dispatch({ type: 'loadStorage' })
        },
    },
};
