import React, { useState, useEffect } from 'react'
import LogAuth from './View'
import { listTransSelect } 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,
    dropdownStoreNew,
    dropdownPaymentType,
} from 'app/apis/Functions/dropdown'
import useAuth from 'app/hooks/useAuth'

const ToolNotificate = (props) => {
    const { customSelect, handleClose, idPaymentPartner, onAddTrans, id } =
        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 [store, setStore] = useState()
    const [listStore, setListStore] = useState([])
    const [listMedthodPay, setListMethodPay] = useState([])

    const { user } = useAuth()

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

    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 listTransSelect({
                text: searchDebount,
                date_from: startDate ? convertDate(startDate) : null,
                date_to: endDate ? convertDate(endDate) : null,
                statusReconciliation: 0,
                payment_gate_code: props?.idPaymentPartner,
                order_code: searchDebount,
                type_payment: type || type,
                reconciliation_id: id,
            })

            props.hideLoading()
            if (res.data.code == 200 && res.data.data) {
                const newList = res.data.data?.map((e, i) => {
                    return { ...e, index: i + 1 + pageIndex * pageSize }
                })
                setData(newList)
            } else {
                toast.error(t(res.data.error), {
                    theme: 'colored',
                })
            }
        }
    }

    useEffect(() => {
        if (props?.open) {
            console.log('run')
            getData(true)
        }
    }, [searchDebount, startDate, type, props])

    const onAdd = () => {
        if (listSelected.length > 0) {
            onAddTrans(listSelected)
        } else {
            toast.warning('Mời bạn chọn giao dịch', {
                theme: 'colored',
            })
        }
    }

    const getDropdownStore = async () => {
        const res = await dropdownStoreNew()

        if (res.data.code == 200 && res.data.data) {
            const newList = res.data.data.map((e) => {
                return { ...e, name: e.store_name }
            })
            setListStore(newList)
        }

        const res2 = await dropdownPaymentType()

        if (res2.data.code == 200 && res2.data.data) {
            const newList = res2.data.data.map((e) => {
                return { ...e, id: e.code }
            })
            setListMethodPay(newList)
        }
    }

    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}
            store={store}
            setStore={setStore}
            listStore={listStore}
            listMedthodPay={listMedthodPay}
        />
    )
}

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