<?php
require_once __DIR__ . '/includes/expense_common.php';
expense_require_role(['employee', 'manager']);
expense_ensure_schema($conn);

$user_id = $_SESSION['user_id'];
$username = $_SESSION['username'] ?? 'User';
$claim_id = (int)($_GET['id'] ?? 0);

$stmt = $conn->prepare("SELECT * FROM expenses WHERE id = ? AND employee_id = ?");
$stmt->bind_param("is", $claim_id, $user_id);
$stmt->execute();
$claim = $stmt->get_result()->fetch_assoc();
$stmt->close();

if (!$claim) {
    set_flash('danger', 'Claim not found.');
    header("Location: expense_claim_list.php");
    exit();
}

require_once __DIR__ . '/includes/expense_theme.php';
[$cls, $icon, $label] = expense_status_badge($claim['status']);
expense_render_head('Claim #' . $claim['id']);
expense_render_sidebar_and_topbar('employee', $username, 'expense_claim_list.php', 'Claim #' . $claim['id']);
?>
  <div class="page">
    <div class="page-header">
      <div>
        <div class="page-title">Claim #<?= (int)$claim['id'] ?></div>
        <div class="page-subtitle">Submitted <?= h(date('d M Y, H:i', strtotime($claim['created_at']))) ?></div>
      </div>
      <a href="expense_claim_list.php" class="back-btn"><i class="fas fa-arrow-left" style="font-size:10px"></i> My Claims</a>
    </div>

    <?php expense_render_flash(); ?>

    <div class="dash-card">
      <div class="dash-card-header">
        <div class="dash-card-title"><i class="fas fa-receipt"></i> Claim Details</div>
        <span class="badge-custom <?= $cls ?>"><i class="fas fa-<?= $icon ?>" style="font-size:9px"></i><?= $label ?></span>
      </div>
      <div class="dash-card-body">
        <div class="task-meta" style="margin-top:0;margin-bottom:14px">
          <span><i class="fas fa-calendar" style="margin-right:3px"></i> <?= h($claim['expense_date']) ?><?php if (!empty($claim['to_date']) && $claim['to_date'] !== $claim['expense_date']): ?> → <?= h($claim['to_date']) ?><?php endif; ?></span>
          <span><i class="fas fa-tag" style="margin-right:3px"></i> <?= h($claim['category']) ?></span>
          <span style="font-weight:700;color:var(--ink)">₹<?= number_format((float)$claim['amount'], 2) ?></span>
        </div>
        <div class="task-desc-text" style="font-size:13px;margin-bottom:14px"><?= nl2br(h($claim['description'])) ?></div>

        <?php if (!empty($claim['bill_image'])): ?>
          <div class="form-group">
            <label class="form-label">Attached Bill</label>
            <a href="<?= h($claim['bill_image']) ?>" target="_blank">
              <img src="<?= h($claim['bill_image']) ?>" style="max-width:280px;border-radius:10px;border:1px solid var(--border)">
            </a>
          </div>
        <?php endif; ?>

        <?php if ($claim['status'] !== 'pending'): ?>
          <div class="remarks-area">
            <strong><?= $claim['status'] === 'approved' ? 'Approved' : 'Rejected' ?></strong>
            by <?= h($claim['reviewed_by_name'] ?? 'reviewer') ?>
            <?php if (!empty($claim['reviewed_at'])): ?> on <?= h(date('d M Y, H:i', strtotime($claim['reviewed_at']))) ?><?php endif; ?>
            <?php if ($claim['status'] === 'rejected' && !empty($claim['rejection_reason'])): ?>
              <br>Reason: <?= h($claim['rejection_reason']) ?>
            <?php elseif (!empty($claim['manager_remarks'])): ?>
              <br>Remarks: <?= h($claim['manager_remarks']) ?>
            <?php endif; ?>
          </div>
        <?php endif; ?>

        <?php if ($claim['status'] === 'pending'): ?>
          <a href="expense_claim_edit.php?id=<?= (int)$claim['id'] ?>" class="btn-action btn-remarks" style="margin-top:14px">
            <i class="fas fa-pen" style="font-size:10px"></i> Edit Claim
          </a>
        <?php endif; ?>
      </div>
    </div>
  </div>
<?php expense_render_footer_scripts(); ?>