﻿import React, { Fragment, memo, useState, useEffect, } from 'react';
import { useHistory, withRouter } from 'react-router-dom';

import { Tabs, Tab, Row, Nav, Col, Modal, Button, Container } from "react-bootstrap";

import Botones from '../../components/UI/botonera/headerBotones.js';
import ComboBox from '../../components/UI/comboBox/combobox.jsx';
import CajaTextoBusqueda from '../../components/UI/cajaTexto/cajaTextoBusqueda.jsx';
import CajaTexto from '../../components/UI/cajaTexto/cajaTexto.jsx';
import CajaTextoNumero from '../../components/UI/cajaTexto/cajaTextoNumero.jsx';
import TextArea from '../../components/UI/textArea/textArea.jsx';
import RadioButton from '../../components/UI/radioButton/radiobutton.jsx';
import ModalBusquedaEmp from '../../components/UI/modalBusqueda/modalBusquedaFiltro.jsx';
import DatePicker from '../../components/UI/dataPicker/dataPicker.jsx';
import CheckBox from '../../components/UI/checkBox/checkBox.jsx';
import ListaDatos from '../../components/UI/listaDatosBusqueda/index.jsx';
import Division from '../../components/UI/division/division.jsx';

import Config from '../../components/Util/constantes.js';
import { farmatearFechaEstandarHora, farmatearFechaEstandar, farmatearMontos } from '../../components/Util/Util.jsx';
import { Notificacion } from '../../components/Util/Notificacion/Notificacion.jsx';

function vacacionesInt(props) {

    const columnasEmp = [{ dataField: 'Empleado', text: 'Número', csvText: 'Numero', sort: true }, { dataField: 'NomCompleto2', text: 'Nombre', sort: true }];
    const columnas = [{ dataField: 'idDetalle', hidden: true, csvExport: false }, { dataField: 'Desde', text: 'Desde', sort: true, align: 'center', headerAlign: 'center', formatter: (row, cell) => { return farmatearFechaEstandar(cell.Desde) }, editable: false, }, { dataField: 'Hasta', text: 'Hasta', sort: true, align: 'center', headerAlign: 'center', formatter: (row, cell) => { return farmatearFechaEstandar(cell.Hasta) }, editable: false, }, {
        dataField: 'DiasAAplicar', text: 'Días', csvText: 'Dias', sort: true, align: 'right', headerAlign: 'right', editable: true, formatter: (row, cell) => { return farmatearMontos(cell.DiasAAplicar) },
        validator: (newValue, row, column) => {

            if (newValue.trim() === '') {
                return {
                    valid: false,
                    message: 'No Digitó los Valores de Días',
                };
            }

            if (isNaN(newValue.trim())) {
                return {
                    valid: false,
                    message: 'Dede ingresar un valor Numérico',
                };
            }

            if (parseInt(newValue.trim()) === 0) {

                return {
                    valid: false,
                    message: 'No Digitó cantidad Días',
                };
            }

            if (parseInt(newValue.trim()) > props.DiasDiferencia(row.Desde, row.Hasta)) {
                return {
                    valid: false,
                    message: 'La cantidad de días no puede ser mayor a la diferencia de días entre la fecha de inicio y la fecha final.',
                };
            }

            return true;
        },
    }, { dataField: 'UltimoReg', text: 'Último', csvText: 'Ultimo', sort: true, formatter: (row, cell) => { return cell.UltimoReg ? "SI" : "NO" }, editable: false, align: 'center', headerAlign: 'center', }, { dataField: 'Estado', hidden: true, csvExport: false },];
    const selectRowEmp = { mode: 'radio', clickToSelect: true, hideSelectColumn: true, onSelect: props.onRowSelectEmp, bgColor: '#10a599', blurToSave: true, autoSelectText: true };

    const selectRow = { onSelect: props.onRowSelect, hideSelectColumn: true, mode: "checkbox", clickToSelect: true, clickToEdit: true, };

    const cellEdit = {
        mode: "click",
        blurToSave: true,
        autoSelectText: true,
        afterSaveCell: (oldValue, newValue, row, column) => {
            newValue !== oldValue &&
                newValue > 0;
        },
    };

    return (
        <Fragment>
            <div className='col'>
                <div className='row'>
                    <div className='col'>
                        <h1>{props.Titulo}</h1>
                    </div>
                    <div className="text-right">
                        <div className="acciones">
                            <Botones
                                botones={props.botones}
                            />
                        </div>
                    </div>
                </div>
                <div className="content">
                    <div className="content-box box-primary">
                        <div className='row'>
                            <ComboBox id="cmTipoPlanilla" col="6" attributes={props.Datos.TipoPlanilla.attributes} value={props.selectedOptionPlanilla} label={props.Datos.TipoPlanilla.attributes.label} datos={props.listaTipoPlanilla} onChange={props.onhandleSelectedOptionPlanilla} />
                            <ComboBox id="cmbTipoFiltro" col="6" attributes={props.Datos.TipoFiltro.attributes} value={props.selectedFiltroPor} label={props.Datos.TipoFiltro.attributes.label} datos={props.listaFiltroPor} onChange={props.handleselectedFiltroPor} />
                        </div>
                        <div className='row'>
                            <ComboBox id="cmbarea" col="6" attributes={props.Datos.Area.attributes} value={props.selectedOptionArea} label={props.Datos.Area.attributes.label} datos={props.listaArea} onChange={props.onhandleSelectedOptionArea} />
                            <ComboBox id="cmbdepto" col="6" attributes={props.Datos.Depto.attributes} value={props.selectedOptionDepto} label={props.Datos.Depto.attributes.label} datos={props.listaDeptoFiltro} onChange={props.onhandleSelectedOptionDepto} />
                        </div>
                        <div className='row'>
                            <ComboBox id="cmbseccion" col="6" attributes={props.Datos.Seccion.attributes} value={props.selectedOptionSeccion} label={props.Datos.Seccion.attributes.label} datos={props.listaSeccionFiltro} onChange={props.onhandleSelectedOptionSeccion} />
                            <ComboBox id="cmboficina" col="6" attributes={props.Datos.Oficina.attributes} value={props.selectedOptionOficina} label={props.Datos.Oficina.attributes.label} datos={props.listaOficinaFiltro} onChange={props.onhandleSelectedOptionOficina} />
                        </div>
                        <div className='row'>
                            <CajaTextoBusqueda col="2" id="CodigoEmpleado" type="text" label={props.Datos.CodigoEmpleado.attributes.label} value={props.Datos.CodigoEmpleado.value} attributes={props.Datos.CodigoEmpleado.attributes} onChangeInput={props.onChangeInputEmp} onClick={props.handleModalEmp} onKeyPress={props.onKeyPressEmp} onBlur={props.onBlurEmpl} />
                            <CajaTexto col="4" id="NombreEmpleado" type="text" label={props.Datos.NombreEmpleado.attributes.label} value={props.Datos.NombreEmpleado.value} attributes={props.Datos.NombreEmpleado.attributes} onChangeInput={() => { }} />
                            <CajaTextoNumero col="6" id="VacacionesPendientes" type="text" label={props.Datos.VacacionesPendientes.attributes.label} value={props.Datos.VacacionesPendientes.value} attributes={props.Datos.VacacionesPendientes.attributes} onChangeInput={() => { }} align='right' />
                        </div>
                        <hr />
                        <div className='row'>
                            <CajaTextoNumero col="6" id="DiasVacaciones" type="text" label={props.Datos.DiasVacaciones.attributes.label} value={props.Datos.DiasVacaciones.value} attributes={props.Datos.DiasVacaciones.attributes} onChangeInput={props.onChangeInputPorMonto} onBlur={props.onChangeEditar} onFocus={props.onChangeEditar} onFormatear={props.onFormato} align='right' />
                            <CheckBox id='MedioDia' col="6" attributes={props.Datos.MedioDia.attributes} value={props.Datos.MedioDia.value} label={props.Datos.MedioDia.attributes.label} onChange={props.handleCheckedChange} />
                        </div>
                        <div className='row'>
                            <DatePicker id="Desde" col="6" value={props.Datos.Desde.value} attributes={props.Datos.Desde.attributes} label={props.Datos.Desde.attributes.label} formatDate={Config.dateformat} handleChange={props.onhandleChangeDate} minDate={props.Datos.Desde.attributes.minDate} />
                            <DatePicker id="Hasta" col="6" value={props.Datos.Hasta.value} attributes={props.Datos.Hasta.attributes} label={props.Datos.Hasta.attributes.label} formatDate={Config.dateformat} handleChange={props.onhandleChangeDate} minDate={props.Datos.Hasta.attributes.minDate} />
                        </div>
                        <div className='row'>
                            <DatePicker id="Desde_Info" col="6" value={props.Datos.Desde_Info.value} attributes={props.Datos.Desde_Info.attributes} label={props.Datos.Desde_Info.attributes.label} formatDate={Config.dateformat} handleChange={props.onhandleChangeDate} />
                            <DatePicker id="Hasta_Info" col="6" value={props.Datos.Hasta_Info.value} attributes={props.Datos.Hasta_Info.attributes} label={props.Datos.Hasta_Info.attributes.label} formatDate={Config.dateformat} handleChange={props.onhandleChangeDate} />
                        </div>
                        <hr />
                        <div className='row'>
                            <TextArea id='Comentario' col='12' value={props.Datos.Comentario.value} label={props.Datos.Comentario.attributes.label} attributes={props.Datos.Comentario.attributes} onChangeInput={props.onChangeInputEmp} rows='3' />
                        </div>
                        <div className="row">
                            <div className="col-xs-12 col-md-12">
                                <div id="master-tabla" className="table-responsive  col-xs-12 col-sm-12 col-md-12 col-lg-12">
                                    <ListaDatos
                                        id={"idDetalle"}
                                        datos={props.listaDatos}
                                        columnas={columnas}
                                        striped={false}
                                        hover={true}
                                        condensed={false}
                                        selectRow={selectRow}
                                        onDoubleClick={() => { }}
                                        cellEdit={cellEdit}
                                        borrando={false}
                                        verLHOC={false}
                                        nombreCSV={"Vacaciones"}
                                    />
                                </div>
                            </div>
                        </div>
                        <div className='row'>
                            <div className='col-xs-12 col-md-6'>
                                <h4><strong>{props.Datos.TotalDias.attributes.label} &nbsp;</strong>{props.Datos.TotalDias.value}</h4>
                            </div>
                        </div>
                    </div>
                </div>
            </div>
            <ModalBusquedaEmp
                selectedModal={props.selectModalEmp}
                contentLabel={'selectedBusquedasEmp'}
                datos={props.listaEmpleados}
                id={'Empleado'}
                columnas={columnasEmp}
                striped={false}
                hover={true}
                condensed={false}
                selectRow={selectRowEmp}
                onDoubleClick={props.onDoubleClickEmp}
                label={'Lista de Empleados'}
                cerrarModal={props.cerrarModalEmp}
                seleccionModal={props.seleccionModalEmp}
                verLHOC={false}
                nombreCSV={'BusquedaEmpleados'}
            />

        </Fragment>
    )

}
export default withRouter(memo(vacacionesInt));