import React, { useState, useEffect } from 'react'
import ToolUserView from './View'
import {
    getListSupplier,
    deleteSupplier,
    changeStatusSupplier,
} from 'app/apis/Functions/supplier'
import { useHistory } from 'react-router-dom'
import KEY from 'app/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 useAuth from 'app/hooks/useAuth'

const ToolNotificate = (props) => {
    const [txtSearch, setTxtSearch] = useState('')
    const [activeSelected, setActiveSeleted] = useState(null)
    const [changeActive, setChangeActive] = useState(1)
    const searchDebount = useDebounce(txtSearch, 1000)
    const [pageIndex, setPageIndex] = useState(0)
    const [pageSize] = useState(10)
    const [totalRecords, setTotalRecord] = useState(0)
    const [status, setStatus] = useState()
    const [listDrop, setListDrop] = useState([])
    const [store, setStore] = useState()
    const history = useHistory()
    const [listGroup, setListGroup] = useState([])
    const [group, setGroup] = useState()
    const { user } = useAuth()

    const [data, setData] = useState([])

    const handeChangeActive = async (id, status_id) => {
        props.showLoading()
        const res = await changeStatusSupplier({ id, status_id })
        props.hideLoading()
        if (res.data.code == 200) {
            getData()
            toast.success('Thay đổi trạng thái thành công!', {
                theme: 'colored',
            })
        } else {
            toast.error('Thay đổi trạng thái thất bại!', {
                theme: 'colored',
            })
        }
    }

    const getData = async (isInit) => {
        props.showLoading()
        let res
        if (isInit) {
            setPageIndex(0)
            res = await getListSupplier({
                provider_name: searchDebount,
                page_no: 0,
                page_size: pageSize,
                status,
                type: 1,
            })
        } else {
            res = await getListSupplier({
                provider_name: searchDebount,
                page_no: pageIndex + 1,
                page_size: pageSize,
                status,
                type: 1,
            })
        }

        props.hideLoading()
        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) {
            localStorage.removeItem(KEY.API_TOKEN)
            setTimeout(() => {
                history.push('/')
            }, 100)
        } else {
            // enqueueSnackbar('Error!', { variant: 'error' })
        }
    }

    useEffect(() => {
        getData(false)
    }, [pageIndex])
    useEffect(() => {
        getData(true)
    }, [searchDebount, status, store, group])

    const removeItem = async (id) => {
        props.showLoading()
        const res = await deleteSupplier({ id })
        props.hideLoading()
        if (res.data.code == 200) {
            getData(true)
            toast.success('Xoá bản ghi thành công!', {
                theme: 'colored',
            })
        } else if (res.data.code == 401) {
            localStorage.removeItem(KEY.API_TOKEN)
            setTimeout(() => {
                history.push('/')
            }, 100)
        } else {
            toast.error('Xoá bản ghi thất bại!', {
                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}
            status={status}
            setStatus={setStatus}
            listDrop={listDrop}
            store={store}
            setStore={setStore}
            listGroup={listGroup}
            group={group}
            setGroup={setGroup}
        />
    )
}

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