<?php
session_start();
require_once '../config/database.php';

// Auth check: admin or teacher can view
if (!isset($_SESSION['user_id']) || !in_array($_SESSION['user_role'], ['admin','teacher'])) {
    header('Location: ../login.php');
    exit();
}

// Inputs
$report_type = $_GET['type'] ?? 'daily'; // daily | range | student
$class_id = isset($_GET['class_id']) ? (int)$_GET['class_id'] : 0; // 0 = all
$selected_session = $_GET['session'] ?? date('Y');
$date = $_GET['date'] ?? date('Y-m-d');
$start_date = $_GET['start_date'] ?? date('Y-m-01');
$end_date = $_GET['end_date'] ?? date('Y-m-d');
$status_filter = $_GET['status'] ?? ''; // present | absent | late | excused | ''
$show_records = isset($_GET['show_records']) ? (int)$_GET['show_records'] : 0; // for student view
$export = $_GET['export'] ?? ''; // 'csv' to export

// Load classes for dropdown
$classes = $pdo->query("SELECT id, name, section FROM classes WHERE status='active' ORDER BY name, section")->fetchAll(PDO::FETCH_ASSOC);

// Load sessions for dropdown
$sessions = $pdo->query("SELECT DISTINCT session FROM students WHERE session IS NOT NULL AND session != '' ORDER BY session DESC")->fetchAll(PDO::FETCH_COLUMN);
if (empty($sessions)) {
    $currentYear = date('Y');
    for ($i = 0; $i < 5; $i++) {
        $sessions[] = $currentYear - $i;
    }
}

// Helper to find class name
function findClassName($classes, $cid) {
    foreach ($classes as $c) { if ((int)$c['id'] === (int)$cid) { return $c['name'] . ' - ' . $c['section']; } }
    return 'সব ক্লাস';
}

// Data holders
$daily_summary = [];
$trend_data = [];
$class_range_summary = [];
$student_summary = [];
$student_records = [];

try {
    if ($report_type === 'daily') {
        $params = [$date, $selected_session];
        $classCond = '';
        if ($class_id > 0) { $classCond = ' AND s.class_id = ?'; $params[] = $class_id; }
        $stmt = $pdo->prepare("SELECT 
                c.id as class_id, c.name, c.section,
                SUM(CASE WHEN a.status = 'present' THEN 1 ELSE 0 END) as present_count,
                SUM(CASE WHEN a.status = 'absent' THEN 1 ELSE 0 END) as absent_count,
                SUM(CASE WHEN a.status = 'late' THEN 1 ELSE 0 END) as late_count,
                SUM(CASE WHEN a.status = 'excused' THEN 1 ELSE 0 END) as excused_count,
                COUNT(a.id) as total
            FROM students s
            JOIN classes c ON s.class_id = c.id
            LEFT JOIN attendance a ON a.student_id = s.id AND a.date = ?
            WHERE s.session = ? AND c.status = 'active' {$classCond}
            GROUP BY c.id
            ORDER BY c.name, c.section");
        $stmt->execute($params);
        $daily_summary = $stmt->fetchAll(PDO::FETCH_ASSOC);
    }

    if ($report_type === 'range') {
        $trendParams = [$selected_session, $start_date, $end_date];
        $trendClassCond = '';
        if ($class_id > 0) { $trendClassCond = ' AND s.class_id = ?'; $trendParams[] = $class_id; }
        $trendSql = "SELECT 
                a.date as day,
                COUNT(a.id) as total,
                SUM(CASE WHEN a.status='present' THEN 1 ELSE 0 END) as present_count,
                SUM(CASE WHEN a.status='absent' THEN 1 ELSE 0 END) as absent_count,
                SUM(CASE WHEN a.status='late' THEN 1 ELSE 0 END) as late_count,
                SUM(CASE WHEN a.status='excused' THEN 1 ELSE 0 END) as excused_count
            FROM attendance a
            JOIN students s ON a.student_id = s.id
            WHERE s.session = ? AND a.date BETWEEN ? AND ? {$trendClassCond}
            GROUP BY a.date
            ORDER BY a.date";
        $stmt = $pdo->prepare($trendSql);
        $stmt->execute($trendParams);
        $trend_data = $stmt->fetchAll(PDO::FETCH_ASSOC);

        $rangeParams = [$start_date, $end_date, $selected_session];
        $rangeClassCond = '';
        if ($class_id > 0) { $rangeClassCond = ' AND s.class_id = ?'; $rangeParams[] = $class_id; }
        $rangeSql = "SELECT 
                c.id as class_id, c.name, c.section,
                COUNT(a.id) as total,
                SUM(CASE WHEN a.status='present' THEN 1 ELSE 0 END) as present_count,
                SUM(CASE WHEN a.status='absent' THEN 1 ELSE 0 END) as absent_count,
                SUM(CASE WHEN a.status='late' THEN 1 ELSE 0 END) as late_count,
                SUM(CASE WHEN a.status='excused' THEN 1 ELSE 0 END) as excused_count
            FROM students s
            JOIN classes c ON s.class_id = c.id
            LEFT JOIN attendance a ON a.student_id = s.id AND a.date BETWEEN ? AND ?
            WHERE s.session = ? AND c.status='active' {$rangeClassCond}
            GROUP BY c.id
            ORDER BY c.name, c.section";
        $stmt = $pdo->prepare($rangeSql);
        $stmt->execute($rangeParams);
        $class_range_summary = $stmt->fetchAll(PDO::FETCH_ASSOC);
    }

    if ($report_type === 'student') {
        $where = "WHERE s.status='active'";
        $params = [$start_date, $end_date];

        if ($class_id > 0) {
            $where .= " AND s.class_id = ?";
            $params[] = $class_id;
        }
        
        $where .= " AND s.session = ?";
        $params[] = $selected_session;

        $sql = "SELECT 
                s.id, s.student_id, s.first_name, s.last_name, s.roll_number,
                c.name as class_name, c.section,
                SUM(CASE WHEN a.status='present' THEN 1 ELSE 0 END) as present_count,
                SUM(CASE WHEN a.status='absent' THEN 1 ELSE 0 END) as absent_count,
                SUM(CASE WHEN a.status='late' THEN 1 ELSE 0 END) as late_count,
                SUM(CASE WHEN a.status='excused' THEN 1 ELSE 0 END) as excused_count,
                COUNT(a.id) as total
            FROM students s
            JOIN classes c ON c.id = s.class_id
            LEFT JOIN attendance a ON a.student_id = s.id AND a.date BETWEEN ? AND ?
            {$where}
            GROUP BY s.id
            ORDER BY c.name, c.section, s.roll_number, s.first_name";
        
        $stmt = $pdo->prepare($sql);
        $stmt->execute($params);
        $student_summary = $stmt->fetchAll(PDO::FETCH_ASSOC);

        if ($show_records) {
            $recParams = [$selected_session, $start_date, $end_date];
            $recWhere = " WHERE s.session = ? AND a.date BETWEEN ? AND ?";
            if ($class_id > 0) { $recWhere .= " AND s.class_id = ?"; $recParams[] = $class_id; }
            if ($status_filter !== '') { $recWhere .= " AND a.status = ?"; $recParams[] = $status_filter; }
            $recSql = "SELECT 
                    a.date, c.name as class_name, c.section, s.roll_number,
                    s.first_name, s.last_name, s.student_id, a.status, a.remarks
                FROM attendance a
                JOIN students s ON s.id = a.student_id
                JOIN classes c ON c.id = a.class_id
                {$recWhere}
                ORDER BY a.date, c.name, c.section, s.roll_number";
            $stmt = $pdo->prepare($recSql);
            $stmt->execute($recParams);
            $student_records = $stmt->fetchAll(PDO::FETCH_ASSOC);
        }
    }
} catch (Exception $e) {
    $error = $e->getMessage();
}

if ($export === 'csv') {
    header('Content-Type: text/csv; charset=utf-8');
    $filename = 'attendance_' . $report_type . '_' . $selected_session . '_' . date('Ymd_His') . '.csv';
    header('Content-Disposition: attachment; filename=' . $filename);
    $out = fopen('php://output', 'w');

    if ($report_type === 'daily') {
        fputcsv($out, ['Class','Section','Session','Present','Absent','Late','Excused','Total','Present %']);
        foreach ($daily_summary as $row) {
            $rate = ($row['total'] > 0) ? round(($row['present_count'] * 100.0) / $row['total'], 2) : 0;
            fputcsv($out, [$row['name'], $row['section'], $selected_session, $row['present_count'], $row['absent_count'], $row['late_count'], $row['excused_count'], $row['total'], $rate]);
        }
    } elseif ($report_type === 'range') {
        fputcsv($out, ['Date','Present','Total','Present %']);
        foreach ($trend_data as $row) {
            $rate = ($row['total'] > 0) ? round(($row['present_count'] * 100.0) / $row['total'], 2) : 0;
            fputcsv($out, [$row['day'], $row['present_count'], $row['total'], $rate]);
        }
        fputcsv($out, []);
        fputcsv($out, ['Class','Section','Session','Present','Absent','Late','Excused','Total','Present %']);
        foreach ($class_range_summary as $row) {
            $rate = ($row['total']>0) ? round(($row['present_count']*100.0)/$row['total'],2) : 0;
            fputcsv($out, [$row['name'], $row['section'], $selected_session, $row['present_count'], $row['absent_count'], $row['late_count'], $row['excused_count'], $row['total'], $rate]);
        }
    } elseif ($report_type === 'student') {
        if ($show_records) {
            fputcsv($out, ['Date','Class','Section','Session','Roll','Name','Student ID','Status','Remarks']);
            foreach ($student_records as $row) {
                $name = $row['first_name'] . ' ' . $row['last_name'];
                fputcsv($out, [$row['date'], $row['class_name'], $row['section'], $selected_session, $row['roll_number'], $name, $row['student_id'], $row['status'], $row['remarks']]);
            }
        } else {
            fputcsv($out, ['Class','Section','Session','Roll','Name','Student ID','Present','Absent','Late','Excused','Total','Present %']);
            foreach ($student_summary as $row) {
                $total = (int)$row['total'];
                $rate = ($total > 0) ? round(($row['present_count'] * 100.0) / $total, 2) : 0;
                $name = $row['first_name'] . ' ' . $row['last_name'];
                fputcsv($out, [$row['class_name'], $row['section'], $selected_session, $row['roll_number'], $name, $row['student_id'], $row['present_count'], $row['absent_count'], $row['late_count'], $row['excused_count'], $total, $rate]);
            }
        }
    }

    fclose($out);
    exit;
}

?>
<!DOCTYPE html>
<html lang="bn">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>উপস্থিতি রিপোর্ট</title>
    <link href="https://cdn.jsdelivr.net/npm/bootstrap@5.1.3/dist/css/bootstrap.min.css" rel="stylesheet">
    <link href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.0.0/css/all.min.css" rel="stylesheet">
    <link href="../assets/css/style.css" rel="stylesheet">
    <script src="https://cdn.jsdelivr.net/npm/chart.js"></script>
</head>
<body>
<nav class="navbar navbar-expand-lg navbar-dark bg-primary">
    <div class="container-fluid">
        <a class="navbar-brand" href="../index.php"><i class="fas fa-graduation-cap"></i> স্কুল ম্যানেজমেন্ট সিস্টেম</a>
        <div class="navbar-nav ms-auto">
            <a class="nav-link" href="../index.php"><i class="fas fa-home"></i> হোম</a>
            <a class="nav-link" href="../logout.php"><i class="fas fa-sign-out-alt"></i> লগআউট</a>
        </div>
    </div>
</nav>
<div class="container-fluid mt-4">
    <div class="d-flex justify-content-between align-items-center mb-3">
        <h2><i class="fas fa-clipboard-check"></i> উপস্থিতি রিপোর্ট</h2>
        <div>
            <a href="?<?=http_build_query(array_merge($_GET,['export'=>'csv']))?>" class="btn btn-success me-2"><i class="fas fa-file-csv"></i> CSV</a>
            <button onclick="window.print()" class="btn btn-primary"><i class="fas fa-print"></i> প্রিন্ট</button>
        </div>
    </div>

    <!-- Filters -->
    <form method="GET" class="card mb-4 p-3">
        <div class="row g-3 align-items-end">
            <div class="col-md-2">
                <label class="form-label">রিপোর্ট টাইপ</label>
                <select class="form-select" name="type">
                    <option value="daily" <?= $report_type==='daily'?'selected':''; ?>>দৈনিক</option>
                    <option value="range" <?= $report_type==='range'?'selected':''; ?>>রেঞ্জ</option>
                    <option value="student" <?= $report_type==='student'?'selected':''; ?>>স্টুডেন্ট</option>
                </select>
            </div>
            <div class="col-md-2">
                <label class="form-label">ক্লাস</label>
                <select class="form-select" name="class_id">
                    <option value="0">সব ক্লাস</option>
                    <?php foreach ($classes as $c): ?>
                        <option value="<?=$c['id']?>" <?= $class_id==(int)$c['id']?'selected':''; ?>><?=$c['name']?> - <?=$c['section']?></option>
                    <?php endforeach; ?>
                </select>
            </div>
            <div class="col-md-2">
                <label class="form-label">সেশন</label>
                <select class="form-select" name="session">
                    <?php foreach ($sessions as $session): ?>
                        <option value="<?= $session ?>" <?= $selected_session == $session ? 'selected' : ''; ?>><?= $session ?></option>
                    <?php endforeach; ?>
                </select>
            </div>

            <?php if ($report_type==='daily'): ?>
                <div class="col-md-3">
                    <label class="form-label">তারিখ</label>
                    <input type="date" class="form-control" name="date" value="<?=htmlspecialchars($date)?>" required>
                </div>
            <?php else: ?>
                <div class="col-md-2">
                    <label class="form-label">শুরুর তারিখ</label>
                    <input type="date" class="form-control" name="start_date" value="<?=htmlspecialchars($start_date)?>" required>
                </div>
                <div class="col-md-2">
                    <label class="form-label">শেষ তারিখ</label>
                    <input type="date" class="form-control" name="end_date" value="<?=htmlspecialchars($end_date)?>" required>
                </div>
            <?php endif; ?>

            <?php if ($report_type==='student'): ?>
                <div class="col-md-2">
                    <label class="form-label">স্ট্যাটাস</label>
                    <select class="form-select" name="status">
                        <option value="" <?= $status_filter===''?'selected':''; ?>>সব</option>
                        <option value="present" <?= $status_filter==='present'?'selected':''; ?>>Present</option>
                        <option value="absent" <?= $status_filter==='absent'?'selected':''; ?>>Absent</option>
                        <option value="late" <?= $status_filter==='late'?'selected':''; ?>>Late</option>
                        <option value="excused" <?= $status_filter==='excused'?'selected':''; ?>>Excused</option>
                    </select>
                </div>
                <div class="col-md-2 form-check mt-4">
                    <input class="form-check-input" type="checkbox" id="show_records" name="show_records" value="1" <?= $show_records? 'checked':''; ?>>
                    <label class="form-check-label" for="show_records">দৈনিক রেকর্ড</label>
                </div>
            <?php endif; ?>

            <div class="col-md-2">
                <button class="btn btn-primary"><i class="fas fa-search"></i> দেখুন</button>
            </div>
        </div>
    </form>

    <?php if (!empty($error)): ?>
        <div class="alert alert-danger"><i class="fas fa-exclamation-triangle"></i> <?=htmlspecialchars($error)?></div>
    <?php endif; ?>

    <?php if ($report_type==='daily'): ?>
        <div class="card mb-4">
            <div class="card-header d-flex justify-content-between align-items-center">
                <h5><i class="fas fa-calendar-day"></i> দৈনিক সারাংশ (<?=date('d/m/Y', strtotime($date))?>) - <?=htmlspecialchars(findClassName($classes,$class_id))?> (সেশন: <?=$selected_session?>)</h5>
            </div>
            <div class="card-body table-responsive">
                <table class="table table-striped">
                    <thead>
                        <tr><th>ক্লাস</th><th>শাখা</th><th>Present</th><th>Absent</th><th>Late</th><th>Excused</th><th>Total</th><th>Present %</th></tr>
                    </thead>
                    <tbody>
                        <?php foreach ($daily_summary as $row): ?>
                            <?php $rate = ($row['total']>0) ? round(($row['present_count']*100.0)/$row['total'],2) : 0; ?>
                            <tr>
                                <td><?=$row['name']?></td><td><?=$row['section']?></td>
                                <td><span class="badge bg-success"><?=$row['present_count']?:0?></span></td>
                                <td><span class="badge bg-danger"><?=$row['absent_count']?:0?></span></td>
                                <td><span class="badge bg-warning text-dark"><?=$row['late_count']?:0?></span></td>
                                <td><span class="badge bg-info text-dark"><?=$row['excused_count']?:0?></span></td>
                                <td><?=$row['total']?:0?></td><td><?=$rate?>%</td>
                            </tr>
                        <?php endforeach; ?>
                        <?php if (empty($daily_summary)): ?>
                            <tr><td colspan="8" class="text-center text-muted">কোন ডাটা নেই</td></tr>
                        <?php endif; ?>
                    </tbody>
                </table>
            </div>
        </div>
    <?php elseif ($report_type==='range'): ?>
        <div class="row">
            <div class="col-md-6">
                <div class="card mb-4">
                    <div class="card-header"><h5><i class="fas fa-chart-line"></i> উপস্থিতি ট্রেন্ড (সেশন: <?=$selected_session?>)</h5></div>
                    <div class="card-body"><canvas id="trendChart" height="160"></canvas></div>
                </div>
            </div>
            <div class="col-md-6">
                <div class="card mb-4">
                    <div class="card-header"><h5><i class="fas fa-list"></i> ক্লাসভিত্তিক সারাংশ - <?=htmlspecialchars(findClassName($classes,$class_id))?> (সেশন: <?=$selected_session?>)</h5></div>
                    <div class="card-body table-responsive">
                        <table class="table table-striped">
                            <thead><tr><th>ক্লাস</th><th>শাখা</th><th>Present</th><th>Absent</th><th>Late</th><th>Excused</th><th>Total</th><th>Present %</th></tr></thead>
                            <tbody>
                                <?php foreach ($class_range_summary as $row): ?>
                                    <?php $rate = ($row['total']>0) ? round(($row['present_count']*100.0)/$row['total'],2) : 0; ?>
                                    <tr>
                                        <td><?=$row['name']?></td><td><?=$row['section']?></td>
                                        <td><span class="badge bg-success"><?=$row['present_count']?:0?></span></td>
                                        <td><span class="badge bg-danger"><?=$row['absent_count']?:0?></span></td>
                                        <td><span class="badge bg-warning text-dark"><?=$row['late_count']?:0?></span></td>
                                        <td><span class="badge bg-info text-dark"><?=$row['excused_count']?:0?></span></td>
                                        <td><?=$row['total']?:0?></td><td><?=$rate?>%</td>
                                    </tr>
                                <?php endforeach; ?>
                                <?php if (empty($class_range_summary)): ?>
                                    <tr><td colspan="8" class="text-center text-muted">কোন ডাটা নেই</td></tr>
                                <?php endif; ?>
                            </tbody>
                        </table>
                    </div>
                </div>
            </div>
        </div>
    <?php else: // student report ?>
        <div class="card mb-4">
            <div class="card-header d-flex justify-content-between align-items-center">
                <h5><i class="fas fa-user-check"></i> স্টুডেন্ট ডিটেইলস - <?=htmlspecialchars(findClassName($classes,$class_id))?> (সেশন: <?=$selected_session?>)</h5>
            </div>
            <div class="card-body table-responsive">
                <?php if (!$show_records): ?>
                    <table class="table table-striped">
                        <thead><tr><th>ক্লাস</th><th>শাখা</th><th>সেশন</th><th>রোল</th><th>নাম</th><th>স্টুডেন্ট আইডি</th><th>Present</th><th>Absent</th><th>Late</th><th>Excused</th><th>Total</th><th>Present %</th></tr></thead>
                        <tbody>
                            <?php foreach ($student_summary as $row): ?>
                                <?php $total = (int)$row['total']; $rate = $total>0 ? round(($row['present_count']*100.0)/$total,2) : 0; ?>
                                <tr>
                                    <td><?=$row['class_name']?></td><td><?=$row['section']?></td><td><?=$selected_session?></td><td><?=$row['roll_number']?></td>
                                    <td><strong><?=$row['first_name'].' '.$row['last_name']?></strong></td><td><?=$row['student_id']?></td>
                                    <td><span class="badge bg-success"><?=$row['present_count']?:0?></span></td>
                                    <td><span class="badge bg-danger"><?=$row['absent_count']?:0?></span></td>
                                    <td><span class="badge bg-warning text-dark"><?=$row['late_count']?:0?></span></td>
                                    <td><span class="badge bg-info text-dark"><?=$row['excused_count']?:0?></span></td>
                                    <td><?=$total?></td><td><?=$rate?>%</td>
                                </tr>
                            <?php endforeach; ?>
                            <?php if (empty($student_summary)): ?>
                                <tr><td colspan="12" class="text-center text-muted">কোন ডাটা নেই</td></tr>
                            <?php endif; ?>
                        </tbody>
                    </table>
                <?php else: ?>
                    <table class="table table-striped">
                        <thead><tr><th>তারিখ</th><th>ক্লাস</th><th>শাখা</th><th>সেশন</th><th>রোল</th><th>নাম</th><th>স্টুডেন্ট আইডি</th><th>স্ট্যাটাস</th><th>মন্তব্য</th></tr></thead>
                        <tbody>
                            <?php foreach ($student_records as $row): ?>
                                <tr>
                                    <td><?=date('d/m/Y', strtotime($row['date']))?></td><td><?=$row['class_name']?></td><td><?=$row['section']?></td><td><?=$selected_session?></td>
                                    <td><?=$row['roll_number']?></td><td><strong><?=$row['first_name'].' '.$row['last_name']?></strong></td>
                                    <td><?=$row['student_id']?></td><td><?=ucfirst($row['status'])?></td><td><?=htmlspecialchars($row['remarks'])?></td>
                                </tr>
                            <?php endforeach; ?>
                            <?php if (empty($student_records)): ?>
                                <tr><td colspan="9" class="text-center text-muted">কোন ডাটা নেই</td></tr>
                            <?php endif; ?>
                        </tbody>
                    </table>
                <?php endif; ?>
            </div>
        </div>
    <?php endif; ?>
</div>

<?php if ($report_type==='range' && !empty($trend_data)): ?>
<script>
const trendCtx = document.getElementById('trendChart').getContext('2d');
const trendLabels = <?=json_encode(array_map(function($r){return $r['day'];}, $trend_data))?>;
const presentData = <?=json_encode(array_map(function($r){return (int)($r['present_count'] ?? 0);}, $trend_data))?>;
const absentData = <?=json_encode(array_map(function($r){return (int)($r['absent_count'] ?? 0);}, $trend_data))?>;
const lateData = <?=json_encode(array_map(function($r){return (int)($r['late_count'] ?? 0);}, $trend_data))?>;
const excusedData = <?=json_encode(array_map(function($r){return (int)($r['excused_count'] ?? 0);}, $trend_data))?>;
const totalData = <?=json_encode(array_map(function($r){return (int)($r['total'] ?? 0);}, $trend_data))?>;
const rateData = presentData.map((p, i) => totalData[i] > 0 ? Math.round((p * 100.0) / totalData[i] * 100) / 100 : 0);
new Chart(trendCtx, {
    type: 'line',
    data: {
        labels: trendLabels,
        datasets: [
            { label: 'Present %', data: rateData, borderColor: '#198754', backgroundColor: 'rgba(25,135,84,0.1)', yAxisID: 'y1' },
            { label: 'Present', data: presentData, borderColor: '#198754', backgroundColor: 'rgba(25,135,84,0.1)', borderDash: [5, 5], yAxisID: 'y2' },
            { label: 'Absent', data: absentData, borderColor: '#dc3545', backgroundColor: 'rgba(220,53,69,0.1)', borderDash: [5, 5], yAxisID: 'y2' },
            { label: 'Late', data: lateData, borderColor: '#ffc107', backgroundColor: 'rgba(255,193,7,0.1)', borderDash: [5, 5], yAxisID: 'y2' },
            { label: 'Excused', data: excusedData, borderColor: '#0dcaf0', backgroundColor: 'rgba(13,202,240,0.1)', borderDash: [5, 5], yAxisID: 'y2' },
            { label: 'Total', data: totalData, borderColor: '#6c757d', backgroundColor: 'rgba(108,117,125,0.1)', yAxisID: 'y2' }
        ]
    },
    options: {
        responsive: true,
        interaction: { mode: 'index', intersect: false },
        scales: {
            y1: { type: 'linear', position: 'left', min: 0, max: 100, ticks: { callback: v => v + '%' } },
            y2: { type: 'linear', position: 'right', grid: { drawOnChartArea: false } }
        }
    }
});
</script>
<?php endif; ?>
</body>
</html>