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


import Loop from "../svg/loupe.svg";
import Arrow from '../svg/arrow.svg';

// 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,
      counter: 0,
      order: '',
      word: '',
      test: false,
      error: null,
      tablePage: 0,
      reestr: '',
      reestrCount: 0,
  };
  }

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

 

  componentWillUnmount() {
    this._mounted = false
  }

  makeRemoteRequest = (query, test) => {
    if(query){
      this.setState({tablePage: this.state.tablePage + 1})
      }
    else if(this.state.tablePage > 1){
      this.setState({tablePage: this.state.tablePage - 1})
    }

    if(test){
      this.setState({tablePage: 1})
    }
    this.setState({test: false})

    const url = 'https://etalon.gov.tm:8080/api/v1/reestr';
  
    if (this._mounted) {
      this.setState({ DataisLoaded: true });
    }
    setTimeout(() => {
  
      fetch(`${url}?sort=${this.state.order}&s=${this.state.word}&page=${this.state.tablePage}&reestr_no=${this.state.reestr}`, {
        method: 'GET',
       
      })
  
        .then(res => res.json())
        .then(res => {
          if (this._mounted) {
            this.setState({
              items: res.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="regulations-head">
                <div className="regulations-head-left">
                  <form className="search-form">
                    <input type="text" placeholder="Поиск" onChange={(e) => {
                      const input = e.target.value;
                      this.setState({word: input});
                      this.makeRemoteRequest(this.state.tablePage, true);
                    }} />
                    <button type="button">
                      <div>
                        <img src={Loop} alt="Loop" />
                      </div>
                    </button>
                  </form>
                </div>
                {/* <div className="regulations-head-right">
                  <form className="btn-form">
                    <button type="button" className="button-prev regulations-btn">
                      <div>
                        <img src={Prev} alt="Previous" />
                      </div>
                    </button>
                    <button type="button" className="button-right regulations-btn">
                      <div>
                        <img src={Next} alt="Next" />
                      </div>
                    </button>
                  </form>
                </div> */}
              </div>
            <div className="mr-content">
              <div className="mr-table">
                <table className="structure-table">
                  <tbody>

                  

                    <tr className="table-head">
                      <th className={this.state.counter % 2 === 0 ? "switch" : "switch active"}
                      onClick={() => {
                        this.setState({reestr: ''});
                        this.setState({reestrCount: 0});
                          if(this.state.counter % 2 === 0){
                            this.setState({order: 'desc'})
                            this.makeRemoteRequest(this.state.tablePage, true);
                            this.setState({counter: this.state.counter + 1})
                          }else{
                            this.setState({order: 'asc'})
                            this.makeRemoteRequest(this.state.tablePage, true);
                            this.setState({counter: this.state.counter + 1})
                          }
                        }}
                        >
                        <div>
                          <span>{t('№ серт.')}</span>
                            <div className="th-img-block">
                             <div className="th-img-1"><img src={Arrow} alt="arrow" /></div>
                             <div className="th-img-2"><img src={Arrow} alt="arrow" /></div>
                            </div>
                        </div>
                      </th>
                      <th className={this.state.reestrCount % 2 === 0 ? 'switch2' : 'switch2 active'} onClick={() => {
                        this.setState({order: ''});
                        this.setState({counter: 0});
                        if(this.state.reestrCount % 2 === 0){
                          this.setState({reestr: 'desc'})
                          this.makeRemoteRequest(this.state.tablePage, true);
                          this.setState({reestrCount: this.state.reestrCount + 1})
                        }else{
                          this.setState({reestr: 'asc'})
                          this.makeRemoteRequest(this.state.tablePage, true);
                          this.setState({reestrCount: this.state.reestrCount + 1})
                        }
                      }}>
                        <div>
                        <span>{t('№ Рос-реестра')}</span>
                          <div className="th-img-block">
                            <div className="th2-img-1"><img src={Arrow} alt="arrow" /></div>
                            <div className="th2-img-2"><img src={Arrow} alt="arrow" /></div>
                          </div>
                        </div>
                      </th>
                      <th>
                        <span>{t('Наименования и тип СИ')}</span>
                      </th>
                      <th>
                        <span>{t('Изготовитель')}</span>
                      </th>
                      <th>
                        <span>{t('Срок действия')}</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);

