@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">编辑 6 档参数并一键保存 (索引 0-5)</small>
                </div>
                <div class="card-body">
                    <!-- 标签页导航 -->
                    <ul class="nav nav-tabs mb-3" role="tablist">
                        <li class="nav-item">
                            <a class="nav-link active" data-toggle="tab" href="#common-config" role="tab">
                                <i class="mdi mdi-cog"></i> 常规配置
                            </a>
                        </li>
                        <li class="nav-item">
                            <a class="nav-link" data-toggle="tab" href="#special-config" role="tab">
                                <i class="mdi mdi-package-variant"></i> 库存模式
                            </a>
                        </li>
                    </ul>

                    <!-- 标签页内容 -->
                    <div class="tab-content">
                        <!-- 常规配置标签页 -->
                        <div class="tab-pane fade show active" id="common-config" role="tabpanel">
                            <div class="table-responsive">
                                <table class="table table-striped table-hover align-middle mb-0" style="min-width: 900px;">
                                    <thead class="thead-light">
                                        <tr>
                                            <th style="width:82px;">档位</th>
                                            <th style="width:180px;">充值区间下限 (RechargeMin)</th>
                                            <th style="width:180px;">充值区间上限 (RechargeMax)</th>
                                            <th style="width:180px;">固定值 (FreeWinMax)</th>
                                            <th style="width:180px;">充值最大百分比 (RechargeMaxPercent)</th>
                                            <th style="width:160px;">税收比例 (TaxPercent)</th>
                                        </tr>
                                    </thead>
                                    <tbody class="config-form" data-config-type="common">
                                        @foreach([0,1,2,3,4,5] as $index => $level)
                                            <tr>
                                                <td>
                                                    <span class="badge badge-primary">等级{{ $level }}</span>
                                                </td>
                                                <td>
                                                    <input type="number" class="form-control form-control-sm"
                                                           name="level[{{ $level }}][RechargeMin]"
                                                           value="{{ $config[$level]['RechargeMin'] ?? 0 }}" />
                                                </td>
                                                <td>
                                                    <input type="number" class="form-control form-control-sm"
                                                           name="level[{{ $level }}][RechargeMax]"
                                                           value="{{ $config[$level]['RechargeMax'] ?? 0 }}" />
                                                </td>
                                                <td>
                                                    <input type="number" class="form-control form-control-sm"
                                                           name="level[{{ $level }}][FreeWinMax]"
                                                           value="{{ $config[$level]['FreeWinMax'] ?? 0 }}" />
                                                </td>
                                                <td>
                                                    <div class="input-group input-group-sm">
                                                        <input type="number" class="form-control"
                                                               name="level[{{ $level }}][RechargeMaxPercent]"
                                                               value="{{ $config[$level]['RechargeMaxPercent'] ?? 0 }}" />
                                                        <div class="input-group-append"><span class="input-group-text">%</span></div>
                                                    </div>
                                                </td>
                                                <td>
                                                    <div class="input-group input-group-sm">
                                                        <input type="number" class="form-control"
                                                               name="level[{{ $level }}][TaxPercent]"
                                                               value="{{ $config[$level]['TaxPercent'] ?? 0 }}" />
                                                        <div class="input-group-append"><span class="input-group-text">%</span></div>
                                                    </div>
                                                </td>
                                            </tr>
                                        @endforeach
                                    </tbody>
                                </table>
                            </div>

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

                        <!-- 库存模式标签页 -->
                        <div class="tab-pane fade" id="special-config" role="tabpanel">
                            <div class="table-responsive">
                                <table class="table table-striped table-hover align-middle mb-0" style="min-width: 900px;">
                                    <thead class="thead-light">
                                        <tr>
                                            <th style="width:82px;">档位</th>
                                            <th style="width:180px;">充值区间下限 (RechargeMin)</th>
                                            <th style="width:180px;">充值区间上限 (RechargeMax)</th>
                                            <th style="width:180px;">固定值 (FreeWinMax)</th>
                                            <th style="width:180px;">充值最大百分比 (RechargeMaxPercent)</th>
                                            <th style="width:160px;">税收比例 (TaxPercent)</th>
                                        </tr>
                                    </thead>
                                    <tbody class="config-form" data-config-type="special">
                                        @foreach([0,1,2,3,4,5] as $index => $level)
                                            <tr>
                                                <td>
                                                    <span class="badge badge-success">等级{{ $level }}</span>
                                                </td>
                                                <td>
                                                    <input type="number" class="form-control form-control-sm"
                                                           name="level[{{ $level }}][RechargeMin]"
                                                           value="{{ $specialConfig[$level]['RechargeMin'] ?? 0 }}" />
                                                </td>
                                                <td>
                                                    <input type="number" class="form-control form-control-sm"
                                                           name="level[{{ $level }}][RechargeMax]"
                                                           value="{{ $specialConfig[$level]['RechargeMax'] ?? 0 }}" />
                                                </td>
                                                <td>
                                                    <input type="number" class="form-control form-control-sm"
                                                           name="level[{{ $level }}][FreeWinMax]"
                                                           value="{{ $specialConfig[$level]['FreeWinMax'] ?? 0 }}" />
                                                </td>
                                                <td>
                                                    <div class="input-group input-group-sm">
                                                        <input type="number" class="form-control"
                                                               name="level[{{ $level }}][RechargeMaxPercent]"
                                                               value="{{ $specialConfig[$level]['RechargeMaxPercent'] ?? 0 }}" />
                                                        <div class="input-group-append"><span class="input-group-text">%</span></div>
                                                    </div>
                                                </td>
                                                <td>
                                                    <div class="input-group input-group-sm">
                                                        <input type="number" class="form-control"
                                                               name="level[{{ $level }}][TaxPercent]"
                                                               value="{{ $specialConfig[$level]['TaxPercent'] ?? 0 }}" />
                                                        <div class="input-group-append"><span class="input-group-text">%</span></div>
                                                    </div>
                                                </td>
                                            </tr>
                                        @endforeach
                                    </tbody>
                                </table>
                            </div>

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

<script>
$(function() {
    $('.save-config').click(function(event) {
        const $btn = $(this);
        const configType = $btn.data('config-type'); // 获取配置类型 common 或 special
        const $form = $('.config-form[data-config-type="' + configType + '"]');
        const $status = $btn.closest('.tab-pane').find('.save-status');

        $btn.prop('disabled', true).html('<i class="fa fa-spinner fa-spin"></i> 保存中...');
        $status.html('').removeClass('text-success text-danger');

        // 构建索引数组
        let configArray = [];
        for (let i = 0; i <= 5; i++) {
            configArray[i] = {
                "RechargeMin": 0,
                "RechargeMax": 0,
                "FreeWinMax": 0,
                "RechargeMaxPercent": 0,
                "TaxPercent": 0
            };
        }

        $form.find('input').each(function() {
            const name = this.name; // level[0][RechargeMin]
            const val = this.value;
            const m = name.match(/level\[(\d+)\]\[([^\]]+)\]/);
            if (m) {
                const lvl = parseInt(m[1]);
                const key = m[2];
                let num = parseInt(val || 0);
                if (isNaN(num)) num = 0;
                configArray[lvl][key] = num;
            }
        });

        $.post("{{ url('/admin/common-config/update') }}", {
            config: JSON.stringify(configArray),
            config_type: configType,
            _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>
.table thead th { white-space: nowrap; }
.table tbody td { vertical-align: middle; }
.input-group-text { min-width: 34px; justify-content: center; }
.badge { font-size: .85rem; padding: .5em .6em; }
</style>
@endsection
