// 默认文档功能
?>
<div class="card">
    <div class="card-header">
        <h3 class="card-title mb-0"><?php echo L('default_doc', 'menu'); ?></h3>
    </div>
    <div class="card-body">
        <div class="alert alert-info d-flex align-items-center mb-4">
            <i class="fas fa-info-circle mr-2"></i>
            <div>设置网站的默认访问文档，支持多个文档，按优先级排序。</div>
        </div>
        <form id="mrwdForm">
            <div class="form-group mb-4">
                <label class="font-weight-bold" for="index">默认文档 (每行一个)</label>
                <textarea name="index" id="index" class="form-control text-monospace" rows="8" placeholder="index.php
index.html
index.htm">index.html
index.php
index.htm
default.html
default.php</textarea>
            </div>
            <button type="button" class="btn btn-success" data-action="set-mrwd">
                <i class="fas fa-check mr-1"></i> 确认修改
            </button>
        </form>
    </div>
</div>

<script>
// 页面加载完成后获取当前默认文档设置
$(document).ready(function() {
    loadCurrentIndex();
});

// 获取服务器中已有的默认文档设置
function loadCurrentIndex() {
    $.ajax({
        type: 'POST',
        url: './api/api_router.php',
        data: 'action=getmrwd',
        dataType: 'json',
        success: function(data) {
            if(data.code == 'success' && data.index) {
                $('#index').val(data.index);
            }
        },
        error: function(xhr, status, error) {
            console && console.error && console.error('loadCurrentIndex error', status, error);
        }
    });
}

function setmrwd(btn) {
    var $btn = $(btn);
    var originalHtml = $btn.html();
    $btn.prop('disabled', true).html('<i class="fas fa-spinner fa-spin"></i> 处理中...');
    
    var ii = layer.load(2, {shade: [0.1, '#fff']});
    $.ajax({
        type: 'POST',
        url: 'ajax.php',
        data: $('#mrwdForm').serialize() + '&egn=setmrwd',
        dataType: 'json',
        success: function(data) {
            layer.close(ii);
            $btn.prop('disabled', false).html(originalHtml);
            if(data.status) {
                layer.msg(data.msg || '修改成功');
                setTimeout(function(){ location.reload(); }, 1000);
            } else {
                layer.msg(data.msg || '修改失败');
            }
        },
        error: function() {
            layer.close(ii);
            $btn.prop('disabled', false).html(originalHtml);
            layer.msg('网络错误，请重试');
        }
    });
}
</script>