<?php
/*
 * 钱包页面
 */

$user_id = get_current_user_id();
$user_cash = floatval(get_user_meta($user_id, 'ppo_balance', true));
$user_credit = intval(get_user_meta($user_id, 'ppo_credit', true));

$cash_icon = PPO_Down::pay_icon('cash')['icon'];
$credit_icon = PPO_Down::pay_icon('credit')['icon'];
$cash_name = PPO_Down::pay_icon('cash')['name'];
$credit_name = PPO_Down::pay_icon('credit')['name'];

$tab = isset($_GET['tab']) ? sanitize_text_field($_GET['tab']) : '';
$page = isset($_GET['page']) ? max(1, intval($_GET['page'])) : 1;
$is_balance_tab = $tab === 'balance';
$is_credit_tab = $tab === 'credit';

$credit_htmx_base = '/wp-json/ppo/v1/user-credit-records?_wpnonce=' . wp_create_nonce('wp_rest');
$balance_htmx_base = '/wp-json/ppo/v1/user-balance-records?_wpnonce=' . wp_create_nonce('wp_rest');

$credit_htmx = 'href="' . $credit_htmx_base . '&page=1" '
    . 'hx-get="' . $credit_htmx_base . '&page=1" '
    . 'hx-target=".wallet_record_list" '
    . 'hx-swap="innerHTML" '
    . 'hx-push-url="' . home_url('/dashboard/wallet?tab=credit') . '" '
    . 'data-skeleton="normal-list" '
    . 'hx-history="false"';

$balance_htmx = 'href="' . $balance_htmx_base . '&page=1" '
    . 'hx-get="' . $balance_htmx_base . '&page=1" '
    . 'hx-target=".wallet_record_list" '
    . 'hx-swap="innerHTML" '
    . 'hx-push-url="' . home_url('/dashboard/wallet?tab=balance') . '" '
    . 'data-skeleton="normal-list" '
    . 'hx-history="false"';

?>
<div class="dash-wallet user-dash-box">
    <div class="uk-child-width-1-2@s uk-grid-small" uk-grid>
        <div>
            <div class="cash-box wallet-box">
                <div class="top-name wallet-top-name">
                    <span><?php echo $cash_name; ?></span>
                    <a class="card-redeem-cash-btn wallet-card-btn"><i class="ri-coupon-3-line"></i> 卡密充值</a>
                </div>
                <div class="cash-center wallet-center">
                    <div class="cash-value"><span class="cash-icon"><?php echo $cash_icon; ?></span><?php echo $user_cash; ?></div>
                    <div class="charge-cash"><a class="transfer-cash-btn">转账</a><a class="charge-cash-btn">充值</a></div>
                </div>
            </div>
        </div>

        <div>
            <div class="credit-box wallet-box">
                <div class="top-name wallet-top-name">
                    <span><?php echo $credit_name; ?></span>
                    <span class="wallet-top-right">
                        <a class="card-redeem-credit-btn wallet-card-btn"><i class="ri-coupon-3-line"></i> 卡密充值</a>
                        <span class="wallet-signin-wrap">
                        <?php if (function_exists('check_daily_checked') && check_daily_checked($user_id)): ?>
                        <a class="user-has-sign-btn wallet-signed">今日已签到</a>
                        <?php else: ?>
                        <a class="user-sign-btn wallet-sign-btn">签到领积分<i class="ri-arrow-right-s-line"></i></a>
                        <?php endif; ?>
                        </span>
                    </span>
                </div>
                <div class="credit-center wallet-center">
                    <div class="credit-value"><span class="credit-icon"><?php echo $credit_icon; ?></span><?php echo $user_credit; ?></div>
                    <div class="charge-credit"><a class="transfer-credit-btn">转账</a><a class="charge-credit-btn">购买<?php echo $credit_name; ?></a></div>
                </div>
            </div>
        </div>

    </div>

    <div id="charge-modal-here"><?php echo charge_modal(); ?></div>

    <div id="card-redeem-modal" class="uk-flex-top" uk-modal="esc: false;bg-close: false;">
        <div class="uk-modal-dialog uk-modal-body uk-margin-auto-vertical uk-animation-scale-up uk-animation-fast" style="width:420px;">
            <button class="uk-modal-close-default" type="button" uk-close></button>
            <div class="card-redeem-inner">
                <div class="card-redeem-title">
                    <i class="ri-coupon-3-line"></i> 卡密充值
                </div>
                <div class="redeem-input-group" style="display:flex;gap:10px;margin-bottom:10px;">
                    <input type="text" id="wallet-card-code" class="uk-input" placeholder="请输入16位卡密" autocomplete="off">
                    <a class="uk-button uk-button-primary" id="btn-wallet-redeem">兑换</a>
                </div>
                <div class="redeem-result" style="min-height:30px;"></div>
                <div class="redeem-tips" style="color:#999;font-size:13px;margin-top:8px;">
                    <i class="ri-information-line"></i> 支持余额充值卡和积分充值卡
                </div>
            </div>
        </div>
    </div>

    <div class="user_wallet_info">
        <div class="wallet-tab dashboard-tab big-rounded">
            <a <?php echo $balance_htmx; ?> class="tab-item <?php echo $is_balance_tab || (!$is_balance_tab && !$is_credit_tab) ? 'active' : ''; ?>">余额记录</a>
            <a <?php echo $credit_htmx; ?> class="tab-item <?php echo $is_credit_tab ? 'active' : ''; ?>">积分记录</a>
        </div>
        <div class="wallet_record_list">
            <?php
            if ($is_credit_tab) {
                echo user_credit_get_info($page);
            } else {
                echo user_balance_get_info($page);
            }
            ?>
        </div>
    </div>
</div>
