@extends('base.base')
@section('base')
<meta name="csrf-token" content="{{ csrf_token() }}">
<div class="main-panel">
    <div class="content-wrapper">
        <div class="page-header">
            <h3 class="page-title">
                <span class="page-title-icon bg-gradient-primary text-white mr-2">
                    <i class="mdi mdi-percent"></i>
                </span>
                游戏税收比例管理
            </h3>
            <nav aria-label="breadcrumb">
                <ol class="breadcrumb">
                    <li class="breadcrumb-item"><a href="#">游戏配置</a></li>
                    <li class="breadcrumb-item active" aria-current="page">税收比例管理</li>
                </ol>
            </nav>
        </div>
        <div class="row">
            <div class="col-12">
                <div class="card">
                    <div class="card-header">
                        <h3 class="card-title mb-0">游戏税收比例配置</h3>
                    </div>
                    <div class="card-body">
                        <div class="row">
                            <div class="col-md-8">
                                <form id="taxForm">
                                    <div class="form-group">
                                        <label for="revenue_ratio">税收比例 <small class="text-muted">(所有游戏共用此比例)</small></label>
                                        <div class="input-group" style="max-width: 300px;">
                                            <input type="number" 
                                                   class="form-control" 
                                                   id="revenue_ratio" 
                                                   name="revenue_ratio" 
                                                   value="{{ $revenueRatio }}" 
                                                   min="0" 
                                                   max="100" 
                                                   step="0.1"
                                                   placeholder="请输入税收比例">
                                            <div class="input-group-append">
                                                <span class="input-group-text">%</span>
                                            </div>
                                        </div>
                                        <small class="form-text text-muted">
                                            说明：此税收比例将应用到所有游戏房间。输入5表示5%，系统会自动转换为千分比（50）存储到数据库。
                                        </small>
                                    </div>
                                    <div class="form-group">
                                        <button type="submit" class="btn btn-gradient-primary" id="saveBtn">
                                            <i class="mdi mdi-content-save"></i> 保存修改
                                        </button>
                                        <span class="ml-3" id="saveStatus"></span>
                                    </div>
                                </form>
                            </div>
                        </div>
                        <div class="row mt-4">
                            <div class="col-md-12">
                                <div class="alert alert-info">
                                    <h5 class="alert-heading"><i class="mdi mdi-information"></i> 注意事项</h5>
                                    <ul class="mb-0">
                                        <li>税收比例范围为 0% - 100%</li>
                                        <li>所有游戏的税收值都是统一的，修改后将更新 <code>QPPlatformDB.dbo.GameRoomInfo</code> 表中所有记录的 <code>RevenueRatio</code> 字段</li>
                                        <li>数据库存储格式为千分比，例如：输入 5% 会存储为 50</li>
                                        <li>修改操作会影响所有游戏房间，请谨慎操作</li>
                                    </ul>
                                </div>
                            </div>
                        </div>
                    </div>
                </div>
            </div>
        </div>
    </div>
</div>

<script>
$(function() {
    // 设置CSRF token
    $.ajaxSetup({
        headers: {
            'X-CSRF-TOKEN': $('meta[name="csrf-token"]').attr('content')
        }
    });

    $('#taxForm').on('submit', function(e) {
        e.preventDefault();
        
        const $btn = $('#saveBtn');
        const $status = $('#saveStatus');
        const revenueRatio = $('#revenue_ratio').val();

        // 验证输入
        if (!revenueRatio || revenueRatio === '') {
            $status.html('<span class="text-danger">请输入税收比例</span>');
            return;
        }

        const revenueRatioFloat = parseFloat(revenueRatio);
        if (isNaN(revenueRatioFloat) || revenueRatioFloat < 0 || revenueRatioFloat > 100) {
            $status.html('<span class="text-danger">税收比例必须在 0-100 之间</span>');
            return;
        }

        // 禁用按钮，显示加载状态
        $btn.prop('disabled', true).html('<i class="fa fa-spinner fa-spin"></i> 保存中...');
        $status.html('');

        // 发送AJAX请求
        $.ajax({
            url: "{{ url('/admin/game-tax/update') }}",
            type: 'POST',
            data: {
                revenue_ratio: revenueRatioFloat
            },
            success: function(response) {
                $btn.prop('disabled', false).html('<i class="mdi mdi-content-save"></i> 保存修改');
                
                if (response.code == 200) {
                    $status.html('<span class="text-success"><i class="mdi mdi-check-circle"></i> ' + response.msg + '</span>');
                    // 3秒后清除提示
                    setTimeout(function() {
                        $status.fadeOut(function() {
                            $(this).html('').show();
                        });
                    }, 3000);
                } else {
                    $status.html('<span class="text-danger"><i class="mdi mdi-alert-circle"></i> ' + (response.msg || '保存失败') + '</span>');
                }
            },
            error: function(xhr) {
                $btn.prop('disabled', false).html('<i class="mdi mdi-content-save"></i> 保存修改');
                
                let errorMsg = '系统错误';
                if (xhr.responseJSON && xhr.responseJSON.msg) {
                    errorMsg = xhr.responseJSON.msg;
                } else if (xhr.responseText) {
                    try {
                        const response = JSON.parse(xhr.responseText);
                        errorMsg = response.msg || errorMsg;
                    } catch(e) {
                        errorMsg = '保存失败，请稍后重试';
                    }
                }
                
                $status.html('<span class="text-danger"><i class="mdi mdi-alert-circle"></i> ' + errorMsg + '</span>');
            }
        });
    });
});
</script>

<style>
.input-group-text {
    min-width: 40px;
    justify-content: center;
}
.alert ul {
    padding-left: 20px;
}
.alert code {
    background-color: rgba(0,0,0,0.1);
    padding: 2px 6px;
    border-radius: 3px;
}
</style>
@endsection
