<?php
if (!defined('ABSPATH')) {
    exit;
}
$chama_view_nonce          = wp_create_nonce('chama_view_transactions_nonce');
$templates = new Chama_Template_Loader;
// phpcs:ignore WordPress.Security.NonceVerification.Recommended
$tab = isset($_GET['tab']) ? sanitize_text_field( wp_unslash($_GET['tab'])) : 'profile';
$title = 'Profile';
$icon = 'far fa-user-circle';
$account_url = CHAMA_ACCOUNT_URL;
$display_donate_page = get_option('chama_display_donate');
$display_membership_page = get_option('chama_display_membership');
$display_crowdfunding_page = get_option('chama_display_crowdfunding');
$display_commissions_page = get_option('chama_display_commissions');
$has_subscription = false;
$user = wp_get_current_user();
$user_id = $user->ID;
if($user_id){
    $has_subscription = get_user_meta($user_id, 'chama_stripe_subscription_id', true);
}

switch ($tab) {
    case 'profile':
        $title = 'Profile';
        $icon = 'far fa-user-circle';
        break;
    case 'donations':
        $title = 'Donations';
        $icon = 'fa-solid fa-hand-holding-heart';
        break;
    case 'subscriptions':
        $title = 'Subscriptions';
        $icon = 'far fa-address-card';
        break;
    case 'crowdfunding':
        $title = 'Crowdfunding';
        $icon = 'fa-solid fa-people-group';
        break;
    case 'commissions':
        $title = 'Commissions';
        $icon = 'fa-solid fa-hand-holding-dollar';
        break;
    case 'billing':
        $title = 'Billing';
        $icon = 'fa-regular fa-credit-card';
        break;
    default:
        $title = 'Profile';
        $icon = 'far fa-user-circle';
        break;
}
?>
<div class="chama-page-wrapper">
    <div id="chama-page-header">
    <i class="<?php echo esc_attr($icon); ?> fa-2x"></i>

        <h3><?php echo esc_html($title); ?></h3>

</div>
<div class="chama-account-nav-wrapper">
    <ul id="chama-header-nav">
<li class="chama-nav-item                                                   <?php echo $tab == 'profile' ? 'active' : ''; ?>">
    <a href="<?php echo esc_url($account_url); ?>" class="chama-nav-link">Profile</a>
</li>

<?php if ($display_donate_page): ?>
    <li class="chama-nav-item<?php echo $tab == 'donations' ? 'active' : ''; ?>">
        <a href="<?php echo esc_url(add_query_arg(['tab' => 'donations', 'cfn' => $chama_view_nonce], $account_url)); ?>" class="chama-nav-link">Donations</a>
    </li>
<?php endif; ?>

<?php if ($display_membership_page): ?>
    <li class="chama-nav-item<?php echo $tab == 'subscriptions' ? 'active' : ''; ?>">
        <a href="<?php echo esc_url(add_query_arg(['tab' => 'subscriptions', 'cfn' => $chama_view_nonce], $account_url)); ?>" class="chama-nav-link">Subscriptions</a>
    </li>
<?php endif; ?>

<?php if ($display_crowdfunding_page): ?>
    <li class="chama-nav-item<?php echo $tab == 'crowdfunding' ? 'active' : ''; ?>">
        <a href="<?php echo esc_url(add_query_arg(['tab' => 'crowdfunding', 'cfn' => $chama_view_nonce], $account_url)); ?>" class="chama-nav-link">Crowdfunding</a>
    </li>
<?php endif; ?>

<?php if ($display_commissions_page): ?>
    <li class="chama-nav-item<?php echo $tab == 'commissions' ? 'active' : ''; ?>">
        <a href="<?php echo esc_url(add_query_arg(['tab' => 'commissions', 'cfn' => $chama_view_nonce], $account_url)); ?>" class="chama-nav-link">Commissions</a>
    </li>
<?php endif; ?>

<?php if ($has_subscription): ?>
    <li class="chama-nav-item<?php echo $tab == 'billing' ? 'active' : ''; ?>">
        <a href="<?php echo esc_url(add_query_arg(['tab' => 'billing', 'cfn' => $chama_view_nonce], $account_url)); ?>" class="chama-nav-link">Billing</a>
    </li>
<?php endif; ?>
</ul>
</div>
<?php
 set_query_var('user_id', $user_id);
switch ($tab) {
    case 'profile':
        $templates->get_template_part('content', 'profile');
        break;
    case 'donations':
    case 'subscriptions':
    case 'crowdfunding':
    case 'commissions':
        $templates->get_template_part('content', 'accounttransactions');
        break;
    case 'billing':
        $templates->get_template_part('content', 'accountbilling');
        break;
    default:
        $templates->get_template_part('content', 'profile');
        break;
}
?>
</div>

<?php
