import React, { useState, useEffect } from 'react'
import ToolUserView from './View'
import {
    getListtPaymentCost,
    acceptPaymentCost,
} from 'app/apis/Functions/landingpage'
import { useHistory } from 'react-router-dom'
import KEY from '../../../assets/Key'
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'
const ToolNotificate = (props) => {
    const [txtSearch, setTxtSearch] = useState('')
    const searchDebount = useDebounce(txtSearch, 1000)
    const { t } = useTranslation()
    const [activeSelected, setActiveSeleted] = useState(null)
    const [changeActive, setChangeActive] = useState(1)

    const [pageIndex, setPageIndex] = useState(0)
    const [pageSize] = useState(10)
    const [totalRecords, setTotalRecord] = useState(0)

    const history = useHistory()

    const [data, setData] = useState([])
    const [permissions, setPermissions] = useState([])
    //   useEffect(() => {
    //     getListPermission();
    //   }, []);
    //   const getListPermission = () => {
    //     let temp = localStorage.getItem(KEY.LISTPATH);
    //     let listPath = JSON.parse(temp);
    //     if (listPath) {
    //       const newlist = listPath.map((e) => {
    //         if (e.function_code) return e.function_code;
    //         return e.action_code;
    //       });
    //       setPermissions(newlist);
    //     }
    //   };

    const handeChangeActive = async (id, status_id) => {}

    const getData = async (isInit) => {
        props.showLoading()

        let res

        if (isInit) {
            setPageIndex(0)
            res = await getListtPaymentCost({
                gas_name: searchDebount,
                page_no: 0,
                page_size: pageSize,
            })
        } else {
            res = await getListtPaymentCost({
                gas_name: searchDebount,
                page_no: pageIndex + 1,
                page_size: pageSize,
            })
        }

        props.hideLoading()

        if (res.data.code == 200 && res.data.data) {
            console.log(res.data)
            const newList = res.data.data.data.map((e, i) => {
                return {
                    ...e,
                    index: i + 1 + pageIndex * pageSize,
                    status_name:
                        e.status == 1 ? 'Đã thanh toán' : 'Chưa thanh toán',
                }
            })

            setData(newList)
            setTotalRecord(res.data.data.total_elements)
        } else if (res.data.code == 401) {
            localStorage.removeItem(KEY.API_TOKEN)
            setTimeout(() => {
                history.push('/')
            }, 100)
        } else {
            toast.error(t(res.data.error), {
                theme: 'colored',
            })
        }
    }

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

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

    const removeItem = async (id) => {
        props.showLoading()
        const res = await acceptPaymentCost(id)
        props.hideLoading()
        if (res.data.code == 200) {
            getData()
            toast.success('Xác nhận thanh toán thành công!', {
                theme: 'colored',
            })
        } else {
            toast.error(t(res.data.error), {
                theme: 'colored',
            })
        }
    }

    return (
        <ToolUserView
            data={data}
            removeItem={removeItem}
            setTxtSearch={setTxtSearch}
            setActiveSeleted={setActiveSeleted}
            pageIndex={pageIndex}
            changeActive={changeActive}
            setChangeActive={setChangeActive}
            setPageIndex={setPageIndex}
            activeSelected={activeSelected}
            handeChangeActive={handeChangeActive}
            totalRecords={totalRecords}
            permissions={permissions}
        />
    )
}

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