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


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

// IMPORT IMAGES
// import EtalonImg from "../img/etalonImg.jpg";

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

class Etalon 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/posts/`+this.props.match.params.slug;
  
    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,
              error: res.message || null,
              DataisLoaded: true,
            })
            // console.log(this.state.items[0][0]['content'])
            // console.log(JSON.parse(this.state.items[0][0]['translations'][0]['attribute_data']).content_html);
          }
         
        })
        .catch(error => {
          if (this._mounted) {
            this.setState({ error, DataisLoaded: false});
          }
        });
    }, 1500);
  };

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


  // const [pageIndex, setPageIndex] = useState(1);


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

      return (
        <section className="etalon">
          <Breadcrumb
            path_1={t('Государственные эталоны')}
            link_1="/etalons"
            // path_2={this.props.match.params.slug}
            currentUrl="../etalons/1"
          />
          <div className="container">

          {items.map(item=>(
            <div className="etalon-inner" key={item[0]['id']}>
              <div className="novelty-head">
                <div className="novelty-img">
                  <img src={item[0]['featured_images'][0]['path']} alt="Etalon" />
                </div>
                <h4 className="novelty-title">
                { i18n.language === 'en' &&
                    JSON.parse(item[0]['translations'][0]['attribute_data']).title 
                }
                { i18n.language === 'ru' && 
                    item[0]['title']
                } 
                { i18n.language === 'tm' && 
                    JSON.parse(item[0]['translations'][1]['attribute_data']).title 
                }
                </h4>
              </div>
              <div className="novelty-text">

              {/* {item[0]['content']} */}

              { i18n.language === 'en' &&
              parse(JSON.parse(item[0]['translations'][0]['attribute_data']).content_html )
                  
              }
              { i18n.language === 'ru' && 
              parse(item[0]['content_html'])
                  
              } 
              { i18n.language === 'tm' && 
              parse(JSON.parse(item[0]['translations'][1]['attribute_data']).content_html )
                  
              } 

              </div>
            </div>

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

// export default Etalon;
export default withTranslation()(Etalon);
