import React, { Component, Fragment } from 'react';
import toastr from 'toastr';
import AutoBind from 'react-autobind';




class ModalNotificacion extends Component {
    constructor(props) {
        super(props);
        this.state = {
            lstNotificacionShow: [],
            tipoFiltro: 'SMS',
            idPadre: 0,
            text: ''
        }
        AutoBind(this);
    }


    componentDidUpdate(prevProps) {
        let lstNotificacionShow = [];
        // Typical usage (don't forget to compare props):
        if (this.props.lstNotificacion !== prevProps.lstNotificacion) {
            lstNotificacionShow = this.props.lstNotificacionShow;
            this.setState({ lstNotificacionShow, idPadre: this.props.IdPadre });
        }
    }

    OnChange(event) {
        let lstNotificacionShow = [];
        let tipoFiltro = event.target.value;
        lstNotificacionShow = this.props.lstNotificacion.filter(item => item.NotificacionTipo == event.target.value);
        this.setState({ lstNotificacionShow, tipoFiltro });
    }

    Eliminar(event) {
        this.props.EliminarNotificacion(event.target.id, event.target.getAttribute('indicador'), this.state.tipoFiltro);
    }

    GuardarNotificacion() {
        console.log('GuardarNotificacion','Modal');
        const { idPadre, tipoFiltro, text } = this.state;
        this.props.GuardarNotificacion(idPadre, tipoFiltro, text);
        this.setState({ text: '' });
    }

    handleChange(event) {
        this.setState({ text: event.target.value });
    }
    onCerrar() {
        this.setState({ tipoFiltro: 'SMS', idPadre: 0, text: '' });
    }

    render() {


        const { lstNotificacionShow, tipoFiltro, text } = this.state;

        return (
            <div className="modal fade" id="ModalNotificacion" tabIndex="-1" role="dialog" aria-labelledby="exampleModalLabel" aria-hidden="true">
                <div className="modal-dialog" role="document">
                    <div className="modal-content">
                        <div className="modal-header">
                            <button type="button" className="close" data-dismiss="modal" aria-label="Close" onClick={this.onCerrar}>
                                <span aria-hidden="true">&times;</span>
                            </button>
                            <h5 className="modal-title" id="exampleModalLabel">Par&aacute;metros {this.props.NombrePadre}</h5>

                        </div>
                        <div className="modal-body">
                            <div className="row">
                                <div className="col-xs-6">
                                    <label >Filtar por:</label>
                                    <select className="btn dropdown-toggle form-control text-left" onChange={this.OnChange} value={tipoFiltro} >
                                        <option data-tokens="" value="SMS">SMS</option>
                                        <option data-tokens="" value="Email">Email</option>
                                      
                                    </select>
                                </div>
                                <div className="col-xs-4">
                                    <label>Agregar:</label>
                                    <input type="text" className="form-control" value={text} onChange={this.handleChange} />

                                </div>
                                <div className="col-xs-2">
                                    <button onClick={this.GuardarNotificacion} className="btn btn-primary btn-add" type="button"><i
                                        className="fa fa-plus"
                                        aria-hidden="true"></i></button>
                                </div>
                            </div>
                            <hr />
                            <div className="row">
                                <div className="col-xs-12">
                                    <h4 className='titlemodal'>{tipoFiltro}</h4>
                                    <ul className="lista-datos">
                                        {
                                            lstNotificacionShow.map((option) =>
                                                <li key={option.Id}><span>{option.NotificacionValor}</span>
                                                    <a onClick={this.Eliminar}><i id={option.Id} indicador={option.Indicador} className="fa fa-trash" aria-hidden="true"></i></a>
                                                </li>
                                            )
                                        }
                                    </ul>
                                </div>
                            </div>
                        </div>
                    </div>
                </div>
            </div>
        );
    }
}

export default ModalNotificacion;