import React, { useState, useEffect } from 'react'
import LogAuth from './View'
import { listFee } from 'app/apis/Functions/transaction'
import { useHistory } from 'react-router-dom'
import { toPriceVnd, convertDate } from 'app/config/Function'
import { connect } from 'react-redux'
import { showLoading, hideLoading } from 'app/redux/actions/loadingAction'
import { toast } from 'react-toastify'
import useDebounce from 'app/hooks/useDebounce'
import { dropdownStore, dropdownMerchant } from 'app/apis/Functions/dropdown'
import { useTranslation } from 'react-i18next'
const ToolNotificate = (props) => {
    const [txtSearch, setTxtSearch] = useState('')
    const [applications, setApplications] = useState('')
    const [pageIndex, setPageIndex] = useState(0)
    const [pageSize] = useState(10)
    const [totalRecords, setTotalRecord] = useState(0)
    const history = useHistory()
    const searchDebount = useDebounce(txtSearch, 1000)
    const [startDate, setStartDate] = useState(new Date())
    const [endDate, setEndDate] = useState(new Date())
    const [report, setReport] = useState()
    const { t } = useTranslation()
    const [listModule, setListModule] = useState([])
    const [listMerchant, setListMerchant] = useState([])
    const [merchant, setMerchant] = useState()
    const [status, setStatus] = useState()

    const [module, setModule] = useState(null)

    const [data, setData] = useState([])
    useEffect(() => {
        getDropDownMerchant()
    }, [])

    useEffect(() => {
        setTimeout(() => {
            setModule()
        }, 500)

        if (merchant) {
            getDropDownStore(merchant)
        }
    }, [merchant])

    const changeDateRange = (start, end) => {
        setStartDate(start)
        setEndDate(end)
    }

    const getDropDownStore = async (id) => {
        try {
            const res = await dropdownStore(id)
            console.log(res)
            setListModule(res.data.data)
        } catch (err) {
            //toast.error(t(err))
        }
    }

    const getDropDownMerchant = async () => {
        try {
            const res = await dropdownMerchant()
            console.log(res)
            const newList = res.data.data.map((e) => {
                return { ...e, name: e.merchant_name }
            })
            setListMerchant(newList)
        } catch (err) {
            //toast.error(t(err))
        }
    }

    const getData = async (init) => {
        try {
            props.showLoading()

            let res

            if (init) {
                setPageIndex(0)
                res = await listFee({
                    text_search: searchDebount,
                    page_no: 0,
                    page_size: pageSize,
                    from_date: startDate ? convertDate(startDate) : null,
                    to_date: endDate ? convertDate(endDate) : null,
                    store_id: module,
                    merchant_id: merchant,
                    status: status,
                })
            } else {
                res = await listFee({
                    text_search: searchDebount,
                    page_no: pageIndex + 1,
                    page_size: pageSize,
                    from_date: startDate ? convertDate(startDate) : null,
                    to_date: endDate ? convertDate(endDate) : null,
                    store_id: module,
                    merchant_id: merchant,
                    status: status,
                })
            }

            props.hideLoading()

            if (res.data.code == 200 && res.data.data) {
                setReport(res.data.data.data)
                const newList = res.data.data.data.map((e, i) => {
                    return { ...e, index: i + 1 + pageIndex * pageSize }
                })
                setData(newList)
                setTotalRecord(res.data.data.total_elements)
            } else if (res.data.code == 401) {
                setTimeout(() => {
                    history.push('/')
                }, 100)
            } else {
                toast.error(t(res.data.error), {
                    theme: 'colored',
                })
            }
        } catch (error) {
            toast.error('Hệ thống đang bận, vui lòng thử lại sau', {
                theme: 'colored',
            })
        }
    }

    useEffect(() => {
        getData(false)
    }, [pageIndex])

    useEffect(() => {
        getData(true)
    }, [searchDebount, startDate, module, merchant, status])

    return (
        <LogAuth
            data={data}
            setTxtSearch={setTxtSearch}
            pageIndex={pageIndex}
            setPageIndex={setPageIndex}
            totalRecords={totalRecords}
            setApplications={setApplications}
            applications={applications}
            startDate={startDate}
            changeDateRange={changeDateRange}
            listModule={listModule}
            module={module}
            setModule={setModule}
            report={report}
            listMerchant={listMerchant}
            merchant={merchant}
            setMerchant={setMerchant}
            status={status}
            setStatus={setStatus}
        />
    )
}

const mapStateToProps = (state) => {
    return {}
}
export default connect(mapStateToProps, { showLoading, hideLoading })(
    ToolNotificate
)
