<?= $this->element('page_top', array('title' => __('Add'), 'permissions' => $user_permission)); ?>

<div class="container-fluid box box-primary">
    <?= $this->Form->create(null, ['id' => 'course_form', 'class' => 'input-form-section']) ?>
    <!-- /.box-header -->
    <div class="box-body">
        <div class="col-12 ">
            <h4 class="text-center rounded text-white fw-bold pt-1 pb-1 hd-line-height"
                style="background:#ccc">
                <b>Course Information</b>
            </h4>
        </div>
        <div class="row form-group">
            <div class="col-md-6 <?php if (isset($errors['name'])) {
                echo 'has-error';
            } ?>">
                <label class="control-label required">Course Name</label>
                <?php echo $this->Form->input('name', ['label' => false, ['class' => 'required'], 'placeholder' => 'Course Title', 'class' => 'form-control', 'required' => false]); ?>
                <span class="help-block text-center">
                                                    <?php
                                                    if (isset($errors['name']['_empty'])) {
                                                        echo $errors['name']['_empty'];
                                                    }
                                                    ?>
                                    </span>

            </div>
            <div class="col-md-6 <?= isset($errors['course_category_id']) ? 'has-error' : '' ?>">
                <label class="control-label required">Course Category</label>
                <?= $this->Form->input('course_category_id', [
                    'label' => false,
                    'options' => $course_categories,
                    'class' => 'form-control',
                    'placeholder' => 'Select Course Category',
                    'empty' => 'Select One',
                    'required' => true
                ]) ?>
                <span class="help-block text-center">
                        <?= $errors['course_category_id']['_empty'] ?? '' ?>
                    </span>

            </div>
        </div>

        <div class="row form-group">
            <div class="col-md-6 <?= isset($errors['qualification_type_id']) ? 'has-error' : '' ?>">
                <label class="control-label required">Qualification Type</label>

                <?= $this->Form->input('qualification_type_id', [
                    'label' => false,
                    'class' => 'form-control',
                    'options' => $qualification_types,
                    'placeholder' => 'Select Qualification Type',
                    'empty' => 'Select One',
                    'required' => true
                ]) ?>
                <span class="help-block text-center">
            <?= $errors['qualification_type_id']['_empty'] ?? '' ?>
        </span>

            </div>

            <div class="col-md-6 <?= isset($errors['level']) ? 'has-error' : '' ?>">
                <label class="control-label required">Level</label>

                <?= $this->Form->input('level', [
                    'label' => false,
                    'class' => 'form-control',
                    'placeholder' => '1 to 8'
                ]) ?>
                <span class="help-block text-center">
            <?= $errors['level']['_empty'] ?? '' ?>
        </span>

            </div>
        </div>

        <div class="row form-group">
            <div class="col-md-6 <?= isset($errors['total_credit']) ? 'has-error' : '' ?>">
                <label class="control-label  required">Total Credit</label>

                <?= $this->Form->input('total_credit', [
                    'label' => false,
                    'class' => 'form-control',
                    'placeholder' => 'e.g., 120.5'
                ]) ?>
                <span class="help-block text-center">
            <?= $errors['total_credit']['_empty'] ?? '' ?>
        </span>

            </div>

            <div class="col-md-6 <?= isset($errors['code']) ? 'has-error' : '' ?>">
                <label class="control-label  required">Course Code</label>

                <?= $this->Form->input('code', [
                    'label' => false,
                    'class' => 'form-control',
                    'placeholder' => 'e.g., 603/4373/4'
                ]) ?>
                <span class="help-block text-center">
            <?= $errors['code']['_empty'] ?? '' ?>
        </span>

            </div>
        </div>

        <div class="row form-group">
            <div class="col-md-6 <?= isset($errors['accreditation_status']) ? 'has-error' : '' ?>">
                <label class="control-label  required">Accreditation Status</label>

                <?= $this->Form->input('accreditation_status', [
                    'label' => false,
                    'class' => 'form-control',
                    'options' => ACCREDITATION_STATUS,
                    'placeholder' => 'Select Accreditation Status',
                    'empty' => 'Select One',
                    'required' => true
                ]) ?>

                <span class="help-block text-center">
            <?= $errors['accreditation_status']['_empty'] ?? '' ?>
        </span>

            </div>
            <div class="col-md-6 <?= isset($errors['qualification_time']) ? 'has-error' : '' ?>">
                <label class="control-label ">Qualification Time(Hour)</label>

                <?= $this->Form->input('qualification_time', [
                    'label' => false,
                    'class' => 'form-control',
                    'placeholder' => 'e.g., 600'
                ]) ?>
                <span class="help-block text-center">
            <?= $errors['qualification_time']['_empty'] ?? '' ?>
        </span>

            </div>
        </div>

        <div class="row form-group">
            <div class="col-md-6 <?= isset($errors['progression_routes']) ? 'has-error' : '' ?>">
                <label class="control-label ">Progression Routes</label>

                <?= $this->Form->input('progression_routes', [
                    'label' => false,
                    'class' => 'form-control',
                    'placeholder' => 'Progression details'
                ]) ?>
                <span class="help-block text-center">
            <?= $errors['progression_routes']['_empty'] ?? '' ?>
        </span>

            </div>

            <div class="col-md-6 <?= isset($errors['availability_type']) ? 'has-error' : '' ?>">
                <label class="control-label  required">Availability Type</label>

                <?= $this->Form->input('availability_type', [
                    'label' => false,
                    'class' => 'form-control',
                    'options' => AVAILABILITY_TYPE,
                    'placeholder' => 'Select Availability Type',
                    'empty' => 'Select One',
                    'required' => true
                ]) ?>

                <span class="help-block text-center">
            <?= $errors['availability_type']['_empty'] ?? '' ?>
        </span>

            </div>
        </div>

        <div class="row form-group">
            <div class="col-md-6 <?= isset($errors['delivery_mode']) ? 'has-error' : '' ?>">
                <label class="control-label  required">Delivery Mode</label>

                <?= $this->Form->input('delivery_mode', [
                    'label' => false,
                    'class' => 'form-control',
                    'options' => DELIVERY_MODE,
                    'placeholder' => 'Select Delivery Mode',
                    'empty' => 'Select One',
                    'required' => true
                ]) ?>
                <span class="help-block text-center">
            <?= $errors['delivery_mode']['_empty'] ?? '' ?>
        </span>
            </div>

            <div class="col-md-6 <?= isset($errors['duration']) ? 'has-error' : '' ?>">
                <label class="control-label required">Duration (months)</label>

                <?= $this->Form->input('duration', [
                    'label' => false,
                    'class' => 'form-control',
                    'placeholder' => 'e.g., 12'
                ]) ?>
                <span class="help-block text-center">
            <?= $errors['duration']['_empty'] ?? '' ?>
        </span>

            </div>
        </div>

        <div class="row form-group">
            <div class="col-md-6 <?= isset($errors['assessment']) ? 'has-error' : '' ?>">
                <label class="control-label">Assessment</label>

                <?= $this->Form->input('assessment', [
                    'label' => false,
                    'class' => 'form-control',
                    'placeholder' => 'e.g., Assignment Based'
                ]) ?>
                <span class="help-block text-center">
            <?= $errors['assessment']['_empty'] ?? '' ?>
        </span>

            </div>

            <div class="col-md-6 <?php if (isset($errors['description'])) {
                echo 'has-error';
            } ?>">
                <label class="control-label required">Description</label>

                <?php echo $this->Form->input('description', ['label' => false, ['class' => 'required'], 'placeholder' => 'Course Description', 'class' => 'form-control', 'required' => false]); ?>
                <span class="help-block text-center">
                                                        <?php
                                                        if (isset($errors['description']['_empty'])) {
                                                            echo $errors['description']['_empty'];
                                                        }
                                                        ?>
                                        </span>

            </div>
        </div>

        <div class="row form-group">
            <div class="col-md-6 <?= isset($errors['centre_specification_link']) ? 'has-error' : '' ?>">
                <label class="control-label">Centre Specification Link </label>

                <?= $this->Form->input('centre_specification_link', [
                    'label' => false,
                    'class' => 'form-control',
                    'placeholder' => 'e.g., https://qualifi.net/wp-content/uploads/2023/09/Qualifi-Diplomas-in-Business-Specifications-September-2023.pdf'
                ]) ?>
                <span class="help-block text-center">
                    <?= $errors['centre_specification_link']['_empty'] ?? '' ?>
                </span>

            </div>

        </div>

        <div class="row">
            <div class="col-md-6">
                <div class="col-12">
                    <h4 class="text-center rounded text-white fw-bold pt-1 pb-1 hd-line-height"
                        style="background:#ccc">
                        <b>Course Fee Information</b>
                    </h4>

                    <div class="row form-group">
                        <div class="col-md-6 <?= isset($errors['online_fee']) ? 'has-error' : '' ?>">
                            <label class="control-label required">Online Fee</label>
                            <?= $this->Form->input('online_fee', [
                                'label' => false,
                                'class' => 'form-control',
                                'placeholder' => '0.00',
                                'id' => 'online_fee',
                                'required' => true
                            ]) ?>
                            <span class="help-block text-center">
            <?= $errors['online_fee']['_empty'] ?? '' ?>
        </span>
                        </div>
                        <div class="col-md-6 <?= isset($errors['discount']) ? 'has-error' : '' ?>">
                            <label class="control-label">Discount</label>
                            <?= $this->Form->input('discount', [
                                'label' => false,
                                'class' => 'form-control',
                                'id' => 'discount',
                                'placeholder' => '0.00'
                            ]) ?>
                            <span class="help-block text-center">
            <?= $errors['discount']['_empty'] ?? '' ?>
        </span>
                        </div>
                    </div>

                    <div class="row form-group">
                        <div class="col-md-6 <?= isset($errors['monthly_installment_amount']) ? 'has-error' : '' ?>">
                            <label class="control-label">Monthly Installment Amount</label>
                            <?= $this->Form->input('monthly_installment_amount', [
                                'label' => false,
                                'class' => 'form-control',
                                'id' => 'monthly_installment_amount',
                                'placeholder' => '0.00'
                            ]) ?>
                            <span class="help-block text-center">
                              <?= $errors['monthly_installment_amount']['_empty'] ?? '' ?>
                             </span>
                        </div>
                        <div class="col-md-6 <?= isset($errors['upfront_deposit']) ? 'has-error' : '' ?>">
                            <label class="control-label">Upfront Deposit</label>
                            <?= $this->Form->input('upfront_deposit', [
                                'label' => false,
                                'class' => 'form-control',
                                'id' => 'upfront_deposit',
                                'placeholder' => '0.00'
                            ]) ?>
                            <span class="help-block text-center">
            <?= $errors['upfront_deposit']['_empty'] ?? '' ?>
        </span>
                        </div>
                    </div>

                    <div class="row form-group">
                        <div class="col-md-6 <?= isset($errors['blended_fee']) ? 'has-error' : '' ?>">
                            <label class="control-label">Blended Fee</label>
                            <?= $this->Form->input('blended_fee', [
                                'label' => false,
                                'class' => 'form-control',
                                'id' => 'blended_fee',
                                'placeholder' => '0.00'
                            ]) ?>
                            <span class="help-block text-center">
            <?= $errors['blended_fee']['_empty'] ?? '' ?>
        </span>
                        </div>
                        <div class="col-md-6 <?= isset($errors['certificate_fee']) ? 'has-error' : '' ?>">
                            <label class="control-label">Certificate Fee</label>
                            <?= $this->Form->input('certificate_fee', [
                                'label' => false,
                                'class' => 'form-control',
                                'id' => 'certificate_fee',
                                'placeholder' => '100.00'
                            ]) ?>
                            <span class="help-block text-center">
                                    <?= $errors['certificate_fee']['_empty'] ?? '' ?>
                            </span>
                        </div>
                    </div>

                </div>
            </div>
            <div class="col-md-6">
                <div class="col-12">
                    <h4 class="text-center rounded text-white fw-bold pt-1 pb-1 hd-line-height"
                        style="background:#ccc">
                        <b>Course Unit Information</b>
                    </h4>
                    <table class="table table-bordered" id="subjects-table">
                        <thead>
                        <tr>
                            <th>Title</th>
                            <th>Credit</th>
                            <th>Mandatory</th>
                            <th>Action</th>
                        </tr>
                        </thead>
                        <tbody id="subjects-body">
                        <!-- Initial row -->
                        <tr data-index="0">
                            <td><?= $this->Form->text('course_semester_subjects.0.title', ['class' => 'form-control']) ?></td>
                            <td><?= $this->Form->number('course_semester_subjects.0.credit', ['class' => 'form-control', 'step' => '0.1']) ?></td>
                            <td><input type="checkbox" name="course_semester_subjects[0][is_mandatory]" value="1"></td>
                            <td><button type="button" class="btn btn-sm btn-danger remove-row btn-grid btn-flat">Remove</button></td>
                        </tr>
                        </tbody>
                    </table>

                    <!-- Add Subject Button -->
                    <button type="button" id="add-subject" class="btn btn-sm btn-success btn-grid btn-flat">Add Subject</button>

                </div>
            </div>
        </div>

        <div class="row form-group">

            <div class="col-lg-9 center-block">
                <?= $this->Form->button(__('Submit'), ['class' => 'btn btn-success tab3Save pull-left btn-grid btn-flat', 'id' => 'submit']) ?>
                <a href="<?php echo $this->Url->build('/course'); ?>"
                   class="btn btn-default tab3Save pull-left btn-grid btn-flat btn-orange-color" id="default-btn">
                    <?php echo __('Back'); ?>
                </a>
            </div>
        </div>

        <!-- /.row -->
    </div>
    <!-- /.box-body -->
    <?= $this->Form->end() ?>
</div>

<script>
    $('#online_fee,#discount,#monthly_installment_amount,#upfront_deposit,#blended_fee,#certificate_fee').on('input', function() {
        this.value = decimalChecking(this.value);
    });

    function decimalChecking(value){
        let sanitized = value.replace(/[^0-9.]/g, '');
        sanitized = sanitized.replace(/(\..*?)\..*/g, '$1');
        return sanitized;
    }

    $('#course_form').validate({
        rules: {
            'name': 'required',
            'course_category_id': 'required',
            'qualification_type_id': 'required',
            'level': 'required',
            'total_credit': 'required',
            'code': 'required',
            'availability_type': 'required',
            'delivery_mode': 'required',
            'duration': 'required',
            'description': 'required',
            'online_fee' : 'required'
        },
        messages: {
            'name': {
                required: "Please enter course title"
            },
            'course_category_id': {
                required: "Please enter course category"
            },
            'qualification_type_id': {
                required: "Please enter qualification type"
            },
            'level': {
                required: "Please enter level"
            },
            'total_credit': {
                required: "Please enter total credit"
            },
            'code': {
                required: "Please enter code"
            },
            'availability_type': {
                required: "Please enter availability "
            },
            'delivery_mode': {
                required: "Please enter delivery mode"
            },
            'duration': {
                required: "Please enter duration"
            },
            'description': {
                required: "Please enter course description"
            },
            'online_fee': {
                required: "Please enter course fee"
            }

        },
        highlight: function (element) {
            $(element).parent().addClass("has-error");
        },
        submitHandler: function (form) {
            form.submit();
        }
    });

</script>
<script>
    $(document).ready(function () {
        let index = 1;

        $('#add-subject').on('click', function () {
            const newRow = `
            <tr data-index="${index}">
                <td><input type="text" name="course_semester_subjects[${index}][title]" class="form-control" /></td>
                <td><input type="number" name="course_semester_subjects[${index}][credit]" class="form-control" step="0.1" /></td>
                <td><input type="checkbox" name="course_semester_subjects[${index}][is_mandatory]" value="1" /></td>
                <td><button type="button" class="btn btn-danger remove-row">Remove</button></td>
            </tr>
        `;

            $('#subjects-body').append(newRow);
            index++;
        });

        // Delegate remove button click
        $('#subjects-body').on('click', '.remove-row', function () {
            $(this).closest('tr').remove();
        });
    });
</script>
