import React, { useEffect, useState } from 'react';
import axios from 'axios';
import { CircularProgress  } from '@material-ui/core';
import { LoadingButton } from '@material-ui/lab';

import IconButton from '@mui/material/IconButton';
import RemoveIcon from '@mui/icons-material/Remove';

import Alert from '../../component/alert';


const MeetingEdit = (props) => {
    
    const [title, setTitle] = useState('');
    const [memo, setMemo] = useState('');
    const [text, setText] = useState('');
    const [pdf, setPdf] = useState('');
    const [meeting_image, setMeetingImages] = useState([]);
    const [imageUri, setImageUri] = useState('');

    const [_422errors, set422Errors] = useState({title:'', text:'', memo:'', pdf:''})
    const [_400error, set400Error] = useState('');
    const [_success, setSuccess] = useState('');

    const [loaded, setLoaded] = useState(false);
    const [submit, setSubmit] = useState(false);

    useEffect(() => {
        setLoaded(false);

        axios.get(`/api/admin/meetings/detail/${props.match.params?.meeting_id}`, {params: { father_id: 1 }})
        .then((response) => {
            setLoaded(true);
            if(response.data.status_code==200){
                console.log(response.data.params[0]);
                setTitle(response.data.params[0]?.title);
                setMemo(response.data.params[0]?.memo);
                setText(response.data.params[0]?.text);
                setMeetingImages(response.data.params[0]?.meeting_image);
            } else if(response.data.status_code==400){
                //TODO
            }
        });
      }, []);



     const handleSubmit = (e) => {

        e.preventDefault();

        set422Errors({title:'',memo:'',text:'',pdf:'',images:''});
    
        try {
            const request = { title: title, text: text, memo: memo, pdf: pdf };
            setSubmit(true);
            axios.put(`/api/admin/meetings/update/${props.match.params?.meeting_id}`, request)
            .then(response => {
                setSubmit(false);
                switch(response.data.status_code){
                    case 200: setSuccess(response.data.success_messages); break;
                    case 400: set400Error(response.data.error_messages); break;
                    case 422: set422Errors(response.data.error_messages); break;
                }
            });
        } catch (error) {
          console.log('error', error);
        }
    }


    const handleImageChange = (e) => {

        e.preventDefault();
        let reader = new FileReader();
        let _file = e.target.files[0];

        reader.readAsDataURL(_file);

        reader.onloadend = () => {
            
            axios.put(`/api/admin/children/updateImage/${props.match.params?.child_id}`, {image: reader.result})
            .then(response => {
                switch(response.data.status_code){
                    case 200: {
                        setImage(reader.result);
                        setSuccess(response.data.success_messages);
                        break;
                    }
                    case 400: set400Error(response.data.error_messages); break;
                    case 422: set422Errors(response.data.error_messages); break;
                } 
            });

        };
    };


	return (
        <div className="l-content">
            <div className="l-content-w560">
                <div className="l-content__ttl">
                    <div className="l-content__ttl__left">
                        <h2>ミーティング編集</h2>
                    </div>
                </div>
        
                <div className="l-content-wrap">
                    <div className="p-article">
                    <div className="p-article-wrap position-relative" style={{ minHeight:'500px'}}>
                    {
                        !loaded &&
                        <CircularProgress color="secondary" style={{top:'150px',  left:'calc(50% - 22px)', color:'green', position:'absolute'}}/>
                    }
                    {
                        loaded && 
                        <article className="p-article__body">
                            <div className="p-article__content">       
                                <div className="p-article__context">
                                    <form className="edit-form" onSubmit={handleSubmit}>
                                        <div className="edit-set">
                                            <label className="control-label" htmlFor="title">タイトル</label>
                                            <input type="text" name="title"   value={ title } onChange={e=>setTitle(e.target.value)} className={`input-default input-title input-h60 input-w480 ${  _422errors.title && 'is-invalid c-input__target'} `} id="title" />
                                            {
                                                _422errors.title &&
                                                    <span className="l-alert__text--error ft-16 ft-md-14">
                                                        {_422errors.title}
                                                    </span> 
                                            }
                                        </div>
                                        <div className="edit-set">
                                            <label className="control-label" htmlFor="meeting_textarea">本文</label>
                                            <textarea value={ text } onChange={e=>setText(e.target.value)} rows="8" className={`textarea-default  ${  _422errors.text && 'is-invalid c-input__target'} `}  id="meeting_textarea" />
                                            {
                                                _422errors.text &&
                                                    <span className="l-alert__text--error ft-16 ft-md-14">
                                                        {_422errors.text}
                                                    </span> 
                                            }
                                        </div>
                                        <div className="edit-set">
                                            <label className="control-label" htmlFor="meeting_textarea">メモ</label>
                                            <textarea value={ memo } onChange={e=>setMemo(e.target.value)}  rows="8" className={`textarea-default  ${  _422errors.memo && 'is-invalid c-input__target'} `} id="meeting_textarea" />
                                            {
                                                _422errors.memo &&
                                                    <span className="l-alert__text--error ft-16 ft-md-14">
                                                        {_422errors.memo}
                                                    </span> 
                                            }
                                        </div>
                                        <div className="edit-set edit-set-mt15">
                                            <label className="edit-set-file-label" htmlFor="file_pdf">
                                                PDFアップロード
                                                <input type="file" name="file_pdf" accept=".pdf" id="file_pdf" value={pdf} onChange={e=>setPdf(e.target.file)} />
                                            </label> 
                                            {
                                                _422errors.pdf &&
                                                  <span className="l-alert__text--error ft-16 ft-md-14">
                                                      {_422errors.pdf}
                                                  </span> 
                                            }
                                        </div>
                                        <div className="edit-set edit-set-mt15">
                                            <label className="edit-set-file-label" htmlFor="file_image">
                                                画像アップロード
                                                <input type="file" name="file_image" accept=".png, .jpg, .jpeg" id="file_image"  value={imageUri} onChange={handleImageChange}/> 
                                            </label>
                                        </div>
                                        
                                        <div className="p-file-image">
                                        {
                                            meeting_image?.map((x, k)=>
                                                <figure className="image-upload" key={k}>
                                                    <img src={x.image} alt={x.image} />
                                                    <IconButton
                                                        style={{position: 'absolute',
                                                            bottom: '-6px',
                                                            right: '-6px'}}>
                                                        <RemoveIcon 
                                                            style={{width:'22px', height:'22px',
                                                            color: 'white',
                                                            background: '#dd0000',
                                                            borderRadius: '50%'}}/>
                                                    </IconButton>
                                                </figure>
                                            )
                                        }
                                        </div>
                
                                        <div className="edit-set edit-set-send">
                                            <label htmlFor="allmember_send">
                                            <input className="boolean optional" type="checkbox" name="allmember_send" id="allmember_send" />全員に送信</label>
                                        </div>
                
                                        <div className="edit-set-mt5 edit-set-send">
                                            <label htmlFor="pickup_send">
                                            <input className="boolean optional" type="checkbox" name="pickup_send" id="pickup_send" />選んで送信</label>
                                        </div>
                
                                        <div className="checkbox-wrap edit-bg">
                                            <div className="checkbox">
                                                <label htmlFor="user_name01">
                                                <input className="boolean optional" type="checkbox" name="chk[]" id="user_name01" />田中 達也</label>
                                            </div>
                                            <div className="checkbox">
                                                <label htmlFor="user_name02">
                                                <input className="boolean optional" type="checkbox" name="chk[]" id="user_name02" />田中 達也</label>
                                            </div>
                                            <div className="checkbox">
                                                <label htmlFor="user_name03">
                                                <input className="boolean optional" type="checkbox" name="chk[]" id="user_name03" />田中 達也</label>
                                            </div>
                                            <div className="checkbox">
                                                <label htmlFor="user_name04">
                                                <input className="boolean optional" type="checkbox" name="chk[]" id="user_name04" />田中 達也</label>
                                            </div>
                                            <div className="checkbox">
                                                <label htmlFor="user_name05">
                                                <input className="boolean optional" type="checkbox" name="chk[]" id="user_name05" />田中 達也</label>
                                            </div>
                                        </div>
                
                                        <LoadingButton 
                                            type="submit" 
                                            loading={submit}
                                            className="btn-edit btn-default btn-h70 btn-r14 btn-yellow rounded-20"
                                            style={{fontSize:'18px',fontWeight:'bold', backgroundColor:'#ffed4a'}}>ミーティングを更新</LoadingButton>
                                        {
                                            _400error && <Alert type="fail" hide={()=>set400Error('')}>{_400error}</Alert>
                                        } 
                                        {
                                            _success && 
                                            <Alert type="success" 
                                            hide={()=>  
                                                history.push({
                                                pathname: `/admin/child/detail/${props.match.params?.child_id}`,
                                                state: {}
                                            })}>{_success}</Alert>
                                        }
                                    </form>
                                </div>     
                            </div>
                        </article>
                    }
                        
                    </div>
                    </div>
                </div>
            </div>
        </div>   
	)
}

export default MeetingEdit;