<?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" crossorigin="anonymous" />
<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: 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="paper_edit_form" method="post" class="card-body" enctype="multipart/form-data">
                            <h4>Edit Paper</h4><hr>

                            <input type="hidden" name="id" value="<?= esc($paper['id']) ?>">

                            <div class="row">
                                <div class="col-md-6">
                                    <div class="form-group">
                                        <label class="font-weight-bold">Paper Name</label>
                                        <input type="text" name="paper_name" value="<?= esc($paper['paper_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">Paper Type</label>
                                        <input type="text" name="paper_type" value="<?= esc($paper['paper_type']) ?>" class="form-control" required>
                                    </div>
                                </div>
                            </div>
                             <div class="row mt-3">
                                <!-- Course Name -->
                                <div class="col-md-6">
                                    <div class="form-group">
                                        <label class="font-weight-bold">Calender Year</label>
                                        <input type="text" id="calender_year" name="calender_year" value="<?= esc($paper['calender_year']) ?>" class="form-control" required>
                                    </div>
                                </div>
                            </div>
                            <div class="row mt-3">
                                <!-- Course Name -->
                                <div class="col-md-6">
                                    <div class="form-group">
                                        <label class="font-weight-bold">Prof. Year</label>
                                        <input type="text" id="prof_year" name="prof_year" value="<?= esc($paper['prof_year']) ?>" 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">Attachment</label>
                                        <?php if (!empty($paper['file'])): ?>
                                            <p>
                                                <a href="<?= base_url($paper['file']) ?>" target="_blank" class="badge badge-info">
                                                    <i class="mdi mdi-file-document"></i> View Current File
                                                </a>
                                            </p>
                                        <?php endif; ?>
                                        <input type="file" name="paper_attachment" class="form-control-file">
                                        <small class="form-text text-muted">Upload to replace existing file (PDF/DOC/ZIP max 10MB)</small>
                                    </div>
                                </div>
                            </div>

                            <button type="submit" class="btn btn-success mt-3">Update</button>
                        </form>
                    </div>

                    <footer class="footer">
                        <div class="d-sm-flex justify-content-center justify-content-sm-between">
                            <span class="text-muted">Copyright © <?= date('Y') . ' ' . COMPANY_FULL_NAME ?></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>

    <?= 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>
        $(document).ready(function () {
            $('#paper_edit_form').on('submit', function (e) {
                e.preventDefault();
                const jwtToken = localStorage.getItem('jwt_token');
                const csrfTokenName = '<?= csrf_token() ?>';
                const csrfTokenValue = '<?= csrf_hash() ?>';

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

                $.ajax({
                    url: '<?= base_url('api/courses/update-paper') ?>',
                    method: 'POST',
                    data: formData,
                    processData: false,
                    contentType: false,
                    headers: {
                        'Authorization': 'Bearer ' + jwtToken
                    },
                    success: function (response) {
                        if (response.status) {
                            toast('success', response.message || 'Paper updated successfully!');
                            window.location.href = '<?= base_url('backdesk/courses') ?>';
                        } else {
                            toast('danger', response.message || 'Something went wrong!');
                        }
                    },
                    error: function (xhr) {
                        toast('danger', xhr.responseText || 'Error occurred!');
                    }
                });
            });
        });
    </script>
</body>
</html>
