<?php
$last_join_date = $Student->last_join_date ? $Student->last_join_date->format('d-m-Y') : '';
?>
<?= $this->Form->create($Student, ['id' => 'student_reference_form', 'class'=> 'input-form-section']) ?>
<div class="container-fluid box box-primary">
    <input type="hidden" name="step" value="<?php echo (FORM_SEQUENCE)[2]?>">
    <div class="row form-group">

        <div class="col-md-4 mb-1">
            <label for="reference_number" class="control-label">Reference Number<span
                        class="text-danger">*</span></label>
            <input required type="text" name="reference_number" id="reference_number"
                   class="form-control" readonly="readonly" value="<?php echo $Student->reference_number ?>">
        </div>
    </div>
    <div class="row form-group">

        <div class="col-md-4 mb-1">
            <label for="last_join_date" class="control-label">Latest Joining Date<span
                        class="text-danger">*</span></label>
            <input required type="text" name="last_join_date" id="last_join_date"
                   class="form-control" placeholder="Latest Joining Date" value="<?php echo $last_join_date ?>">
        </div>
    </div>

    <div class="row form-group">

        <div class="col-md-4 mb-1">
            <label for="mod_of_study" class="control-label">Mod Of Study<span
                        class="text-danger">*</span></label>
            <select required name="mod_of_study" id="mod_of_study" class="form-control">
                <option selected="" disabled="" value="">Select One</option>
                <?php foreach (STUDY_MOD as $key => $value): ?>
                    <option value="<?= h($key) ?>" <?= $Student->mod_of_study == $key ? 'selected' : '' ?>><?= h($value) ?></option>
                <?php endforeach; ?>
            </select>
        </div>
    </div>
    <div class="row form-group">

        <div class="col-md-4 mb-1">
            <label for="referring_agent_id" class="control-label">Registered Agent</label>
            <select  name="referring_agent_id" id="referring_agent_id" class="form-control">
                <option selected="" value="">Select One</option>
                <?php foreach ($agents as $key=>$value):?>
                    <option value="<?= h($key) ?>" <?= $Student->referring_agent_id == $key ? 'selected' : '' ?>><?= h($value) ?></option>
                <?php endforeach; ?>
            </select>
        </div>
    </div>
    <div class="row form-group">

        <div class="col-md-4 mb-1">
            <label for="referring_agent_name" class="control-label">Referred Agent Name</label>
            <input  type="text" name="referring_agent_name" id="referring_agent_name"
                   class="form-control" placeholder="Referred Agent Name" value="<?php echo $Student->referring_agent_name ?>">
        </div>
    </div>

    <div class="row form-group">

        <div class="col-lg-9 center-block">
            <?= $this->Form->button(__('Next'), ['class' => 'btn btn-success tab3Save pull-left btn-grid btn-flat','id'=>'submit']) ?>

        </div>
    </div>
</div>
<?= $this->Form->end() ?>

<style type="text/css">

    .error-message {
        color: #ff0000;
    }


</style>

<script>
    $(document).ready(function () {
        $('input[name="last_join_date"]').datepicker({autoclose: true, todayHighlight: true, format: 'dd-mm-yyyy',orientation: 'bottom auto'});
    });
        $('#student_reference_form').validate({
        rules:{
        'reference_number':'required',
        'last_join_date':'required',
        'mod_of_study':'required'
    },
        messages: {
             'reference_number':{
               required: "Please enter reference_number"
             },
            'last_join_date':{
                required: "Please enter last_join_date"
            },
            'mod_of_study':{
                required: "Please enter mod_of_study"
            }

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

    $('#referring_agent_id').on('change', function () {
        var selectedValue = $(this).val();
        var selectedText = '';
        if (selectedValue !== '' && selectedValue > 0) {
             selectedText  = $('#referring_agent_id option:selected').text();
        }

        $('#referring_agent_name').val(selectedText);

    });


</script>


