import React, { useState, useEffect } from 'react';
import AdminQuiz from './AdminQuiz'
import AdminSetting from './AdminSetting'

let QuizBtn = []
let QnAadmins
function Admin() {

    const [result, setresult] = useState('')
    const [QuizBtnCount, setQuizBtnCount] = useState(4)
    const [n, setn] = useState(0)


    function handleClickSetting() {
        setresult(<AdminSetting />)
    }

    function handleClickQuiz(e) {
        if (QnAadmins) {
            let QnAadmins = []
            setn(e.target.id)
            setresult(<AdminQuiz quizNum={e.target.id} fn={handleClickSave} data={QnAadmins} />)
        }
        else {
            setn(e.target.id)
            // let QnAadmins = []
            let QnAadmins = JSON.parse(localStorage.getItem('QnA'))
            console.log(QnAadmins)
            setresult(<AdminQuiz quizNum={e.target.id} fn={handleClickSave} data={QnAadmins[Number(n)]} />)
        }
    }

    function handleClickAddQuiz() {
        QuizBtn.push(
            <button type='button' className='btn btn-outline-primary' id={QuizBtnCount} onClick={handleClickQuiz}>Quiz {QuizBtnCount}</button>
        )
        setQuizBtnCount(QuizBtnCount + 1)
    }

    function handleClickSave(QnAadmin) {
        QnAadmins.splice(n, 0, QnAadmin)
        localStorage.setItem('QnA', JSON.stringify(QnAadmins))
    }

    return (
        <>
            <div className="container-fluid">
                <div className="row justify-content-md-center mt-5">
                    <div className="col-2 text-center border py-5 bg-white">
                        <div className="py-3 border-top border-bottom">
                            <h5 className="font-weight-bold">ADMIN</h5>
                        </div>
                        <div className="border-bottom py-4">
                            <button type="button" className="btn btn-info" onClick={handleClickSetting}>Setting</button>
                        </div>
                        <div className="btn-group-vertical py-4">
                            <button type="button" className="btn btn-outline-primary" id="1" onClick={handleClickQuiz}>Quiz 1</button>
                            <button type="button" className="btn btn-outline-primary" id="2" onClick={handleClickQuiz}>Quiz 2</button>
                            <button type="button" className="btn btn-outline-primary" id="3" onClick={handleClickQuiz}>Quiz 3</button>
                            {QuizBtn.map((element) => element)}
                            <button type="button" className="btn btn-outline-primary" onClick={handleClickAddQuiz}>+</button>

                        </div>
                    </div>

                    <div className="col-8 ">
                        {result}
                    </div>
                </div>
            </div>


        </>
    )
}

export default Admin