import React, { useState } from 'react';
import { Table, Row, Col, Nav, Tab, Button, Form, InputGroup, Dropdown, DropdownButton, Modal, Spinner } from 'react-bootstrap';
import useTopics from '../hooks/useTopics';
import ModalOperation from '../../../../components/ui/adminMessageModal/ModalOperation';
import TablePaginate from '../../../../components/ui/TablePaginate';

const ManageTopics = () => {
    const { 
        topics, 
        sections,
        sectionSlug,
        loading, 
        loadingSlug,
        error, 
        selectedTopic, 
        formData, 
        show,
        showLoader,
        loaderState,
        createTopic, 
        editTopic, 
        deleteTopic,
        handleFormChange,
        resetForm,
        handleShow,
        handleClose,
        handleShowLoader,
        handleCloseLoader,
        setLoaderState,
        
        page, 
        totalData,
        totalPages,
        setSearch,
        handlePageChange,
        handleLimitsChange,
        handleSerachChange
    } = useTopics();

    if (error) {
        <>{error}</>
    }

    return (
        <div className='admin-teachers'>
            <h2 className='page-title strip'>Topics</h2>
            <p className='page-desc'>Description of Topics.</p>
            <Tab.Container id="left-tabs-example" defaultActiveKey="detail" onSelect={resetForm}>
                <Row className='mb-45'>
                    <Col xs={12}>
                        <Nav variant="pills" className='col-tabs'>
                            <Nav.Item>
                                <Nav.Link eventKey="detail">View Entries</Nav.Link>
                            </Nav.Item>
                            <Nav.Item>
                                <Nav.Link eventKey="create">Create Data</Nav.Link>
                            </Nav.Item>
                        </Nav>
                    </Col>
                </Row>
                <Row className='mb-45'>
                    <Col xs={12} className='col-tabs-content'>
                        <Tab.Content>
                            <Tab.Pane eventKey="detail">
                                <div className="cards">
                                    <div className="cards-title">
                                        <h4>Topic List</h4>
                                    </div>
                                    <div className="cards-body">
                                        <Form className="mb-3 d-flex align-items-strech" onSubmit={(e) => { e.preventDefault(); handleSerachChange(); }}>
                                            <Form.Control type='search'
                                                aria-label="Large"
                                                aria-describedby="inputGroup-sizing-sm"
                                                placeholder='Search'
                                                className='table-input-search mb-0 me-2 rounded-3'
                                                onChange={(e) => { setSearch(e.target.value); }}
                                            />
                                            <Button type='submit' variant='blue rounded-3'>Search</Button>
                                        </Form>
                                        <Table hover>
                                            <thead>
                                                <tr>
                                                    <th>No</th>
                                                    <th>Section</th>
                                                    <th>Topic</th>
                                                    <th>Description</th>
                                                    <th className='text-center'>Action</th>
                                                </tr>
                                            </thead>
                                            <tbody>
                                                {loading?(
                                                    <tr>
                                                        <td colSpan={5} style={{height:"20vh"}}>
                                                            <Spinner animation="grow" variant="primary" />
                                                            <Spinner animation="grow" variant="secondary" />
                                                            <Spinner animation="grow" variant="success" />
                                                            <Spinner animation="grow" variant="danger" />
                                                            <Spinner animation="grow" variant="warning" />
                                                            <Spinner animation="grow" variant="info" />
                                                        </td>
                                                    </tr>
                                                ):(
                                                    topics.length > 0 ?(
                                                        topics.map((topic, index) => (
                                                            <tr key={index}>
                                                                <td>{index + 1}</td>
                                                                <td>{sectionSlug[topic.ID_SECTION]}</td>
                                                                <td>{topic.NAME_TOPIC}</td>
                                                                <td>{topic.DESCRIPTION_TOPIC}</td>
                                                                <td className='text-center action-col'>
                                                                    <div className="d-flex">
                                                                        <Button size='sm' className='btn-edit' onClick={() => handleShow(topic)}>
                                                                            <i className="bi bi-pencil-square"></i>
                                                                        </Button>
                                                                        <Button size='sm' className='btn-delete' onClick={() => deleteTopic(topic.ID_TOPIC)}>
                                                                            <i className="bi bi-trash3"></i>
                                                                        </Button>
                                                                    </div>
                                                                </td>
                                                            </tr>
                                                        ))
                                                    ):(
                                                        <tr>
                                                            <td colSpan={5} style={{height:'20vh'}}>
                                                                <h3>Empty Data</h3>
                                                            </td>
                                                        </tr>
                                                    )
                                                )}
                                            </tbody>
                                        </Table>
                                        <div className="mt-2 w-100 d-flex justify-content-between align-items-center">
                                            <div className='d-flex align-items-center'>
                                                <small className="me-2">Item per page</small>
                                                <Form.Select 
                                                    size='sm' 
                                                    className='py-0 px-1 me-2' 
                                                    aria-label="Default select example" 
                                                    defaultValue='7' 
                                                    onChange={handleLimitsChange}
                                                    style={{ width: '50px' }} 
                                                >
                                                    <option value="7">7</option>
                                                    <option value="10">10</option>
                                                    <option value="20">20</option>
                                                </Form.Select>
                                                <small>of {totalData}</small>
                                            </div>
                                            <TablePaginate
                                                totalPages={totalPages}
                                                currentPage={page}
                                                onPageChange={handlePageChange}
                                            />
                                        </div>
                                    </div>
                                </div>
                            </Tab.Pane>
                            <Tab.Pane eventKey="create">
                                <div className="cards">
                                    <div className="cards-title">
                                        <h4>Add Topic Data</h4>
                                    </div>
                                    <div className="cards-body">
                                        <Form onSubmit={(e) => { e.preventDefault(); createTopic(); }}>
                                            <Row> 
                                                <Form.Group as={Col} controlId="formGridSection" className='mb-3 col-12 col-md-6'>
                                                    <Form.Label>Section<sup className='text-red fw-bold'>*</sup></Form.Label>
                                                    <InputGroup className="mb-2 input-group-icon">
                                                        <InputGroup.Text id="basic-addon1"><i className="bi bi-book"></i></InputGroup.Text>
                                                        <Form.Select 
                                                            name="sectionId"
                                                            defaultValue={formData.sectionId || ''}
                                                            onChange={handleFormChange}
                                                            required
                                                        >
                                                            <option value="" disabled hidden>Select Section</option>
                                                            {loadingSlug?(
                                                                <></>
                                                            ):(
                                                                sections.map((data) => (
                                                                    <option key={data.ID_SECTION} value={data.ID_SECTION}>{data.NAME_SECTION}</option>
                                                                ))
                                                            )}
                                                        </Form.Select>
                                                    </InputGroup>
                                                </Form.Group>
                                            </Row>
                                            <Row className="mb-2">
                                                <Form.Group as={Col} controlId="formGridName" className='col-12 col-md-6'>
                                                    <Form.Label>Topic Name<sup className='text-red fw-bold'>*</sup></Form.Label>
                                                    <InputGroup className="mb-2 input-group-icon">
                                                        <InputGroup.Text id="basic-addon1"><i className="bi bi-card-list"></i></InputGroup.Text>
                                                        <Form.Control
                                                            name="topicName"
                                                            placeholder="Enter Topic Name"
                                                            value={formData.topicName || ''}
                                                            onChange={handleFormChange}
                                                        />
                                                    </InputGroup>
                                                </Form.Group>
                                            </Row>
                                            <Row className="mb-2">
                                                <Form.Group as={Col} controlId="formGridDescription">
                                                    <Form.Label>Topic Description<sup className='text-red fw-bold'>*</sup></Form.Label>
                                                    <Form.Control 
                                                        name="description"
                                                        className='mb-2' 
                                                        as="textarea" 
                                                        rows={3} 
                                                        placeholder='Type the topic description here...' 
                                                        value={formData.description || ''}
                                                        onChange={handleFormChange}
                                                    />
                                                </Form.Group>
                                            </Row>
                                            <div className="d-flex justify-content-end">
                                                <Button variant="outline-blue" type="reset" className='ms-auto py-2 rounded-35' onClick={resetForm}>
                                                    reset
                                                </Button>
                                                <Button variant="blue" type="submit" className='ms-2 py-2 px-5 rounded-35'>
                                                    Add
                                                </Button>
                                            </div>
                                        </Form>
                                    </div>
                                </div>
                            </Tab.Pane>
                        </Tab.Content>
                    </Col>
                </Row>
            </Tab.Container>

            <Modal show={show} onHide={handleClose} className='modal-admin' size='lg' centered>
                <Modal.Header closeButton>
                    <Modal.Title>Update topic Data</Modal.Title>
                </Modal.Header>
                <Modal.Body>
                    <Form onSubmit={(e) => { e.preventDefault(); editTopic(); }}>
                        <Row className="mb-2">
                            <Form.Group as={Col} controlId="updateGridSection" className='col-12 col-md-6'>
                                <Form.Label>Section<sup className='text-red fw-bold'>*</sup></Form.Label>
                                <InputGroup className="mb-2 input-group-icon">
                                    <InputGroup.Text id="basic-addon1"><i className="bi bi-book"></i></InputGroup.Text>
                                    <Form.Select 
                                        name="sectionId"
                                        defaultValue={formData.sectionId || ''}
                                        onChange={handleFormChange}
                                        required
                                    >
                                        <option value="" disabled hidden>Select Section</option>
                                        {loadingSlug?(
                                            <></>
                                        ):(
                                            sections.map((data) => (
                                                <option key={data.ID_SECTION} value={data.ID_SECTION}>{data.NAME_SECTION}</option>
                                            ))
                                        )}
                                    </Form.Select>
                                </InputGroup>
                            </Form.Group>
                            <Form.Group as={Col} controlId="updateGridName">
                                <Form.Label>Topic Name<sup className='text-red fw-bold'>*</sup></Form.Label>
                                <InputGroup className="mb-2 input-group-icon">
                                    <InputGroup.Text id="basic-addon1"><i className="bi bi-book"></i></InputGroup.Text>
                                    <Form.Control
                                        name="topicName"
                                        placeholder="Enter Topic Name"
                                        value={formData.topicName || ''}
                                        onChange={handleFormChange}
                                    />
                                </InputGroup>
                            </Form.Group>
                        </Row>
                        <Row className="mb-2">
                            <Form.Group as={Col} controlId="updateGridDescription">
                                <Form.Label>Topic Description<sup className='text-red fw-bold'>*</sup></Form.Label>
                                <Form.Control 
                                    name="description"
                                    className='mb-2' 
                                    as="textarea" 
                                    rows={3} 
                                    placeholder='Type the topic description here...' 
                                    value={formData.description || ''}
                                    onChange={handleFormChange}
                                />
                            </Form.Group>
                        </Row>
                        <div className="d-flex justify-content-end">
                            <Button variant="blue" type="submit" className='py-2 px-5 w-100 rounded-35'>
                                Update
                            </Button>
                        </div>
                    </Form>
                </Modal.Body>
            </Modal>

            <ModalOperation
                show={showLoader}
                handleClose={handleCloseLoader}
                title={loaderState.title}
                description={loaderState.description}
                loading={loaderState.loading}
                successMessage={loaderState.successMessage}
                confirmAction={loaderState.confirmAction}
                handleConfirm={loaderState.handleConfirm}
            />
        </div>
    );
};

export default ManageTopics;
