<?php
/**
 * HuSNS - 清理缓存页面
 */
?>
<div class="admin-content">
    <div class="admin-header">
        <h1>清理缓存</h1>
    </div>
    
    <div class="panel">
        <div class="panel-header">
            <h3>缓存统计</h3>
        </div>
        <div class="panel-body">
            <div class="stats-cards">
                <div class="stat-card">
                    <div class="stat-icon bg-blue">
                        <i class="fa fa-database"></i>
                    </div>
                    <div class="stat-info">
                        <div class="stat-value"><?php echo $cacheSize; ?></div>
                        <div class="stat-label">缓存大小</div>
                    </div>
                </div>
            </div>
        </div>
    </div>
    
    <div class="panel">
        <div class="panel-header">
            <h3>清理选项</h3>
        </div>
        <div class="panel-body">
            <div class="cache-options">
                <div class="cache-option">
                    <label class="checkbox">
                        <input type="checkbox" id="clearTemp" checked>
                        <span>临时文件</span>
                    </label>
                    <p class="option-desc">清理缓存目录中的临时文件</p>
                </div>
                
                <div class="cache-option">
                    <label class="checkbox">
                        <input type="checkbox" id="clearCache" checked>
                        <span>缓存数据</span>
                    </label>
                    <p class="option-desc">清理系统缓存数据</p>
                </div>
                
                <div class="cache-option">
                    <label class="checkbox">
                        <input type="checkbox" id="clearThumbnails" checked>
                        <span>缩略图缓存</span>
                    </label>
                    <p class="option-desc">清理生成的图片缩略图</p>
                </div>
                
                <div class="cache-option">
                    <label class="checkbox">
                        <input type="checkbox" id="clearSetting" checked>
                        <span>设置缓存</span>
                    </label>
                    <p class="option-desc">清理设置项的内存缓存</p>
                </div>
                
                <div class="cache-option">
                    <label class="checkbox">
                        <input type="checkbox" id="clearTemplate" checked>
                        <span>模板缓存</span>
                    </label>
                    <p class="option-desc">清理编译后的模板文件</p>
                </div>
            </div>
            
            <button id="clearCacheBtn" class="btn btn-primary btn-lg" style="margin-top: 20px;">
                <i class="fa fa-trash"></i> 开始清理
            </button>
            
            <div id="clearResult" style="display: none; margin-top: 20px;">
                <div class="alert alert-success" id="successMsg"></div>
                <div id="clearDetails"></div>
            </div>
        </div>
    </div>
</div>

<script>
$(function() {
    $('#clearCacheBtn').click(function() {
        var $btn = $(this);
        $btn.prop('disabled', true).html('<i class="fa fa-spinner fa-spin"></i> 清理中...');
        
        var url = '<?php echo $this->url('admin/clearCache/do'); ?>';
        console.log('Request URL:', url);
        
        $.post(url, {
            csrf_token: '<?php echo isset($_SESSION['csrf_token']) ? $_SESSION['csrf_token'] : ''; ?>'
        }, function(data) {
            console.log('Response:', data);
            if (data.success) {
                $('#successMsg').text(data.message);
                
                if (data.data && data.data.details) {
                    var html = '<ul>';
                    $.each(data.data.details, function(i, item) {
                        html += '<li>' + item + '</li>';
                    });
                    html += '</ul>';
                    $('#clearDetails').html(html);
                }
                
                $('#clearResult').show();
            } else {
                alert('错误: ' + data.message);
            }
            
            $btn.prop('disabled', false).html('<i class="fa fa-trash"></i> 开始清理');
        }, 'json').fail(function(xhr, status, error) {
            console.error('Request failed:', status, error);
            console.error('Response text:', xhr.responseText);
            alert('清理失败: ' + error + '\n\n响应内容: ' + xhr.responseText.substring(0, 500));
            $btn.prop('disabled', false).html('<i class="fa fa-trash"></i> 开始清理');
        });
    });
});
</script>

<style>
.cache-options {
    display: grid;
    grid-template-columns: repeat(auto-fit, minmax(250px, 1fr));
    gap: 16px;
}

.cache-option {
    padding: 16px;
    background: #f9fafb;
    border-radius: 8px;
}

.cache-option .checkbox {
    display: flex;
    align-items: center;
    gap: 8px;
    font-weight: 600;
    color: #1a1a2e;
}

.cache-option .checkbox input {
    width: 18px;
    height: 18px;
}

.option-desc {
    margin: 8px 0 0 26px;
    font-size: 13px;
    color: #6b7280;
}

.btn-primary {
    background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
    border: none;
    color: #fff;
    padding: 12px 24px;
    border-radius: 8px;
    font-size: 15px;
    cursor: pointer;
    transition: all 0.2s ease;
}

.btn-primary:hover {
    transform: translateY(-1px);
    box-shadow: 0 4px 12px rgba(102, 126, 234, 0.4);
}

.btn-primary:disabled {
    opacity: 0.7;
    cursor: not-allowed;
    transform: none;
}
</style>
