<?php
session_start();
include 'layouts/session.php';
include 'layouts/main.php';
include 'layouts/dbconn.php';

if (!isset($_SESSION['user_id'])) {
    header('Location: login.php');
    exit();
}

$doctor_id = $_SESSION['user_id'];

// Get today's surgeries
$today_query = "SELECT COUNT(*) as count 
                FROM procedures 
                WHERE doctor_id = ? 
                AND DATE(procedure_date) = CURDATE() 
                AND procedure_status IN ('SCHEDULED', 'IN_PROGRESS')";
$today_stmt = $conn->prepare($today_query);
$today_stmt->bind_param("i", $doctor_id);
$today_stmt->execute();
$today_count = $today_stmt->get_result()->fetch_assoc()['count'];

// Get this week's surgeries
$week_query = "SELECT COUNT(*) as count 
               FROM procedures 
               WHERE doctor_id = ? 
               AND YEARWEEK(procedure_date) = YEARWEEK(CURDATE()) 
               AND procedure_status IN ('SCHEDULED', 'IN_PROGRESS')";
$week_stmt = $conn->prepare($week_query);
$week_stmt->bind_param("i", $doctor_id);
$week_stmt->execute();
$week_count = $week_stmt->get_result()->fetch_assoc()['count'];
?>

<!DOCTYPE html>
<html>
<head>
    <?php includeFileWithVariables('layouts/title-meta.php', array('title' => 'My Surgery Schedule')); ?>
    <?php include 'layouts/head-css.php'; ?>
    <link href="assets/libs/fullcalendar/main.min.css" rel="stylesheet" type="text/css" />
    <link href="assets/libs/sweetalert2/sweetalert2.min.css" rel="stylesheet">
</head>

<body>
    <div id="layout-wrapper">
        <?php include 'layouts/menu.php'; ?>

        <div class="main-content">
            <div class="page-content">
                <div class="container-fluid">
                    <!-- Page Title -->
                    <div class="row">
                        <div class="col-12">
                            <div class="page-title-box d-sm-flex align-items-center justify-content-between">
                                <h4 class="mb-sm-0">My Surgery Schedule</h4>
                                <div class="page-title-right">
                                    <a href="schedule_procedure.php" class="btn btn-success">
                                        <i class="ri-add-line me-1"></i> Schedule Surgery
                                    </a>
                                </div>
                            </div>
                        </div>
                    </div>

                    <!-- Quick Stats -->
                    <div class="row">
                        <div class="col-md-6">
                            <div class="card card-animate">
                                <div class="card-body">
                                    <div class="d-flex align-items-center">
                                        <div class="flex-grow-1">
                                            <p class="text-uppercase fw-medium text-muted mb-0">Today's Surgeries</p>
                                        </div>
                                    </div>
                                    <div class="d-flex align-items-end justify-content-between mt-4">
                                        <div>
                                            <h4 class="fs-22 fw-semibold ff-secondary mb-4"><?php echo $today_count; ?></h4>
                                        </div>
                                        <div class="avatar-sm flex-shrink-0">
                                            <span class="avatar-title bg-soft-success rounded fs-3">
                                                <i class="bx bx-calendar-check text-success"></i>
                                            </span>
                                        </div>
                                    </div>
                                </div>
                            </div>
                        </div>

                        <div class="col-md-6">
                            <div class="card card-animate">
                                <div class="card-body">
                                    <div class="d-flex align-items-center">
                                        <div class="flex-grow-1">
                                            <p class="text-uppercase fw-medium text-muted mb-0">This Week</p>
                                        </div>
                                    </div>
                                    <div class="d-flex align-items-end justify-content-between mt-4">
                                        <div>
                                            <h4 class="fs-22 fw-semibold ff-secondary mb-4"><?php echo $week_count; ?></h4>
                                        </div>
                                        <div class="avatar-sm flex-shrink-0">
                                            <span class="avatar-title bg-soft-info rounded fs-3">
                                                <i class="bx bx-calendar-week text-info"></i>
                                            </span>
                                        </div>
                                    </div>
                                </div>
                            </div>
                        </div>
                    </div>

                    <!-- Calendar -->
                    <div class="row">
                        <div class="col-12">
                            <div class="card">
                                <div class="card-body">
                                    <div id="calendar"></div>
                                </div>
                            </div>
                        </div>
                    </div>
                </div>
            </div>
            <?php include 'layouts/footer.php'; ?>
        </div>
    </div>

    <!-- Procedure Details Modal -->
    <div class="modal fade" id="procedureModal" tabindex="-1">
        <div class="modal-dialog modal-lg">
            <div class="modal-content">
                <div class="modal-header">
                    <h5 class="modal-title">Procedure Details</h5>
                    <button type="button" class="btn-close" data-bs-dismiss="modal"></button>
                </div>
                <div class="modal-body" id="procedureModalBody">
                    <!-- Content loaded via AJAX -->
                </div>
            </div>
        </div>
    </div>

    <?php include 'layouts/vendor-scripts.php'; ?>
    <script src="assets/libs/fullcalendar/main.min.js"></script>
    <script src="assets/libs/sweetalert2/sweetalert2.min.js"></script>
    <script src="assets/js/app.js"></script>
    
    <script>
        document.addEventListener('DOMContentLoaded', function() {
            var calendarEl = document.getElementById('calendar');
            var calendar = new FullCalendar.Calendar(calendarEl, {
                headerToolbar: {
                    left: 'prev,next today',
                    center: 'title',
                    right: 'dayGridMonth,timeGridWeek,timeGridDay,listWeek'
                },
                initialView: 'timeGridWeek',
                editable: false,
                selectable: true,
                selectMirror: true,
                dayMaxEvents: true,
                events: 'ajax/get_surgery_schedule.php',
                eventClick: function(info) {
                    var procedureId = info.event.id;
$('#procedureModalBody').html('<div class="text-center py-4"><div class="spinner-border text-primary"></div></div>');
$('#procedureModal').modal('show');
$.ajax({
                    url: 'ajax/get_procedure_details.php',
                    type: 'POST',
                    data: { procedure_id: procedureId },
                    success: function(response) {
                        $('#procedureModalBody').html(response);
                    }
                });
            },
            eventDidMount: function(info) {
                // Color code by status
                if (info.event.extendedProps.status === 'SCHEDULED') {
                    info.el.style.backgroundColor = '#0ab39c';
                } else if (info.event.extendedProps.status === 'IN_PROGRESS') {
                    info.el.style.backgroundColor = '#f7b84b';
                } else if (info.event.extendedProps.status === 'COMPLETED') {
                    info.el.style.backgroundColor = '#299cdb';
                }
            }
        });

        calendar.render();
    });
</script>
</body>
</html>