<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Payslip — <?php echo htmlspecialchars($record['name']); ?> — <?php echo date('F Y', mktime(0,0,0,$record['period_month'],1,$record['period_year'])); ?></title>
    <link href="https://fonts.googleapis.com/css2?family=Inter:wght@300;400;500;600;700&display=swap" rel="stylesheet">
    <style>
        * { margin:0; padding:0; box-sizing:border-box; font-family:'Inter',sans-serif; }
        body { background:#f8fafc; color:#1e293b; padding:32px; }
        .slip { max-width:680px; margin:0 auto; background:white; border-radius:16px; box-shadow:0 4px 32px rgba(0,0,0,0.1); overflow:hidden; }
        .slip-header { background:linear-gradient(135deg,#6366f1,#4f46e5); color:white; padding:28px 32px; }
        .slip-header h1 { font-size:22px; font-weight:700; letter-spacing:-0.3px; }
        .slip-header p  { font-size:13px; opacity:0.75; margin-top:4px; }
        .slip-meta  { display:grid; grid-template-columns:1fr 1fr; gap:20px; padding:24px 32px; border-bottom:1px solid #f1f5f9; }
        .meta-block label { font-size:10px; font-weight:700; text-transform:uppercase; letter-spacing:0.08em; color:#94a3b8; display:block; margin-bottom:4px; }
        .meta-block span  { font-size:14px; font-weight:600; color:#1e293b; }
        .breakdown { padding:0 32px 24px; }
        .breakdown h3 { font-size:11px; font-weight:700; text-transform:uppercase; letter-spacing:0.1em; color:#94a3b8; margin:24px 0 12px; }
        .row { display:flex; justify-content:space-between; padding:8px 0; font-size:14px; border-bottom:1px solid #f8fafc; }
        .row .label  { color:#64748b; }
        .row .value  { font-weight:600; color:#1e293b; }
        .row.plus .value  { color:#059669; }
        .row.minus .value { color:#dc2626; }
        .row.total { border-top:2px solid #e2e8f0; margin-top:8px; padding-top:14px; }
        .row.total .label { font-weight:700; font-size:15px; color:#1e293b; }
        .row.total .value { font-size:20px; font-weight:800; color:#6366f1; }
        .badge { display:inline-block; padding:3px 10px; border-radius:20px; font-size:11px; font-weight:600; }
        .badge-paid  { background:#dcfce7; color:#166534; }
        .badge-draft { background:#fef9c3; color:#854d0e; }
        .footer { text-align:center; padding:16px 32px; font-size:11px; color:#94a3b8; border-top:1px solid #f1f5f9; background:#f8fafc; }
        .attendance-grid { display:grid; grid-template-columns:repeat(4,1fr); gap:10px; margin-top:12px; }
        .att-cell { text-align:center; padding:10px; background:#f8fafc; border-radius:8px; }
        .att-cell .num { font-size:18px; font-weight:700; }
        .att-cell .lbl { font-size:10px; color:#94a3b8; margin-top:2px; }
        .att-cell.green .num { color:#059669; }
        .att-cell.red .num   { color:#dc2626; }
        .att-cell.amber .num { color:#d97706; }
        .att-cell.blue .num  { color:#2563eb; }
        .print-btn { position:fixed; bottom:24px; right:24px; padding:12px 20px; background:#6366f1; color:white; border:none; border-radius:10px; font-size:14px; font-weight:600; cursor:pointer; box-shadow:0 4px 16px rgba(99,102,241,0.4); }
        @media print { .print-btn { display:none; } body { padding:0; background:white; } .slip { box-shadow:none; border-radius:0; } }
    </style>
</head>
<body>
<div class="slip">
    <!-- Header -->
    <div class="slip-header">
        <div style="display:flex; justify-content:space-between; align-items:flex-start">
            <div>
                <h1><?php echo htmlspecialchars($record['restaurant_name']); ?></h1>
                <p>PAYSLIP — <?php echo date('F Y', mktime(0,0,0,$record['period_month'],1,$record['period_year'])); ?></p>
            </div>
            <span class="badge <?php echo $record['status']==='paid'?'badge-paid':'badge-draft'; ?>" style="margin-top:4px">
                <?php echo $record['status'] === 'paid' ? '✓ Paid' : '⏳ Draft'; ?>
            </span>
        </div>
    </div>

    <!-- Employee Info -->
    <div class="slip-meta">
        <div class="meta-block">
            <label>Employee Name</label>
            <span><?php echo htmlspecialchars($record['name']); ?></span>
        </div>
        <div class="meta-block">
            <label>Email</label>
            <span><?php echo htmlspecialchars($record['email']); ?></span>
        </div>
        <div class="meta-block">
            <label>Designation</label>
            <span><?php echo htmlspecialchars($record['designation'] ?: ucfirst($record['role'])); ?></span>
        </div>
        <div class="meta-block">
            <label>Department</label>
            <span><?php echo htmlspecialchars($record['department'] ?: '—'); ?></span>
        </div>
        <div class="meta-block">
            <label>Pay Period</label>
            <span><?php echo date('F Y', mktime(0,0,0,$record['period_month'],1,$record['period_year'])); ?></span>
        </div>
        <div class="meta-block">
            <label>Payment Date</label>
            <span><?php echo $record['paid_at'] ? date('M d, Y', strtotime($record['paid_at'])) : 'Not paid yet'; ?></span>
        </div>
    </div>

    <div class="breakdown">
        <!-- Attendance Summary -->
        <h3>Attendance Summary</h3>
        <div class="attendance-grid">
            <div class="att-cell green">
                <div class="num"><?php echo $record['days_present']; ?><span style="font-size:12px;font-weight:400;color:#94a3b8">/<?php echo $record['working_days']; ?></span></div>
                <div class="lbl">Present</div>
            </div>
            <div class="att-cell red">
                <div class="num"><?php echo $record['days_absent']; ?></div>
                <div class="lbl">Absent</div>
            </div>
            <div class="att-cell amber">
                <div class="num"><?php echo $record['days_late']; ?></div>
                <div class="lbl">Late</div>
            </div>
            <div class="att-cell blue">
                <div class="num"><?php echo number_format($record['overtime_hours'],1); ?>h</div>
                <div class="lbl">Overtime</div>
            </div>
        </div>

        <!-- Earnings & Deductions -->
        <h3>Earnings</h3>
        <div class="row plus">
            <span class="label">Base Salary (<?php echo ucfirst($record['salary_type']); ?>)</span>
            <span class="value"><?php echo $currency . number_format($record['base_salary'], 2); ?></span>
        </div>
        <?php if ($record['overtime_pay'] > 0): ?>
        <div class="row plus">
            <span class="label">Overtime Pay (<?php echo number_format($record['overtime_hours'],1); ?>h × <?php echo $currency . number_format($record['overtime_rate'],2); ?>)</span>
            <span class="value"><?php echo $currency . number_format($record['overtime_pay'], 2); ?></span>
        </div>
        <?php endif; ?>
        <?php if ($record['bonus'] > 0): ?>
        <div class="row plus">
            <span class="label">Bonus</span>
            <span class="value"><?php echo $currency . number_format($record['bonus'], 2); ?></span>
        </div>
        <?php endif; ?>

        <h3>Deductions</h3>
        <?php if ((float)$record['gross_pay'] < (float)$record['base_salary']): ?>
        <div class="row minus">
            <span class="label">Absence / Late Deductions</span>
            <span class="value">- <?php echo $currency . number_format($record['base_salary'] - $record['gross_pay'] + $record['overtime_pay'], 2); ?></span>
        </div>
        <?php endif; ?>
        <?php if ($record['deductions'] > 0): ?>
        <div class="row minus">
            <span class="label">Other Deductions</span>
            <span class="value">- <?php echo $currency . number_format($record['deductions'], 2); ?></span>
        </div>
        <?php endif; ?>
        <?php if ($record['deductions'] == 0 && (float)$record['gross_pay'] >= (float)$record['base_salary']): ?>
        <div class="row">
            <span class="label" style="color:#94a3b8; font-style:italic">No deductions</span>
            <span class="value" style="color:#94a3b8">—</span>
        </div>
        <?php endif; ?>

        <!-- Net Pay -->
        <div class="row total">
            <span class="label">Net Pay</span>
            <span class="value"><?php echo $currency . number_format($record['net_pay'], 2); ?></span>
        </div>

        <?php if ($record['notes']): ?>
        <div style="margin-top:16px; padding:12px 16px; background:#f8fafc; border-radius:8px; border-left:3px solid #6366f1">
            <p style="font-size:11px; color:#64748b; font-weight:600; margin-bottom:4px; text-transform:uppercase; letter-spacing:0.05em">Notes</p>
            <p style="font-size:13px; color:#475569;"><?php echo htmlspecialchars($record['notes']); ?></p>
        </div>
        <?php endif; ?>
    </div>

    <div class="footer">
        This payslip is system-generated by RestPO · <?php echo htmlspecialchars($record['restaurant_name']); ?> · <?php echo date('Y'); ?>
    </div>
</div>

<button class="print-btn" onclick="window.print()">🖨️ Print Payslip</button>
</body>
</html>
