<!-- Content Wrapper. Contains page content -->
<div class="content-wrapper">
    <!-- Content Header (Page header) -->
    <section class="content-header">
        <h1>
            <i class="fa fa-gavel"></i> <?php echo $this->lang->line('discipline'); ?> <small><?php echo $this->lang->line('student_fee1'); ?></small>
        </h1>
        <a href="<?php echo base_url(); ?>admin/discipline/incident_reports" class="col-md pull-right">Report New Incident</a>
        <b style="padding-left:5px;padding-right:5px;" class="pull-right"> | </b>
        <a href="<?php echo base_url(); ?>admin/discipline/incident_categories" class="col-md pull-right">Incident Categories</a>
        <b style="padding-left:5px;padding-right:5px;" class="pull-right"> | </b>
        <a href="<?php echo base_url(); ?>admin/discipline/incident_types" class="col-md pull-right">Incident Types</a>
    </section>

    <!-- Main content -->
    <section class="content">
        <div class="row">
            <div class="col-md-12">
                <div class="box box-primary">
                    <div class="box-header with-border">
                        <h3 class="box-title"><i class="fa fa-search"></i> Select Class and Section</h3>
                    </div>
                    <form id='form1' action="<?php echo site_url('admin/discipline/incident_reports') ?>" method="post" accept-charset="utf-8">
                        <div class="box-body">
                            <?php echo $this->customlib->getCSRF(); ?>
                            <div class="row">
                                <div class="col-md-6">
                                    <div class="form-group">
                                        <label for="exampleInputEmail1"><?php echo $this->lang->line('class'); ?></label><small class="req"> *</small>
                                        <select autofocus="" id="class_id" name="class_id" class="form-control">
                                            <option value=""><?php echo $this->lang->line('select'); ?></option>
                                            <?php
                                            foreach ($classlist as $class) {
                                                ?>
                                                <option value="<?php echo $class['id'] ?>" <?php if (isset($class_post) && $class_post == $class['id']) echo "selected=selected"; ?>><?php echo $class['class'] ?></option>
                                                <?php
                                            }
                                            ?>
                                        </select>
                                        <span class="text-danger"><?php echo form_error('class_id'); ?></span>
                                    </div>
                                </div>
                                <div class="col-md-6">
                                    <div class="form-group">
                                        <label for="exampleInputEmail1"><?php echo $this->lang->line('section'); ?></label><small class="req"> *</small>
                                        <select id="section_id" name="section_id" class="form-control">
                                            <option value=""><?php echo $this->lang->line('select'); ?></option>
                                            <?php
                                            if (isset($class_post) && isset($section_post)) {
                                                $sections = $this->section_model->getByClass($class_post);
                                                foreach ($sections as $section) {
                                                    ?>
                                                    <option value="<?php echo $section['section_id'] ?>" <?php if ($section_post == $section['section_id']) echo "selected=selected"; ?>><?php echo $section['section'] ?></option>
                                                    <?php
                                                }
                                            }
                                            ?>
                                        </select>
                                        <span class="text-danger"><?php echo form_error('section_id'); ?></span>
                                    </div>
                                </div>
                            </div>
                        </div>
                        <div class="box-footer">
                            <button type="submit" class="btn btn-primary btn-sm pull-right"><?php echo $this->lang->line('search'); ?></button>
                        </div>
                    </form>
                </div>
            </div>

            <?php if (isset($resultlist)) { ?>
                <input type="hidden" class="class_post" value="<?php echo $class_post; ?>">
                <input type="hidden" class="section_post" value="<?php echo $section_post; ?>">
                
                <div class="col-md-12">
                    <div class="box box-info">
                        <div class="box-header with-border">
                            <h3 class="box-title"><i class="fa fa-list"></i> Report Incident for Selected Learners</h3>
                        </div>
                        <div class="box-body">
                            <div class="table-responsive">
                                <table class="table table-striped">
                                    <thead>
                                        <tr>
                                            <th>Admission No</th>
                                            <th>Learner Name</th>
                                            <th>Father Name</th>
                                            <th>Date of Birth</th>
                                            <th class="text-center">Action</th>
                                        </tr>
                                    </thead>
                                    <tbody>
                                        <?php if (empty($resultlist)) { ?>
                                            <tr>
                                                <td colspan="5" class="text-danger text-center">No learners found</td>
                                            </tr>
                                        <?php } else { 
                                            foreach ($resultlist as $student) { ?>
                                                <tr>
                                                    <td><?php echo $student['admission_no']; ?></td>
                                                    <td><?php echo $student['firstname'] . " " . $student['lastname']; ?></td>
                                                    <td><?php echo $student['father_name']; ?></td>
                                                    <td><?php echo date($this->customlib->getSchoolDateFormat(), $this->customlib->dateyyyymmddTodateformat($student['dob'])); ?></td>
                                                    <td class="text-center">
                                                        <button type="button" class="btn btn-primary btn-sm report-incident-btn" 
                                                                data-student-id="<?php echo $student['id']; ?>"
                                                                data-student-name="<?php echo $student['firstname'] . ' ' . $student['lastname']; ?>"
                                                                data-admission-no="<?php echo $student['admission_no']; ?>">
                                                            <i class="fa fa-plus"></i> Report Incident
                                                        </button>
                                                    </td>
                                                </tr>
                                            <?php } 
                                        } ?>
                                    </tbody>
                                </table>
                            </div>
                        </div>
                    </div>
                </div>
            <?php } ?>
        </div>
    </section>
</div>

<!-- Incident Report Modal -->
<div class="modal fade" id="incidentReportModal" tabindex="-1" role="dialog" aria-labelledby="incidentReportModalLabel">
    <div class="modal-dialog modal-lg" role="document">
        <div class="modal-content">
            <div class="modal-header">
                <button type="button" class="close" data-dismiss="modal" aria-label="Close"><span aria-hidden="true">&times;</span></button>
                <h4 class="modal-title" id="incidentReportModalLabel">Report Incident</h4>
            </div>
            <form id="incidentReportForm" method="post">
                <div class="modal-body">
                    <?php echo $this->customlib->getCSRF(); ?>
                    <input type="hidden" name="student_id" id="modal_student_id">
                    <input type="hidden" name="class_id" value="<?php echo isset($class_post) ? $class_post : ''; ?>">
                    <input type="hidden" name="section_id" value="<?php echo isset($section_post) ? $section_post : ''; ?>">
                    
                    <div class="row">
                        <div class="col-md-6">
                            <div class="form-group">
                                <label>Learner</label>
                                <input type="text" class="form-control" id="modal_student_name" readonly>
                            </div>
                        </div>
                        <div class="col-md-6">
                            <div class="form-group">
                                <label>Admission No</label>
                                <input type="text" class="form-control" id="modal_admission_no" readonly>
                            </div>
                        </div>
                    </div>
                    
                    <div class="row">
                        <div class="col-md-6">
                            <div class="form-group">
                                <label>Incident Type <small class="req"> *</small></label>
                                <select name="incident_type_id" class="form-control" required>
                                    <option value="">Select Incident Type</option>
                                    <?php foreach ($incident_types as $type) { ?>
                                        <option value="<?php echo $type['id']; ?>"><?php echo $type['name']; ?></option>
                                    <?php } ?>
                                </select>
                            </div>
                        </div>
                        <div class="col-md-6">
                            <div class="form-group">
                                <label>Incident Category <small class="req"> *</small></label>
                                <select name="incident_category_id" class="form-control" required>
                                    <option value="">Select Incident Category</option>
                                    <?php foreach ($incident_categories as $category) { ?>
                                        <option value="<?php echo $category['id']; ?>"><?php echo $category['name']; ?></option>
                                    <?php } ?>
                                </select>
                            </div>
                        </div>
                    </div>
                    
                    <div class="row">
                        <div class="col-md-6">
                            <div class="form-group">
                                <label>Reported To <small class="req"> *</small></label>
                                <input type="text" name="reported_to" class="form-control" placeholder="Name of person reported to" required>
                            </div>
                        </div>
                        <div class="col-md-6">
                            <div class="form-group">
                                <label>Reported By <small class="req"> *</small></label>
                                <input type="text" name="reported_by" class="form-control" placeholder="Name of reporter" required>
                            </div>
                        </div>
                    </div>
                    
                    <div class="row">
                        <div class="col-md-6">
                            <div class="form-group">
                                <label>Incident Date <small class="req"> *</small></label>
                                <input type="text" name="incident_date" class="form-control date" placeholder="Incident Date" required>
                            </div>
                        </div>
                    </div>
                    
                    <div class="form-group">
                        <label>Description of Incident <small class="req"> *</small></label>
                        <textarea name="description" class="form-control" rows="3" placeholder="Describe the incident in detail" required></textarea>
                    </div>
                    
                    <div class="form-group">
                        <label>Action Taken</label>
                        <textarea name="action_taken" class="form-control" rows="2" placeholder="Actions taken to address the incident"></textarea>
                    </div>
                    
                    <div class="form-group">
                        <label>Outcome</label>
                        <textarea name="outcome" class="form-control" rows="2" placeholder="Result or outcome of the incident"></textarea>
                    </div>
                </div>
                <div class="modal-footer">
                    <button type="button" class="btn btn-default" data-dismiss="modal">Close</button>
                    <button type="submit" class="btn btn-primary">Save Incident Report</button>
                </div>
            </form>
        </div>
    </div>
</div>

<script type="text/javascript">
    $(document).ready(function () {
        // Get sections when class changes
        $(document).on('change', '#class_id', function (e) {
            $('#section_id').html("");
            var class_id = $(this).val();
            var base_url = '<?php echo base_url() ?>';
            var div_data = '<option value=""><?php echo $this->lang->line('select'); ?></option>';
            
            $.ajax({
                type: "GET",
                url: base_url + "sections/getByClass",
                data: {'class_id': class_id},
                dataType: "json",
                success: function (data) {
                    $.each(data, function (i, obj) {
                        div_data += "<option value=" + obj.section_id + ">" + obj.section + "</option>";
                    });
                    $('#section_id').append(div_data);
                }
            });
        });

        // Initialize datepicker
        $('.date').datepicker({
            format: "dd-mm-yyyy",
            autoclose: true
        });

        // Handle report incident button click
        $(document).on('click', '.report-incident-btn', function () {
            var studentId = $(this).data('student-id');
            var studentName = $(this).data('student-name');
            var admissionNo = $(this).data('admission-no');
            
            $('#modal_student_id').val(studentId);
            $('#modal_student_name').val(studentName);
            $('#modal_admission_no').val(admissionNo);
            
            $('#incidentReportModal').modal('show');
        });

        // Handle form submission
        $('#incidentReportForm').on('submit', function (e) {
            e.preventDefault();
            
            var formData = $(this).serialize();
            
            $.ajax({
                type: "POST",
                url: "<?php echo site_url('admin/discipline/save_incident_report'); ?>",
                data: formData,
                dataType: "json",
                success: function (response) {
                    if (response.status == 'success') {
                        $('#incidentReportModal').modal('hide');
                        $('#incidentReportForm')[0].reset();
                        successMsg(response.msg);
                    } else {
                        // Handle validation errors
                        $.each(response.msg, function (key, value) {
                            $('[name="' + key + '"]').next('.text-danger').remove();
                            $('[name="' + key + '"]').after('<span class="text-danger">' + value + '</span>');
                        });
                    }
                },
                error: function () {
                    alert('Error occurred while saving incident report');
                }
            });
        });

        // Clear errors when modal is hidden
        $('#incidentReportModal').on('hidden.bs.modal', function () {
            $('.text-danger').remove();
            $('#incidentReportForm')[0].reset();
        });
    });

    function successMsg(msg) {
        $.ajax({
            type: "POST",
            url: "<?php echo base_url(); ?>admin/notification/successmsg",
            data: {msg: msg, timeout: 2000},
            dataType: "html",
            success: function (response) {
                $('.alert-success').remove();
                $('.content-header').after(response);
                $("html, body").animate({scrollTop: 0}, "slow");
            }
        });
    }
</script>