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

const ManageSections = () => {
    const { 
        sections,
        selectedImage,
        loading, 
        error, 
        selectedSection, 
        formData, 
        show,
        showLoader,
        loaderState,
        createSection, 
        editSection, 
        deleteSection,
        handleFormChange,
        handleImageChange,
        resetForm,
        handleShow,
        handleClose,
        handleShowLoader,
        handleCloseLoader,
        setLoaderState,
        thumbPath,
        
        page, 
        totalData,
        totalPages,
        setSearch,
        handlePageChange,
        handleLimitsChange,
        handleSerachChange
    } = useSections();

    return (
        <div className='admin-teachers'>
            <h2 className='page-title strip'>Sections</h2>
            <p className='page-desc'>Description of Sectionss.</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>Section 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>Section 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>
                                                ):(
                                                    sections.length > 0 ?(
                                                        sections.map((section, index) => (
                                                            <tr key={section.ID_SECTION}>
                                                                <td>{index + 1}</td>
                                                                <td>{section.NAME_SECTION}</td>
                                                                <td>{section.DESCRIPTION_SECTION}</td>
                                                                <td className='text-center action-col'>
                                                                    <Button size='sm' className='btn-edit' onClick={() => handleShow(section)}>
                                                                        <i className="bi bi-pencil-square"></i>
                                                                    </Button>
                                                                    <Button size='sm' className='btn-delete' onClick={() => deleteSection(section.ID_SECTION)}>
                                                                        <i className="bi bi-trash3"></i>
                                                                    </Button>
                                                                </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 Student Data</h4>
                                    </div>
                                    <div className="cards-body">
                                        <Form onSubmit={(e) => { e.preventDefault(); createSection(); }}>
                                            <Row className="mb-2">
                                                <Form.Group as={Col} controlId="formGridName">
                                                    <Form.Label>Section 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
                                                            placeholder="Enter Section Name"
                                                            name="sectionName"
                                                            value={formData.sectionName || ''}
                                                            onChange={handleFormChange}
                                                        />
                                                    </InputGroup>
                                                </Form.Group>
                                            </Row>
                                            <Row className="mb-2">
                                                <Form.Group as={Col} controlId="formGridDescription">
                                                    <Form.Label>Section Description<sup className='text-red fw-bold'>*</sup></Form.Label>
                                                    <Form.Control
                                                        name="description"
                                                        className='mb-2' 
                                                        as="textarea" 
                                                        rows={3} 
                                                        placeholder='Type the section description here...' 
                                                        value={formData.description || ''}
                                                        onChange={handleFormChange} 
                                                    />
                                                </Form.Group>
                                            </Row>
                                            <Row className="mb-2">
                                                <Form.Group as={Col} controlId="formGridThumbnail" className='mb-3 col-12 col-md-6'>
                                                    <Form.Label>Thumbnail<sup className='text-red fw-bold'>*</sup></Form.Label><br/>
                                                    <img
                                                        src={selectedImage ? URL.createObjectURL(selectedImage) : " "}
                                                        alt="thumbnail"
                                                        className={`mb-2 ${selectedImage ? "d-block" : "d-none"}`}
                                                        style={{width:"30%"}}
                                                    />
                                                    <InputGroup className="mb-2 input-group-icon">
                                                        <InputGroup.Text id="basic-addon1" className='border-dashed'><i className="bi bi-cloud-arrow-up"></i></InputGroup.Text>
                                                        <Form.Control type='file' accept="image/*"
                                                            placeholder="Choose Image"
                                                            aria-label="fullname"
                                                            aria-describedby="basic-addon1"
                                                            className='border-dashed'
                                                            onChange={handleImageChange}
                                                        />
                                                    </InputGroup>
                                                    <small>Ensure the file size is no larger than 5 MB</small>
                                                </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 Teacher Data</Modal.Title>
                </Modal.Header>
                <Modal.Body>
                    <Form onSubmit={(e) => { e.preventDefault(); editSection(); }}>
                        <Row className="mb-2">
                            <Form.Label>Thumbnail<sup className='text-red fw-bold'>*</sup></Form.Label>
                            <Col className='col-12'>
                                <img
                                    src={selectedImage ? URL.createObjectURL(selectedImage) : formData.thumbnail ? `${thumbPath}${formData.thumbnail}` : " "}
                                    alt="thumbnail"
                                    className='mb-2'
                                    style={{width:"30%"}}
                                />
                            </Col>
                            <Form.Group as={Col} controlId="updateGridDescription" className='col-12 col-md-6 mb-2'>
                                <InputGroup className="input-group-icon">
                                    <InputGroup.Text id="basic-addon1" className='border-dashed'><i className="bi bi-cloud-arrow-up"></i></InputGroup.Text>
                                    <Form.Control type='file'
                                        placeholder="Choose Image"
                                        aria-label="fullname"
                                        aria-describedby="basic-addon1"
                                        className='border-dashed'
                                        onChange={handleImageChange}
                                    />
                                </InputGroup>
                                <small>Ensure the file size is no larger than 5 MB</small>
                            </Form.Group>
                        </Row>
                        <Row className="mb-2">
                            <Form.Group as={Col} controlId="updateGridName" className='col-12 col-md-6'>
                                <Form.Label>Section 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
                                        placeholder="Enter Section Name"
                                        name="sectionName"
                                        value={formData.sectionName || ''}
                                        onChange={handleFormChange}
                                    />
                                </InputGroup>
                            </Form.Group>
                        </Row>
                        <Row className="mb-2">
                            <Form.Group as={Col} controlId="updateGridThumbnail">
                                <Form.Label>Section Description<sup className='text-red fw-bold'>*</sup></Form.Label>
                                <Form.Control 
                                    name="description"
                                    className='mb-2' 
                                    as="textarea" 
                                    rows={3} 
                                    placeholder='Type the section 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 ManageSections;
