<?php
require_once $_SERVER['DOCUMENT_ROOT'] . '/erp/config/database.php';
require_once $_SERVER['DOCUMENT_ROOT'] . '/erp/config/auth.php';
require_once $_SERVER['DOCUMENT_ROOT'] . '/erp/config/functions.php';

// ── Autoload PhpSpreadsheet ──
$autoloadPaths = [
    $_SERVER['DOCUMENT_ROOT'] . '/erp/vendor/autoload.php',
    __DIR__ . '/../../vendor/autoload.php',
    __DIR__ . '/../../../vendor/autoload.php',
];
$spreadsheetLoaded = false;
foreach ($autoloadPaths as $path) {
    if (file_exists($path)) {
        require_once $path;
        $spreadsheetLoaded = true;
        break;
    }
}

requireRole('director', 'accountant', 'manager', 'production');
$pdo        = getDBConnection();
$user       = currentUser();
$isDirector = hasRole('director');

// ══════════════════════════════════════════════════════════════════════════
// XỬ LÝ IMPORT EXCEL
// ══════════════════════════════════════════════════════════════════════════
$importResult = null;

if ($_SERVER['REQUEST_METHOD'] === 'POST'
    && ($_POST['action'] ?? '') === 'import_excel'
    && !empty($_FILES['import_file']['tmp_name'])) {

    if (!$spreadsheetLoaded) {
        $importResult = ['error' => 'Thư viện PhpSpreadsheet chưa được cài. Chạy: composer require phpoffice/phpspreadsheet'];
    } else {
        $tmpFile  = $_FILES['import_file']['tmp_name'];
        $origName = $_FILES['import_file']['name'];
        $ext      = strtolower(pathinfo($origName, PATHINFO_EXTENSION));

        try {
            // Đọc file Excel
            $reader = match($ext) {
                'xlsx' => new \PhpOffice\PhpSpreadsheet\Reader\Xlsx(),
                'xls'  => new \PhpOffice\PhpSpreadsheet\Reader\Xls(),
                'ods'  => new \PhpOffice\PhpSpreadsheet\Reader\Ods(),
                default => null,
            };

            if (!$reader) {
                $importResult = ['error' => 'Chỉ hỗ trợ file .xlsx, .xls, .ods'];
            } else {
                $reader->setReadDataOnly(true);
                $spreadsheet = $reader->load($tmpFile);
                $sheet       = $spreadsheet->getActiveSheet();
                $rows        = $sheet->toArray(null, true, true, false);
                // $rows[0] = header, $rows[1..n] = data (index bắt đầu từ 0)

                // ── Xác định vị trí cột từ header ──
                $header     = array_map(fn($v) => mb_strtolower(trim((string)$v)), $rows[0] ?? []);
                $colDate    = array_search('date',  $header);
                $colMaNV    = array_search('manv',  $header);
                $colIn      = false;
                $colOut     = false;

                // Tìm cột IN* và OUT* (có thể có ký tự *)
                foreach ($header as $idx => $h) {
                    $clean = str_replace(['*', ' '], '', $h);
                    if ($clean === 'in')  $colIn  = $idx;
                    if ($clean === 'out') $colOut = $idx;
                }

                // Fallback: tìm theo vị trí nếu không tìm được theo tên
                // Mặc định: A=Date, B=MaNV, C=TEN, D=IN*, E=OUT*
                if ($colDate === false) $colDate = 0;
                if ($colMaNV === false) $colMaNV = 1;
                if ($colIn   === false) $colIn   = 3;
                if ($colOut  === false) $colOut  = 4;

                // ── Lấy map employee_code → user_id ──
                $empMap = [];
                foreach ($pdo->query("SELECT id, employee_code FROM users WHERE is_active=1")->fetchAll() as $e) {
                    $empMap[$e['employee_code']] = $e['id'];
                }

                $inserted = 0; $updated = 0; $skipped = 0; $errors = [];

                foreach ($rows as $rowIdx => $row) {
                    if ($rowIdx === 0) continue; // bỏ header

                    $rawDate = trim((string)($row[$colDate] ?? ''));
                    $maNV    = trim((string)($row[$colMaNV] ?? ''));
                    $rawIn   = trim((string)($row[$colIn]   ?? ''));
                    $rawOut  = trim((string)($row[$colOut]  ?? ''));

                    // Bỏ dòng trống
                    if (empty($rawDate) && empty($maNV)) { $skipped++; continue; }
                    if (empty($maNV)) { $skipped++; continue; }

                    // ── Parse ngày ──
                    // Excel có thể trả về: "02/04/2026", "2026-04-02", hoặc số serial Excel
                    $workDate = null;

                    if (is_numeric($rawDate)) {
                        // Số serial Excel → convert
                        $workDate = \PhpOffice\PhpSpreadsheet\Shared\Date::excelToDateTimeObject((float)$rawDate)
                                        ->format('Y-m-d');
                    } elseif (preg_match('#^(\d{1,2})/(\d{1,2})/(\d{4})$#', $rawDate, $m)) {
                        // dd/mm/yyyy
                        $workDate = sprintf('%04d-%02d-%02d', $m[3], $m[2], $m[1]);
                    } elseif (preg_match('#^(\d{4})-(\d{2})-(\d{2})$#', $rawDate)) {
                        // yyyy-mm-dd
                        $workDate = $rawDate;
                    } elseif (preg_match('#^(\d{1,2})-(\d{1,2})-(\d{4})$#', $rawDate, $m)) {
                        // dd-mm-yyyy
                        $workDate = sprintf('%04d-%02d-%02d', $m[3], $m[2], $m[1]);
                    } else {
                        // Thử strtotime
                        $ts = strtotime($rawDate);
                        if ($ts) $workDate = date('Y-m-d', $ts);
                    }

                    if (!$workDate || !strtotime($workDate)) {
                        $errors[] = "Dòng " . ($rowIdx + 1) . ": ngày '$rawDate' không đọc được";
                        $skipped++; continue;
                    }

                    // ── Tìm user ──
                    if (!isset($empMap[$maNV])) {
                        $errors[] = "Dòng " . ($rowIdx + 1) . ": mã NV '$maNV' không tồn tại";
                        $skipped++; continue;
                    }
                    $userId = $empMap[$maNV];

                    // ── Nếu không có giờ vào → bỏ qua ──
                    if (empty($rawIn)) { $skipped++; continue; }

                    // ── Parse giờ ──
                    // Excel có thể trả về: "07:55", số thập phân (0.33...), hoặc chuỗi "07:55:00"
                    $parseTime = function($val) use ($workDate) {
                        $val = trim((string)$val);
                        if (empty($val)) return null;

                        // Số thập phân Excel (fraction of day)
                        if (is_numeric($val) && (float)$val < 1) {
                            $seconds = round((float)$val * 86400);
                            return sprintf('%02d:%02d', intdiv($seconds, 3600), intdiv($seconds % 3600, 60));
                        }
                        // HH:MM hoặc HH:MM:SS
                        if (preg_match('/^(\d{1,2}):(\d{2})/', $val, $m)) {
                            return sprintf('%02d:%02d', $m[1], $m[2]);
                        }
                        return null;
                    };

                    $timeIn  = $parseTime($rawIn);
                    $timeOut = $parseTime($rawOut);

                    if (!$timeIn) { $skipped++; continue; }

                    // ── Tạo datetime ──
                    $checkInDT  = $workDate . ' ' . $timeIn  . ':00';
                    $checkOutDT = null;

                    if ($timeOut) {
                        $inH  = (int)explode(':', $timeIn)[0];
                        $outH = (int)explode(':', $timeOut)[0];
                        // Ca đêm: giờ ra nhỏ hơn giờ vào
                        if ($outH < $inH || ($inH >= 18 && $outH <= 8)) {
                            $nextDate   = date('Y-m-d', strtotime($workDate . ' +1 day'));
                            $checkOutDT = $nextDate . ' ' . $timeOut . ':00';
                        } else {
                            $checkOutDT = $workDate . ' ' . $timeOut . ':00';
                        }
                    }

                    // ── Tính work_hours ──
                    $workHours = 0;
                    if ($checkOutDT) {
                        $diffSec   = strtotime($checkOutDT) - strtotime($checkInDT);
                        $workHours = round($diffSec / 3600, 2);
                        if ($workHours < 0) $workHours = 0;
                        // Trừ 1h nghỉ trưa nếu ca ngày và làm >= 6h
                        $inH = (int)explode(':', $timeIn)[0];
                        if ($workHours >= 6 && $inH < 14) $workHours -= 1;
                    }

                    // ── Lấy ca + tính trễ ──
                    $shiftStmt = $pdo->prepare("
                        SELECT ws.start_time, ws.end_time, ws.late_threshold
                        FROM employee_shifts es
                        JOIN work_shifts ws ON es.shift_id = ws.id
                        WHERE es.user_id = ?
                          AND es.effective_date <= ?
                          AND (es.end_date IS NULL OR es.end_date >= ?)
                        ORDER BY es.effective_date DESC LIMIT 1
                    ");
                    $shiftStmt->execute([$userId, $workDate, $workDate]);
                    $shift = $shiftStmt->fetch();

                    $isLate = 0; $lateMinutes = 0;
                    if ($shift) {
                        $threshold  = (int)($shift['late_threshold'] ?? 0);
                        $shiftStart = strtotime($workDate . ' ' . $shift['start_time']);
                        $actualIn   = strtotime($checkInDT);
                        $diffMin    = ($actualIn - $shiftStart) / 60;
                        if ($diffMin > $threshold) {
                            $isLate      = 1;
                            $lateMinutes = (int)($diffMin - $threshold);
                        }
                    }

                    // ── Upsert attendance_logs ──
                    $existStmt = $pdo->prepare("SELECT id FROM attendance_logs WHERE user_id=? AND work_date=?");
                    $existStmt->execute([$userId, $workDate]);
                    $existRow = $existStmt->fetch();

                    if ($existRow) {
                        $pdo->prepare("
                            UPDATE attendance_logs
                            SET check_in=?, check_out=?, work_hours=?,
                                is_late=?, late_minutes=?,
                                early_leave=0, early_leave_minutes=0,
                                source='import'
                            WHERE id=?
                        ")->execute([$checkInDT, $checkOutDT, $workHours,
                                     $isLate, $lateMinutes, $existRow['id']]);
                        $updated++;
                    } else {
                        $pdo->prepare("
                            INSERT INTO attendance_logs
                                (user_id, work_date, check_in, check_out, work_hours,
                                 is_late, late_minutes, early_leave, early_leave_minutes, source)
                            VALUES (?,?,?,?,?,?,?,0,0,'import')
                        ")->execute([$userId, $workDate, $checkInDT, $checkOutDT, $workHours,
                                     $isLate, $lateMinutes]);
                        $inserted++;
                    }
                }

                $importResult = [
                    'inserted' => $inserted,
                    'updated'  => $updated,
                    'skipped'  => $skipped,
                    'errors'   => $errors,
                ];
            }
        } catch (\Exception $e) {
            $importResult = ['error' => 'Lỗi đọc file: ' . $e->getMessage()];
        }
    }
}

// ══════════════════════════════════════════════════════════════════════════
// CÁC THAM SỐ LỌC
// ══════════════════════════════════════════════════════════════════════════
$viewMonth  = (int)($_GET['month']   ?? date('m'));
$viewYear   = (int)($_GET['year']    ?? date('Y'));
$filterDept = (int)($_GET['dept']    ?? 0);
$filterUser = (int)($_GET['user_id'] ?? 0);
$viewMode   = $_GET['view']          ?? 'month';

if ($viewMonth < 1)  { $viewMonth = 12; $viewYear--; }
if ($viewMonth > 12) { $viewMonth = 1;  $viewYear++; }

$daysInMon = cal_days_in_month(CAL_GREGORIAN, $viewMonth, $viewYear);
$viewDay   = (int)($_GET['day'] ?? date('j'));
if ($viewDay < 1 || $viewDay > $daysInMon) $viewDay = (int)date('j');

// ── Query nhân viên ──
$empSQL = "SELECT u.id, u.full_name, u.employee_code, d.name AS dept_name, d.id AS dept_id,
                  r.display_name AS role_name,
                  ws.shift_name, ws.color AS shift_color,
                  ws.start_time AS shift_start, ws.end_time AS shift_end,
                  ws.work_hours AS shift_work_hours, ws.late_threshold
           FROM users u
           LEFT JOIN departments d ON u.department_id = d.id
           LEFT JOIN roles r ON u.role_id = r.id
           LEFT JOIN employee_shifts es ON es.user_id = u.id
               AND es.effective_date <= LAST_DAY(?)
               AND (es.end_date IS NULL OR es.end_date >= ?)
           LEFT JOIN work_shifts ws ON es.shift_id = ws.id
           WHERE u.is_active = 1 AND r.name != 'director'";
$empParams = ["$viewYear-$viewMonth-01", "$viewYear-$viewMonth-01"];

if ($filterDept) { $empSQL .= " AND u.department_id = ?"; $empParams[] = $filterDept; }
if ($filterUser) { $empSQL .= " AND u.id = ?";            $empParams[] = $filterUser; }
$empSQL .= " ORDER BY d.name, u.full_name";

$empStmt = $pdo->prepare($empSQL);
$empStmt->execute($empParams);
$employees = $empStmt->fetchAll();

// ── Query attendance ──
$attStmt = $pdo->prepare("
    SELECT al.user_id, al.work_date, al.check_in, al.check_out,
           al.work_hours, al.is_late, al.late_minutes, al.source,
           al.early_leave, al.early_leave_minutes
    FROM attendance_logs al
    WHERE MONTH(al.work_date) = ? AND YEAR(al.work_date) = ?
    ORDER BY al.work_date
");
$attStmt->execute([$viewMonth, $viewYear]);
$attMap = [];
foreach ($attStmt->fetchAll() as $a) {
    $attMap[$a['user_id']][$a['work_date']] = $a;
}

// ── Query nghỉ phép ──
$leaveStmt = $pdo->prepare("
    SELECT user_id, start_date, end_date, leave_type
    FROM leave_requests
    WHERE status = 'approved'
      AND ((MONTH(start_date)=? AND YEAR(start_date)=?)
        OR (MONTH(end_date)=?   AND YEAR(end_date)=?))
");
$leaveStmt->execute([$viewMonth, $viewYear, $viewMonth, $viewYear]);
$leaveMap = [];
foreach ($leaveStmt->fetchAll() as $lv) {
    $s = strtotime($lv['start_date']);
    $e = strtotime($lv['end_date']);
    for ($d = $s; $d <= $e; $d += 86400) {
        $leaveMap[$lv['user_id']][date('Y-m-d', $d)] = $lv['leave_type'];
    }
}

// ── Query OT ──
$otStmt = $pdo->prepare("
    SELECT user_id, ot_date, hours, ot_type
    FROM overtime_requests
    WHERE status = 'approved'
      AND MONTH(ot_date) = ? AND YEAR(ot_date) = ?
");
$otStmt->execute([$viewMonth, $viewYear]);
$otMap = [];
foreach ($otStmt->fetchAll() as $ot) {
    $otMap[$ot['user_id']][$ot['ot_date']] = $ot;
}

// ── Ngày lễ ──
$holidays = $pdo->prepare("SELECT holiday_date FROM holidays WHERE MONTH(holiday_date)=? AND YEAR(holiday_date)=?");
$holidays->execute([$viewMonth, $viewYear]);
$holidayDates = array_column($holidays->fetchAll(), 'holiday_date');

// ── Hàm tính thống kê ──
function calcStats($userId, $attMap, $leaveMap, $otMap, $viewMonth, $viewYear, $daysInMon, $holidayDates) {
    $stats = [
        'work_days'=>0,'absent_days'=>0,'leave_days'=>0,
        'late_count'=>0,'late_minutes'=>0,
        'early_count'=>0,'early_minutes'=>0,
        'total_hours'=>0,
        'ot_hours'=>0,'ot_weekday'=>0,'ot_weekend'=>0,'ot_holiday'=>0,
        'sunday_work'=>0,
    ];
    for ($d = 1; $d <= $daysInMon; $d++) {
        $dateStr  = sprintf('%04d-%02d-%02d', $viewYear, $viewMonth, $d);
        $dow      = date('N', strtotime($dateStr));
        $isSun    = ($dow == 7);
        $isFuture = ($dateStr > date('Y-m-d'));
        if ($isFuture) continue;

        $att   = $attMap[$userId][$dateStr]   ?? null;
        $leave = $leaveMap[$userId][$dateStr] ?? null;
        $ot    = $otMap[$userId][$dateStr]    ?? null;

        if ($ot) {
            $stats['ot_hours'] += $ot['hours'];
            if ($ot['ot_type'] === 'holiday')     $stats['ot_holiday'] += $ot['hours'];
            elseif ($ot['ot_type'] === 'weekend') $stats['ot_weekend'] += $ot['hours'];
            else                                  $stats['ot_weekday'] += $ot['hours'];
        }

        if ($isSun) {
            if ($att && $att['check_in']) $stats['sunday_work']++;
            continue;
        }

        if ($leave && !$att)              { $stats['leave_days']++; }
        elseif ($att && $att['check_in']) {
            $stats['work_days']++;
            $stats['total_hours'] += ($att['work_hours'] ?? 0);
            if ($att['is_late'])      { $stats['late_count']++;  $stats['late_minutes']  += ($att['late_minutes'] ?? 0); }
            if ($att['early_leave'])  { $stats['early_count']++; $stats['early_minutes'] += (int)($att['early_leave_minutes'] ?? 0); }
        } else {
            $stats['absent_days']++;
        }
    }
    return $stats;
}

$depts   = $pdo->query("SELECT * FROM departments ORDER BY name")->fetchAll();
$empList = $pdo->query("SELECT id, full_name, employee_code FROM users WHERE is_active=1 ORDER BY full_name")->fetchAll();

$summaryStmt = $pdo->prepare("
    SELECT
        COUNT(DISTINCT user_id)                                                         AS total_checkins,
        COALESCE(SUM(is_late), 0)                                                       AS total_late,
        COALESCE(ROUND(SUM(work_hours),1), 0)                                           AS total_hours,
        COUNT(CASE WHEN check_in IS NOT NULL AND check_out IS NULL THEN 1 END)          AS missing_checkout
    FROM attendance_logs
    WHERE MONTH(work_date)=? AND YEAR(work_date)=?
");
$summaryStmt->execute([$viewMonth, $viewYear]);
$summary = $summaryStmt->fetch();

include $_SERVER['DOCUMENT_ROOT'] . '/erp/includes/header.php';
include $_SERVER['DOCUMENT_ROOT'] . '/erp/includes/sidebar.php';
?>

<div class="main-content">
<div class="container-fluid py-4">

    <!-- Tiêu đề -->
    <div class="d-flex justify-content-between align-items-center mb-4">
        <div>
            <h4 class="mb-1">📊 Bảng chấm công tổng hợp</h4>
            <p class="text-muted small mb-0">
                Tháng <?= $viewMonth ?>/<?= $viewYear ?> &bull; <?= count($employees) ?> nhân viên
            </p>
        </div>
        <div class="d-flex gap-2 flex-wrap justify-content-end">
            <button class="btn btn-warning btn-sm" data-bs-toggle="modal" data-bs-target="#importModal">
                <i class="fas fa-file-excel me-1"></i>Import Excel
            </button>
            <button onclick="exportExcel()" class="btn btn-success btn-sm">
                <i class="fas fa-file-excel me-1"></i>Xuất Excel
            </button>
            <button onclick="window.print()" class="btn btn-outline-secondary btn-sm">
                <i class="fas fa-print me-1"></i>In
            </button>
        </div>
    </div>

    <!-- ── Kết quả Import ── -->
    <?php if ($importResult): ?>
    <div class="alert <?= isset($importResult['error']) ? 'alert-danger' : 'alert-success' ?> alert-dismissible fade show">
        <?php if (isset($importResult['error'])): ?>
            <i class="fas fa-times-circle me-2"></i><strong>Lỗi:</strong> <?= htmlspecialchars($importResult['error']) ?>
        <?php else: ?>
            <i class="fas fa-check-circle me-2"></i>
            <strong>Import hoàn tất!</strong> &nbsp;
            <span class="badge bg-success">✅ Thêm mới: <?= $importResult['inserted'] ?></span> &nbsp;
            <span class="badge bg-info text-dark">🔄 Cập nhật: <?= $importResult['updated'] ?></span> &nbsp;
            <span class="badge bg-secondary">⏭️ Bỏ qua: <?= $importResult['skipped'] ?></span>
            <?php if (!empty($importResult['errors'])): ?>
            <hr class="my-2">
            <div class="small">
                <strong>⚠️ <?= count($importResult['errors']) ?> cảnh báo:</strong><br>
                <?php foreach (array_slice($importResult['errors'], 0, 8) as $err): ?>
                    — <?= htmlspecialchars($err) ?><br>
                <?php endforeach; ?>
                <?php if (count($importResult['errors']) > 8): ?>
                    <em>...và <?= count($importResult['errors']) - 8 ?> cảnh báo khác</em>
                <?php endif; ?>
            </div>
            <?php endif; ?>
        <?php endif; ?>
        <button type="button" class="btn-close" data-bs-dismiss="alert"></button>
    </div>
    <?php endif; ?>

    <!-- Thống kê nhanh -->
    <div class="row g-3 mb-4">
        <div class="col-6 col-md-3">
            <div class="card border-0 shadow-sm text-center py-3">
                <div class="fs-2 fw-bold text-primary"><?= count($employees) ?></div>
                <div class="small text-muted">👥 Tổng nhân viên</div>
            </div>
        </div>
        <div class="col-6 col-md-3">
            <div class="card border-0 shadow-sm text-center py-3">
                <div class="fs-2 fw-bold text-success"><?= $summary['total_checkins'] ?? 0 ?></div>
                <div class="small text-muted">✅ NV đã chấm công</div>
            </div>
        </div>
        <div class="col-6 col-md-3">
            <div class="card border-0 shadow-sm text-center py-3">
                <div class="fs-2 fw-bold text-warning"><?= $summary['total_late'] ?? 0 ?></div>
                <div class="small text-muted">⚡ Lượt đi trễ</div>
            </div>
        </div>
        <div class="col-6 col-md-3">
            <div class="card border-0 shadow-sm text-center py-3">
                <div class="fs-2 fw-bold text-danger"><?= $summary['missing_checkout'] ?? 0 ?></div>
                <div class="small text-muted">⚠️ Thiếu giờ ra</div>
            </div>
        </div>
    </div>

    <!-- Bộ lọc -->
    <div class="card border-0 shadow-sm mb-3">
        <div class="card-body py-2">
            <form method="GET" class="row g-2 align-items-end">
                <input type="hidden" name="view" value="<?= htmlspecialchars($viewMode) ?>">
                <div class="col-6 col-md-2">
                    <label class="form-label small fw-semibold mb-1">Tháng</label>
                    <select name="month" class="form-select form-select-sm">
                        <?php for ($m = 1; $m <= 12; $m++): ?>
                        <option value="<?= $m ?>" <?= $m == $viewMonth ? 'selected' : '' ?>>Tháng <?= $m ?></option>
                        <?php endfor; ?>
                    </select>
                </div>
                <div class="col-6 col-md-1">
                    <label class="form-label small fw-semibold mb-1">Năm</label>
                    <select name="year" class="form-select form-select-sm">
                        <?php for ($y = date('Y') - 1; $y <= date('Y') + 1; $y++): ?>
                        <option value="<?= $y ?>" <?= $y == $viewYear ? 'selected' : '' ?>><?= $y ?></option>
                        <?php endfor; ?>
                    </select>
                </div>
                <div class="col-6 col-md-2">
                    <label class="form-label small fw-semibold mb-1">Phòng ban</label>
                    <select name="dept" class="form-select form-select-sm">
                        <option value="">-- Tất cả --</option>
                        <?php foreach ($depts as $d): ?>
                        <option value="<?= $d['id'] ?>" <?= $filterDept == $d['id'] ? 'selected' : '' ?>>
                            <?= htmlspecialchars($d['name']) ?>
                        </option>
                        <?php endforeach; ?>
                    </select>
                </div>
                <div class="col-6 col-md-3">
                    <label class="form-label small fw-semibold mb-1">Nhân viên</label>
                    <select name="user_id" class="form-select form-select-sm">
                        <option value="">-- Tất cả --</option>
                        <?php foreach ($empList as $e): ?>
                        <option value="<?= $e['id'] ?>" <?= $filterUser == $e['id'] ? 'selected' : '' ?>>
                            <?= htmlspecialchars($e['employee_code'] . ' - ' . $e['full_name']) ?>
                        </option>
                        <?php endforeach; ?>
                    </select>
                </div>
                <div class="col-md-2 d-flex gap-1">
                    <button type="submit" class="btn btn-primary btn-sm flex-grow-1">
                        <i class="fas fa-search me-1"></i>Lọc
                    </button>
                    <a href="/erp/modules/attendance/all_attendance.php" class="btn btn-outline-secondary btn-sm">↺</a>
                </div>
                <div class="col-md-2">
                    <label class="form-label small fw-semibold mb-1">Chế độ xem</label>
                    <div class="btn-group w-100">
                        <a href="?month=<?= $viewMonth ?>&year=<?= $viewYear ?>&dept=<?= $filterDept ?>&user_id=<?= $filterUser ?>&view=month"
                           class="btn btn-sm <?= $viewMode==='month'   ? 'btn-primary' : 'btn-outline-primary' ?>">
                            <i class="fas fa-calendar-alt"></i> Tháng
                        </a>
                        <a href="?month=<?= $viewMonth ?>&year=<?= $viewYear ?>&dept=<?= $filterDept ?>&user_id=<?= $filterUser ?>&view=summary"
                           class="btn btn-sm <?= $viewMode==='summary' ? 'btn-primary' : 'btn-outline-primary' ?>">
                            <i class="fas fa-table"></i> Tổng hợp
                        </a>
                    </div>
                </div>
            </form>
        </div>
    </div>

    <!-- ══ CHẾ ĐỘ THÁNG ══ -->
    <?php if ($viewMode === 'month'): ?>
    <div class="d-flex flex-wrap gap-2 mb-2">
        <?php $legends = [
            ['leg-ok',    '✓ Đúng giờ'],
            ['leg-late',  '⚡ Đi trễ'],
            ['leg-early', '🔚 Về sớm'],
            ['leg-leave', '🏖️ Nghỉ phép'],
            ['leg-absent','✗ Vắng'],
            ['leg-ot',    '🌙 OT'],
            ['leg-sun',   '— Chủ nhật'],
        ]; foreach ($legends as [$cls, $lbl]): ?>
        <span class="legend-badge <?= $cls ?>"><?= $lbl ?></span>
        <?php endforeach; ?>
    </div>

    <div class="card border-0 shadow-sm">
        <div class="card-body p-0">
            <div class="table-responsive">
                <table class="table table-bordered table-sm att-table mb-0" id="attTable">
                    <thead>
                        <tr class="table-dark">
                            <th class="sticky-col text-center" rowspan="2" style="min-width:170px;vertical-align:middle;">
                                <div class="d-flex align-items-center justify-content-center gap-1">
                                    <a href="?month=<?= $viewMonth-1 ?>&year=<?= $viewYear ?>&dept=<?= $filterDept ?>&user_id=<?= $filterUser ?>&view=month"
                                       class="btn btn-xs btn-outline-light"><i class="fas fa-chevron-left"></i></a>
                                    <span>T<?= $viewMonth ?>/<?= $viewYear ?></span>
                                    <a href="?month=<?= $viewMonth+1 ?>&year=<?= $viewYear ?>&dept=<?= $filterDept ?>&user_id=<?= $filterUser ?>&view=month"
                                       class="btn btn-xs btn-outline-light"><i class="fas fa-chevron-right"></i></a>
                                </div>
                            </th>
                            <?php for ($d = 1; $d <= $daysInMon; $d++):
                                $ds  = sprintf('%04d-%02d-%02d', $viewYear, $viewMonth, $d);
                                $dow = date('N', strtotime($ds));
                                $isToday   = ($ds === date('Y-m-d'));
                                $isSun     = ($dow == 7);
                                $isSat     = ($dow == 6);
                                $isHoliday = in_array($ds, $holidayDates);
                            ?>
                            <th class="text-center px-0
                                <?= $isToday ? 'bg-primary' : '' ?>
                                <?= !$isToday && $isHoliday ? 'bg-danger bg-opacity-75' : '' ?>
                                <?= !$isToday && !$isHoliday && $isSun ? 'text-danger' : '' ?>
                                <?= !$isToday && !$isHoliday && $isSat ? 'text-warning' : '' ?>"
                                style="min-width:54px;font-size:11px;white-space:nowrap;">
                                <div><?= ['','T2','T3','T4','T5','T6','T7','CN'][$dow] ?></div>
                                <div class="fw-bold"><?= $d ?></div>
                                <?php if ($isHoliday): ?><div style="font-size:9px;">🎉</div><?php endif; ?>
                            </th>
                            <?php endfor; ?>
                            <th class="text-center" style="min-width:40px;font-size:11px;">Công</th>
                            <th class="text-center" style="min-width:38px;font-size:11px;">Phép</th>
                            <th class="text-center" style="min-width:38px;font-size:11px;">Vắng</th>
                            <th class="text-center" style="min-width:38px;font-size:11px;">Trễ</th>
                            <th class="text-center" style="min-width:50px;font-size:11px;">Trừ(p)</th>
                            <th class="text-center" style="min-width:42px;font-size:11px;">OT(h)</th>
                        </tr>
                    </thead>
                    <tbody>
                    <?php
                    $prevDept = null;
                    foreach ($employees as $emp):
                        if ($emp['dept_name'] !== $prevDept):
                            $prevDept = $emp['dept_name'];
                    ?>
                    <tr class="dept-row">
                        <td colspan="<?= $daysInMon + 7 ?>" class="py-1 ps-3 fw-bold small">
                            🏢 <?= htmlspecialchars($emp['dept_name'] ?? 'Chưa phân phòng ban') ?>
                        </td>
                    </tr>
                    <?php endif;
                        $st = calcStats($emp['id'], $attMap, $leaveMap, $otMap, $viewMonth, $viewYear, $daysInMon, $holidayDates);
                        $totalDeductMin = $st['late_minutes'] + $st['early_minutes'];
                    ?>
                    <tr class="emp-row">
                        <td class="sticky-col py-1">
                            <div class="fw-semibold" style="font-size:12px;line-height:1.2;">
                                <?= htmlspecialchars($emp['full_name']) ?>
                            </div>
                            <div style="font-size:10px;" class="text-muted">
                                <?= $emp['employee_code'] ?>
                                <?php if ($emp['shift_name']): ?>
                                &bull; <span style="color:<?= $emp['shift_color'] ?>;font-weight:600;">
                                    <?= htmlspecialchars($emp['shift_name']) ?>
                                </span>
                                <?php endif; ?>
                            </div>
                        </td>
                        <?php for ($d = 1; $d <= $daysInMon; $d++):
                            $dateStr   = sprintf('%04d-%02d-%02d', $viewYear, $viewMonth, $d);
                            $dow       = date('N', strtotime($dateStr));
                            $isSun     = ($dow == 7);
                            $isFuture  = ($dateStr > date('Y-m-d'));
                            $isHoliday = in_array($dateStr, $holidayDates);
                            $att   = $attMap[$emp['id']][$dateStr]   ?? null;
                            $leave = $leaveMap[$emp['id']][$dateStr] ?? null;
                            $ot    = $otMap[$emp['id']][$dateStr]    ?? null;

                            $bg = '#fff'; $content = ''; $title = '';

                            if ($isSun) {
                                $bg = '#f5f5f5';
                                $content = '<span style="color:#ccc;font-size:10px;">—</span>';
                            } elseif ($isHoliday && !$att) {
                                $bg = '#fff0f0';
                                $content = '<span style="font-size:11px;">🎉</span>';
                            } elseif ($isFuture) {
                                $bg = '#fafafa';
                            } elseif ($leave && !$att) {
                                $leaveLabels = ['annual'=>'Phép','sick'=>'Ốm','unpaid'=>'KL','other'=>'Khác'];
                                $bg      = '#e8f4fd';
                                $content = '<span style="font-size:10px;color:#0284c7;font-weight:600;">' . ($leaveLabels[$leave] ?? 'Phép') . '</span>';
                                $title   = 'Nghỉ phép';
                            } elseif ($att && $att['check_in']) {
                                $isLate   = $att['is_late'];
                                $isEarly  = $att['early_leave'];
                                $checkIn  = date('H:i', strtotime($att['check_in']));
                                $checkOut = $att['check_out'] ? date('H:i', strtotime($att['check_out'])) : '—';

                                if ($isLate && $isEarly)  $bg = '#fff7e6';
                                elseif ($isLate)          $bg = '#fffbf0';
                                elseif ($isEarly)         $bg = '#fdf2f8';
                                else                      $bg = '#f0fff4';

                                $title = "Vào: $checkIn | Ra: $checkOut";
                                if ($isLate)  $title .= " | Trễ: {$att['late_minutes']}p";
                                if ($isEarly) $title .= " | Về sớm: {$att['early_leave_minutes']}p";
                                if ($ot)      $title .= " | OT: {$ot['hours']}h";

                                $icon = '';
                                if ($isLate && $isEarly) $icon = '<span style="color:#d97706;font-size:9px;">⚡🔚</span>';
                                elseif ($isLate)         $icon = '<span style="color:#d97706;font-weight:bold;">⚡</span>';
                                elseif ($isEarly)        $icon = '<span style="color:#9333ea;font-weight:bold;">🔚</span>';
                                else                     $icon = '<span style="color:#16a34a;font-weight:bold;">✓</span>';

                                $content = '<div style="font-size:10px;line-height:1.4;padding:1px 2px;">'
                                    . $icon
                                    . '<div style="color:#1e40af;font-weight:600;">' . $checkIn  . '</div>'
                                    . '<div style="color:#dc2626;">'                . $checkOut . '</div>'
                                    . ($ot ? '<div style="color:#6f42c1;font-size:9px;font-weight:600;">OT</div>' : '')
                                    . '</div>';
                            } else {
                                $bg      = '#fff5f5';
                                $content = '<span style="color:#dc2626;font-size:11px;font-weight:bold;">✗</span>';
                                $title   = 'Vắng không phép';
                            }
                        ?>
                        <td class="text-center p-0 att-cell"
                            style="background:<?= $bg ?>;height:54px;vertical-align:middle;cursor:pointer;"
                            title="<?= htmlspecialchars($title) ?>"
                            onclick="showDayDetail(<?= $emp['id'] ?>, '<?= $dateStr ?>', '<?= htmlspecialchars(addslashes($emp['full_name'])) ?>')">
                            <?= $content ?>
                        </td>
                        <?php endfor; ?>
                        <td class="text-center fw-bold text-success small"><?= $st['work_days'] ?></td>
                        <td class="text-center text-info small"><?= $st['leave_days'] ?: '—' ?></td>
                        <td class="text-center small <?= $st['absent_days'] > 0 ? 'text-danger fw-bold' : 'text-muted' ?>"><?= $st['absent_days'] ?: '—' ?></td>
                        <td class="text-center small">
                            <?= $st['late_count'] > 0
                                ? '<span class="badge-sm bg-warning text-dark">' . $st['late_count'] . '</span>'
                                : '<span class="text-muted">—</span>' ?>
                        </td>
                        <td class="text-center small <?= $totalDeductMin > 0 ? 'text-danger fw-semibold' : 'text-muted' ?>">
                            <?= $totalDeductMin > 0 ? $totalDeductMin . 'p' : '—' ?>
                        </td>
                        <td class="text-center small">
                            <?= $st['ot_hours'] > 0
                                ? '<span class="badge-sm bg-purple">' . number_format($st['ot_hours'],1) . '</span>'
                                : '<span class="text-muted">—</span>' ?>
                        </td>
                    </tr>
                    <?php endforeach; ?>
                    </tbody>
                </table>
            </div>
        </div>
    </div>

    <!-- ══ CHẾ ĐỘ TỔNG HỢP ══ -->
    <?php elseif ($viewMode === 'summary'): ?>
    <div class="card border-0 shadow-sm">
        <div class="card-header bg-white fw-bold d-flex justify-content-between">
            <span>📋 Bảng tổng hợp tháng <?= $viewMonth ?>/<?= $viewYear ?></span>
            <small class="text-muted fw-normal"><?= count($employees) ?> nhân viên</small>
        </div>
        <div class="card-body p-0">
            <div class="table-responsive">
                <table class="table table-hover table-bordered align-middle mb-0" id="summaryTable">
                    <thead class="table-dark">
                        <tr>
                            <th class="sticky-col" style="min-width:180px;">Nhân viên</th>
                            <th class="text-center">Ca</th>
                            <th class="text-center">Ngày công</th>
                            <th class="text-center">Giờ làm</th>
                            <th class="text-center">Nghỉ phép</th>
                            <th class="text-center">Vắng</th>
                            <th class="text-center">Đi trễ</th>
                            <th class="text-center">Phút trễ</th>
                            <th class="text-center">Về sớm</th>
                            <th class="text-center">Phút về sớm</th>
                            <th class="text-center text-danger fw-bold">Tổng phút trừ</th>
                            <th class="text-center">OT thường</th>
                            <th class="text-center">OT T7/CN</th>
                            <th class="text-center">OT lễ</th>
                            <th class="text-center fw-bold">Tổng OT</th>
                            <th class="text-center">Làm CN</th>
                        </tr>
                    </thead>
                    <tbody>
                    <?php
                    $prevDept    = null;
                    $grandTotals = array_fill_keys([
                        'work_days','total_hours','leave_days','absent_days',
                        'late_count','late_minutes','early_count','early_minutes',
                        'ot_hours','ot_weekday','ot_weekend','ot_holiday','sunday_work'
                    ], 0);

                    foreach ($employees as $emp):
                        if ($emp['dept_name'] !== $prevDept):
                            $prevDept = $emp['dept_name'];
                    ?>
                    <tr class="table-secondary">
                        <td colspan="16" class="fw-bold small py-1 ps-3">
                            🏢 <?= htmlspecialchars($emp['dept_name'] ?? 'Chưa phân phòng ban') ?>
                        </td>
                    </tr>
                    <?php endif;
                        $st = calcStats($emp['id'], $attMap, $leaveMap, $otMap, $viewMonth, $viewYear, $daysInMon, $holidayDates);
                        foreach ($grandTotals as $k => $v) $grandTotals[$k] += $st[$k];
                        $totalDeductMin = $st['late_minutes'] + $st['early_minutes'];
                    ?>
                    <tr>
                        <td class="sticky-col">
                            <div class="fw-semibold small"><?= htmlspecialchars($emp['full_name']) ?></div>
                            <div class="text-muted" style="font-size:10px;">
                                <?= $emp['employee_code'] ?>
                                <?php if ($emp['shift_name']): ?>
                                &bull; <span style="color:<?= $emp['shift_color'] ?>"><?= htmlspecialchars($emp['shift_name']) ?></span>
                                <?php endif; ?>
                            </div>
                        </td>
                        <td class="text-center">
                            <?php if ($emp['shift_name']): ?>
                            <span class="badge" style="background:<?= $emp['shift_color'] ?>;font-size:10px;">
                                <?= htmlspecialchars($emp['shift_name']) ?>
                            </span>
                            <?php else: ?><span class="text-danger small">Chưa có</span><?php endif; ?>
                        </td>
                        <td class="text-center fw-bold text-success"><?= $st['work_days'] ?></td>
                        <td class="text-center"><?= number_format($st['total_hours'] ?? 0, 1) ?>h</td>
                        <td class="text-center text-info"><?= $st['leave_days'] ?: '—' ?></td>
                        <td class="text-center <?= $st['absent_days'] > 0 ? 'text-danger fw-bold' : 'text-muted' ?>">
                            <?= $st['absent_days'] ?: '—' ?>
                        </td>
                        <td class="text-center">
                            <?= $st['late_count'] > 0
                                ? '<span class="badge bg-warning text-dark">' . $st['late_count'] . ' lần</span>'
                                : '<span class="text-muted">—</span>' ?>
                        </td>
                        <td class="text-center small <?= $st['late_minutes'] > 0 ? 'text-warning fw-semibold' : 'text-muted' ?>">
                            <?= $st['late_minutes'] > 0 ? $st['late_minutes'] . 'p' : '—' ?>
                        </td>
                        <td class="text-center">
                            <?= $st['early_count'] > 0
                                ? '<span class="badge bg-purple">' . $st['early_count'] . ' lần</span>'
                                : '<span class="text-muted">—</span>' ?>
                        </td>
                        <td class="text-center small <?= $st['early_minutes'] > 0 ? 'text-purple fw-semibold' : 'text-muted' ?>">
                            <?= $st['early_minutes'] > 0 ? $st['early_minutes'] . 'p' : '—' ?>
                        </td>
                        <td class="text-center fw-bold <?= $totalDeductMin > 0 ? 'text-danger' : 'text-muted' ?>">
                            <?= $totalDeductMin > 0 ? $totalDeductMin . 'p' : '—' ?>
                        </td>
                        <td class="text-center small"><?= $st['ot_weekday'] > 0 ? number_format($st['ot_weekday'],1).'h' : '—' ?></td>
                        <td class="text-center small"><?= $st['ot_weekend'] > 0 ? number_format($st['ot_weekend'],1).'h' : '—' ?></td>
                        <td class="text-center small"><?= $st['ot_holiday'] > 0 ? number_format($st['ot_holiday'],1).'h' : '—' ?></td>
                        <td class="text-center fw-bold">
                            <?= $st['ot_hours'] > 0
                                ? '<span class="badge bg-purple">' . number_format($st['ot_hours'],1) . 'h</span>'
                                : '<span class="text-muted">—</span>' ?>
                        </td>
                        <td class="text-center small text-muted">
                            <?= $st['sunday_work'] > 0 ? $st['sunday_work'] . ' ngày' : '—' ?>
                        </td>
                    </tr>
                    <?php endforeach; ?>
                    </tbody>
                    <tfoot class="table-dark fw-bold">
                        <tr>
                            <td class="sticky-col" colspan="2">📊 TỔNG CỘNG</td>
                            <td class="text-center text-success"><?= $grandTotals['work_days'] ?></td>
                            <td class="text-center"><?= number_format($grandTotals['total_hours'] ?? 0, 1) ?>h</td>
                            <td class="text-center text-info"><?= $grandTotals['leave_days'] ?></td>
                            <td class="text-center text-danger"><?= $grandTotals['absent_days'] ?></td>
                            <td class="text-center text-warning"><?= $grandTotals['late_count'] ?></td>
                            <td class="text-center"><?= $grandTotals['late_minutes'] ?>p</td>
                            <td class="text-center"><?= $grandTotals['early_count'] ?></td>
                            <td class="text-center"><?= $grandTotals['early_minutes'] ?>p</td>
                            <td class="text-center text-danger"><?= ($grandTotals['late_minutes'] + $grandTotals['early_minutes']) ?>p</td>
                            <td class="text-center"><?= number_format($grandTotals['ot_weekday'] ?? 0, 1) ?>h</td>
                            <td class="text-center"><?= number_format($grandTotals['ot_weekend'] ?? 0, 1) ?>h</td>
                            <td class="text-center"><?= number_format($grandTotals['ot_holiday'] ?? 0, 1) ?>h</td>
                            <td class="text-center text-warning"><?= number_format($grandTotals['ot_hours'] ?? 0, 1) ?>h</td>
                            <td class="text-center"><?= $grandTotals['sunday_work'] ?></td>
                        </tr>
                    </tfoot>
                </table>
            </div>
        </div>
    </div>
    <?php endif; ?>

</div>
</div>

<!-- ══ MODAL Import Excel ══ -->
<div class="modal fade" id="importModal" tabindex="-1">
    <div class="modal-dialog modal-lg">
        <div class="modal-content">
            <form method="POST" enctype="multipart/form-data">
                <input type="hidden" name="action" value="import_excel">
                <div class="modal-header" style="background:linear-gradient(135deg,#1d6f42,#2e9c5c);color:#fff;">
                    <h5 class="modal-title fw-bold">
                        <i class="fas fa-file-excel me-2"></i>Import dữ liệu chấm công từ Excel
                    </h5>
                    <button type="button" class="btn-close btn-close-white" data-bs-dismiss="modal"></button>
                </div>
                <div class="modal-body">

                    <!-- Cấu trúc file yêu cầu -->
                    <div class="card border-success mb-3">
                        <div class="card-header bg-success bg-opacity-10 py-2 text-success fw-bold small">
                            <i class="fas fa-info-circle me-1"></i>Cấu trúc file Excel yêu cầu
                        </div>
                        <div class="card-body py-2 px-3">
                            <div class="table-responsive">
                                <table class="table table-sm table-bordered mb-0" style="font-size:12px;">
                                    <thead class="table-dark">
                                        <tr>
                                            <th>A — Date</th>
                                            <th>B — MaNV</th>
                                            <th>C — TEN</th>
                                            <th>D — IN*</th>
                                            <th>E — OUT*</th>
                                        </tr>
                                    </thead>
                                    <tbody>
                                        <tr>
                                            <td>02/04/2026</td>
                                            <td>DNA24030001</td>
                                            <td>Dương Đức Minh</td>
                                            <td class="text-success fw-bold">07:55</td>
                                            <td class="text-danger fw-bold">18:38</td>
                                        </tr>
                                        <tr>
                                            <td>19/04/2026</td>
                                            <td>DNA24030001</td>
                                            <td>Dương Đức Minh</td>
                                            <td class="text-success fw-bold">19:27</td>
                                            <td class="text-danger fw-bold">02:00</td>
                                        </tr>
                                        <tr class="text-muted">
                                            <td>06/04/2026</td>
                                            <td>DNA24030001</td>
                                            <td>Dương Đức Minh</td>
                                            <td colspan="2" class="text-center fst-italic">trống = bỏ qua</td>
                                        </tr>
                                    </tbody>
                                </table>
                            </div>
                            <ul class="small text-muted mb-0 mt-2">
                                <li>Dòng đầu là <strong>tiêu đề</strong> (Date, MaNV, TEN, IN*, OUT*)</li>
                                <li>Cột <strong>Date</strong>: định dạng <code>dd/mm/yyyy</code></li>
                                <li>Cột <strong>MaNV</strong>: phải khớp mã nhân viên trong hệ thống</li>
                                <li>Ca đêm (OUT < IN, ví dụ vào 19:27 ra 02:00) → tự động sang ngày hôm sau</li>
                                <li>Dòng không có giờ vào (IN* trống) → bỏ qua</li>
                                <li>Nếu đã có dữ liệu → <strong>cập nhật đè</strong></li>
                            </ul>
                        </div>
                    </div>

                    <!-- Upload -->
                    <div class="mb-3">
                        <label class="form-label fw-semibold">
                            <i class="fas fa-upload me-1 text-success"></i>
                            Chọn file Excel <span class="text-danger">*</span>
                        </label>
                        <input type="file" name="import_file" id="importFileInput"
                               class="form-control" accept=".xlsx,.xls,.ods" required
                               onchange="previewExcelFile(this)">
                        <div class="form-text">Hỗ trợ: <strong>.xlsx</strong>, .xls, .ods</div>
                    </div>

                    <!-- Preview thông tin file -->
                    <div id="filePreviewBox" class="d-none">
                        <div class="alert alert-light border d-flex align-items-center gap-3 py-2">
                            <i class="fas fa-file-excel fa-2x text-success"></i>
                            <div id="filePreviewText" class="small"></div>
                        </div>
                    </div>

                    <!-- Cảnh báo -->
                    <div class="alert alert-warning py-2 small mb-0">
                        <i class="fas fa-exclamation-triangle me-1"></i>
                        <strong>Yêu cầu:</strong> PHP extension <code>zip</code> và thư viện
                        <a href="https://phpspreadsheet.readthedocs.io" target="_blank">PhpSpreadsheet</a>
                        phải được cài (<code>composer require phpoffice/phpspreadsheet</code>).
                        <?php if (!$spreadsheetLoaded): ?>
                        <br><span class="text-danger fw-bold">⚠️ Thư viện chưa được tìm thấy!</span>
                        <?php endif; ?>
                    </div>
                </div>
                <div class="modal-footer border-0 pt-0">
                    <button type="button" class="btn btn-secondary" data-bs-dismiss="modal">
                        <i class="fas fa-times me-1"></i>Huỷ
                    </button>
                    <button type="submit" class="btn btn-success" <?= !$spreadsheetLoaded ? 'disabled' : '' ?>>
                        <i class="fas fa-file-import me-1"></i>Import ngay
                    </button>
                </div>
            </form>
        </div>
    </div>
</div>

<!-- ══ MODAL Chi tiết ngày ══ -->
<div class="modal fade" id="dayDetailModal" tabindex="-1">
    <div class="modal-dialog modal-lg">
        <div class="modal-content">
            <div class="modal-header border-0 pb-0">
                <h6 class="modal-title fw-bold" id="dayDetailTitle">Chi tiết chấm công</h6>
                <button type="button" class="btn-close" data-bs-dismiss="modal"></button>
            </div>
            <div class="modal-body" id="dayDetailBody">
                <div class="text-center py-3"><i class="fas fa-spinner fa-spin"></i></div>
            </div>
        </div>
    </div>
</div>

<style>
.sticky-col { position:sticky; left:0; background:#fff; z-index:3; box-shadow:2px 0 5px rgba(0,0,0,.06); }
.att-table thead th.sticky-col { background:#212529; z-index:4; }
.dept-row td  { background:#f1f5f9 !important; }
.att-cell     { transition:filter .15s; }
.att-cell:hover { filter:brightness(.9); }
.bg-purple    { background:#6f42c1 !important; color:#fff; }
.text-purple  { color:#6f42c1 !important; }
.badge-sm     { display:inline-block; padding:1px 5px; border-radius:3px; font-size:10px; }
.btn-xs       { padding:1px 6px; font-size:11px; }

.legend-badge { display:inline-flex; align-items:center; padding:2px 8px; border-radius:4px; font-size:11px; font-weight:500; }
.leg-ok     { background:#f0fff4; color:#16a34a; border:1px solid #bbf7d0; }
.leg-late   { background:#fffbf0; color:#d97706; border:1px solid #fde68a; }
.leg-early  { background:#fdf2f8; color:#9333ea; border:1px solid #e9d5ff; }
.leg-leave  { background:#e8f4fd; color:#0284c7; border:1px solid #bae6fd; }
.leg-absent { background:#fff5f5; color:#dc2626; border:1px solid #fecaca; }
.leg-ot     { background:#f5f3ff; color:#6f42c1; border:1px solid #ddd6fe; }
.leg-sun    { background:#f5f5f5; color:#aaa;    border:1px solid #e5e7eb; }

@media print {
    .main-content { margin-left:0 !important; }
    .sidebar, nav.navbar, .card-header form, .btn, .modal { display:none !important; }
    .att-table { font-size:9px; }
    .sticky-col { position:static; box-shadow:none; }
}
</style>

<script>
const IS_DIRECTOR = <?= json_encode($isDirector) ?>;

// ── Preview thông tin file được chọn ──
function previewExcelFile(input) {
    const box  = document.getElementById('filePreviewBox');
    const text = document.getElementById('filePreviewText');
    if (!input.files[0]) { box.classList.add('d-none'); return; }

    const f    = input.files[0];
    const size = f.size > 1024*1024
        ? (f.size/1024/1024).toFixed(2) + ' MB'
        : (f.size/1024).toFixed(1) + ' KB';

    text.innerHTML = `<strong>${f.name}</strong> &nbsp;·&nbsp; ${size}
        <br><span class="text-muted">Sẵn sàng import. Nhấn <strong>Import ngay</strong> để tiếp tục.</span>`;
    box.classList.remove('d-none');
}

// ── Modal chi tiết ngày ──
async function showDayDetail(userId, dateStr, empName) {
    document.getElementById('dayDetailTitle').textContent = `📅 ${empName} — ${dateStr}`;
    document.getElementById('dayDetailBody').innerHTML =
        '<div class="text-center py-3"><i class="fas fa-spinner fa-spin"></i> Đang tải...</div>';
    new bootstrap.Modal(document.getElementById('dayDetailModal')).show();

    try {
        const res  = await fetch(`/erp/api/attendance/detail.php?user_id=${userId}&date=${dateStr}`);
        const data = await res.json();

        const checkInVal  = data.att?.check_in  ? data.att.check_in.substring(11,16)  : '';
        const checkOutVal = data.att?.check_out ? data.att.check_out.substring(11,16) : '';

        let html = `<div class="row g-3">
        <div class="col-md-6">
            <div class="card border-0 bg-light h-100"><div class="card-body">
                <h6 class="fw-bold mb-3">⏰ Chấm công</h6>
                <table class="table table-sm mb-0">
                    <tr><th width="40%">Giờ vào</th>
                        <td class="fw-bold text-success">${checkInVal || '—'}</td></tr>
                    <tr><th>Giờ ra</th>
                        <td class="fw-bold text-danger">${checkOutVal || '⚠️ Chưa ra'}</td></tr>
                    <tr><th>Số giờ</th>
                        <td>${data.att?.work_hours ?? '—'}h</td></tr>
                    <tr><th>Đi trễ</th>
                        <td>${data.att?.is_late
                            ? `<span class="badge bg-warning text-dark">⚡ ${data.att.late_minutes}p</span>`
                            : '<span class="text-success small">✓ Đúng giờ</span>'}</td></tr>
                    <tr><th>Về sớm</th>
                        <td>${data.att?.early_leave
                            ? `<span class="badge bg-purple">🔚 ${data.att.early_leave_minutes}p</span>`
                            : '<span class="text-muted small">—</span>'}</td></tr>
                    <tr><th>Nguồn</th>
                        <td><span class="badge bg-secondary">
                            ${ data.att?.source === 'machine' ? '🖥️ Máy chấm'
                             : data.att?.source === 'import'  ? '📥 Import Excel'
                             : '✍️ Thủ công'}
                        </span></td></tr>
                </table>
            </div></div>
        </div>
        <div class="col-md-6">
            <div class="card border-0 bg-light h-100"><div class="card-body">
                <h6 class="fw-bold mb-3">📋 OT & Nghỉ phép</h6>`;

        if (data.ot) {
            const otTypes = {weekday:'Ngày thường',weekend:'Cuối tuần',holiday:'Ngày lễ'};
            html += `<div class="alert alert-light border-start border-4 border-purple py-2 mb-2">
                <strong>🌙 OT ${data.ot.hours}h</strong><br>
                <small class="text-muted">${data.ot.ot_date} · ${otTypes[data.ot.ot_type]||data.ot.ot_type}</small>
            </div>`;
        }
        if (data.leave) {
            const lt = {annual:'Phép năm',sick:'Nghỉ ốm',unpaid:'Không lương',other:'Khác'};
            html += `<div class="alert alert-info py-2 mb-2">
                <strong>🏖️ ${lt[data.leave.leave_type]||data.leave.leave_type}</strong><br>
                <small>${data.leave.reason||''}</small>
            </div>`;
        }
        if (!data.ot && !data.leave) html += `<p class="text-muted small mt-2">Không có OT hoặc đơn nghỉ</p>`;
        html += `</div></div></div>`;

        if (data.shift) {
            html += `<div class="col-12">
                <div class="card border-0 border-start border-4" style="border-color:${data.shift.color}!important;">
                    <div class="card-body py-2">
                        <span class="badge me-2" style="background:${data.shift.color}">${data.shift.shift_name}</span>
                        <small class="text-muted">
                            ${data.shift.start_time.substring(0,5)} – ${data.shift.end_time.substring(0,5)}
                            &nbsp;|&nbsp; ${data.shift.work_hours}h/ca
                            &nbsp;|&nbsp; Ngưỡng trễ: ${data.shift.late_threshold ?? 0} phút
                        </small>
                    </div>
                </div>
            </div>`;
        }

        if (IS_DIRECTOR) {
            html += `<div class="col-12">
                <div class="card border-warning border-2">
                    <div class="card-header bg-warning bg-opacity-10 py-2">
                        <h6 class="mb-0 fw-bold text-warning"><i class="fas fa-edit me-1"></i>Chỉnh sửa thủ công</h6>
                    </div>
                    <div class="card-body">
                        <div class="row g-2">
                            <div class="col-md-3">
                                <label class="form-label small fw-semibold">Giờ vào</label>
                                <input type="time" id="editCheckIn" class="form-control form-control-sm" value="${checkInVal}">
                            </div>
                            <div class="col-md-3">
                                <label class="form-label small fw-semibold">Giờ ra</label>
                                <input type="time" id="editCheckOut" class="form-control form-control-sm" value="${checkOutVal}">
                            </div>
                            <div class="col-md-6">
                                <label class="form-label small fw-semibold">Ghi chú</label>
                                <input type="text" id="editNote" class="form-control form-control-sm"
                                       placeholder="Lý do chỉnh sửa...">
                            </div>
                        </div>
                        <div id="editResult" class="mt-2"></div>
                        <div class="mt-2 d-flex gap-2">
                            <button class="btn btn-warning btn-sm" onclick="saveAttendance(${userId}, '${dateStr}')">
                                <i class="fas fa-save me-1"></i>Lưu
                            </button>
                            <button class="btn btn-outline-danger btn-sm"
                                    onclick="if(confirm('Xóa bản ghi này?')) deleteAttendance(${userId}, '${dateStr}')">
                                <i class="fas fa-trash me-1"></i>Xóa
                            </button>
                        </div>
                    </div>
                </div>
            </div>`;
        }

        if (data.audit?.length > 0) {
            html += `<div class="col-12"><div class="card border-0 bg-light"><div class="card-body py-2">
                <h6 class="fw-bold small mb-2">📜 Lịch sử chỉnh sửa</h6>
                <table class="table table-sm mb-0" style="font-size:11px;">
                    <thead><tr><th>Thời gian</th><th>Người sửa</th><th>Giờ vào</th><th>Giờ ra</th><th>Ghi chú</th></tr></thead>
                    <tbody>`;
            data.audit.forEach(a => {
                html += `<tr>
                    <td>${a.created_at?.substring(0,16)||'—'}</td>
                    <td>${a.changed_by_name||'—'}</td>
                    <td>${a.new_check_in?.substring(11,16)||'—'}</td>
                    <td>${a.new_check_out?.substring(11,16)||'—'}</td>
                    <td class="text-muted">${a.note||'—'}</td>
                </tr>`;
            });
            html += `</tbody></table></div></div></div>`;
        }

        html += `</div>`;
        document.getElementById('dayDetailBody').innerHTML = html;
    } catch(e) {
        document.getElementById('dayDetailBody').innerHTML =
            '<div class="alert alert-danger">Lỗi tải dữ liệu.</div>';
    }
}

async function saveAttendance(userId, dateStr) {
    const checkIn  = document.getElementById('editCheckIn').value;
    const checkOut = document.getElementById('editCheckOut').value;
    const note     = document.getElementById('editNote').value;
    const result   = document.getElementById('editResult');
    if (!checkIn) { result.innerHTML = '<div class="alert alert-warning py-1 small">Vui lòng nhập giờ vào.</div>'; return; }
    result.innerHTML = '<div class="text-muted small"><i class="fas fa-spinner fa-spin me-1"></i>Đang lưu...</div>';
    try {
        const res  = await fetch('/erp/api/attendance/update.php', {
            method: 'POST', headers: {'Content-Type':'application/json'},
            body: JSON.stringify({user_id:userId, date:dateStr, check_in:checkIn, check_out:checkOut, note})
        });
        const data = await res.json();
        if (data.ok) {
            result.innerHTML = `<div class="alert alert-success py-1 small">✅ Đã lưu! ${data.work_hours}h</div>`;
            setTimeout(() => location.reload(), 1500);
        } else {
            result.innerHTML = `<div class="alert alert-danger py-1 small">❌ ${data.msg}</div>`;
        }
    } catch(e) { result.innerHTML = '<div class="alert alert-danger py-1 small">Lỗi kết nối.</div>'; }
}

async function deleteAttendance(userId, dateStr) {
    try {
        const res  = await fetch('/erp/api/attendance/update.php', {
            method:'POST', headers:{'Content-Type':'application/json'},
            body: JSON.stringify({user_id:userId, date:dateStr, check_in:'', check_out:'', note:'Xóa bản ghi', action:'delete'})
        });
        const data = await res.json();
        if (data.ok) { alert('Đã xóa!'); location.reload(); }
        else alert('❌ ' + data.msg);
    } catch(e) { alert('Lỗi kết nối.'); }
}

function exportExcel() {
    const tableId = document.getElementById('summaryTable') ? 'summaryTable' : 'attTable';
    const table   = document.getElementById(tableId);
    if (!table) { alert('Không tìm thấy bảng dữ liệu.'); return; }
    const wb = XLSX.utils.book_new();
    const ws = XLSX.utils.table_to_sheet(table);
    XLSX.utils.book_append_sheet(wb, ws, 'ChamCong');
    XLSX.writeFile(wb, `ChamCong_T<?= $viewMonth ?>_<?= $viewYear ?>.xlsx`);
}
</script>

<script src="https://cdnjs.cloudflare.com/ajax/libs/xlsx/0.18.5/xlsx.full.min.js"></script>
<?php include $_SERVER['DOCUMENT_ROOT'] . '/erp/includes/footer.php'; ?>