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



const UnknownError = () => {
    const [email, setEmail] = useState('');
    const [text, setText] = useState('');

    const [errors, setErrors] = useState({})
    const [err_400, setErr400] = useState(false)

    const handleSubmit = (e) => {
        e.preventDefault();
        setErr400(false);
        if(!validateForm()) return;
        const formdata = new FormData();
        formdata.append('email', email);
        formdata.append('text', text);
        axios.post('/contacts/register/', formdata)
        .then(response => {
            if(response.status==200){
                window.location.href = '/contact-us/complete';
            }
        })
        .catch(err=>{setErr400(true)})
    }

    const validateForm = () => {
        let errors = {};
        let formIsValid = true;
        if (email.length == 0) {
          formIsValid = false;
          errors["email"] = true;
        }
        else {
          //regular expression for email validation
            var pattern = new RegExp(/^(("[\w-\s]+")|([\w-]+(?:\.[\w-]+)*)|("[\w-\s]+")([\w-]+(?:\.[\w-]+)*))(@((?:[\w-]+\.)*\w[\w-]{0,66})\.([a-z]{2,6}(?:\.[a-z]{2})?)$)|(@\[?((25[0-5]\.|2[0-4][0-9]\.|1[0-9]{2}\.|[0-9]{1,2}\.))((25[0-5]|2[0-4][0-9]|1[0-9]{2}|[0-9]{1,2})\.){2}(25[0-5]|2[0-4][0-9]|1[0-9]{2}|[0-9]{1,2})\]?$)/i);
            if (!pattern.test(email)) {
                formIsValid = false;
                errors["email"] = true;
            }
            else {
                errors['email'] = false;
            }
        }
        if(text.length == 0){
            formIsValid = false;
            errors['text'] = true;
        }
        else errors['text'] = false;
        setErrors(errors);
        return formIsValid;
    }

	return (
        <form onSubmit={handleSubmit} noValidate>
            <p className="text-center font-weight-bold ft-20">不明なエラー</p>

            <span className="mb-4 mt-50-px  l-alert__text--red ft-16 ft-md-14 ">
                不明なエラーが発生しました。<br/>
                お手数ですが以下から詳細をお問い合わせくださ いませ。
            </span> 
            
            {
                err_400 && 
                <span className="my-4 l-alert__text--red ft-16 ft-md-14">エラーメッセージ</span> 
            }
            <div className="c-input mt-4">
                <label htmlFor="email"   className="c-input__label"> メールアドレス </label>
                {
                    errors['email'] ?
                        <input type="email" name="email" id="email" className="w-100 c-input__edit is-invalid c-input__target"  value={email} onChange={e=>setEmail(e.target.value)} autoFocus/>
                    :<input type="email" name="email" id="email" className="w-100 c-input__edit c-input__target"  value={email} onChange={e=>setEmail(e.target.value)} autoFocus/>
                }
                {
                    errors['email'] && 
                        <span className="l-alert__text--red ft-16 ft-md-14">メールアドレスの形式がありません。</span> 
                }
            </div>

            <div className="c-input mt-4">
                <label htmlFor="text"   className="c-input__label"> お問合せ内容 </label>
                {
                    errors['text'] ? 
                        <textarea name="text" id="text" rows="7" className="w-100 c-input__edit is-invalid c-input__target"  value={text} onChange={e=>setText(e.target.value)}/>
                    :<textarea name="text" id="text" rows="7" className="w-100 c-input__edit c-input__target"  value={text} onChange={e=>setText(e.target.value)}/>
                }
                {
                    errors['text'] && 
                        <span className="l-alert__text--red ft-16 ft-md-14">テキストの内容を入力してください。</span> 
                }
            </div>
            <div className="mt-3">
                <LoadingButton type="submit" fullWidth className="p-3 rounded-20 ft-15 font-weight-bold text-black bg-color-2">送信</LoadingButton>
            </div>
        </form>
	)
}

if(document.getElementById('unknown-error')){
	ReactDOM.render(
		<UnknownError />,
		document.getElementById('unknown-error')
	)
}