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

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

// IMPORT IMAGES
// import Img_1 from "../img/etalonsImg_1.jpg";
// import Img_2 from "../img/etalonsImg_2.jpg";
// import Img_3 from "../img/etalonsImg_3.jpg";
// import Img_4 from "../img/etalonsImg_4.jpg";
// import Img_5 from "../img/etalonsImg_5.jpg";
// import Img_6 from "../img/etalonsImg_6.jpg";
// import Img_7 from "../img/etalonsImg_7.jpg";
// import Img_8 from "../img/etalonsImg_8.jpg";
// import Img_9 from "../img/etalonsImg_9.jpg";
// import Img_10 from "../img/etalonsImg_10.jpg";
// import Img_11 from "../img/etalonsImg_11.jpg";
// import Img_12 from "../img/etalonsImg_12.jpg";
// import Img_13 from "../img/etalonsImg_13.jpg";
// import Img_14 from "../img/etalonsImg_14.jpg";
// import Img_15 from "../img/etalonsImg_15.jpg";
// import Img_16 from "../img/etalonsImg_16.jpg";
// import Img_17 from "../img/etalonsImg_17.jpg";

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

class Etalons extends React.Component {

  constructor(props) {
    super(props);

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

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

 

  componentWillUnmount() {
    this._mounted = false
  }

  makeRemoteRequest = () => {

    const url = `https://etalon.gov.tm:8080/api/v1/categories/5 + ?pgn=50`;
  
    if (this._mounted) {
      this.setState({ DataisLoaded: true });
    }
    setTimeout(() => {
  
      fetch(url, {
        method: 'GET',
       
      })
  
        .then(res => res.json())
        .then(res => {
          if (this._mounted) {
            this.setState({
              items: res.data[0].data,
              error: res.message || null,
              DataisLoaded: true,
            })
            // console.log(res.data)
          }
         
        })
        .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="etalons">
          <Breadcrumb path_1={t('Государственные эталоны')} currentUrl="../etalons" />
          <div className="container">
            <div className="etalons-inner">
              <div className="tab-header">
                <h4 className="tab-header-text" id="tab-header-text">
                {t('Государственные эталоны')}
                </h4>
                <div className="tab-header-line"></div>
              </div>
              <div className="depts-wrapper">

              {items.map(item=>(

                <EtalonsCard
                  key={item.id}
                  img={ item['featured_images'][0]['path'] }
                  title={
                    (() => {
                        if(i18n.language === 'en') {
                                return (
                                  JSON.parse(item['translations'][0]['attribute_data']).title 
                                )
                            } else if (i18n.language === 'ru') {
                                return (
                                  item.title
                                )
                            } else {
                                return (
                                  JSON.parse(item['translations'][1]['attribute_data']).title 
                                )
                            }
                    })()  
                }
                  link={`/etalons/${item.slug}`}
                />

              ))}

        {/* {
                (() => {
                    if(i18n.language === 'en') {
                            return (
                                <p>Hi</p>
                            )
                        } else if (i18n.language === 'ru') {
                            return (
                            <p>Hello</p>
                            )
                        } else {
                            return (
                                <p>Bye</p>
                            )
                        }
                })()  
            }    */}

              </div>
            </div>
          </div>
        </section>
      );
    }
  }
};

// export default Etalons;
export default withTranslation()(Etalons);
