// // IMPORT MODULES
import React from "react";
import { withTranslation } from 'react-i18next'; 


// IMPORT COMPONENTS
import Breadcrumb from "../components/Global/Breadcrumb";

// const MeasuringRegister = () => {
//   useEffect(() => {
//     window.scrollTo(0, 0);
//   }, []);

class MeasuringRegister extends React.Component {

  constructor(props) {
    super(props);

    this.state = {
      items: [],
      DataisLoaded: false,
      error: null,
      tablePage: 0
  };
  }

  componentDidMount() {
    this._mounted = true;
    this.makeRemoteRequest(true);
  }

 

  componentWillUnmount() {
    this._mounted = false
  }

  makeRemoteRequest = (query) => {
    if(query){
      this.setState({tablePage: this.state.tablePage + 1})
      }
    else if(this.state.tablePage > 1){
      this.setState({tablePage: this.state.tablePage - 1})
    }
    const url = 'https://etalon.gov.tm:8080/api/v1/reestr';
  
    if (this._mounted) {
      this.setState({ DataisLoaded: true });
    }
    setTimeout(() => {
  
      fetch(url + '?page=' + this.state.tablePage, {
        method: 'GET',
       
      })
  
        .then(res => res.json())
        .then(res => {
          if (this._mounted) {
            this.setState({
              items: res.data['data'],
              error: res.message || null,
              DataisLoaded: true,
            })
            // console.log(this.state.tablePage)
            console.log(this.state.items)
          }
         
        })
        .catch(error => {
          if (this._mounted) {
            this.setState({ error, DataisLoaded: false});
          }
        });
    }, 1500);
  };


  render() {
    const { t } = this.props;

    var {DataisLoaded, items} = this.state;
    if(!DataisLoaded){
      return <div>Loading ....</div>
    }else{


      return (
        <section className="measuring-register">
          <Breadcrumb
            path_1={t('Государственный реестр средств измерений')}
            currentUrl="/measuring_register"
          />
          <div className="container">
            <div className="measuring-register-inner">
              <div className="tab-header">
                <h4 className="tab-header-text" id="tab-header-text">
                {t('Утвержденные типы средств измерений, внесенные в Госреестр СИ Туркменистана на 31/12/2021')}
                </h4>
                <div className="tab-header-line"></div>
              </div>
            </div>
            <div className="mr-content">
              <div className="mr-table">
                <table className="structure-table">
                  <tbody>

                  

                    <tr className="table-head">
                      <th>
                        <span>№ серт.</span>
                      </th>
                      <th>
                        <span>№ Рос-реестра</span>
                      </th>
                      <th>
                        <span>Наименования и тип СИ</span>
                      </th>
                      <th>
                        <span>Изготовитель</span>
                      </th>
                      <th>
                        <span>Срок действия</span>
                      </th>
                    </tr>

                    {items.map(item=>(

                    <tr key={item.id}>
                    <td>
                      <span>{ item.certificate_no }</span>
                    </td>
                    <td>
                      <span>{ item.reestr_no }</span>
                    </td>
                    <td>
                      <span>{ item.reestr_name }</span>
                    </td>
                    <td>
                      <span>{ item.manufacture }</span>
                    </td>
                    <td>
                      <span>{ item.valid_until }</span>
                    </td>
                    </tr>
                  
                  ))}

                  </tbody>
                </table>
              </div>
            </div>
            <div className="pagination">
              <button type="button" className="pagination-btn prev" onClick={() => {this.makeRemoteRequest(false)}}>Предыдущая</button>
              <button type="button" className="pagination-btn next" onClick={() => {this.makeRemoteRequest(true)}}>Следующая</button>
            </div>
          </div>
        </section>
      );
    }
  }
};

// export default MeasuringRegister;
export default withTranslation()(MeasuringRegister);

