import React, { useEffect, useState } from 'react';
import { useHistory } from 'react-router-dom'
import moment from 'moment';
import axios from 'axios';

import ModalConfirm from '../../component/modal_confirm';
import ModalAlert from '../../component/modal_alert';

import { LoadingButton } from '@material-ui/lab';

import { CircularProgress  } from '@material-ui/core';
import Alert from '../../component/alert';

import Button from '@mui/material/Button';
import Dialog from '@mui/material/Dialog';
import DialogActions from '@mui/material/DialogActions';
import DialogContent from '@mui/material/DialogContent';
import DialogContentText from '@mui/material/DialogContentText';
import DialogTitle from '@mui/material/DialogTitle';
import Slide from '@mui/material/Slide';

const Transition = React.forwardRef(function Transition(props, ref) {
  return <Slide direction="up" ref={ref} {...props} />;
});



const MeetingDetail = (props) => {

  const history = useHistory();
  const [open, setOpen] = useState(false);
  const [loaded, setLoaded] = useState(false);
  const [submit, setSubmit] = useState(false);
  const [meeting, setMeeting] = useState(null);

  const [_400error, set400error] = useState('');
  const [_success, setSuccess] = useState('');
  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);
        setMeeting(response.data.params[0]);
      } else if(response.data.status_code==400){
        //TODO
      }
    });
  }, []);

  async function openModal() {
    setOpen(true);
  };

  async function closeModal() {
    setOpen(false);
  };

  async function handleAccept() {
    try {
      setSubmit(true);
      axios.delete(`/api/admin/meetings/delete/${props.match.params?.meeting_id}`)
        .then(response => {
          closeModal();
          setSubmit(false);
          if(response.data.status_code == 200){
              setSuccess('ミーティングの削除に成功しました！');
          } else {
              set400error("ミーティングの削除に失敗しました。");
          }
        });
    } catch (error) {
      console.log('error', error);
    }
  };


	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 && 
                (
                  meeting ? 
                <article className="p-article__body">
                  <div className="p-article__content">       
                    <div className="meeting-member">
                      <div className="meeting-member-wrap">
                        <div data-url="login.html" className="meeting-member-link">
                          <ul className="meeting-member-count">
                            <li className="numerator">3</li>
                            <li className="denominator">4</li>
                          </ul>
  
                          <ul className="meeting-member-list" role="list">
                            <li className="meeting-member__item" role="listitem">
                              <div className="avatar">
                                <img alt="name" className="avatar-img" src="/assets/img/avatar/avatar-sample01@2x.png" />
                              </div>
                            </li>
                            <li className="meeting-member__item" role="listitem">
                              <div className="avatar">
                                <img alt="name" className="avatar-img" src="/assets/img/avatar/avatar-sample02@2x.png" />
                              </div>
                            </li>
                            <li className="meeting-member__item" role="listitem">
                              <div className="avatar">
                                <img alt="name" className="avatar-img" src="/assets/img/avatar/avatar-sample03@2x.png" />
                              </div>
                            </li>
                          </ul>

                          <div className="meeting-member__read">
                            <p>3人既読</p>
                          </div>
                        </div>
                      </div>
                    </div>
                    <h3 className="meeting-ttl">{ meeting.title }</h3>
                    <time dateTime="2021-07-30" className="meeting-time">
                      <span className="meeting-date">{ moment(meeting.updated_at).format('YYYY/MM/DD') || '' }</span>
                    </time>
                    <ul className="p-article-btn-list">
                      <li className="p-article__pdf__btn">
                        <a 
                          onClick={e => {
                            e.preventDefault();
                            history.push({
                              pathname: `/admin/meeting/edit/${props.match.params?.meeting_id}`,
                              state: {}
                            });
                          }} 
                          className="btn-default btn-yellow btn-pdf btn-r8 btn-h48">
                          編集
                        </a>
                      </li>
                      <li className="p-article__pdf__btn mr-0">
                        <a onClick={openModal} className="btn-default btn-yellow btn-pdf btn-r8 btn-h48">削除</a>
                      </li>
                    </ul>
                   
                    <div className="p-article__context">
  
                      <div className="p-file-list">
                        <div className="p-file-for">
                          <figure><img src="../../../assets/img/dummy/post-dummy01.jpg" alt="" /></figure>
                        </div>
                        <div className="p-file-nav">
                          <figure><img src="../../../assets/img/dummy/post-dummy01.jpg" alt="" /></figure>
                          <figure><img src="../../../assets/img/dummy/post-dummy02.jpg" alt="" /></figure>
                          <figure><img src="../../../assets/img/dummy/post-dummy03.jpg" alt="" /></figure>
                          <figure><img src="../../../assets/img/dummy/post-dummy04.jpg" alt="" /></figure>
                          <figure><img src="../../../assets/img/dummy/post-dummy05.jpg" alt="" /></figure>
                          <figure><img src="../../../assets/img/dummy/post-dummy01.jpg" alt="" /></figure>
                          <figure><img src="../../../assets/img/dummy/post-dummy02.jpg" alt="" /></figure>
                          <figure><img src="../../../assets/img/dummy/post-dummy03.jpg" alt="" /></figure>
                          <figure><img src="../../../assets/img/dummy/post-dummy04.jpg" alt="" /></figure>
                          <figure><img src="../../../assets/img/dummy/post-dummy05.jpg" alt="" /></figure>
                          <figure><img src="../../../assets/img/dummy/post-dummy03.jpg" alt="" /></figure>
                          <figure><img src="../../../assets/img/dummy/post-dummy04.jpg" alt="" /></figure>
                          <figure><img src="../../../assets/img/dummy/post-dummy05.jpg" alt="" /></figure>
                        </div>
                      </div>
  
                      <div className="p-article__pdf">
                        <div className="p-article__pdf__btn">
                          <a data-v-ade1d018="" className="btn-default btn-yellow btn-pdf btn-r8 btn-h52">
                            <span>PDFを確認する</span>
                          </a>
                        </div>
                        <button type="button" aria-label="お気に入り" data-tooltip="お気に入り" aria-pressed="false" className="icon a-icon text-icon icon-text icon-star-wrap a-icon-size_medium" style={{border:'1px solid #f0de00'}}>
                        </button>
                      </div>
                   
                      <p className="p-article__txt">{ meeting.text }</p>
                    </div>
                  </div>
                </article>
                : <p className="text-center pt-5">データが存在していません。</p>
                )
              }
           
              </div>
            </div>
          </div>
        </div>
        {/* <ModalConfirm 
          show={show} 
          message={"本当に削除しても\nよろしいでしょうか？"}
          handleClose={handleClose} 
          handleAccept={handleAccept} 
        /> */}
        <Dialog
            open={open}
            TransitionComponent={Transition}
            keepMounted
            aria-describedby="alert-dialog-slide-description"
        >
            <DialogContent style={{width:'290px', padding:'25px 10px 10px 10px'}}>
                <DialogContentText id="alert-dialog-slide-description" style={{fontSize:'16px', textAlign:'center'}}>
                  本当に削除しても<br/>よろしいでしょうか？
                </DialogContentText>
            </DialogContent>
            <DialogActions style={{justifyContent:'space-evenly', padding:'0 20px 20px 20px'}}>
                <Button 
                  onClick={closeModal} 
                  size="large" 
                  style={{fontSize:'16px'}}>いいえ</Button>
                
                <LoadingButton 
                  variant="text"
                  onClick={handleAccept}
                  loading={submit} 
                  size="large" 
                  style={{fontSize:'16px'}}>はい</LoadingButton>
            </DialogActions>
        </Dialog>
        {
          _400error && <Alert type="fail" hide={()=>set400error('')}>{_400error}</Alert>
        } 
        {
          _success && 
            <Alert type="success" 
              hide={()=>  
                    history.push({
                      pathname: "/admin/meeting",
                      state: {}
                    })}>{_success}</Alert>
        }
    </div>  
	)
}

export default MeetingDetail;