<?php

require_once __DIR__ . '/includes/settings_helpers.php';

settings_require_auth();
settings_ensure_tables($conn);

$org = settings_get_current_org($conn);
$orgId = (int) ($org['id'] ?? 0);

if ($_SERVER['REQUEST_METHOD'] === 'POST') {
    $appCode = trim((string) ($_POST['app_code'] ?? ''));
    $appName = trim((string) ($_POST['app_name'] ?? ''));
    $status = ($_POST['integration_action'] ?? '') === 'disconnect' ? 'inactive' : 'active';
    if ($appCode !== '' && $appName !== '') {
        settings_set_zoho_app_status($conn, $orgId, $appCode, $appName, $status);
        if ($appCode === 'zoho_books') {
            settings_set_setup_step_state($conn, $orgId, 'zoho_books', $status === 'active');
        }
        settings_set_flash('success', $appName . ($status === 'active' ? ' connected successfully.' : ' disconnected.'));
        if ($appCode === 'zoho_books' && $status === 'active') {
            header('Location: organc.php?setup=complete&step=8');
        } else {
            header('Location: zoho_apps.php');
        }
        exit();
    }
}

$apps = settings_get_zoho_app_integrations($conn, $orgId);
$flash = settings_flash_message();
?>
<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>Zoho Apps | Settings</title>
  <link rel="stylesheet" href="assets/fontawesome/css/all.min.css">
  <link rel="stylesheet" href="assets/font-inter.css">
  <link rel="stylesheet" href="orgprofile.css">
  <style>
    .integration-list{display:grid;gap:18px;}
    .integration-card{display:grid;grid-template-columns:120px 1fr auto;gap:20px;align-items:center;padding:22px;border:1px solid var(--border);border-radius:18px;background:#fff;}
    .integration-logo{width:92px;height:92px;border-radius:22px;background:#f7f9ff;display:flex;align-items:center;justify-content:center;font-size:32px;color:#2563eb;}
    .integration-card h3{margin:0 0 8px;font-size:22px;}
    .integration-card p{margin:0;color:#475569;line-height:1.6;}
    .state-badge{display:inline-block;margin-left:10px;padding:4px 9px;border-radius:999px;background:#16a34a;color:#fff;font-size:12px;font-weight:700;vertical-align:middle;}
    .integration-actions{display:flex;gap:10px;align-items:center;}
    .btn-soft{padding:10px 16px;border-radius:10px;border:1px solid var(--border);background:#fff;color:#334155;font-weight:700;text-decoration:none;}
    .btn-main{padding:10px 16px;border-radius:10px;border:none;background:#3b82f6;color:#fff;font-weight:700;}
    @media (max-width: 960px){
      .integration-card{grid-template-columns:1fr;}
      .integration-actions{justify-content:flex-start;}
    }
  </style>
</head>
<body class="orgprofile-page">
  <?php settings_render_header($_SESSION['org_name'] ?? 'demo', 'dashboard.php', 'fa-link'); ?>

  <main class="profile-layout">
    <?php settings_render_sidebar('zoho_apps'); ?>

    <section class="profile-main">
      <div class="profile-head">
        <h1>Integrations - Zoho Apps</h1>
      </div>

      <div class="profile-card">
        <?php if (!empty($flash['message'])): ?>
          <div class="alert-ok"><?php echo htmlspecialchars($flash['message']); ?></div>
        <?php endif; ?>

        <div class="integration-list">
          <?php foreach ($apps as $app): ?>
            <div class="integration-card">
              <div class="integration-logo"><i class="fa-solid fa-cubes"></i></div>
              <div>
                <h3>
                  <?php echo htmlspecialchars($app['name']); ?>
                  <?php if (($app['status'] ?? 'inactive') === 'active'): ?>
                    <span class="state-badge">ACTIVE</span>
                  <?php endif; ?>
                </h3>
                <p><?php echo htmlspecialchars($app['description']); ?></p>
              </div>
              <div class="integration-actions">
                <?php if (($app['status'] ?? 'inactive') === 'active'): ?>
                  <form method="POST" style="display:flex;gap:10px;">
                    <input type="hidden" name="app_code" value="<?php echo htmlspecialchars($app['code']); ?>">
                    <input type="hidden" name="app_name" value="<?php echo htmlspecialchars($app['name']); ?>">
                    <button class="btn-soft" type="submit" name="integration_action" value="disconnect">Disconnect</button>
                    <a class="btn-main" href="reports.php">Access</a>
                  </form>
                <?php else: ?>
                  <form method="POST">
                    <input type="hidden" name="app_code" value="<?php echo htmlspecialchars($app['code']); ?>">
                    <input type="hidden" name="app_name" value="<?php echo htmlspecialchars($app['name']); ?>">
                    <button class="btn-main" type="submit" name="integration_action" value="connect">Connect</button>
                  </form>
                <?php endif; ?>
              </div>
            </div>
          <?php endforeach; ?>
        </div>
      </div>
    </section>
  </main>
</body>
</html>
