import React, { useState, useEffect } from 'react'
import LogAuth from './View'
import {
    listSheetReconciliation,
    updateSheetReconciliation,
} from 'app/apis/Functions/reconciliation'
import { useHistory, useLocation } 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 { useTranslation } from 'react-i18next'
import { dropdownMerchant, dropdownProvider } from 'app/apis/Functions/dropdown'

const ToolNotificate = (props) => {
    const location = useLocation()
    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()
    const [endDate, setEndDate] = useState()
    const { t } = useTranslation()
    const [data, setData] = useState([])

    const [status, setStatus] = useState()
    const [listProvide, setListProvide] = useState([])
    const [provide, setProvide] = useState()

    useEffect(() => {
        getDataDropdown()
    }, [])

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

    const getData = async (init) => {
        let res
        if (init) {
            setPageIndex(0)
            res = await listSheetReconciliation({
                order_code: searchDebount,
                page_no: 0,
                page_size: pageSize,
                date_from: startDate ? convertDate(startDate) : null,
                date_to: endDate ? convertDate(endDate) : null,
                status: status ? status : 0,
                idPaymentPartner: location.state?.provider_code,
            })
        } else {
            res = await listSheetReconciliation({
                order_code: searchDebount,
                page_no: pageIndex + 1,
                page_size: pageSize,
                from_date: startDate ? convertDate(startDate) : null,
                to_date: endDate ? convertDate(endDate) : null,
                status: status ? status : 0,
                idPaymentPartner: location.state?.provider_code,
            })
        }

        if (res.data.code == 200 && res.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',
            })
        }
    }

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

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

    const getDataDropdown = async () => {
        const res = await dropdownProvider({})
        if (res.data.code == 200 && res.data.data) {
            const newList = res.data.data.map((e) => {
                return { ...e, id: e.code }
            })
            setListProvide(newList)
        }
    }
    const removeItem = async (id) => {
        props.showLoading()
        const res = await updateSheetReconciliation({
            id: id,
            status: 3,
        })
        getData(false)
        props.hideLoading()
        if (res.data.code == 200) {
            if (res.data.code == 200) {
                toast.success(
                    'Cập nhật trạng thái phiếu đối soát thành công!',
                    {
                        theme: 'colored',
                    }
                )
            }
        } else {
            toast.error(t(res.data.error), {
                theme: 'colored',
            })
        }
    }

    return (
        <LogAuth
            data={data}
            setTxtSearch={setTxtSearch}
            pageIndex={pageIndex}
            setPageIndex={setPageIndex}
            totalRecords={totalRecords}
            setApplications={setApplications}
            applications={applications}
            startDate={startDate}
            changeDateRange={changeDateRange}
            listProvide={listProvide}
            provide={provide}
            setProvide={setProvide}
            status={status}
            setStatus={setStatus}
            removeItem={removeItem}
        />
    )
}

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