﻿import React from 'react';
import { toHHMMSS } from '../../../../util';

class TimeLabel extends React.Component {
    constructor() {
        super();
        this.state = {
            segundosLabel: '',
            acualizado:false
        }
    }
    componentDidMount() {
     this.startInterval();
    }
   
    componentWillReceiveProps() {
        this.stopInterval();
        this.startInterval();
    }
    componentWillUnmount() {
        this.stopInterval();
    }
    startInterval() {
        let { tiempo, estado, updateTimeCurrentOrder, row, vista, itemId } = this.props;
        if (vista == "pendientes") {
            if (row != undefined) {

                let _Seg = tiempo;
                if (estado == 'IN') {
                    this.timer = setInterval(() => {
                        _Seg += 1;
                        this.setState({ segundosLabel: toHHMMSS(_Seg) });
                        if (updateTimeCurrentOrder != undefined) {
                            updateTimeCurrentOrder(_Seg, row);
                        }
                    }, 500);
                } else
                    if (estado == 'PA' || estado == 'FI' || estado == 'CA') {
                        this.setState({ segundosLabel: toHHMMSS(_Seg) });
                    }
            }
        } else {
            this.setState({ segundosLabel: toHHMMSS(tiempo) });
        }
       
    }

    stopInterval() {
        if (this.timer) {
            clearInterval(this.timer)
            this.timer = null
        }
    }
    render() {
        return (<label id="tiempo">{this.state.segundosLabel}</label>)
    }
};
export default TimeLabel; 
