import React, { useState, useEffect } from 'react'
import {
    ValidatorForm,
    TextValidator,
    SelectValidator,
} from 'react-material-ui-form-validator'
import {
    Button,
    Grid,
    MenuItem,
    FormControlLabel,
    Checkbox,
    Typography,
    TextField,
    Icon,
} from '@material-ui/core'

import { showLoading, hideLoading } from 'app/redux/actions/loadingAction'
import { toast } from 'react-toastify'
import { Breadcrumb, SimpleCard } from 'app/components'
import { Link, useHistory, useLocation } from 'react-router-dom'
import { trimObject, encryString, convertDate } from 'app/config/Function'
import { connect } from 'react-redux'
import { KeyboardDatePicker } from '@material-ui/pickers'
import {
    dropdownDepartment,
    dropdownUserGroup,
    dropdownStore,
} from 'app/apis/Functions/dropdown'
import PickerImage from 'app/components/Input/PickerImage'
import {
    detailSheetReconciliation,
    updateSheetReconciliation,
} from 'app/apis/Functions/reconciliation'
import { useTranslation } from 'react-i18next'
import useAuth from 'app/hooks/useAuth'
import ListTrans from './ListTrans/Index'

const SimpleForm = (props) => {
    const location = useLocation()

    const [state, setState] = useState({})

    const [listDrop, setListDrop] = useState([])
    const [listAction, setListAction] = useState([])

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

    const { user } = useAuth()

    const { t } = useTranslation()

    const history = useHistory()

    const getDataDefault = async () => {
        props.showLoading()
        try {
            const res = await detailSheetReconciliation(location.state?.id, {})
            if (res.data.data) {
                setState({ ...res.data.data })
            }
        } catch (err) {
            toast.error(t(err), {
                theme: 'colored',
            })
        }
        props.hideLoading()
    }

    const handleSubmit = async (event) => {
        const newValue = trimObject(state)
        props.showLoading()
        const res = await updateSheetReconciliation({
            ...newValue,
        })

        props.hideLoading()
        if (res.data.code == 200) {
            history.push('/reconciliation/sheet')
            if (res.data.code == 200) {
                toast.success('Cập nhật phiếu đối soát thành công!', {
                    theme: 'colored',
                })
            }
        } else {
            toast.error(t(res.data.error), {
                theme: 'colored',
            })
        }
    }

    const handleChange = (event) => {
        event.persist()
        setState({
            ...state,
            [event.target.name]: event.target.value,
        })
    }
    console.log('state', state)
    const { code, idPaymentPartner, note } = state
    console.log('location.state', location.state.id)
    return (
        <div className="m-sm-30">
            <div className="mb-sm-30">
                <div className="mb-sm-30">
                    <Breadcrumb
                        routeSegments={[
                            {
                                name: 'Cấu hình đối soát',
                                path: '/reconciliation/sheet',
                            },
                            { name: 'Cập nhật' },
                        ]}
                    />
                </div>
                <SimpleCard>
                    <ValidatorForm onSubmit={handleSubmit} onError={() => null}>
                        <Grid container spacing={3} alignItems="center">
                            <Grid item lg={6} md={6} sm={12} xs={12}>
                                <Typography variant="h6">
                                    Sửa phiếu đối soát
                                </Typography>
                            </Grid>
                            <Grid
                                item
                                lg={6}
                                md={6}
                                sm={12}
                                xs={12}
                                container
                                justify={'flex-end'}
                            >
                                <Link to="/reconciliation/sheet">
                                    <Button
                                        style={{
                                            marginRight: 20,
                                        }}
                                        color="inherit"
                                        variant="contained"
                                        onClick={() => {}}
                                    >
                                        <span className="capitalize">
                                            Quay lại
                                        </span>
                                    </Button>
                                </Link>
                                <Button
                                    color="primary"
                                    variant="contained"
                                    type="submit"
                                >
                                    <span className="capitalize">Cập nhật</span>
                                </Button>
                            </Grid>
                            <Grid item lg={6} md={6} sm={12} xs={12}>
                                <TextValidator
                                    variant="outlined"
                                    className="mb-4 w-full"
                                    label="Mã phiếu đối soát *"
                                    onChange={handleChange}
                                    disabled={true}
                                    type="text"
                                    name="code"
                                    value={code || ''}
                                    validators={['required']}
                                    errorMessages={[
                                        'Không được để trống trường này',
                                    ]}
                                />
                            </Grid>
                            <Grid item lg={6} md={6} sm={12} xs={12}>
                                <TextValidator
                                    variant="outlined"
                                    className="mb-4 w-full"
                                    label="Đối tác *"
                                    disabled={true}
                                    onChange={handleChange}
                                    type="text"
                                    name="idPaymentPartner"
                                    value={idPaymentPartner || ''}
                                    validators={['required']}
                                    errorMessages={[
                                        'Không được để trống trường này',
                                    ]}
                                />
                            </Grid>

                            <Grid item lg={12} md={12} sm={12} xs={12}>
                                <TextValidator
                                    variant="outlined"
                                    className="mb-4 w-full"
                                    label="Ghi chú"
                                    onChange={handleChange}
                                    type="text"
                                    name="note"
                                    value={note || ''}
                                />
                            </Grid>

                            <Grid
                                item
                                lg={12}
                                md={12}
                                sm={12}
                                xs={12}
                                container
                                spacing={2}
                            >
                                <ListTrans
                                    isDetail={false}
                                    idReconciliation={location.state?.id}
                                    idPaymentPartner={
                                        location.state?.idPaymentPartner
                                    }
                                    id={location.state.id}
                                />
                            </Grid>
                        </Grid>
                    </ValidatorForm>
                </SimpleCard>
            </div>
        </div>
    )
}

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