<?php echo view('common/top_header_backdesk.php') ?>

<!-- External CSS -->
<link href="https://cdn.jsdelivr.net/npm/select2@4.1.0-rc.0/dist/css/select2.min.css" rel="stylesheet" />
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/datatables/1.10.21/css/dataTables.bootstrap4.min.css" />

<style>
    select.form-control, .form-control {
        outline: 1px solid #67676b;
        border-radius: 5px;
        color: #67676b;
    }
    input[type="text"], input[type="email"], input[type="file"], select {
        padding: 5px 10px;
    }
    .error {
        color: red;
        text-align: right;
        width: 100%;
        padding-top: 15px;
        margin-bottom: 0 !important;
    }
</style>
</head>

<?php $session = session(); ?>

<body>
<div class="container-scroller">
    <?= view('common/top_navbar') ?>
    <div class="container-fluid page-body-wrapper">
        <?= view('common/side_navbar') ?>
        <div class="main-panel">
            <div class="content-wrapper">
                <div class="card">
                    <form id="team_add_form" method="post" class="card-body" enctype="multipart/form-data">
                        <h4 class="card-title">Add Team Member</h4>
                        <hr>

                        <!-- Name -->
                        <div class="row">
                            <div class="col-md-6">
                                <div class="form-group">
                                    <label class="font-weight-bold">Name</label>
                                    <input type="text" name="name" class="form-control" required />
                                </div>
                            </div>

                            <!-- Designation -->
                            <div class="col-md-6">
                                <div class="form-group">
                                    <label class="font-weight-bold">Designation</label>
                                    <input type="text" name="designation" class="form-control" required />
                                </div>
                            </div>
                        </div>

                        <!-- Email -->
                        <div class="row mt-3">
                            <div class="col-md-6">
                                <div class="form-group">
                                    <label class="font-weight-bold">Email</label>
                                    <input type="email" name="email" class="form-control" required />
                                </div>
                            </div>

                            <!-- Image -->
                            <div class="col-md-6 ">
                                <div class="form-group">
                                    <label class="font-weight-bold">Image <small>(JPG, PNG)</small></label>
                                    <input type="file" name="image" class="form-control" accept="image/*" required />
                                </div>
                            </div>
                        </div>

                        <!-- Description -->
                        <div class="row mt-3">
                            <div class="col-md-12">
                                <div class="form-group">
                                    <label class="font-weight-bold">Short Description</label>
                                    <textarea name="small_description" id="small_description" class="form-control" rows="6"></textarea>
                                </div>
                            </div>
                        </div>

                        <!-- Submit -->
                        <button type="submit" class="btn btn-success btn-sm mt-4">
                            <i class="mdi mdi-table-row-plus-after"></i> &nbsp;Add
                        </button>
                    </form>
                </div>

                <!-- Footer -->
                <footer class="footer mt-4">
                    <div class="d-sm-flex justify-content-center justify-content-sm-between">
                        <span class="text-muted text-center text-sm-left d-block d-sm-inline-block">
                            Copyright © <?= date('Y') . ' ' . COMPANY_FULL_NAME ?>. All rights reserved.
                        </span>
                        <span class="float-none float-sm-right d-block mt-1 mt-sm-0 text-center">
                            With gratitude from <i class="mdi mdi-heart text-danger"></i>
                            <a href="<?= CREDIT_LINK ?>" target="_blank"><?= CREDIT_TITLE ?></a>
                        </span>
                    </div>
                </footer>
            </div>
        </div>
    </div>
</div>

<!-- Scripts -->
<?= view('common/footer_backdesk.php') ?>
<script src="https://cdn.jsdelivr.net/npm/select2@4.1.0-rc.0/dist/js/select2.min.js"></script>
<script>
    let descriptionEditor;

    $(document).ready(function () {
        // Initialize Trumbowyg on #small_description textarea
        $('#small_description').trumbowyg({
            btns: [['viewHTML'], ['formatting'], 'bold', 'italic', 'underline'],
            autogrow: true,
            resetCss: true,
            semantic: false
        });
        descriptionEditor = $('#small_description');

        $('#team_add_form').on('submit', function (e) {
            e.preventDefault();

            // Get HTML content from Trumbowyg
            const descriptionData = descriptionEditor.trumbowyg('html');
            $('textarea[name="small_description"]').val(descriptionData);

            const csrfTokenName = '<?= csrf_token() ?>';
            const csrfTokenValue = '<?= csrf_hash() ?>';
            const jwtToken = localStorage.getItem('jwt_token');

            if (!jwtToken) {
                toast('danger', 'Unauthorized. Token missing.');
                return;
            }

            const formData = new FormData(this);
            formData.append(csrfTokenName, csrfTokenValue);

            $.ajax({
                url: '<?= base_url("api/library/add-team") ?>',
                method: 'POST',
                data: formData,
                processData: false,
                contentType: false,
                headers: {
                    'Authorization': 'Bearer ' + jwtToken
                },
                success: function (response) {
                    if (response.status) {
                        toast('success', response.message || 'Team member added successfully!');
                        setTimeout(() => {
                            window.location.href = '<?= base_url("backdesk/library/team") ?>';
                        }, 1500);
                    } else {
                        toast('danger', response.message || 'Something went wrong!');
                    }
                },
                error: function (xhr) {
                    toast('danger', 'Server error: ' + xhr.statusText);
                }
            });
        });
    });
</script>

</body>
</html>
