<?php
/* ============================================================================
 *  staff/my_information.php — read-only view of a staff member's own profile
 *  (the data the company admin entered on the staff form).
 *  Bilingual EN/Lao.
 * ==========================================================================*/
session_start();
if (!isset($_SESSION["user_id"]) || empty($_SESSION["company_id"])) {
    header("Location: ../auth/login.php"); exit;
}
require_once '../config/database.php';

$userId    = (int)$_SESSION["user_id"];
$companyId = (int)$_SESSION["company_id"];

$row = $pdo->prepare("
    SELECT u.name, u.username, u.phone, u.email, u.status,
           cs.full_name_lo, cs.nickname, cs.staff_code, cs.dob, cs.photo,
           cs.position, cs.department, cs.joined_date,
           cs.bank_name, cs.bank_account, cs.contract_file, cs.cv_file,
           c.name AS company_name
    FROM company_staff cs
    JOIN users u ON u.id = cs.user_id
    JOIN companies c ON c.id = cs.company_id
    WHERE cs.user_id = ? AND cs.company_id = ? LIMIT 1");
$row->execute([$userId, $companyId]);
$d = $row->fetch(PDO::FETCH_ASSOC) ?: [];

function showd($v){ return ($v===null || $v==='') ? '—' : htmlspecialchars($v); }
function showdate($v){ return ($v===null || $v==='' || $v==='0000-00-00') ? '—' : date('d/m/Y', strtotime($v)); }
?>
<!DOCTYPE html>
<html lang="lo">
<head>
    <?php $pwaTheme='#0f766e'; include __DIR__.'/../config/pwa_head.php'; ?>
    <title>My Information</title>
    <link href="https://fonts.googleapis.com/css2?family=Noto+Sans+Lao:wght@400;500;600;700;800&family=Inter:wght@400;500;600;700;800&display=swap" rel="stylesheet">
    <script src="../assets/lang.js" defer></script>
    <style>
        *,*::before,*::after{box-sizing:border-box;margin:0;padding:0;}
        :root{--primary:#0f766e;--surface:#fff;--bg:#f0f4f8;--border:#e2e8f0;--text:#0f172a;--muted:#64748b;}
        body{font-family:"Noto Sans Lao","Inter",Arial,sans-serif;background:var(--bg);color:var(--text);min-height:100vh;}
        .top-bar{background:var(--surface);border-bottom:1px solid var(--border);
            padding:calc(12px + env(safe-area-inset-top,0px)) 18px 12px;display:flex;justify-content:space-between;
            align-items:center;gap:12px;position:sticky;top:0;z-index:100;box-shadow:0 1px 6px rgba(15,23,42,.06);}
        .back-link{background:#f1f5f9;color:var(--text);border:1.5px solid var(--border);padding:7px 13px;border-radius:8px;text-decoration:none;font-size:13px;font-weight:700;}
        .lang-bar{display:flex;gap:7px;}
        .lang-btn{padding:4px 11px;border-radius:999px;border:1.5px solid var(--border);background:var(--surface);font-size:11px;font-weight:700;color:var(--muted);cursor:pointer;font-family:inherit;}
        .lang-btn.active{background:var(--primary);border-color:var(--primary);color:#fff;}
        .page{max-width:560px;margin:auto;padding:18px 16px;}
        .head{display:flex;align-items:center;gap:16px;background:linear-gradient(135deg,#042f2e,#0f766e);border-radius:20px;padding:22px;color:#fff;margin-bottom:18px;}
        .ph{width:78px;height:78px;border-radius:18px;object-fit:cover;border:2px solid rgba(255,255,255,.5);background:rgba(255,255,255,.15);display:flex;align-items:center;justify-content:center;font-size:32px;font-weight:800;flex-shrink:0;}
        .nm{font-size:20px;font-weight:800;line-height:1.2;}
        .nmlo{font-size:14px;color:rgba(255,255,255,.85);margin-top:3px;}
        .idtag{display:inline-block;margin-top:8px;font-size:12px;font-weight:800;background:rgba(255,255,255,.15);padding:3px 10px;border-radius:999px;}
        .card{background:var(--surface);border:1px solid var(--border);border-radius:16px;box-shadow:0 2px 14px rgba(15,23,42,.09);padding:6px 18px;margin-bottom:16px;}
        .sec{font-size:12px;font-weight:800;color:var(--muted);text-transform:uppercase;letter-spacing:.5px;padding:14px 0 8px;}
        .r{display:flex;justify-content:space-between;gap:12px;padding:11px 0;border-bottom:1px solid var(--border);font-size:14px;}
        .r:last-child{border-bottom:none;}
        .k{color:var(--muted);font-weight:600;}
        .v{font-weight:700;text-align:right;word-break:break-word;}
        .filelink{display:inline-flex;align-items:center;gap:6px;color:var(--primary);font-weight:700;text-decoration:none;background:#f0fdfa;border:1.5px solid #99f6e4;padding:6px 11px;border-radius:9px;font-size:13px;}
    </style>
</head>
<body>

<div class="top-bar">
    <a class="back-link" href="../customer/company_home.php">← <span data-lo="ກັບຄືນ" data-en="Back">Back</span></a>
    <div class="lang-bar">
        <button class="lang-btn" id="btn-lo" onclick="setLang('lo')">🇱🇦 ລາວ</button>
        <button class="lang-btn" id="btn-en" onclick="setLang('en')">🇬🇧 EN</button>
    </div>
</div>

<div class="page">
    <div class="head">
        <?php if (!empty($d['photo'])): ?>
            <img class="ph" src="../<?= htmlspecialchars($d['photo']) ?>" alt="">
        <?php else: ?>
            <div class="ph"><?= strtoupper(mb_substr($d['name'] ?? 'S',0,1)) ?></div>
        <?php endif; ?>
        <div>
            <div class="nm"><?= showd($d['name'] ?? '') ?></div>
            <?php if (!empty($d['full_name_lo'])): ?><div class="nmlo"><?= showd($d['full_name_lo']) ?></div><?php endif; ?>
            <?php if (!empty($d['staff_code'])): ?><div class="idtag">ID: <?= showd($d['staff_code']) ?></div><?php endif; ?>
        </div>
    </div>

    <div class="card">
        <div class="sec" data-lo="ຂໍ້ມູນສ່ວນຕົວ" data-en="Personal">Personal</div>
        <div class="r"><span class="k" data-lo="ຊື່ຫຼິ້ນ" data-en="Nickname">Nickname</span><span class="v"><?= showd($d['nickname'] ?? '') ?></span></div>
        <div class="r"><span class="k" data-lo="ເບີໂທ" data-en="Phone">Phone</span><span class="v"><?= showd($d['phone'] ?? '') ?></span></div>
        <div class="r"><span class="k" data-lo="ອີເມວ" data-en="Email">Email</span><span class="v"><?= showd($d['email'] ?? '') ?></span></div>
        <div class="r"><span class="k" data-lo="ວັນເກີດ" data-en="Date of Birth">Date of Birth</span><span class="v"><?= showdate($d['dob'] ?? '') ?></span></div>
    </div>

    <div class="card">
        <div class="sec" data-lo="ຂໍ້ມູນວຽກ" data-en="Work">Work</div>
        <div class="r"><span class="k" data-lo="ບໍລິສັດ" data-en="Company">Company</span><span class="v"><?= showd($d['company_name'] ?? '') ?></span></div>
        <div class="r"><span class="k" data-lo="ຕຳແໜ່ງ" data-en="Position">Position</span><span class="v"><?= showd($d['position'] ?? '') ?></span></div>
        <div class="r"><span class="k" data-lo="ພະແນກ" data-en="Department">Department</span><span class="v"><?= showd($d['department'] ?? '') ?></span></div>
        <div class="r"><span class="k" data-lo="ວັນເລີ່ມວຽກ" data-en="Date Joined">Date Joined</span><span class="v"><?= showdate($d['joined_date'] ?? '') ?></span></div>
    </div>

    <div class="card">
        <div class="sec" data-lo="ຂໍ້ມູນທະນາຄານ" data-en="Bank">Bank</div>
        <div class="r"><span class="k" data-lo="ຊື່ທະນາຄານ" data-en="Bank Name">Bank Name</span><span class="v"><?= showd($d['bank_name'] ?? '') ?></span></div>
        <div class="r"><span class="k" data-lo="ເລກບັນຊີ" data-en="Account No.">Account No.</span><span class="v"><?= showd($d['bank_account'] ?? '') ?></span></div>
    </div>

    <div class="card">
        <div class="sec" data-lo="ເອກະສານ" data-en="Documents">Documents</div>
        <div class="r"><span class="k" data-lo="ສັນຍາ" data-en="Contract">Contract</span><span class="v">
            <?php if (!empty($d['contract_file'])): ?><a class="filelink" href="view_doc.php?d=contract">📎 <span data-lo="ເບິ່ງ" data-en="View">View</span></a><?php else: ?>—<?php endif; ?>
        </span></div>
        <div class="r"><span class="k" data-lo="ປະຫວັດ (CV)" data-en="CV">CV</span><span class="v">
            <?php if (!empty($d['cv_file'])): ?><a class="filelink" href="view_doc.php?d=cv">📎 <span data-lo="ເບິ່ງ" data-en="View">View</span></a><?php else: ?>—<?php endif; ?>
        </span></div>
    </div>

    <p style="font-size:12px;color:var(--muted);text-align:center;padding:4px 0 20px;"
       data-lo="ຖ້າຂໍ້ມູນບໍ່ຖືກຕ້ອງ ກະລຸນາແຈ້ງຝ່າຍບຸກຄະລາກອນ (HR)." data-en="If anything is wrong, please contact your HR/admin.">
        If anything is wrong, please contact your HR/admin.</p>
</div>

<?php require __DIR__ . '/../config/auto_update.php'; ?>
</body>
</html>