// 防盗链功能
?>
<div class="card">
    <div class="card-header">
        <h3 class="card-title mb-0"><i class="fas fa-shield-alt mr-2"></i>防盗链设置</h3>
    </div>
    
    <div class="card-body">
        <div class="alert alert-info mb-4">
            <i class="fas fa-info-circle mr-1"></i>
            开启防盗链后，只有在“许可域名”列表中的站点才能正常访问您的静态资源（如图片、视频等）。
        </div>

        <div class="form-section mb-4">
            <h5 class="font-weight-bold mb-3">状态控制</h5>
            <div class="d-flex align-items-center justify-content-between p-3 bg-light rounded" style="max-width: 800px;">
                <div>
                    <div class="font-weight-bold">启用防盗链</div>
                    <div class="small text-muted">开启后将限制指定域名之外的站点访问您的资源</div>
                </div>
                <div class="custom-control custom-switch">
                    <input type="checkbox" class="custom-control-input" id="fdl-status">
                    <label class="custom-control-label" for="fdl-status"></label>
                </div>
            </div>
        </div>

        <div class="form-section mb-4">
            <h5 class="font-weight-bold mb-3">详细设置</h5>
            <div class="form-group mb-4">
                <label class="font-weight-bold">许可域名</label>
                <p class="small text-muted mb-2">允许访问资源的域名列表，每行一个。支持通配符，如 *.example.com。建议包含本站域名。</p>
                <textarea class="form-control" id="fdl-domains" rows="5" placeholder="example.com&#10;www.example.com" style="max-width: 800px;"></textarea>
            </div>

            <div class="form-group mb-4">
                <label class="font-weight-bold">资源后缀</label>
                <p class="small text-muted mb-2">设置需要保护的资源后缀，用英文逗号分隔</p>
                <input type="text" class="form-control" id="fdl-exts" placeholder="jpg,png,gif,js,css" style="max-width: 800px;">
            </div>
        </div>

        <div class="border-top pt-4 text-left">
            <button class="btn btn-primary" data-action="save-fdl">
                <i class="fas fa-save mr-1"></i> 保存设置
            </button>
        </div>
    </div>
</div>

<script>
// 加载防盗链状态
function loadFDLStatus() {
    $.ajax({
        url: 'ajax.php?egn=getfdl',
        type: 'GET',
        dataType: 'json',
        success: function(res) {
            if (res.status === true || res.code === 'success') {
                var data = res.data || res;
                $('#fdl-status').prop('checked', data.status === true || data.enable === 'true');
                $('#fdl-domains').val(data.url || data.domains || '');
                $('#fdl-exts').val(data.exts || 'jpg,png,gif,js,css');
            }
        }
    });
}

// 保存防盗链设置
function saveFDL() {
    var $btn = $('button[data-action="save-fdl"]');
    var originalHtml = $btn.html();
    
    var enable = $('#fdl-status').is(':checked');
    var domains = $('#fdl-domains').val();
    var exts = $('#fdl-exts').val();
    
    if (enable && !domains) {
        layer.alert('启用防盗链时，许可域名不能为空');
        return;
    }
    
    if (enable && !exts) {
        layer.alert('启用防盗链时，资源后缀不能为空');
        return;
    }

    var ii = layer.load(2, {shade: [0.1, '#fff']});
    $btn.prop('disabled', true).html('<i class="fa fa-spinner fa-spin"></i> 保存中...');

    $.ajax({
        url: 'ajax.php?egn=setfdl',
        type: 'POST',
        data: {
            enable: enable ? 'true' : 'false',
            domains: domains,
            exts: exts
        },
        dataType: 'json',
        success: function(res) {
            layer.close(ii);
            if (res.status === true || res.code === '设置成功') {
                layer.msg(res.msg || '防盗链设置成功');
                loadFDLStatus();
            } else {
                layer.alert(res.msg || '设置失败');
            }
        },
        error: function() {
            layer.close(ii);
            layer.msg('网络错误');
        },
        complete: function() {
            $btn.prop('disabled', false).html(originalHtml);
        }
    });
}

$(document).ready(function() {
    loadFDLStatus();
});
</script>

<?php endif; ?>

<!-- 统一事件代理：处理 data-action 和重试链接 -->
<script>
// 统一事件代理，避免内联 onclick 并把常用动作绑定到函数上
$(document).on('click', 'button[data-action], a.btn-retry, .btn-guide-toggle, button[data-action^="show-"], button[data-action^="add-"], button[data-action^="select-directory"]', function(e) {
    var $el = $(e.currentTarget);

    // 重试链接的专用处理
    if ($el.is('a.btn-retry')) {
        e.preventDefault();
        loadDomainList();
        return;
    }

    var action = $el.attr('data-action');
    if (!action) return;
    e.preventDefault();

    switch(action) {
        case 'setphp':
            setphp(this);
            break;
        case 'copy-ip':
            copyIP();
            break;
        case 'add-domain':
            setym(this);
            break;
        case 'browse-directory':
            browseDirectory();
            break;
        case 'select-directory':
            selectCurrentDirectory();
            break;
        case 'show-add-encrypt':
            showAddEncryptModal();
            break;
        case 'show-add-forbid':
            showAddForbidModal();
            break;
        case 'add-encrypt':
            addEncrypt();
            break;
        case 'add-forbid':
            addForbid();
            break;
        case 'getBTRewriteRules':
            getBTRewriteRules(this);
            break;
        case 'set-mrwd':
            setmrwd(this);
            break;
        case 'set-yxml':
            setyxml(this);
            break;
        case 'set-wjt':
            setwjt(this);
            break;
        case 'close-ssl-modal':
            $('.ssl-modal').remove();
            break;
        case 'save-ssl':
            saveSSLCertificate();
            break;
        case 'download-ssl':
            downloadSSLCertificate();
            break;
        case 'close-ssl':
            closeSSL();
            break;
        case 'save-fdl':
            saveFDL();
            break;
        default:
            // 未知 action 可在此处记录或忽略
            break;
    }
});

// 目录面包屑委托（已在文件中使用 data-path）
$(document).on('click', '#directoryBreadcrumb a[data-path]', function(e) {
    e.preventDefault();
    var path = $(this).data('path');
    if (path) navigateToDirectory(path);
});

// 将原有单独注册的旧式绑定保留以兼容历史代码
$(document).on('click', '[data-action="set-yxml"]', function(e){ e.preventDefault(); setyxml(this);});
$(document).on('click', '[data-action="set-wjt"]', function(e){ e.preventDefault(); setwjt(this);});
$(document).on('click', '[data-action="set-mrwd"]', function(e){ e.preventDefault(); setmrwd(this);});

// 复制IP的键盘友好行为
$(document).on('keydown', 'input#serverIP', function(e) {
    if (e.key === 'Enter') copyIP();
});
</script>
