import React, { useEffect, useState } from 'react';
import { CircularProgress  } from '@material-ui/core';
import axios from 'axios';
import { useHistory, Link } from 'react-router-dom';

import IconButton from '@mui/material/IconButton';
import SearchIcon from '@mui/icons-material/Search';


const Parent = () => {

  const [keyword, setKeyword] = useState('')
  const [loaded, setLoaded] = useState(false);
  const [father_list, setFatherList ] = useState(null);
  const [_422errors, set422errors] = useState({keyword:''});
  const [_400error, set400error] = useState('');

  useEffect(() => {
    setLoaded(false);
    axios.get('/api/admin/fathers/list')
    .then((response) => {
        if(response.data.status_code==200){
            console.log(response.data.params)
            setFatherList(response.data.params);
        } 
        setLoaded(true);
    });
    ////////////////////////////////////
  }, []);


  const handleSearch = (e) => {
    e.preventDefault();
    initErrors();
    if(keyword == '')
    {
      document.getElementById('keyword').focus();
      return;
    }

    setLoaded(false);
    setFatherList(null);

    axios.get('/api/admin/fathers/search',{params: {keyword: keyword}})
    .then((response) => {

      setLoaded(true);
      if(response.data.status_code==200){
        setFatherList(response.data.params);
      } else if(response.data.status_code==400){
        //TODO
      }
    });
  }

  const initErrors = () => {
    set422errors({keyword:''});
  }


	return (
    <div className="l-content">
      <div className="l-content__ttl">
        <div className="l-content__ttl__left">
          <h2>親一覧</h2>
        </div>
      </div>
      <div className="l-content-wrap">
        <section className="search-container">

          <div className="meeting-head mt-4"  onSubmit={handleSearch}>
              <form className="position-relative">
                  <label className="control-label" htmlFor="keyword">キーワード</label>
                  <input type="search" name="keyword" 
                      className="input-default input-keyword input-w380 input-h60"
                      id="keyword"  value={keyword} 
                      onChange={e=> setKeyword(e.target.value)}
                  />
                  <IconButton size="large" style={{position:'absolute', bottom:'3px', right:'5px'}} type="submit">
                    <SearchIcon fontSize="large" style={{color:'#d0d0d0'}}/>
                  </IconButton>
              </form>
          </div>

          <div className="search-wrap">
            <div className="search-content position-relative" style={{minHeight:'100px'}}>
              {
                !loaded &&
                      <CircularProgress color="secondary" style={{top:'20px', left:'calc(50% - 22px)', color:'green', position:'absolute'}}/>
              }
              { 
                loaded && 
                (
                  father_list && father_list.length>0 ?
                    father_list.map((father, k) => 
                      <div className="search-item" key={k}>
                        <Link to = {`/admin/parent/detail/${father.id}`}>
                          <div className="user-wrap">
                            <div className="user-avatar">
                              <img alt="name" className="avatar-img" src={ father.image } />
                            </div>
                            <div className="user-info">
                              <p className="user-name mb-1 font-weight-bold">{ father.company }</p>
                              <p className="user-tel">{ father.email }</p>
                            </div>
                          </div>
                        </Link>
                      </div>
                    ) : <p className="text-center py-5">データが存在していません。</p>
                )
              }

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

export default Parent;