<?php //error_reporting(0); ?>
<?php    session_start(); ?>
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1">

    <?php include '../admin/includes/styles.php'; ?>
    <title> My Assignments | <?php echo $school_name; ?> </title>

    <style>
        .assignment-card {
            transition: all 0.3s;
            border-left: 4px solid #007bff;
            height: 100%;
            position: relative;
        }
        .assignment-card:hover {
            transform: translateY(-5px);
            box-shadow: 0 4px 8px rgba(0,0,0,0.1);
        }
        .assignment-card.overdue {
            border-left-color: #dc3545;
        }
        .assignment-card.submitted {
            border-left-color: #28a745;
        }
        .assignment-card.graded {
            border-left-color: #17a2b8;
        }
        .due-badge {
            position: absolute;
            top: 10px;
            right: 10px;
            padding: 5px 10px;
            border-radius: 4px;
            font-size: 12px;
            font-weight: 600;
        }
        .due-overdue {
            background: #f8d7da;
            color: #721c24;
        }
        .due-soon {
            background: #fff3cd;
            color: #856404;
        }
        .due-upcoming {
            background: #d4edda;
            color: #155724;
        }
        .status-badge {
            display: inline-block;
            padding: 4px 8px;
            border-radius: 4px;
            font-size: 11px;
            font-weight: 600;
        }
        .status-not-submitted {
            background: #e2e3e5;
            color: #383d41;
        }
        .status-submitted {
            background: #d4edda;
            color: #155724;
        }
        .status-late {
            background: #fff3cd;
            color: #856404;
        }
        .status-graded {
            background: #d1ecf1;
            color: #0c5460;
        }
        .grade-display {
            font-size: 24px;
            font-weight: bold;
            margin: 10px 0;
        }

        .fa-eye{
            background-color: transparent;
        }
    </style>



    <?php
    if(isset($_SESSION['student'])){

    require_once '../admin/Classes/Assignment.php';
    $assignment_mgr = new Assignment();

    // Get student ID
    $sql = "SELECT studentID, name, classesID, sectionid FROM student WHERE username = :username";
    $conn->query($sql);
    $conn->bind(':username', $_SESSION['student']);
    $student = $conn->fetchSingle();

    if (!$student) {
        header("Location: $student_login_link");
        exit();
    }

    $student_id = $student->studentID;

    ?>

</head>
<body class="hold-transition sidebar-mini layout-fixed">
<div class="wrapper">

    <!-- Navbar -->
    <?php include 'includes/notifications.php'; ?>
    <!-- /.navbar -->

    <!-- Main Sidebar Container -->
    <?php include "includes/sidebar.php";?>

    <!-- Content Wrapper. Contains page content -->
    <div class="content-wrapper">
        <!-- Content Header (Page header) -->
        <div class="content-header">
            <div class="container-fluid">
                <div class="row mb-2">
                    <div class="col-sm-6">
                        <h1 class="m-0"> My Assignments </h1>
                    </div><!-- /.col -->
                    <div class="col-sm-6">
                        <ol class="breadcrumb float-sm-right">
                            <li class="breadcrumb-item"><a href="<?php echo $conn->student_url(); ?>">Home</a></li>
                            <li class="breadcrumb-item active">Assignments </li>
                        </ol>
                    </div><!-- /.col -->
                </div><!-- /.row -->
            </div><!-- /.container-fluid -->
        </div>
        <!-- /.content-header -->

        <!-- Main content -->
        <section class="content">
            <div class="container-fluid">

                <!-- Statistics -->
                <?php
                $assignments = $assignment_mgr->getStudentAssignment($student_id);
                $total = count($assignments);
                $submitted = 0;
                $graded = 0;
                $overdue = 0;

                foreach ($assignments as $assign) {
                    if ($assign->submission_status != 'not_submitted') $submitted++;
                    if ($assign->is_graded) $graded++;
                    
                    // Calculate if assignment is overdue based on current date and due date
                    $now = new DateTime();
                    $due_date = new DateTime($assign->due_date);
                    if ($now > $due_date && $assign->submission_status == 'not_submitted') {
                        $overdue++;
                    }
                }
                ?>
                <div class="row">
                    <div class="col-lg-3 col-6">
                        <div class="small-box bg-info">
                            <div class="inner">
                                <h3><?php echo $total; ?></h3>
                                <p>Total Assignments</p>
                            </div>
                            <div class="icon">
                                <i class="fas fa-tasks"></i>
                            </div>
                        </div>
                    </div>

                    <div class="col-lg-3 col-6">
                        <div class="small-box bg-success">
                            <div class="inner">
                                <h3><?php echo $submitted; ?></h3>
                                <p>Submitted</p>
                            </div>
                            <div class="icon">
                                <i class="fas fa-check-circle"></i>
                            </div>
                        </div>
                    </div>

                    <div class="col-lg-3 col-6">
                        <div class="small-box bg-primary">
                            <div class="inner">
                                <h3><?php echo $graded; ?></h3>
                                <p>Graded</p>
                            </div>
                            <div class="icon">
                                <i class="fas fa-star"></i>
                            </div>
                        </div>
                    </div>

                    <div class="col-lg-3 col-6">
                        <div class="small-box bg-danger">
                            <div class="inner">
                                <h3><?php echo $overdue; ?></h3>
                                <p>Overdue</p>
                            </div>
                            <div class="icon">
                                <i class="fas fa-exclamation-triangle"></i>
                            </div>
                        </div>
                    </div>
                </div>

                <!-- Filter Tabs -->
                <ul class="nav nav-tabs mb-3" id="assignmentTabs" role="tablist">
                    <li class="nav-item">
                        <a class="nav-link active" id="all-tab" data-toggle="tab" href="#all" role="tab">
                            All (<?php echo $total; ?>)
                        </a>
                    </li>
                    <li class="nav-item">
                        <a class="nav-link" id="pending-tab" data-toggle="tab" href="#pending" role="tab">
                            Pending (<?php echo $total - $submitted; ?>)
                        </a>
                    </li>
                    <li class="nav-item">
                        <a class="nav-link" id="submitted-tab" data-toggle="tab" href="#submitted" role="tab">
                            Submitted (<?php echo $submitted; ?>)
                        </a>
                    </li>
                    <li class="nav-item">
                        <a class="nav-link" id="graded-tab" data-toggle="tab" href="#graded" role="tab">
                            Graded (<?php echo $graded; ?>)
                        </a>
                    </li>
                    <li class="nav-item">
                        <a class="nav-link" id="overdue-tab" data-toggle="tab" href="#overdue" role="tab">
                            Overdue (<?php echo $overdue; ?>)
                        </a>
                    </li>
                </ul>

                <!-- Assignments List -->
                <div class="tab-content" id="assignmentTabsContent">
                    <!-- All Assignments -->
                    <div class="tab-pane fade show active" id="all" role="tabpanel">
                        <div class="row">
                            <?php
                            if (!empty($assignments)) {
                                foreach ($assignments as $assign) {
                                    $now = new DateTime();
                                    $due_date = new DateTime($assign->due_date);
                                    $diff = $now->diff($due_date);

                                    // Calculate if assignment is overdue based on current date and due date
                                    if ($now > $due_date && $assign->submission_status == 'not_submitted') {
                                        $due_class = 'due-overdue';
                                        $due_text = 'Overdue (' . $diff->days . 'd ago)';
                                        $card_class = 'overdue';
                                    } elseif ($diff->days <= 2 && $now < $due_date) {
                                        $due_class = 'due-soon';
                                        $due_text = 'Due in ' . $diff->days . 'd';
                                        $card_class = '';
                                    } else {
                                        $due_class = 'due-upcoming';
                                        $due_text = 'Due in ' . $diff->days . 'd';
                                        $card_class = '';
                                    }

                                    if ($assign->submission_status == 'submitted' || $assign->submission_status == 'late_submitted') {
                                        $card_class = 'submitted';
                                    }
                                    if ($assign->is_graded) {
                                        $card_class = 'graded';
                                    }

                                    $status_class = 'not-submitted';
                                    $status_text = 'Not Submitted';
                                    if ($assign->submission_status == 'submitted') {
                                        $status_class = 'submitted';
                                        $status_text = 'Submitted';
                                    } elseif ($assign->submission_status == 'late_submitted') {
                                        $status_class = 'late';
                                        $status_text = 'Late Submitted';
                                    }
                                    if ($assign->is_graded) {
                                        $status_class = 'graded';
                                        $status_text = 'Graded';
                                    }
                                    ?>
                                    <div class="col-md-4 mb-4 assignment-item"
                                         data-status="<?php echo $assign->submission_status; ?>"
                                         data-graded="<?php echo $assign->is_graded; ?>"
                                         data-overdue="<?php echo $assign->is_overdue && $assign->submission_status == 'not_submitted' ? '1' : '0'; ?>"
                                         data-assign="{"id": "<?php echo $assign->id; ?>", "due_date": "<?php echo $assign->due_date; ?>", "submission_status": "<?php echo $assign->submission_status; ?>"}">
                                        <div class="card assignment-card <?php echo $card_class; ?>">
                                    <span class="due-badge <?php echo $due_class; ?>">
                                        <?php echo $due_text; ?>
                                    </span>
                                            <div class="card-body">
                                                <h5 class="card-title"><?php echo htmlspecialchars($assign->title); ?></h5>
                                                <p class="text-muted small mb-2">
                                                    <i class="fas fa-book"></i> <?php echo htmlspecialchars($assign->subject_name); ?><br>
                                                    <i class="fas fa-calendar"></i> Due: <?php echo date('M d, Y H:i', strtotime($assign->due_date)); ?><br>
                                                    <i class="fas fa-award"></i> <?php echo $assign->total_marks; ?> marks
                                                </p>

                                                <span class="status-badge status-<?php echo $status_class; ?>">
                                            <?php echo $status_text; ?>
                                        </span>

                                                <?php if ($assign->is_graded): ?>
                                                    <div class="grade-display mt-3">
                                            <span class="badge badge-<?php echo $assign->grade == 'A' || $assign->grade == 'B' ? 'success' : 'warning'; ?>" style="font-size: 18px; padding: 10px 15px;">
                                                <?php echo $assign->marks_obtained; ?> / <?php echo $assign->total_marks; ?> (<?php echo $assign->grade; ?>)
                                            </span>
                                                    </div>
                                                <?php endif; ?>

                                                <div class="mt-3">
                                                    <a href="view-assignment?id=<?php echo $assign->id; ?>" class="btn btn-primary btn-block">
                                                        <i class="fas fa-eye"></i> View Details
                                                    </a>
                                                </div>
                                            </div>
                                        </div>
                                    </div>
                                    <?php
                                }
                            } else {
                                echo '<div class="col-12"><div class="alert alert-info">No assignments found</div></div>';
                            }
                            ?>
                        </div>
                    </div>
                </div>

            </div>
        </section>
        <!-- /.content -->
    </div>
    <!-- /.content-wrapper -->
    <?php include "../admin/includes/footer.php";?>


    <script>
        $(document).ready(function(){
            $('.select2').select2();

        })

    </script>

    <script>
        $(function() {
            // Tab filtering
            $('#assignmentTabs a').on('click', function(e) {
                e.preventDefault();
                var target = $(this).attr('href');

                if (target === '#all') {
                    $('.assignment-item').show();
                } else if (target === '#pending') {
                    $('.assignment-item').hide();
                    $('.assignment-item[data-status="not_submitted"]').show();
                } else if (target === '#submitted') {
                    $('.assignment-item').hide();
                    $('.assignment-item[data-status="submitted"], .assignment-item[data-status="late_submitted"]').show();
                } else if (target === '#graded') {
                    $('.assignment-item').hide();
                    $('.assignment-item[data-graded="1"]').show();
                } else if (target === '#overdue') {
                    $('.assignment-item').hide();
                    // Filter assignments that are overdue based on current date and due date
                    $('.assignment-item').each(function() {
                        var $item = $(this);
                        var assign = $item.data('assign');
                        if (assign && assign.submission_status == 'not_submitted') {
                            var now = new Date();
                            var dueDate = new Date(assign.due_date);
                            if (now > dueDate) {
                                $item.show();
                            }
                        }
                    });
                }

                $(this).tab('show');
            });
        });
    </script>


    <?php
    }else{
    ?>
    <!DOCTYPE html>
    <html lang="en">
    <head>
        <meta charset="utf-8">
        <meta http-equiv="X-UA-Compatible" content="IE=edge">
        <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1, shrink-to-fit=no">


        <?php include '../admin/includes/styles.php'; ?>
        <title>Login to continue </title>

        <!-- END PAGE LEVEL PLUGINS/CUSTOM STYLES -->

    </head>
    <body>
    <div class="container" style="margin-top: 20%;">
        <div class="row">
            <div class="col-lg-12 col-md-12">
                <div class="section-title text-center">
                    <h6> You need to login to continue </h6>
                    <div class="spinner-border text-info" role="status">
                        <span class="sr-only">Loading...</span>
                        <meta http-equiv='refresh' content='3; <?php echo $student_login_link; ?>'>
                    </div>
                    <p>Redirecting to the login  page...</p>
                </div>
            </div>
        </div>
    </div>

    <?php
    }

    ?>
