<?php
// pages/print_form.php
requireRole('any');

$id = isset($_GET['id']) ? (int)$_GET['id'] : null;
$member = null;
if ($id) {
    $stmt = $pdo->prepare("SELECT * FROM members WHERE id = ?");
    $stmt->execute([$id]);
    $member = $stmt->fetch();
}

// Helpers
function dateVal($d) { return $d ? date('d/m/Y', strtotime($d)) : '___/___/______'; }
function textVal($t) { return $t ? htmlspecialchars($t) : '______________________________'; }
function decode_or_default($json, $default) {
    if (empty($json)) return $default;
    $decoded = json_decode($json, true);
    return is_array($decoded) ? $decoded : $default;
}

// Decode Data
$exp = decode_or_default($member['experiences']??'', ['salvation'=>null,'sanctification'=>null,'baptism_holy_ghost'=>null,'water_baptism'=>null]);
$nok = decode_or_default($member['next_of_kin']??'', ['name'=>'','relationship'=>'','address'=>'','phone'=>'','email'=>'']);
$emp = decode_or_default($member['employment']??'', ['business_name'=>'','job_title'=>'','business_address'=>'','date_joined'=>'','qualifications'=>'','skills'=>'']);
$refs = decode_or_default($member['referees']??'', [['name'=>'','relationship'=>'','address'=>'','phone'=>'','email'=>''],['name'=>'','relationship'=>'','address'=>'','phone'=>'','email'=>'']]);
if (empty($refs)) $refs = [['name'=>'','relationship'=>'','address'=>'','phone'=>'','email'=>''],['name'=>'','relationship'=>'','address'=>'','phone'=>'','email'=>'']];
$assigns = decode_or_default($member['past_assignments']??'', []);
while(count($assigns) < 3) $assigns[] = ['role'=>'','location'=>'','dates'=>'','pastor'=>''];

?>
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Member Biodata Form</title>
    <style>
        body { font-family: 'Times New Roman', serif; font-size: 13px; margin: 0; padding: 20px; color: #000; background: #fff !important; line-height: 1.4; }
        .container { max-width: 800px; margin: 0 auto; border: 1px solid #000; padding: 25px; background: #fff !important; }
        
        /* Fail-safe: Hide global UI elements in case they are included */
        header, nav, .navbar, .sidebar, footer, #main-menu, .main-header { display: none !important; }
        
        .header { text-align: center; margin-bottom: 15px; border-bottom: 2px solid #000; padding-bottom: 5px; display: block !important; }
        .header h1 { margin: 0; font-size: 20px; text-transform: uppercase; color: #000; }
        .header h2 { margin: 5px 0 0; font-size: 16px; font-weight: normal; color: #000; }
        
        .section { margin-bottom: 15px; }
        .section-title { font-weight: bold; font-size: 14px; background: #eee; color: #000; padding: 3px 5px; border: 1px solid #000; margin-bottom: 8px; -webkit-print-color-adjust: exact; }
        
        .row { display: flex; margin-bottom: 5px; gap: 15px; }
        .col { flex: 1; }
        
        .field { margin-bottom: 5px; display: flex; align-items: baseline; }
        .label { font-weight: bold; margin-right: 5px; white-space: nowrap; color: #000; }
        .value { border-bottom: 1px dotted #000; flex: 1; padding: 0 5px; min-height: 18px; color: #000; }
        
        table { width: 100%; border-collapse: collapse; margin-top: 5px; font-size: 12px; background: #fff !important; }
        th, td { border: 1px solid #000; padding: 4px; text-align: left; color: #000 !important; background: #fff !important; }
        /* Ensure table header is white */
        th { background: #fff !important; font-weight: bold; }
        
        @media print {
            body { padding: 0; background: #fff !important; }
            .container { border: none; padding: 0; margin: 0; max-width: 100%; }
            button { display: none; }
            /* Ensure these are hidden in print too */
            header, nav, .navbar, .sidebar, footer { display: none !important; }
            
            .section-title { background: #eee !important; -webkit-print-color-adjust: exact; }
            th { background: #fff !important; -webkit-print-color-adjust: exact; }
        }
    </style>
</head>
<body>

<div style="text-align: right; margin-bottom: 20px;">
    <button onclick="window.print()" style="padding: 10px 20px; font-size: 16px; cursor: pointer;">Print Form</button>
</div>

<div class="container">
    <div class="header">
        <h1>Apostolic Faith Church</h1>
        <h2>Member Biodata Form</h2>
    </div>

    <!-- A. Personal Details -->
    <div class="section">
        <div class="section-title">A. Personal Details</div>
        <div class="field"><span class="label">Full Name:</span><span class="value"><?php echo $member['full_name'] ?? ''; ?></span></div>
        <div class="field"><span class="label">Address:</span><span class="value"><?php echo $member['address'] ?? ''; ?></span></div>
        <div class="row">
            <div class="col"><div class="field"><span class="label">Phone:</span><span class="value"><?php echo $member['phone'] ?? ''; ?></span></div></div>
            <div class="col"><div class="field"><span class="label">WhatsApp:</span><span class="value"><?php echo $member['whatsapp'] ?? ''; ?></span></div></div>
            <div class="col"><div class="field"><span class="label">Email:</span><span class="value"><?php echo $member['email'] ?? ''; ?></span></div></div>
        </div>
        <div class="row">
            <div class="col"><div class="field"><span class="label">DOB:</span><span class="value"><?php echo dateVal($member['dob']??null); ?></span></div></div>
            <div class="col"><div class="field"><span class="label">Gender:</span><span class="value"><?php echo $member['gender'] ?? '______'; ?></span></div></div>
            <div class="col"><div class="field"><span class="label">State of Origin:</span><span class="value"><?php echo $member['state_origin'] ?? ''; ?></span></div></div>
        </div>
        <div class="row">
             <div class="col"><div class="field"><span class="label">Marital Status:</span><span class="value"><?php echo $member['marital_status'] ?? '______'; ?></span></div></div>
             <div class="col"><div class="field"><span class="label">Wedding Anniversary:</span><span class="value"><?php echo dateVal($member['wedding_anniversary']??null); ?></span></div></div>
        </div>
        
        <!-- Next of Kin -->
        <div style="margin-top: 10px; font-weight: bold; border-bottom: 1px solid #ccc;">Next of Kin</div>
        <div class="row" style="margin-top: 5px;">
             <div class="col"><div class="field"><span class="label">Name:</span><span class="value"><?php echo $nok['name']; ?></span></div></div>
             <div class="col"><div class="field"><span class="label">Relationship:</span><span class="value"><?php echo $nok['relationship']; ?></span></div></div>
        </div>
        <div class="field"><span class="label">Address:</span><span class="value"><?php echo $nok['address']; ?></span></div>
        <div class="row">
             <div class="col"><div class="field"><span class="label">Phone:</span><span class="value"><?php echo $nok['phone']; ?></span></div></div>
             <div class="col"><div class="field"><span class="label">Email:</span><span class="value"><?php echo $nok['email']; ?></span></div></div>
        </div>
    </div>

    <!-- B. Work -->
    <div class="section">
        <div class="section-title">B. Work / Education</div>
        <div class="row">
             <div class="col"><div class="field"><span class="label">Business/School Name:</span><span class="value"><?php echo $emp['business_name']; ?></span></div></div>
             <div class="col"><div class="field"><span class="label">Job Title/Course:</span><span class="value"><?php echo $emp['job_title']; ?></span></div></div>
        </div>
        <div class="row">
             <div class="col"><div class="field"><span class="label">Address:</span><span class="value"><?php echo $emp['business_address']; ?></span></div></div>
             <div class="col"><div class="field"><span class="label">Date Joined:</span><span class="value"><?php echo dateVal($emp['date_joined']); ?></span></div></div>
        </div>
        <div class="field"><span class="label">Qualifications:</span><span class="value"><?php echo $emp['qualifications']; ?></span></div>
        <div class="field"><span class="label">Skills:</span><span class="value"><?php echo $emp['skills']; ?></span></div>
    </div>

    <!-- C. Spiritual Info -->
    <div class="section">
        <div class="section-title">C. Spiritual Information</div>
        <div class="row">
            <div class="col"><div class="field"><span class="label">Salvation:</span><span class="value"><?php echo dateVal($exp['salvation']); ?></span></div></div>
            <div class="col"><div class="field"><span class="label">Sanctification:</span><span class="value"><?php echo dateVal($exp['sanctification']); ?></span></div></div>
        </div>
        <div class="row">
            <div class="col"><div class="field"><span class="label">Holy Ghost:</span><span class="value"><?php echo dateVal($exp['baptism_holy_ghost']); ?></span></div></div>
            <div class="col"><div class="field"><span class="label">Water Baptism:</span><span class="value"><?php echo dateVal($exp['water_baptism']); ?></span></div></div>
        </div>
        <div class="row" style="margin-top: 5px;">
            <div class="col"><div class="field"><span class="label">Date Joined AFM:</span><span class="value"><?php echo dateVal($member['date_joined'] ?? null); ?></span></div></div>
            <div class="col"><div class="field"><span class="label">Current Branch:</span><span class="value"><?php echo $member['branch'] ?? ''; ?></span></div></div>
            <div class="col"><div class="field"><span class="label">Category:</span><span class="value"><?php echo $member['category'] ?? ''; ?></span></div></div>
        </div>
        <div class="row">
             <div class="col"><div class="field"><span class="label">Department:</span><span class="value"><?php echo $member['department'] ?? ''; ?></span></div></div>
             <div class="col"><div class="field"><span class="label">Unit:</span><span class="value"><?php echo $member['unit'] ?? ''; ?></span></div></div>
             <div class="col"><div class="field"><span class="label">Role:</span><span class="value"><?php echo $member['unit_role'] ?? ''; ?></span></div></div>
        </div>

        <div style="margin-top: 5px; font-weight: bold;">Current / Past Assignments</div>
        <table>
            <thead><tr><th>Role / Assignment</th><th>Location</th><th>Dates</th><th>Pastor's Name</th></tr></thead>
            <tbody>
                <?php foreach($assigns as $row): ?>
                <tr>
                    <td><?php echo textVal($row['role']); ?></td>
                    <td><?php echo textVal($row['location']); ?></td>
                    <td><?php echo textVal($row['dates']); ?></td>
                    <td><?php echo textVal($row['pastor']); ?></td>
                </tr>
                <?php endforeach; ?>
            </tbody>
        </table>
    </div>

    <!-- D. Referees -->
    <div class="section">
        <div class="section-title">D. Referees</div>
        <?php foreach($refs as $k => $ref): ?>
        <div style="margin-top: 5px; font-style: italic;">Referee <?php echo $k+1; ?></div>
        <div class="row">
             <div class="col"><div class="field"><span class="label">Name:</span><span class="value"><?php echo $ref['name']; ?></span></div></div>
             <div class="col"><div class="field"><span class="label">Relationship:</span><span class="value"><?php echo $ref['relationship']; ?></span></div></div>
        </div>
        <div class="field"><span class="label">Address:</span><span class="value"><?php echo $ref['address']; ?></span></div>
        <div class="row">
             <div class="col"><div class="field"><span class="label">Phone:</span><span class="value"><?php echo $ref['phone']; ?></span></div></div>
             <div class="col"><div class="field"><span class="label">Email:</span><span class="value"><?php echo $ref['email']; ?></span></div></div>
        </div>
        <?php endforeach; ?>
    </div>
    
    <div class="section" style="margin-top: 30px;">
        <div class="field"><span class="label">Signature:</span><span class="value"></span></div>
        <div class="field"><span class="label">Date:</span><span class="value"></span></div>
    </div>
</div>
</body>
</html>
