<?php
include("./includes/common.php");

if(!$conf['userlogin']){
    @header('Content-Type: text/html; charset=UTF-8');
	exit("<script language='javascript'>alert('未开启用户系统');window.location.href='./';</script>");
}

if(!$islogin2){
    @header('Content-Type: text/html; charset=UTF-8');
	exit("<script language='javascript'>alert('请先登录');window.location.href='login.php';</script>");
}

$csrf_token = md5(mt_rand(0,999).time());
$_SESSION['csrf_token'] = $csrf_token;

$title = '卡密兑换 - ' . $conf['title'];

include SYSTEM_ROOT.'header.php';
?>
<style>
.redeem-page {
    max-width: 500px;
    margin: 50px auto;
    padding: 20px;
}
.redeem-card {
    background: #fff;
    border-radius: 20px;
    box-shadow: 0 10px 40px rgba(0,0,0,0.08);
    overflow: hidden;
}
.redeem-card-header {
    background: linear-gradient(135deg, #0f766e 0%, #14b8a6 100%);
    padding: 40px 30px;
    text-align: center;
    color: #fff;
    position: relative;
}
.redeem-card-header::before {
    content: '';
    position: absolute;
    right: -30px;
    top: -30px;
    width: 120px;
    height: 120px;
    background: rgba(255,255,255,0.1);
    border-radius: 50%;
}
.redeem-card-header::after {
    content: '';
    position: absolute;
    left: -20px;
    bottom: -20px;
    width: 80px;
    height: 80px;
    background: rgba(255,255,255,0.08);
    border-radius: 50%;
}
.redeem-card-header h1 {
    font-size: 24px;
    font-weight: 700;
    margin: 0 0 10px;
    position: relative;
    z-index: 1;
}
.redeem-card-header p {
    font-size: 14px;
    opacity: 0.9;
    margin: 0;
    position: relative;
    z-index: 1;
}
.redeem-card-header .gift-icon {
    font-size: 48px;
    margin-bottom: 15px;
    display: block;
    position: relative;
    z-index: 1;
}
.redeem-card-body {
    padding: 30px;
}
.redeem-form {
    display: flex;
    gap: 10px;
    margin-bottom: 15px;
}
.redeem-form input {
    flex: 1;
    padding: 14px 18px;
    border: 2px solid #e8e8e8;
    border-radius: 12px;
    font-size: 15px;
    transition: all 0.3s;
    background: #fafafa;
}
.redeem-form input:focus {
    border-color: #14b8a6;
    outline: none;
    background: #fff;
    box-shadow: 0 0 0 4px rgba(20,184,166,0.1);
}
.redeem-form button {
    padding: 14px 28px;
    background: linear-gradient(135deg, #0f766e 0%, #14b8a6 100%);
    border: none;
    border-radius: 12px;
    color: #fff;
    font-size: 15px;
    font-weight: 600;
    cursor: pointer;
    transition: all 0.3s;
    white-space: nowrap;
}
.redeem-form button:hover {
    transform: translateY(-2px);
    box-shadow: 0 6px 20px rgba(20,184,166,0.4);
}
.redeem-form button:disabled {
    opacity: 0.7;
    cursor: not-allowed;
    transform: none;
}
.redeem-hint {
    color: #888;
    font-size: 13px;
    display: flex;
    align-items: center;
    gap: 8px;
    padding: 12px 15px;
    background: #f8f9fa;
    border-radius: 10px;
    margin: 0;
}
.redeem-hint i {
    color: #14b8a6;
}
.redeem-history {
    margin-top: 25px;
    padding-top: 25px;
    border-top: 1px solid #f0f0f0;
}
.redeem-history h3 {
    font-size: 16px;
    font-weight: 600;
    color: #333;
    margin: 0 0 15px;
    display: flex;
    align-items: center;
    gap: 8px;
}
.redeem-history h3 i {
    color: #14b8a6;
}
.redeem-list {
    list-style: none;
    padding: 0;
    margin: 0;
}
.redeem-list li {
    display: flex;
    justify-content: space-between;
    align-items: center;
    padding: 12px 15px;
    background: #f8f9fa;
    border-radius: 10px;
    margin-bottom: 8px;
    font-size: 14px;
}
.redeem-list li:last-child {
    margin-bottom: 0;
}
.redeem-list .card-code {
    color: #333;
    font-family: monospace;
    background: #fff;
    padding: 4px 10px;
    border-radius: 6px;
    font-size: 13px;
}
.redeem-list .card-time {
    color: #888;
    font-size: 12px;
}
.redeem-empty {
    text-align: center;
    padding: 30px 20px;
    color: #999;
}
.redeem-empty i {
    font-size: 40px;
    color: #e0e0e0;
    display: block;
    margin-bottom: 12px;
}
.redeem-empty p {
    font-size: 14px;
    margin: 0;
}
.redeem-tips {
    margin-top: 20px;
    padding: 15px;
    background: #fff7ed;
    border-radius: 10px;
    border: 1px solid #fed7aa;
}
.redeem-tips h4 {
    font-size: 14px;
    font-weight: 600;
    color: #c2410c;
    margin: 0 0 10px;
    display: flex;
    align-items: center;
    gap: 6px;
}
.redeem-tips ul {
    margin: 0;
    padding-left: 20px;
    font-size: 13px;
    color: #9a3412;
}
.redeem-tips li {
    margin-bottom: 5px;
}
.redeem-tips li:last-child {
    margin-bottom: 0;
}

@media (max-width: 768px) {
    .redeem-page {
        margin: 20px auto;
        padding: 15px;
    }
    .redeem-card-header {
        padding: 30px 20px;
    }
    .redeem-card-header h1 {
        font-size: 20px;
    }
    .redeem-card-header .gift-icon {
        font-size: 40px;
    }
    .redeem-card-body {
        padding: 20px;
    }
    .redeem-form {
        flex-direction: column;
    }
    .redeem-form button {
        width: 100%;
    }
}
</style>

<div class="redeem-page">
    <div class="redeem-card">
        <div class="redeem-card-header">
            <i class="fa fa-gift gift-icon"></i>
            <h1>卡密兑换</h1>
            <p>输入卡密代码，兑换存储空间和流量</p>
        </div>
        <div class="redeem-card-body">
            <form id="redeemForm" method="post">
                <input type="hidden" name="action" value="redeem_card">
                <input type="hidden" name="csrf_token" value="<?php echo $csrf_token; ?>">
                <div class="redeem-form">
                    <input type="text" name="card_code" placeholder="请输入卡密代码" required autocomplete="off">
                    <button type="submit" id="submitBtn"><i class="fa fa-check"></i> 立即兑换</button>
                </div>
                <p class="redeem-hint"><i class="fa fa-info-circle"></i> 兑换成功后，存储空间和流量配额会立即增加。</p>
            </form>

            <?php
            $redeem_list = $DB->query("SELECT card_code, addtime FROM pre_card_redeem WHERE uid='$uid' ORDER BY id DESC LIMIT 10");
            if($redeem_list && $redeem_list->rowCount() > 0):
            ?>
            <div class="redeem-history">
                <h3><i class="fa fa-history"></i> 兑换记录</h3>
                <ul class="redeem-list">
                    <?php while($row = $redeem_list->fetch(PDO::FETCH_ASSOC)): ?>
                    <li>
                        <span class="card-code"><?php echo htmlspecialchars($row['card_code']); ?></span>
                        <span class="card-time"><?php echo $row['addtime']; ?></span>
                    </li>
                    <?php endwhile; ?>
                </ul>
            </div>
            <?php else: ?>
            <div class="redeem-history">
                <h3><i class="fa fa-history"></i> 兑换记录</h3>
                <div class="redeem-empty">
                    <i class="fa fa-inbox"></i>
                    <p>暂无兑换记录</p>
                </div>
            </div>
            <?php endif; ?>

            <div class="redeem-tips">
                <h4><i class="fa fa-lightbulb-o"></i> 使用说明</h4>
                <ul>
                    <li>卡密可通过购买或参与活动获取</li>
                    <li>每个卡密只能兑换一次</li>
                    <li>兑换成功后存储和流量将立即到账</li>
                    <li>如有问题请联系管理员</li>
                </ul>
            </div>
        </div>
    </div>
</div>

<script src="https://s4.zstatic.net/ajax/libs/layer/3.1.1/layer.min.js"></script>
<script>
$(function(){
    $('#redeemForm').on('submit', function(e){
        e.preventDefault();
        var submitBtn = $('#submitBtn');
        var originalText = submitBtn.html();
        submitBtn.prop('disabled', true).html('<i class="fa fa-spinner fa-spin"></i> 兑换中...');
        
        $.ajax({
            type: 'POST',
            url: 'user_center.php',
            data: $(this).serialize(),
            dataType: 'json',
            success: function(response) {
                if(response.code === 0) {
                    layer.msg(response.msg, {icon: 1});
                    $('input[name="csrf_token"]').val(response.new_csrf || '');
                    setTimeout(function(){ location.reload(); }, 1500);
                } else {
                    layer.msg(response.msg, {icon: 2});
                    submitBtn.prop('disabled', false).html(originalText);
                }
            },
            error: function() {
                layer.msg('网络错误，请稍后重试', {icon: 2});
                submitBtn.prop('disabled', false).html(originalText);
            }
        });
    });
});
</script>
<?php include SYSTEM_ROOT.'footer.php';?>
</body>
</html>
