<?php
// =============================================
// SHARK CRM - Settings
// =============================================

$account = $conn->query("SELECT * FROM accounts WHERE id = $account_id")->fetch_assoc();
$lost_reasons = $conn->query("SELECT * FROM lost_reason WHERE account_id = $account_id ORDER BY date_created DESC");
$all_users = $conn->query("SELECT u.*, r.name as role_name FROM users u LEFT JOIN roles r ON u.role_id = r.id WHERE u.account_id = $account_id ORDER BY u.datetime_created DESC");
$roles = $conn->query("SELECT * FROM roles WHERE account_id = $account_id");
$stages = $conn->query("SELECT * FROM stage WHERE account_id = $account_id ORDER BY position ASC");

// Get configuration settings (create table if needed)
// $config = $conn->query("SELECT * FROM account_config WHERE account_id = $account_id")->fetch_assoc();
// if (!$config) {
//     // Create default config if not exists
//     $conn->query("INSERT INTO account_config (account_id, whatsapp_number, sms_number, email_from) VALUES ($account_id, '', '', '')");
//     $config = ['whatsapp_number' => '', 'sms_number' => '', 'email_from' => ''];
// }

// Check if current user is admin
$current_user = $conn->query("SELECT role_id FROM users WHERE id = $user_id")->fetch_assoc();
$is_admin = $current_user && $current_user['role_id'] == 1; // Assuming role_id 1 is admin

// All countries
$all_countries = $conn->query("
    SELECT *
    FROM countries
    ORDER BY name ASC
");

// Account countries
$account_countries = [];

$res = $conn->query("
    SELECT country_id, is_default
    FROM country_account
    WHERE account_id = $account_id
");

while ($row = $res->fetch_assoc()) {
    $account_countries[$row['country_id']] = $row['is_default'];
}




// include 'whatsapp_connect_tab.php'; 
$wa_connected = $conn->query("
    SELECT client_id, phone_number
    FROM contact_phone
    WHERE account_id = $account_id
    LIMIT 1
")->fetch_assoc();


?>

<div class="fade-in">
<div class="tabs">
    
  <button class="tab-btn <?= (!isset($_GET['tab']) || $_GET['tab'] == 'account') ? 'active' : '' ?>" data-tab="tab-account" onclick="switchTab('account')">Account</button>
  <button class="tab-btn <?= (isset($_GET['tab']) && $_GET['tab'] == 'users') ? 'active' : '' ?>" data-tab="tab-users" onclick="switchTab('users')">Users</button>
  <button class="tab-btn <?= (isset($_GET['tab']) && $_GET['tab'] == 'lost') ? 'active' : '' ?>" data-tab="tab-lost" onclick="switchTab('lost')">Lost Reasons</button>
  <button class="tab-btn <?= (isset($_GET['tab']) && $_GET['tab'] == 'stages') ? 'active' : '' ?>" data-tab="tab-stages" onclick="switchTab('stages')">Stages</button>
  
  
  <button class="tab-btn <?= (isset($_GET['tab']) && $_GET['tab'] == 'whatsapp') ? 'active' : '' ?>"
        data-tab="tab-whatsapp"
        onclick="switchTab('whatsapp')">
  💬 WhatsApp
</button>

<button class="tab-btn <?= (isset($_GET['tab']) && $_GET['tab'] == 'email') ? 'active' : '' ?>"
        data-tab="tab-email"
        onclick="switchTab('email')">
  📧 Emails
</button>

<button class="tab-btn <?= (isset($_GET['tab']) && $_GET['tab'] == 'countries') ? 'active' : '' ?>"
        data-tab="tab-countries"
        onclick="switchTab('countries')">
  🌍 Countries
</button>


  <button class="tab-btn <?= (isset($_GET['tab']) && $_GET['tab'] == 'password') ? 'active' : '' ?>" data-tab="tab-password" onclick="switchTab('password')">Password</button>
  <button class="tab-btn <?= (isset($_GET['tab']) && $_GET['tab'] == 'config') ? 'active' : '' ?>" data-tab="tab-config" onclick="switchTab('config')" style="display:none">⚙️ Configuration</button>
</div>

<?php

$settingsTabsPath = __DIR__ . '/settings_tab';
include $settingsTabsPath . '/account_tab.php';
include $settingsTabsPath . '/users_tab.php';
include $settingsTabsPath . '/lost_reasons_tab.php';
include $settingsTabsPath . '/stages_tab.php';
include $settingsTabsPath . '/config_tab.php';
include $settingsTabsPath . '/whatsapp_tab.php';
include $settingsTabsPath . '/countries_tab.php';
include $settingsTabsPath . '/password_tab.php';
include  './pages/settings_tab/email_tab.php';


?>

</div>

<?php
include $settingsTabsPath . '/modals.php';
include $settingsTabsPath . '/settings_scripts.php';
?>
