<?php echo view ('common/top_header_backdesk.php') ?>
<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"
    integrity="sha512-PT0RvABaDhDQugEbpNMwgYBCnGCiTZMh9yOzUsJHDgl/dMhD9yjHAwoumnUk3JydV3QTcIkNDuN40CJxik5+WQ=="
    crossorigin="anonymous" referrerpolicy="no-referrer" />
<link href="https://cdn.jsdelivr.net/npm/select2@4.1.0-rc.0/dist/css/select2.min.css" rel="stylesheet" />
<style>
    select.form-select, select.form-control, .form-control{outline: 1px solid #67676b;}
    select.form-select{color: #67676b}
    input[type="number"],input[type="date"],input[type="text"]{border-radius: 5px;padding: 5px 10px;}
    .error{color: red;text-align: right;width: 100%;padding-top: 15px;margin-bottom: 0px !important;}
</style>
</head>

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


<body>

    <div class="container-scroller">

        <!--Top navbar-->
        <?= view('common/top_navbar') ?>
        <!-- partial -->
        <div class="container-fluid page-body-wrapper">

            <!--sidenavbar starts-->
            <?= view('common/side_navbar') ?>
            <!--sidenavbar ends-->

            <div class="main-panel">
                <div class="content-wrapper">
                    <div class="card">
                        <form id="faculty_add_form" 
                                method="post" class="card-body" enctype="multipart/form-data">
                            <h4 class="card-title">Add Residents
                                <hr>
                            </h4>
                           
                            <div class="row">
                                <!-- Department Name -->
                                <div class="col-md-6">
                                <div class="form-group">
                                    <label class="font-weight-bold">Department</label>
                                    <select class="form-control" name="department_id" id="department_id">
                                        <option value="">Select</option>
                                        <?php
                                        if(!empty($departments)){
                                            foreach($departments as $row){
                                                ?><option value="<?php echo $row['id']?>"><?php echo $row['department_name']?></option><?php
                                            }
                                        }
                                        ?>
                                    </select>
                                </div>
                                </div>

                            </div>

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

                            </div>
                            <div class="row mt-3">
                                <div class="col-md-6">
                                 <div class="form-group">
                                    <label class="font-weight-bold">Image</label>
                                    <input type="file" id="image" name="image" class="form-control" required>
                                </div>
                                </div>

                            </div>
                            <div class="row mt-3">
                                <div class="col-md-6">
                                 <div class="form-group">
                                    <label class="font-weight-bold">Position</label>
                                    <input type="text" id="position" name="position" class="form-control" required>
                                </div>
                                </div>

                            </div>
                            <div class="row mt-3">
                                <div class="col-md-6">
                                 <div class="form-group">
                                    <label class="font-weight-bold">Registration No</label>
                                    <input type="text" id="registration_no" name="registration_no" class="form-control" required>
                                </div>
                                </div>

                            </div>
                          
                            <div class="row mt-3">
                                <div class="col-md-6">
                                 <div class="form-group">
                                    <label class="font-weight-bold">Publication</label>
                                    <input type="file" id="publication" name="publication" class="form-control" required>
                                </div>
                                </div>

                            </div>
                            <div class="row mt-3">
                                <div class="col-md-6">
                                 <div class="form-group">
                                    <label class="font-weight-bold">Services</label>
                                    <input type="file" id="services" name="services" class="form-control" required>
                                </div>
                                </div>

                            </div>

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

                    </div>
                    <!-- content-wrapper ends -->
                    <!-- partial:../../partials/_footer.html -->
                    <footer class="footer">
                        <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>
                    <!-- partial -->
                </div>
                <!-- main-panel ends -->
            </div>
            <!-- page-body-wrapper ends -->
        </div>
        <!-- container-scroller -->
        <?php echo view ('common/footer_backdesk.php') ?>

        <script>
           $(document).ready(function () {
                $('#faculty_add_form').on('submit', function (e) {
                    e.preventDefault();

                    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/departments/add-residents") ?>',
                        method: 'POST',
                        data: formData,
                        processData: false,
                        contentType: false,
                        headers: {
                            'Authorization': 'Bearer ' + jwtToken
                        },
                        success: function (response) {
                            if (response.status) {
                                toast('success', response.message || 'residents added successfully!');
                                window.location.href = '<?= base_url("backdesk/departments/residents-details") ?>';
                            } else {
                                toast('danger', response.message || 'Something went wrong!');
                            }
                        },
                        error: function (xhr) {
                            toast('danger', 'Server error: ' + xhr.statusText);
                        }
                    });
                });
            });

        </script>
</body>

</html>