<?php
    if(isset($_POST['delete'])){
        $id = $_POST['delete_carryover'];

        $all_carryover = "DELETE FROM carryover WHERE carryover_id=$id";

        if($db->query($all_carryover) == TRUE){
            echo "<script>alert('Carry Over Deleted')</script>";
        }else{
            echo "<script>alert('Carry Over not deleted')</script>";
        }

    }

?>

    
            <!-- Recent Sales Start -->
            <div class="container-fluid pt-4 px-4">
                <div class="bg-secondary text-center rounded p-4">
                    <div class="d-flex align-items-center justify-content-between mb-4">
                        <h6 class="mb-0">Add Carry Over</h6>
                        
                    </div>
                    <div class="table-responsive">
                      <div class="container">
                        <div class="row">
                            <div class="col-md-2"></div>
                            <div class="col-md-8">
                                <form method="POST" action="index.php?env=carryover" enctype="multipart/form-data">
                                    <div class="form-group">
                                        <div class="row">
                                            <div class="col-md-6">
                                                
                                                <select name="sname" value="<?php if(isset($_POST['sname'])){echo $_POST['sname']; } ?>" class="form-control" required>
                                                    <option value="">Select Student</option>
                                                    <?php
                                                        $query = mysqli_query($db, "SELECT * FROM portal ");
                                                        while($row = mysqli_fetch_array($query))
                                                        {
                                                            ?>
                                                            <option value="<?php echo htmlentities($row['sname']); ?>"><?php echo htmlentities($row['sname']); ?></option>
        
                                                        <?php
                                                            }
                                                        ?>
                                                </select><br/>
                                               
                                            </div>
                                            <div class="col-md-6">
                                                <select name="semesta" value="<?php if(isset($_POST['leveld_name'])){echo $_POST['leveld_name']; } ?>" class="form-control" required>
                                                    <option value="">Select Semester</option>
                                                    <?php
                                                        $query = mysqli_query($db, "SELECT * FROM leveld ");
                                                        while($row = mysqli_fetch_array($query))
                                                        {
                                                            ?>
                                                            <option value="<?php echo htmlentities($row['leveld_name']); ?>"><?php echo htmlentities($row['leveld_name']); ?></option>
        
                                                        <?php
                                                            }
                                                        ?>
                                                </select><br/>  
                                            </div>
                                        </div>
                                    </div>
                                   <div class="form-group">
        <div class="row">
            <div class="col-md-6">
                <select name="department" id="department" class="form-control" required>
                    <option value="">Select Department</option>
                    <?php
                    $query = mysqli_query($db, "SELECT * FROM department");
                    while ($row = mysqli_fetch_array($query)) {
                        echo "<option value='" . htmlentities($row['department_name']) . "'>" . htmlentities($row['department_name']) . "</option>";
                    }
                    ?>
                </select><br/>
            </div>
            <div class="col-md-6">
                <select name="courses" id="courses" class="form-control" required>
                    <option value="">Select Courses</option>
                </select>
                <input type="hidden" name="coursecode" id="coursecode">
                <input type="hidden" name="courseunit" id="courseunit">
                <input type="hidden" name="coursestatus" id="coursestatus">
            </div>
        </div>
    </div>
                                
                                    <div class="form-group">
                                        <div class="row">
                                            <div class="col-md-6">
                                            <!-- <select name="coursecode" value="<?php if(isset($_POST['course_code'])){echo $_POST['course_code']; } ?>" class="form-control" required>
                                                    <option value="">Select Course Code</option>
                                                    <?php
                                                        $query = mysqli_query($db, "SELECT * FROM courses ");
                                                        while($row = mysqli_fetch_array($query))
                                                        {
                                                            ?>
                                                            <option value="<?php echo htmlentities($row['course_code']); ?>"><?php echo htmlentities($row['course_code']); ?></option>
        
                                                        <?php
                                                            }
                                                        ?>
                                                </select> <br/>  -->
                                            </div>
                                            <div class="col-md-6">
                                            <!-- <select name="courseunit" value="<?php if(isset($_POST['course_unit'])){echo $_POST['course_unit']; } ?>" class="form-control" required>
                                                    <option value="">Select course Unit</option>
                                                    <?php
                                                        $query = mysqli_query($db, "SELECT * FROM courses ");
                                                        while($row = mysqli_fetch_array($query))
                                                        {
                                                            ?>
                                                            <option value="<?php echo htmlentities($row['course_unit']); ?>"><?php echo htmlentities($row['course_unit']); ?></option>
        
                                                        <?php
                                                            }
                                                        ?>
                                                </select> -->
                                            </div>
                                        </div>
                                    </div>

                                    <div class="form-group">
                                        <div class="row">
                                            <div class="col-md-6">
                                            <!-- <select name="coursestatus" value="<?php if(isset($_POST['course_Status'])){echo $_POST['course_Status']; } ?>" class="form-control" required>
                                                    <option value="">Select Course Status</option>
                                                    <?php
                                                        $query = mysqli_query($db, "SELECT * FROM courses ");
                                                        while($row = mysqli_fetch_array($query))
                                                        {
                                                            ?>
                                                            <option value="<?php echo htmlentities($row['course_Status']); ?>"><?php echo htmlentities($row['course_Status']); ?></option>
        
                                                        <?php
                                                            }
                                                        ?>
                                                </select> <br/>  -->
                                            </div>
                                            <div class="col-md-6">
                                            
                                            </div>
                                        </div>
                                    </div>

                                    <div class="form-group">
                                        <button type="submit" name="addcarryover" class="btn btn-outline-primary btn-block">Add carry Over</button>
                                    </div>
                                </form>

                              
                            </div>
                            <div class="col-md-2"></div>
                        </div>
                        <div class="row">
                            <div class="col-md-12">
                                 <!-- view all carry over  courses -->
                               <table class="table text-start align-middle table-bordered table-hover mb-0" style="margin-top:50px;">
                                    <thead>
                                        <tr>
                                            <th scope="col">S/N</th>
                                            <th scope="col">Student Name</th>
                                            <th scope="col">Department</th>
                                            <th scope="col">Semester</th>
                                            <th scope="col">carry over Course</th>
                                            <th scope="col">Course Code</th>
                                            <th scope="col">Course Unit</th>
                                            <th scope="col">Course Status</th>
                                            <th scope="col">Action</th>
                                            <th scope="col">Action</th>
                                        </tr>
                                    </thead>
                                    <tbody>
                                        <?php
                                            $all_carryover = mysqli_query($db, "SELECT * FROM carryover");

                                            // fetch all carry over 
                                            if(mysqli_fetch_array($all_carryover)){
                                                
                                                // use foreach loop to echo all data
                                                foreach($all_carryover as $carryover){
                                        ?>
                                        <tr>
                                                <td><?php echo $carryover['carryover_id']; ?></td>
                                                <td><?php echo $carryover['sname']; ?></td>
                                                <td><?php echo $carryover['department']; ?></td>
                                                <td><?php echo $carryover['semesta']; ?></td>
                                                <td><?php echo $carryover['courses']; ?></td>
                                                <td><?php echo $carryover['coursecode']; ?></td>
                                                 <td><?php echo $carryover['courseunit']; ?></td>
                                                  <td><?php echo $carryover['coursestatus']; ?></td>
                                                  
                                                <td><a class="btn btn-sm btn-success" href="index.php?env=edit_carryover&carryover_id=<?php echo $carryover['carryover_id']; ?>">Edit</a></td>
                                                <td>
                                                    <form method="POST">
                                                        <input type='hidden' name='delete_carryover' value='<?php echo $carryover['carryover_id']; ?>'>
                                                        <input type='submit'class='btn btn-sm btn-danger' name='delete' value='Delete'>
                                                    </form>
                                          
                                                </td>
                                        </tr>
                                        <?php
                                            }
                                        }
                                        ?>
                                    </tbody>
                                </table>
                            </div>
                        </div>
                      </div>
                    </div>
                </div>
            </div>
            <!-- Recent Sales End -->

            <!-- Footer Start -->
            <?php include("inc/footer.php"); ?>

            <?php
if (isset($_POST['addcarryover'])) {
    $sname = mysqli_real_escape_string($db, $_POST['sname']);
    $department = mysqli_real_escape_string($db, $_POST['department']);
    $semesta = mysqli_real_escape_string($db, $_POST['semesta']);
    $courses = mysqli_real_escape_string($db, $_POST['courses']);
    $coursecode = mysqli_real_escape_string($db, $_POST['coursecode']);
    $courseunit = mysqli_real_escape_string($db, $_POST['courseunit']);
    $coursestatus = mysqli_real_escape_string($db, $_POST['coursestatus']);

    $query = "INSERT INTO carryover (sname, department, semesta, courses, coursecode, courseunit, coursestatus) 
              VALUES ('$sname', '$department', '$semesta', '$courses', '$coursecode', '$courseunit', '$coursestatus')";

    if (mysqli_query($db, $query)) {
        echo "<script>alert('Carry Over Added Successfully')</script>";
    } else {
        echo "<script>alert('Carry Over Not Inserted Successfully')</script>";
    }
}
?>

<script>
document.getElementById('department').addEventListener('change', function() {
    var department = this.value;
    var coursesSelect = document.getElementById('courses');
    coursesSelect.innerHTML = ''; // Clear previous options

    var courses = {
        'Shipping MGT NID': [
            { title: 'COMPUTER APPREC & LIBRARY', code: 'COM 101', unit: 2, status: 'Core' },
            { title: 'MARITIME GEOGRAPHY', code: 'COM 111', unit: 3, status: 'Core' },
            { title: 'FRENCH LANGUAGE', code: 'MRN 111', unit: 3, status: 'Core' },
            { title: 'MARITIME ECONOMICS', code: 'SHM 112', unit: 3, status: 'Core' },
            { title: 'GEN. SHIPPING KNOWLEDGE', code: 'SHM 113', unit: 3, status: 'Core' },
            { title: 'PRINCIPLES OF ACCOUNTING', code: 'SHM 114', unit: 3, status: 'Core' },
            { title: 'PRINCIPLES OF MANAGEMENT', code: 'SHM 115', unit: 3, status: 'Core' },
            { title: 'PRINCIPLES OF LAW', code: 'SHM 116', unit: 3, status: 'Core' },
            { title: 'BUSINESS MATHEMATICS', code: 'MAT 241', unit: 2, status: 'Core' },
            { title: 'USE OF ENGLISH', code: 'CSK 401', unit: 4, status: 'Core' }
        ],

        'Shipping MGT HND': [
            { title: 'FUNDAMENTER OF SHIPPING', code: 'ASS 101', unit: 3, status: 'Core' },
            { title: 'SHIPPING ECONOMICS', code: 'ASS 102', unit: 3, status: 'Core' },
            { title: 'QUANTITATIVE METHODS', code: 'ASS 103', unit: 3, status: 'Core' },
            { title: 'BUSINESS COMMS. SKILL', code: 'ASS 104', unit: 3, status: 'Core' },
            { title: 'FREIGHT FORWARDING', code: 'ASS 105', unit: 3, status: 'Core' },
            { title: 'INTRODUCTION TO TRANSPORT', code: 'ASS 106', unit: 3, status: 'Core' }
        ],

        'Shipping MGT ASS': [
            { title: 'FUNDAMENTER OF SHIPPING', code: 'ASS 101', unit: 2, status: 'Core' },
            { title: 'SHIPPING ECONOMICS', code: 'ASS 102', unit: 3, status: 'Core' },
            { title: 'QUANTITATIVE METHODS', code: 'ASS 103', unit: 3, status: 'Core' },
            { title: 'BUSINESS COMMS. SKILL', code: 'ASS 104', unit: 3, status: 'Core' },
            { title: 'FREIGHT FORWARDING', code: 'ASS 105', unit: 3, status: 'Core' },
            { title: 'INTRODUCTION TO TRANSPORT', code: 'ASS 106', unit: 4, status: 'Core' }
        ], 

        'Shipping MGT ASS Sec': [
            { title: 'INTRO PORTS FACILITY SECURITY', code: 'ASMT 101', unit: 3, status: 'Core' },
            { title: 'DEVELOPMENT OF SECURITY TECTH', code: 'ASMT 102', unit: 3, status: 'Core' },
            { title: 'AVIATION INDUSTRY & SECURITY', code: 'ASMT 103', unit: 3, status: 'Core' },
            { title: 'BUSINESS COMMUNICATION', code: 'ASMT 104', unit: 3, status: 'Core' },
            { title: 'CROWED & RIOT DAMMAGE COUTROL', code: 'ASMT 105', unit: 3, status: 'Core' },
            { title: 'SECURITY INVESIGATION REPORT WRITTING', code: 'ASMT 106', unit: 3, status: 'Core' }
        ],

        'Shipping PGD': [
            { title: 'QUANTITATIVE TECH.', code: 'SHIP 602', unit: 2, status: 'Core' },
            { title: 'TRANSPORT MGT.', code: 'SHIP 603', unit: 3, status: 'Core' },
            { title: 'FREIGHTFORWARDING', code: 'SHIP 604', unit: 4, status: 'Core' },
            { title: 'MARITIME INSURANCE', code: 'SHIP 605', unit: 2, status: 'Core' },
            { title: 'ACCOUNTING FOR MANAGERS', code: 'SHIP 606', unit: 2, status: 'Core' },
            { title: 'RESEARCH METHOD', code: 'SHIP 607', unit: 2, status: 'Core' }
        ],
        
        'Engineering NID': [
            { title: 'USE OF ENGLISH', code: 'GNS 101', unit: 2, status: 'Core' },
            { title: 'FREACH', code: 'MRS 101', unit: 2, status: 'Core' },
            { title: 'GENERAL MATHEMATICS', code: 'MTH 101', unit: 2, status: 'Core' },
            { title: 'COMPUTER APPRECIATION', code: 'COM 101', unit: 4, status: 'Core' },
            { title: 'TECHNICAL DRAWING', code: 'MEC 101', unit: 3, status: 'Core' },
            { title: 'ELECTRICAL ENGINEERING', code: 'IEE 101', unit: 4, status: 'Core' },
            { title: 'ARC WELDING', code: 'IME 101', unit: 4, status: 'Core' },
            { title: 'MECHANICAL ENGINEERING', code: 'MEC 103', unit: 4, status: 'Core' },
            { title: 'MARINE POLLUTION', code: 'IME 103', unit: 2, status: 'Core' },
            { title: 'WORKSHOP TECHNOLOGY', code: 'MEC 105', unit: 3, status: 'Core' },
            { title: 'Maritime Security', code: 'SMT 222', unit: 2, status: 'Core' }
        ],

        'Security MGT NID SEC': [
            { title: 'USE OF ENGLISH', code: 'SMT 101', unit: 2, status: 'Core' },
            { title: 'FREACH', code: 'MRN 101', unit: 2, status: 'Core' },
            { title: 'MATHEMATICS', code: 'MAT 101', unit: 2, status: 'Core' },
            { title: 'INTRODUCTION TO SECURITY', code: 'SMT 103', unit: 2, status: 'Core' },
            { title: 'SECURITY & LAW ENFORCEMENT', code: 'SMT 105', unit: 2, status: 'Core' },
            { title: 'SECURITY REPORT WRITTING', code: 'SMT 107', unit: 2, status: 'Core' },
            { title: 'PHYSICAL SECURITY', code: 'SMT 109', unit: 2, status: 'Core' },
            { title: 'BLUEPRINT READING', code: 'SMT 111', unit: 2, status: 'Core' },
            { title: 'INTRO. TO ELEC INSTALLATION', code: 'SMT 112', unit: 2, status: 'Core' },
            { title: 'GENERAL SHIP KNOWLEDGE', code: 'SMT 113', unit: 2, status: 'Core' }
        
        ]
        
        // Add other departments and their courses here
    };

    if (department in courses) {
        courses[department].forEach(function(course) {
            var option = document.createElement('option');
            option.value = JSON.stringify(course);
            option.text = `${course.title} (${course.code}) - ${course.unit} Units - ${course.status}`;
            coursesSelect.add(option);
        });
    } else {
        var option = document.createElement('option');
        option.value = '';
        option.text = 'No courses available';
        coursesSelect.add(option);
    }
});

document.getElementById('courses').addEventListener('change', function() {
    var selectedCourse = JSON.parse(this.value);
    document.getElementById('coursecode').value = selectedCourse.code;
    document.getElementById('courseunit').value = selectedCourse.unit;
    document.getElementById('coursestatus').value = selectedCourse.status;
});
</script>