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

const ToolNotificate = (props) => {
    const { customSelect, handleClose, idPaymentPartner } = props
    const [listSelected, setListSelected] = useState([])
    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 [type, setType] = useState()
    const [listType, setListType] = useState([])

    const changeDateRange = (start, end) => {
        setStartDate(start)
        setEndDate(end)
    }
    console.log('idPaymentPartner index', idPaymentPartner)

    const getData = async (init) => {
        if (props?.idPaymentPartner) {
            console.log('props?.idPaymentPartner', props?.idPaymentPartner)

            props.showLoading()

            const res = await listTransReconciliation({
                date_from: startDate ? convertDate(startDate) : null,
                date_to: endDate ? convertDate(endDate) : null,
                statusReconciliation: 1,
                payment_gate_code: props?.idPaymentPartner,
                order_code: searchDebount,
                type_payment: type || type,
            })

            console.log('res.data', res.data)
            props.hideLoading()
            if (res.data.code == 200 && res.data.data) {
                console.log('idPaymentPartner', 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(() => {
        console.log('props?.open', props?.open)
        console.log('props?.open', props?.idPaymentPartner)
        if (props?.open) {
            console.log('run')
            getData(true)
        }
    }, [searchDebount, startDate, type, props])

    const onAdd = () => {
        console.log('listSelected', listSelected)
    }

    return (
        <LogAuth
            data={data}
            setTxtSearch={setTxtSearch}
            pageIndex={pageIndex}
            setPageIndex={setPageIndex}
            totalRecords={totalRecords}
            setApplications={setApplications}
            applications={applications}
            startDate={startDate}
            changeDateRange={changeDateRange}
            type={type}
            setType={setType}
            listType={listType}
            customSelect={customSelect}
            handleClose={handleClose}
            listSelected={listSelected}
            setListSelected={setListSelected}
            onAdd={onAdd}
        />
    )
}

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