<?php
session_start();
require_once __DIR__ . '/../includes/auth.php';
require_login();


global $con;
date_default_timezone_set('Asia/Kolkata');

/* ---------------- Date Helpers ---------------- */
function h($s)
{
    return htmlspecialchars((string)$s, ENT_QUOTES, 'UTF-8');
}

function parse_ddmmyyyy(string $s): ?string
{
    $s = trim($s);
    if ($s === '') return null;

    $p = explode('-', $s);
    if (count($p) !== 3) return null;

    [$dd, $mm, $yy] = $p;
    if (!ctype_digit($dd . $mm . $yy)) return null;
    if (!checkdate((int)$mm, (int)$dd, (int)$yy)) return null;

    return sprintf('%04d-%02d-%02d', (int)$yy, (int)$mm, (int)$dd);
}

/* ---------------- Date Range ---------------- */
$from_ui = $_GET['from'] ?? date('d-m-Y', strtotime('-30 days'));
$to_ui   = $_GET['to']   ?? date('d-m-Y');

$from = parse_ddmmyyyy($from_ui) ?: date('Y-m-d', strtotime('-30 days'));
$to   = parse_ddmmyyyy($to_ui)   ?: date('Y-m-d');

$from_ui = date('d-m-Y', strtotime($from));
$to_ui   = date('d-m-Y', strtotime($to));

/* ---------------- Role ---------------- */
$session = $_SESSION['admin_user'];
$role_id = $session['role_id'] ?? 0;
?>
<!doctype html>
<html>

<head>
    <meta charset="utf-8">
    <title>Dashboard</title>

    <link rel="stylesheet" href="/adminconsole/assets/ui.css">
    <link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/flatpickr/dist/flatpickr.min.css">

    <style>
        .filter-bar {
            display: flex;
            gap: 10px;
            align-items: flex-end;
            justify-content: flex-end;
            flex-wrap: wrap;
        }

        .filter-bar label {
            color: rgba(255, 255, 255, .75);
            font-size: 12px;
            display: block;
            margin-bottom: 6px;
        }

        .filter-bar input {
            height: 38px;
            border-radius: 10px;
            border: 1px solid rgba(255, 255, 255, 0.12);
            background: rgba(0, 0, 0, 0.22);
            color: #fff;
            padding: 0 10px;
            min-width: 160px;
        }

        .btn {
            height: 38px;
            border-radius: 10px;
            border: 1px solid rgba(255, 255, 255, 0.10);
            padding: 0 14px;
            cursor: pointer;
            font-weight: 700;
            background: rgba(255, 255, 255, 0.10);
            color: #fff;
        }

        .btn.primary {
            background: #2563eb;
            border-color: #2563eb;
        }

        .btn:hover {
            filter: brightness(1.05);
        }

        .dash-title {
            font-size: 22px;
            font-weight: 900;
            color: #fff;
        }

        .dash-sub {
            font-size: 12px;
            color: rgba(255, 255, 255, .7);
        }
    </style>
</head>

<body>
    <div class="master-wrap">

        <!-- Header -->
        <div style="display:flex;align-items:center;justify-content:space-between;margin-bottom:18px;">
            <div>
                <div class="dash-title">Dashboard</div>
                <div class="dash-sub">Filter all cards by date range</div>
            </div>

            <form method="get" class="filter-bar">
                <div>
                    <label>From</label>
                    <input id="from" name="from" value="<?= h($from_ui) ?>">
                </div>
                <div>
                    <label>To</label>
                    <input id="to" name="to" value="<?= h($to_ui) ?>">
                </div>
                <button class="btn primary" type="submit">Apply</button>
                <a class="btn" href="index.php" style="display:inline-flex;align-items:center;text-decoration:none;">
                    Reset
                </a>
            </form>
        </div>

        <!-- Role Based Dashboard -->
        <div>
            <?php

            switch ($role_id) {

                case 1:
                    // include 'superadmin_dashboard.php';
                    echo "Hello ".$session['name']." Role id = ".$session['role_id'];
                    break;

                case 2:
                    include 'admin_dashboard.php';
                    break;

                case 3:
                    include 'outreach_executive_dashboard.php';
                    break;

                case 4:
                    include 'recruiter_admin_dashboard.php';
                    break;

                case 10:
                    include 'director_dashboard.php';
                    break;

                case 11:
                    include 'receptionist_cum_app_admin_dashboard.php';
                    break;

                case 12:
                    include 'operations_manager_dashboard.php';
                    break;

                case 13:
                    include 'relationship_manager_dashboard.php';
                    break;

                case 14:
                    include 'digital_marketing_executive_dashboard.php';
                    break;

                case 15:
                    include 'accounts_assistant_dashboard.php';
                    break;

                case 16:
                    include 'business_development_manager_dashboard.php';
                    break;

                default:
                    echo "<div style='color:red;'>No dashboard assigned for this role.</div>";
            }
            ?>

        </div>

    </div>

    <script src="https://cdn.jsdelivr.net/npm/flatpickr"></script>
    <script>
        flatpickr("#from", {
            dateFormat: "d-m-Y",
            allowInput: true
        });
        flatpickr("#to", {
            dateFormat: "d-m-Y",
            allowInput: true
        });
    </script>

</body>

</html>