/** @format */

import * as React from 'react';
import Axios from '../utils/xhr';
import {
    GET_BILL_DETAIL_LIST,
    GET_BILL_LIST,
    GET_EXPENSES_LIST,
    GET_EXPENSES_TRENDS,
    GET_EXPORT_BILL,
    GET_MERCHANT_DATA_API_PRICE_LIST,
    GET_MERCHANT_PRICE_LIST,
    GET_RECHARGE_LIST,
    POST_MERCHANT_CHARGE,
    PUT_ADD_BILL_REMARK,
    PUT_BILL_RECHARGE_COUNT,
    PUT_MERCHANT_DATA_API_PRICE,
    PUT_MERCHANT_TYPE,
} from '../const/path';


// 消费列表
export const useExpensesList = (params: any) => {
    const [state, setState] = React.useState<any>({
        loading: false,
        error: null,
        deepData: {
            merchant_id: '',
            page_number: 1,
            data_api_id: '',
            start_time: '',
            end_time: '',
        },
        dataSource: [],
        total: 0,
    });
    const requestData = (params: any) => {
        const combineParams = {...state.deepData, ...params};
        setState((preState: any) => {
            return {...preState, loading: true, error: null};
        });
        Axios({
            url: GET_EXPENSES_LIST,
            params: combineParams,
        }).then(({code, data, msg}) => {
            if (code === 0) {
                const baseSize = data.page_size * (data.page_number - 1);
                if (!data.list) {
                    data.list = [];
                }
                setState((preState: any) => {
                    return {
                        ...preState,
                        deepData: combineParams,
                        loading: false,
                        total: data.total,
                        dataSource: data.list.map((element: any, index: number) => {
                            return {
                                ...element,
                                index: baseSize + index + 1,
                                id: element.consume_id,
                            };
                        }),
                    };
                });
            } else {
                setState((preState: any) => ({...preState, loading: false, error: msg, deepData: combineParams}));
            }
        });
    };
    React.useEffect(() => {
        requestData(params);
    }, []);
    return [state, requestData] as const;
};


// 消费趋势
export const useExpensesTrends = (params: any) => {
    const [state, setState] = React.useState<any>({
        loading: false,
        error: null,
        deepData: {
            merchant_id: '',
            data_api_id: '',
            start_time: '',
            end_time: '',
        },
        total: 0,
    });
    const requestData = (params: any) => {
        const combineParams = {...state.deepData, ...params};
        setState((preState: any) => {
            return {...preState, loading: true, error: null};
        });
        Axios({
            url: GET_EXPENSES_TRENDS,
            params: combineParams,
        }).then(({code, data, msg}) => {
            if (code === 0) {
                const baseSize = data.page_size * (data.page_number - 1);
                if (!data.list) {
                    data.list = [];
                }
                setState((preState: any) => {
                    return {
                        ...preState,
                        deepData: combineParams,
                        loading: false,
                        xData:data.list.map((item:any)=>{
                            return item.day
                        }),
                        yData:data.list.map((item:any)=>{
                            return item.amount
                        })
                    };
                });
            } else {
                setState((preState: any) => ({...preState, loading: false, error: msg, deepData: combineParams}));
            }
        });
    };
    return [state, requestData] as const;
};

export const useRechargeList = (params: any) => {
    const [state, setState] = React.useState<any>({
        loading: false,
        error: null,
        deepData: {
            merchant_id: '',
            page_number: 1,
            start_time: '',
            end_time: '',
        },
        dataSource: [],
        total: 0,
    });
    const requestData = (params: any) => {
        const combineParams = {...state.deepData, ...params};
        setState((preState: any) => {
            return {...preState, loading: true, error: null};
        });
        Axios({
            url: GET_RECHARGE_LIST,
            params: combineParams,
        }).then(({code, data, msg}) => {
            if (code === 0) {
                const baseSize = data.page_size * (data.page_number - 1);
                if (!data.list) {
                    data.list = [];
                }
                setState((preState: any) => {
                    return {
                        ...preState,
                        deepData: combineParams,
                        loading: false,
                        total: data.total,
                        dataSource: data.list.map((element: any, index: number) => {
                            return {
                                ...element,
                                index: baseSize + index + 1,
                                id: element.bill_id,
                            };
                        }),
                    };
                });
            } else {
                setState((preState: any) => ({...preState, loading: false, error: msg, deepData: combineParams}));
            }
        });
    };
    React.useEffect(() => {
        requestData(params);
    }, []);
    return [state, requestData] as const;
};

export const useBillList = (params: any) => {
    const [state, setState] = React.useState<any>({
        loading: false,
        error: null,
        deepData: {
            merchant_id: '',
            page_number: 1,
            start_time: '',
            end_time: '',
        },
        dataSource: [],
        total: 0,
    });
    const requestData = (params: any) => {
        const combineParams = {...state.deepData, ...params};
        setState((preState: any) => {
            return {...preState, loading: true, error: null};
        });
        Axios({
            url: GET_BILL_LIST,
            params: combineParams,
        }).then(({code, data, msg}) => {
            if (code === 0) {
                const baseSize = data.page_size * (data.page_number - 1);
                if (!data.list) {
                    data.list = [];
                }
                setState((preState: any) => {
                    return {
                        ...preState,
                        deepData: combineParams,
                        loading: false,
                        total: data.total,
                        dataSource: data.list.map((element: any, index: number) => {
                            return {
                                ...element,
                                index: baseSize + index + 1,
                                id: element.bill_id,
                            };
                        }),
                    };
                });
            } else {
                setState((preState: any) => ({...preState, loading: false, error: msg, deepData: combineParams}));
            }
        });
    };
    React.useEffect(() => {
        requestData(params);
    }, []);
    return [state, requestData] as const;
};

export const useBillDetail = (params: any) => {
    const [state, setState] = React.useState<any>({
        loading: false,
        error: null,
        remark: [],
        detailRemark: [],
        balance: 0,
        amount: 0,
        deepData: {
            merchant_id: '',
            month: '',
        },
        dataSource: [],
        total: 0,
    });
    const requestData = (params: any) => {
        const combineParams = {...state.deepData, ...params};
        setState((preState: any) => {
            return {...preState, loading: true, error: null};
        });
        Axios({
            url: GET_BILL_DETAIL_LIST,
            params: combineParams,
        }).then(({code, data, msg}) => {
            if (code === 0) {
                if (!data.list) {
                    data.list = [];
                }
                setState((preState: any) => {
                    return {
                        ...preState,
                        deepData: combineParams,
                        amount: data.amount || 0,
                        balance: data.balance || 0,
                        loading: false,
                        remark: data.remark_list || [],
                        detailRemark: data.bill_remark_list || [],
                        dataSource: data.list.map((element: any, index: number) => {
                            return {
                                ...element,
                                id: element.bill_detail_id,
                            };
                        }),
                    };
                });
            } else {
                setState((preState: any) => ({...preState, loading: false, error: msg, deepData: combineParams}));
            }
        });
    };
    React.useEffect(() => {
        requestData(params);
    }, [params?.bill_id]);
    return [state, requestData] as const;
};

export const useExportBill = (params: any) => {
    const [state, setState] = React.useState<any>({
        loading: false,
        error: null,
        remark: [],
        detailRemark: [],
        balance: 0,
        amount: 0,
        deepData: {
            merchant_id: '',
            month: '',
        },
        dataSource: [],
        total: 0,
    });
    const requestData = (params: any) => {
        const combineParams = {...state.deepData, ...params};
        setState((preState: any) => {
            return {...preState, loading: true, error: null};
        });
        Axios({
            url: GET_EXPORT_BILL,
            params: combineParams,
        }).then(({code, data, msg}) => {
            if (code === 0) {
                if (!data.list) {
                    data.list = [];
                }
                window.location.href = data.url
                setState((preState: any) => {
                    return {
                        ...preState,
                        deepData: combineParams,
                        url:data.url,
                        loading: false,
                        dataSource: data.list.map((element: any, index: number) => {
                            return {
                                ...element,
                            };
                        }),
                    };
                });
            } else {
                setState((preState: any) => ({...preState, loading: false, error: msg, deepData: combineParams}));
            }
        });
    };
    React.useEffect(() => {
        requestData(params);
    }, [params?.bill_id]);
    return [state, requestData] as const;
};

export const useUpdateBillDetailChargeCount = () => {
    const [state, setState] = React.useState({
        loading: false,
        error: null,
    });
    const requestData = (params: any) => {
        setState(preState => ({...preState, loading: true, error: null}));
        return Axios({
            url: PUT_BILL_RECHARGE_COUNT,
            method: 'PUT',
            data: {
                bill_detail_id: params.bill_detail_id,
                charge_count: params.charge_count,
            },
        })
            .then(({code, msg}) => {
                if (code === 0) {
                    setState({
                        loading: false,
                        error: null,
                    });
                } else {
                    setState({
                        loading: false,
                        error: msg,
                    });
                    return msg;
                }
            })
            .catch(error => {
                setState({
                    loading: false,
                    error: error.message,
                });
                return error.message;
            });
    };
    return [state, requestData] as const;
};

export const useAddBillDetailRemark = () => {
    const [state, setState] = React.useState({
        loading: false,
        error: null,
    });
    const requestData = (params: any) => {
        setState(preState => ({...preState, loading: true, error: null}));
        return Axios({
            url: PUT_ADD_BILL_REMARK,
            method: 'PUT',
            data: {
                bill_id: params.bill_id,
                remark: params.remark,
            },
        })
            .then(({code, msg}) => {
                if (code === 0) {
                    setState({
                        loading: false,
                        error: null,
                    });
                } else {
                    setState({
                        loading: false,
                        error: msg,
                    });
                    return msg;
                }
            })
            .catch(error => {
                setState({
                    loading: false,
                    error: error.message,
                });
                return error.message;
            });
    };
    return [state, requestData] as const;
};

export const useMerchantPriceList = (params?: any) => {
    const [state, setState] = React.useState<any>({
        loading: false,
        error: null,
        deepData: {
            merchant_id: '',
            page_number: 1,
        },
        dataSource: [],
        total: 0,
    });
    const requestData = (params: any) => {
        const combineParams = {...state.deepData, ...params};
        setState((preState: any) => {
            return {...preState, loading: true, error: null};
        });
        Axios({
            url: GET_MERCHANT_PRICE_LIST,
            params: combineParams,
        }).then(({code, data, msg}) => {
            if (code === 0) {
                const baseSize = data.page_size * (data.page_number - 1);
                if (!data.list) {
                    data.list = [];
                }
                localStorage.setItem('merchant_recharge_accounting',JSON.stringify(combineParams))
                setState((preState: any) => {
                    return {
                        ...preState,
                        deepData: combineParams,
                        loading: false,
                        total: data.total,
                        dataSource: data.list.map((element: any, index: number) => {
                            return {
                                ...element,
                                index: baseSize + index + 1,
                                id: element.merchant_id,
                            };
                        }),
                    };
                });
            } else {
                setState((preState: any) => ({...preState, loading: false, error: msg, deepData: combineParams}));
            }
        });
    };
    React.useEffect(() => {
        requestData(params);
    }, []);
    return [state, requestData] as const;
};

export const useUpdateMerchantType = () => {
    const [state, setState] = React.useState({
        loading: false,
        error: null,
    });
    const requestData = (params: any) => {
        setState(preState => ({...preState, loading: true, error: null}));
        return Axios({
            url: PUT_MERCHANT_TYPE,
            method: 'PUT',
            data: {
                merchant_id: params.merchant_id,
                merchant_type: params.merchant_type,
                arrearage:params.arrearage,
                is_notify:params.is_notify
            },
        })
            .then(({code, msg}) => {
                if (code === 0) {
                    setState({
                        loading: false,
                        error: null,
                    });
                } else {
                    setState({
                        loading: false,
                        error: msg,
                    });
                    return msg;
                }
            })
            .catch(error => {
                setState({
                    loading: false,
                    error: error.message,
                });
                return error.message;
            });
    };
    return [state, requestData] as const;
};

export const useMerchantCharge = () => {
    const [state, setState] = React.useState({
        loading: false,
        error: null,
    });
    const requestData = (params: any) => {
        setState(preState => ({...preState, loading: true, error: null}));
        return Axios({
            url: POST_MERCHANT_CHARGE,
            method: 'POST',
            data: {
                merchant_id: params.merchant_id,
                amount: params.amount,
                remark: params.remark,
            },
        })
            .then(({code, msg}) => {
                if (code === 0) {
                    setState({
                        loading: false,
                        error: null,
                    });
                } else {
                    setState({
                        loading: false,
                        error: msg,
                    });
                    return msg;
                }
            })
            .catch(error => {
                setState({
                    loading: false,
                    error: error.message,
                });
                return error.message;
            });
    };
    return [state, requestData] as const;
};

export const useMerchantDataApiPriceList = (params: any) => {
    const [state, setState] = React.useState<any>({
        loading: false,
        error: null,
        deepData: {
            merchant_id: '',
        },
        dataSource: [],
        total: 0,
    });
    const requestData = (params: any) => {
        const combineParams = {...state.deepData, ...params};
        setState((preState: any) => {
            return {...preState, loading: true, error: null};
        });
        Axios({
            url: GET_MERCHANT_DATA_API_PRICE_LIST,
            params: combineParams,
        }).then(({code, data, msg}) => {
            if (code === 0) {
                const baseSize = data.page_size * (data.page_number - 1);
                if (!data.list) {
                    data.list = [];
                }
                setState((preState: any) => {
                    return {
                        ...preState,
                        deepData: combineParams,
                        loading: false,
                        total: data.total,
                        dataSource: data.list.map((element: any, index: number) => {
                            return {
                                ...element,
                                index: baseSize + index + 1,
                                id: element.merchant_data_api_id,
                            };
                        }),
                    };
                });
            } else {
                setState((preState: any) => ({...preState, loading: false, error: msg, deepData: combineParams}));
            }
        });
    };
    React.useEffect(() => {
        requestData(params);
    }, [params?.merchant_id]);
    return [state, requestData] as const;
};

export const useUpdateMerchantUnitPrice = () => {
    const [state, setState] = React.useState({
        loading: false,
        error: null,
    });
    const requestData = (params: any) => {
        setState(preState => ({...preState, loading: true, error: null}));
        return Axios({
            url: PUT_MERCHANT_DATA_API_PRICE,
            method: 'PUT',
            data: {
                unit_price: params.unit_price,
                merchant_data_api_id: params.merchant_data_api_id,
            },
        })
            .then(({code, msg}) => {
                if (code === 0) {
                    setState({
                        loading: false,
                        error: null,
                    });
                } else {
                    setState({
                        loading: false,
                        error: msg,
                    });
                    return msg;
                }
            })
            .catch(error => {
                setState({
                    loading: false,
                    error: error.message,
                });
                return error.message;
            });
    };
    return [state, requestData] as const;
};
