<?php
require_once __DIR__ . '/../../app/core/bootstrap.php';
require_once __DIR__ . '/../../app/core/project_permissions.php';
require_once __DIR__ . '/../../app/core/i18n.php';
auth_require_login();
$page_title = 'Projects';
$pdo = crm_pdo();

$role = gp_user_role($pdo);

if (gp_is_full_project_access_role($role)) {
    $sql = "SELECT p.id, p.code, p.title, p.status, c.name AS customer_name, p.start_date, p.end_date_planned
            FROM projects p
            LEFT JOIN customers c ON c.id = p.customer_id
            ORDER BY p.id DESC";
    $rows = $pdo->query($sql)->fetchAll();
} elseif (gp_is_secretary_like_role($role)) {
    $stmt = $pdo->prepare("
        SELECT p.id, p.code, p.title, p.status, c.name AS customer_name, p.start_date, p.end_date_planned
        FROM project_edit_permissions pep
        JOIN projects p ON p.id = pep.project_id
        LEFT JOIN customers c ON c.id = p.customer_id
        WHERE pep.user_id = :uid AND pep.can_edit = 1
        ORDER BY p.id DESC
    ");
    $stmt->execute([':uid' => gp_current_user_id()]);
    $rows = $stmt->fetchAll();
} else {
    $rows = [];
}

include __DIR__ . '/../includes/header.php';
?>
<style>
.projects-card-modern{padding:0;overflow:hidden}
.projects-toolbar{display:flex;justify-content:space-between;align-items:center;gap:12px;padding:18px 18px 10px 18px}
.projects-toolbar-left{display:flex;flex-direction:column;gap:4px}
.projects-toolbar-left h2{margin:0;font-size:1.02rem}
.projects-toolbar-left span{opacity:.72;font-size:.92rem}
.projects-table-wrap{padding:0 12px 14px 12px}
.projects-table-modern{width:100%;border-collapse:separate;border-spacing:0 10px;table-layout:fixed}
.projects-table-modern thead th{font-size:.84rem;font-weight:700;opacity:.72;text-transform:uppercase;letter-spacing:.02em;padding:0 10px 6px 10px;text-align:left;vertical-align:bottom}
.projects-table-modern tbody tr{background:rgba(255,255,255,.55)}
.projects-table-modern tbody td{padding:14px 10px;vertical-align:top;border-top:1px solid rgba(15,23,42,.06);border-bottom:1px solid rgba(15,23,42,.06)}
.projects-table-modern tbody td:first-child{border-left:1px solid rgba(15,23,42,.06);border-top-left-radius:14px;border-bottom-left-radius:14px}
.projects-table-modern tbody td:last-child{border-right:1px solid rgba(15,23,42,.06);border-top-right-radius:14px;border-bottom-right-radius:14px}
.project-id-col{width:64px}
.project-main-col{width:25%}
.project-customer-col{width:18%}
.project-status-col{width:14%}
.project-date-col{width:110px}
.project-actions-col{width:240px}
.project-title-strong{display:block;font-weight:700;font-size:.98rem;line-height:1.25;word-break:break-word;white-space:normal}
.project-code-sub{display:block;margin-top:4px;font-size:.82rem;opacity:.68;word-break:break-word}
.project-customer-cell{word-break:break-word;white-space:normal}
.project-date-cell{white-space:nowrap}
.project-actions-row{display:flex;flex-wrap:wrap;gap:8px;align-items:flex-start}
.project-actions-row .btn{white-space:nowrap}
.projects-empty{padding:22px 18px}
@media (max-width: 980px){
  .projects-table-modern,
  .projects-table-modern thead,
  .projects-table-modern tbody,
  .projects-table-modern th,
  .projects-table-modern td,
  .projects-table-modern tr{display:block;width:100%}
  .projects-table-modern thead{display:none}
  .projects-table-modern tbody tr{margin-bottom:12px;border-radius:14px}
  .projects-table-modern tbody td{border-left:1px solid rgba(15,23,42,.06)!important;border-right:1px solid rgba(15,23,42,.06)!important;border-radius:0!important}
  .projects-table-modern tbody td:first-child{border-top-left-radius:14px!important;border-top-right-radius:14px!important}
  .projects-table-modern tbody td:last-child{border-bottom-left-radius:14px!important;border-bottom-right-radius:14px!important}
}
</style>

<div class="page-header">
  <div class="page-header-title">
    <h1>Projects</h1>
    <span>Project center</span>
  </div>
  <div class="page-header-actions">
    <?php if (gp_can_create_project($pdo)): ?>
      <a class="btn btn-primary btn-sm" href="edit.php">+ New</a>
    <?php endif; ?>
  </div>
</div>

<div class="card projects-card-modern">
  <div class="projects-toolbar">
    <div class="projects-toolbar-left">
      <h2>All projects</h2>
      <span>Cleaner layout, wrapped titles, no horizontal scroll</span>
    </div>
  </div>

  <?php if (!$rows): ?>
    <div class="projects-empty">
      <p class="small">No accessible projects found.</p>
    </div>
  <?php else: ?>
    <div class="projects-table-wrap">
      <table class="projects-table-modern">
        <thead>
          <tr>
            <th class="project-id-col">ID</th>
            <th class="project-main-col">Project</th>
            <th class="project-customer-col">Customer</th>
            <th class="project-status-col">Status</th>
            <th class="project-date-col">Start</th>
            <th class="project-date-col">Planned end</th>
            <th class="project-actions-col">Actions</th>
          </tr>
        </thead>
        <tbody>
        <?php foreach ($rows as $row): ?>
          <tr>
            <td>#<?php echo (int)$row['id']; ?></td>
            <td>
              <span class="project-title-strong"><?php echo h($row['title']); ?></span>
              <span class="project-code-sub"><?php echo h($row['code']); ?></span>
            </td>
            <td class="project-customer-cell"><?php echo h($row['customer_name']); ?></td>
            <td><span class="badge"><?php echo h(gp_project_status_label($pdo, (string)$row['status'])); ?></span></td>
            <td class="project-date-cell"><?php echo h($row['start_date']); ?></td>
            <td class="project-date-cell"><?php echo h($row['end_date_planned']); ?></td>
            <td>
              <div class="project-actions-row">
                <a class="btn btn-sm btn-outline-light" href="edit.php?id=<?php echo (int)$row['id']; ?>">Open</a>
                <a class="btn btn-sm btn-outline-light" href="equipment.php?project_id=<?php echo (int)$row['id']; ?>">Equipment</a>
                <a class="btn btn-sm btn-outline-light" href="payments.php?project_id=<?php echo (int)$row['id']; ?>">Payments</a>
                <a class="btn btn-sm btn-outline-light" href="dispatch.php?project_id=<?php echo (int)$row['id']; ?>">Dispatch</a>
                <?php if (gp_can_manage_project_access($pdo)): ?>
                  <a class="btn btn-sm btn-outline-light" href="access.php?project_id=<?php echo (int)$row['id']; ?>">Access</a>
                <?php endif; ?>
              </div>
            </td>
          </tr>
        <?php endforeach; ?>
        </tbody>
      </table>
    </div>
  <?php endif; ?>
</div>

<?php include __DIR__ . '/../includes/footer.php'; ?>
