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

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

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

class Legislation 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/6 + ?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)
            // console.log(res.data[0]['posts'][0]['translations'][1]['attribute_data'])
          }
         
        })
        .catch(error => {
          if (this._mounted) {
            this.setState({ error, DataisLoaded: false});
          }
        });
    }, 1500);
  };



  render() {
    // const { DataisLoaded, items } = this.state;
    //   if (!DataisLoaded) return <div>
    //       <h1> Pleses wait some time.... </h1> </div> ;
  
    //       if(i18n.language == 'en')
    //       {
    //         this.componentDidMount();
    //         console.log('salam-en');
            
    //       }
    //       else
    //       {
    //         this.componentDidMount();
    //         console.log('salam-ruru');
    //       }

    const { t } = this.props;

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

      return (
        <section className="legislation">
          <Breadcrumb path_1={t('Законодательная база')} currentUrl="/legislation" />
          <div className="container">
            <div className="legislation-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="law-card-wrapper">

              {items.map(item=>(
                <div className="law-card" key={item.id}>
                  <p className="law-card-text">

                  {/* {item.title} */}

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


                  </p>
                  <p className="law-card-bold">
                  {/* {item.content} */}
                  { i18n.language === 'en' &&
                      JSON.parse(item['translations'][0]['attribute_data']).content 
                  }
                  { i18n.language === 'ru' &&
                      item.content
                  }
                  { i18n.language === 'tm' &&
                      JSON.parse(item['translations'][1]['attribute_data']).content 
                  }
                  </p>
                </div>
              ))}
                
              </div>
            </div>
          </div>
        </section>
      );
    }
  }
};

// export default Legislation;
export default withTranslation()(Legislation);
