@extends('base.base')
@section('base')
<meta name="csrf-token" content="{{ csrf_token() }}">
<div class="container-fluid">
    <div class="row">
        <div class="col-12">
            <div class="card">
                <div class="card-header d-flex align-items-center justify-content-between">
                    <h3 class="card-title mb-0">免费保护配置</h3>
                    <small class="text-muted">管理免费保护相关参数</small>
                </div>
                <div class="card-body">
                    <div class="row">
                        <div class="col-md-6">
                            <div class="form-group">
                                <label>最小携带身金 (ProtectScore)</label>
                                <input type="number" class="form-control" id="ProtectScore" value="{{ $config['ProtectScore'] ?? 3 }}">
                                <small class="form-text text-muted">用户最小携带金币数量</small>
                            </div>
                        </div>
                        <div class="col-md-6">
                            <div class="form-group">
                                <label>身金/下注最大比 (ScoreBetRatio)</label>
                                <input type="number" class="form-control" id="ScoreBetRatio" value="{{ $config['ScoreBetRatio'] ?? 3 }}">
                                <small class="form-text text-muted">身金与下注的最大比例</small>
                            </div>
                        </div>
                    </div>
                    
                    <div class="row">
                        <div class="col-md-6">
                            <div class="form-group">
                                <label>下注身金 (ScoreBet)</label>
                                <input type="number" class="form-control" id="ScoreBet" value="{{ $config['ScoreBet'] ?? 10 }}">
                                <small class="form-text text-muted">下注时的身金数量</small>
                            </div>
                        </div>
                        <div class="col-md-6">
                            <div class="form-group">
                                <label>触发保护局数 (ProtectRounds)</label>
                                <input type="text" class="form-control" id="ProtectRounds" value="{{ is_array($config['ProtectRounds'] ?? []) ? implode(',', $config['ProtectRounds']) : ($config['ProtectRounds'] ?? '40,100') }}">
                                <small class="form-text text-muted">用逗号分隔多个值，如：40,100</small>
                            </div>
                        </div>
                    </div>
                    
                    <div class="row">
                        <div class="col-md-6">
                            <div class="form-group">
                                <label>触发后循环触发局数 (ProtectRoundsLoop)</label>
                                <input type="number" class="form-control" id="ProtectRoundsLoop" value="{{ $config['ProtectRoundsLoop'] ?? 100 }}">
                                <small class="form-text text-muted">触发保护后的循环局数</small>
                            </div>
                        </div>
                        <div class="col-md-6">
                            <div class="form-group">
                                <label>触发概率 (ProtectRoundsRatio)</label>
                                <div class="input-group">
                                    <input type="number" class="form-control" id="ProtectRoundsRatio" value="{{ $config['ProtectRoundsRatio'] ?? 10 }}">
                                    <div class="input-group-append">
                                        <span class="input-group-text">%</span>
                                    </div>
                                </div>
                                <small class="form-text text-muted">保护触发的概率百分比</small>
                            </div>
                        </div>
                    </div>

                    <div class="d-flex align-items-center mt-4">
                        <button class="btn btn-gradient-primary btn-sm save-config">
                            <i class="mdi mdi-content-save"></i> 保存配置
                        </button>
                        <span class="save-status ml-3"></span>
                    </div>
                </div>
            </div>
        </div>
    </div>
</div>

<script>
$(function() {
    $('.save-config').click(function(event) {
        const $btn = $(this);
        const $status = $('.save-status');
        $btn.prop('disabled', true).html('<i class="fa fa-spinner fa-spin"></i> 保存中...');
        $status.html('').removeClass('text-success text-danger');

        // 构建配置对象
        let configObj = {
            "ProtectScore": parseInt($('#ProtectScore').val()) || 3,
            "ScoreBetRatio": parseInt($('#ScoreBetRatio').val()) || 3,
            "ScoreBet": parseInt($('#ScoreBet').val()) || 10,
            "ProtectRounds": $('#ProtectRounds').val().split(',').map(x => parseInt(x.trim())).filter(x => !isNaN(x)),
            "ProtectRoundsLoop": parseInt($('#ProtectRoundsLoop').val()) || 100,
            "ProtectRoundsRatio": parseInt($('#ProtectRoundsRatio').val()) || 10
        };

        // 验证 ProtectRounds 数组
        if (configObj.ProtectRounds.length === 0) {
            configObj.ProtectRounds = [40, 100];
        }

        $.post("{{ url('/admin/Protect/free_protect_config_update') }}", {
            config: JSON.stringify(configObj),
            _token: "{{ csrf_token() }}"
        }).done(function(res){
            $btn.prop('disabled', false).html('<i class="mdi mdi-content-save"></i> 保存配置');
            if (res.status === 'success') {
                $status.text('更新成功').addClass('text-success');
            } else {
                $status.text(res.message || '更新失败').addClass('text-danger');
            }
            setTimeout(function(){ $status.fadeOut(function(){ $(this).text('').show().removeClass('text-success text-danger'); }); }, 3000);
        }).fail(function(){
            $btn.prop('disabled', false).html('<i class="mdi mdi-content-save"></i> 保存配置');
            $status.text('系统错误').addClass('text-danger');
        });
    });
});
</script>

<style>
.form-group label { font-weight: 600; }
.input-group-text { min-width: 34px; justify-content: center; }
</style>
@endsection
