﻿import React from 'react';
import toastr from 'toastr';
import AutoBind from 'react-autobind';

class InputNumber extends React.Component {

    constructor() {
        super();
        AutoBind(this);
    }

    componentDidMount() {
      
    }

    onPlus(e){
        var antValue = this.props.value;       
        var newValue = parseInt(antValue) + 1;
        if (this.validFloat(newValue + "") == false) {
            toastr.error("La cantidad ingresada no está permitida");
            return;
        } else {
            const inputEvent = { target: { value: newValue, name: 'boton', id: 'cantidad' } };
            this.props.onChange(inputEvent);
            e.target.blur();
        }
        
    }
    validFloat(pValue) {
        let _aNumber = pValue.split(".");
        if (parseInt(_aNumber[0]) > 99999999) {
            return false;
        } else {
            return true;
        }
    }
    onChange(e) {
        const id = e.target.id;
        let value = e.target.value;
        if (this.validFloat(value + "") == true) {
            if (this.props.decimales == true) {
                value = e.target.value;
            } else {
                value = (e.target.validity.valid) ? e.target.value : this.props.value;
            }
            if (value >= 0) {
                const inputEvent = { target: { value: value, name: id, id: id } };
                this.props.onChange(inputEvent);
            }
        }
    }

    onClick(e) {
        let id = e.currentTarget.id;

        switch (id) {
            case 'cantidad':
                e.target.select();
                this.props.onClick({ target: { id: 'cantidad' }, currentTarget: e.currentTarget });
                break;
            case 'btnApply':
                let _val = $("#cantidad").val()+"";
                if (this.validFloat(_val) == false) {
                   toastr.error("La cantidad ingresada no está permitida");
                } else {
                    this.props.onClick({ target: { id: 'btnApply' }, currentTarget: e.currentTarget });
                }
                break;
        }
       
        
    }

    onMinus(e){
      var antValue= this.props.value;
      if (antValue>0){
          var newValue = parseInt(antValue) - 1;
          if (this.validFloat(newValue + "") == false) {
              toastr.error("La cantidad ingresada no está permitida");
              return;
          } else {

              const inputEvent = { target: { value: newValue, name: 'boton', id: 'cantidad' } };
              this.props.onChange(inputEvent);
              e.target.blur();
          }
      }    
     
    }
  
    render() { 
        const { title, value, onChange, showApplyOption, onClick, decimales } = this.props;

        let _atributoPattern = decimales == true ? {} : { pattern: '\d{9}' };
        return (
            <div className="quantity">
                {showApplyOption ? <button id='btnCancel' ref='btnCancel' className="cancel" onClick={onClick}><i className="fa fa-times" aria-hidden="true"></i></button>
                    : <button id='btnMinus' ref='btnMinus' className="less" onClick={this.onMinus.bind(this)}>-</button>}
                <input id='cantidad' max="99999999" {..._atributoPattern} ref='cantidad' type="number" name="cantidad" value={value} onChange={this.onChange} onClick={this.onClick}/>          
                {showApplyOption ?
                    <button id='btnApply' ref='btnApply' className="apply" onClick={this.onClick.bind(this)}><i className="fa fa-check" aria-hidden="true"></i></button>
                    : <button id='btnPlus' ref='btnPlus' className="plus" onClick={this.onPlus.bind(this)}>+</button>   }
                    </div>
        )
    }
};
export default InputNumber; 