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



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

// IMPORT IMAGES
import StructureImg_ru from "../img/structureImg.jpg";
import StructureImg_tm from "../img/schema-tm.png";
import StructureImg_en from "../img/schema-en.jpg";



// const Structure = () => {
//   useEffect(() => {
//     window.scrollTo(0, 0);
//   }, []);
class Structure 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/struktura_sentra`
  
    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(res.data) 
          }
         
        })
        .catch(error => {
          if (this._mounted) {
            this.setState({ error, DataisLoaded: false});
          }
        });
    }, 1500);
  };

  render() {

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

  const { t } = this.props;

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

      return (
        <section className="structure">
          <Breadcrumb
            path_1={t('Структура Государственного эталонного центра')}
            currentUrl="/structure"
          />
          <div className="container">
            <div className="structure-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="structure-main-page">
              { i18n.language === 'en' &&
                  // JSON.parse(item['translations'][0]['attribute_data']).position_held 
                  <img src={StructureImg_en} alt="Structure" />
              }
              { i18n.language === 'ru' && 
                  // item.position_held
                  <img src={StructureImg_ru} alt="Structure" />
              } 
              { i18n.language === 'tm' && 
                  // JSON.parse(item['translations'][1]['attribute_data']).position_held
                  <img src={StructureImg_tm} alt="Structure" /> 
              }
                {/* <img src={StructureImg} alt="Structure" /> */}
              </div>
              <div className="structure-employees">
                <table className="structure-table">
                  <tbody>
                    <tr className="table-head">
                      <th>
                        <span className="name-head">{t('Фамилия, имя, отчество')}</span>
                      </th>
                      <th>
                        <span className="position-head">{t('Занимаемая должность')}</span>
                      </th>
                      <th>
                        <span className="tel-head">{t('Рабочий телефон')}</span>
                      </th>
                    </tr>

                    {items.map(item=>(
                      <tr key={item.id}>
                        <td>
                          <span className="name">
                          { i18n.language === 'en' &&
                              JSON.parse(item['translations'][0]['attribute_data']).name 
                          }
                          { i18n.language === 'ru' && 
                              item.name
                          } 
                          { i18n.language === 'tm' && 
                              JSON.parse(item['translations'][1]['attribute_data']).name 
                          }
                          </span>
                        </td>
                        <td>
                          <span className="position">
                          { i18n.language === 'en' &&
                              JSON.parse(item['translations'][0]['attribute_data']).position_held 
                          }
                          { i18n.language === 'ru' && 
                              item.position_held
                          } 
                          { i18n.language === 'tm' && 
                              JSON.parse(item['translations'][1]['attribute_data']).position_held 
                          }
                          </span>
                        </td>
                        <td>
                          <span className="tel">{t('Teл')} :{item.phone}</span>
                        </td>
                      </tr>
                    ))}

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

// export default Structure;
export default withTranslation()(Structure);

