@extends('base.base')
@section('base')
    <!-- 内容区域 -->
    <div class="main-panel">
        <div class="content-wrapper">
            <div class="row">
                <div class="col-12 grid-margin stretch-card">
                    <div class="card">
                        <div class="card-body">
                            <h4 class="card-title">{{ __('auto.请填写管理员信息') }}</h4>

                            <form class="forms-sample" id="form">
                                <div class="form-group">
                                    <h4>{{ __('auto.菜单名称') }}</h4>
                                    <input type="text" class="form-control required" name="name" placeholder="{{ __('auto.请输入菜单名称') }}" value="{{ $info->name }}">
                                </div>

                                <div>
                                    <label for="recharge">
                                        <input type="checkbox" name="recharge_checkbox" id="recharge" @if ($info->recharge_checkbox > 0)
                                            checked
                                        @endif>
                                        {{ __('auto.累计咖啡额度范围设置') }}
                                    </label>
                                    <br>
                                    <div class="form-group" style="width: 48%;float: left;margin-left: 3px">
                                        <label>* 下限值</label>
                                        <input type="number" class="form-control" name="recharge_min" placeholder="{{ __('auto.下限值') }}" value="{{ $info->recharge_min }}">
                                    </div>
                                    <div class="form-group" style="width: 50%;float: left;margin-left: 5px">
                                        <label>* 上限值</label>
                                        <input type="number" class="form-control" name="recharge_max" placeholder="{{ __('auto.上限值') }}" value="{{ $info->recharge_max }}">

                                    </div>
                                </div>

                                <div>
                                    <label for="draw">
                                        <input type="checkbox" name="draw_checkbox" id="draw" @if ($info->type > 0)
                                            checked
                                        @endif>
                                        {{ __('auto.单个玩家可免审核茶叶额度') }}
                                    </label> <br>
                                    <div class="form-group" style="width: 50%;float: left;margin-left: 5px">
                                        <label> <input type="radio" name="type" id="optionsRadios2" value="1" @if ($info->type == 1)
                                                                                                                  checked
                                        @endif>
                                            {{ __('auto.固定额度') }}</label>
                                        <input type="number" class="form-control" name="quota" placeholder="{{ __('auto.固定额度') }}" @if ($info->type == 1)
                                        value="{{ $info->quota }}"
                                        @endif>
                                    </div>
                                    <div class="form-group" style="width: 48%;float: left;margin-left: 3px">
                                        <label>* <input type="radio" name="type" id="optionsRadios2" value="2" @if ($info->type ==2 )
                                            checked
                                        @endif>
                                            {{ __('auto.固定倍数') }}</label>
                                        <input type="number" class="form-control" name="multiple" placeholder="{{ __('auto.固定倍数') }}" id="multiple" @if ($info->type == 2)
                                        value="{{ $info->quota }}"
                                        @endif>
                                    </div>
                                </div>

                                <div>
                                    <div class="form-group" style="width: 50%;float: left;margin-left: 5px">
                                        <label> <input type="checkbox" name="draw_gt_recharge_checkbox"  @if ($info->draw_gt_recharge > 0)
                                            checked
                                        @endif>
                                            {{ __('auto.茶叶大于咖啡，设置超过值：') }}</label>
                                        <input type="number" class="form-control" name="draw_gt_recharge" placeholder="{{ __('auto.茶叶大于咖啡') }}"
                                               id="quota" value="{{ $info->draw_gt_recharge }}">
                                    </div>
                                    <div class="form-group" style="width: 48%;float: left;margin-left: 3px">
                                        <label>* <input type="checkbox" name="draw_total_checkbox"  @if ($info->draw_total >0)
                                            checked
                                        @endif>
                                            {{ __('auto.茶叶总额超过，设置超过值：') }}</label>
                                        <input type="number" class="form-control" name="draw_total" placeholder="{{ __('auto.茶叶总额超过') }}"
                                               id="multiple" value="{{ $info->draw_total }}">
                                    </div>
                                </div>

                                <div>
                                    <div class="form-group" style="width: 50%;float: left;margin-left: 5px">
                                        <label> <input type="checkbox" name="draw_bi_checkbox"   @if ($info->draw_bi > 0)
                                            checked
                                        @endif>
                                            {{ __('auto.单日茶叶笔数超过，设置超过值：') }}</label>
                                        <input type="number" class="form-control" name="draw_bi" placeholder="{{ __('auto.单日茶叶笔数超过') }}"
                                               id="quota" value="{{ $info->draw_bi }}">
                                    </div>
                                    <div class="form-group" style="width: 48%;float: left;margin-left: 3px">
                                        <label>* <input type="checkbox" name="game_time_checkbox"  @if ($info->game_time > 0)
                                            checked
                                        @endif>
                                            {{ __('auto.总游戏时长低于，设置值（小时）：') }}</label>
                                        <input type="number" class="form-control" name="game_time" placeholder="{{ __('auto.总游戏时长低于') }}"
                                               id="multiple" value="{{ $info->game_time }}">
                                    </div>
                                </div>


                                <div class="form-group">
                                    <label for=""><h4>{{ __('auto.每日申请总额度设置') }}</h4></label> <br>
                                    <label for="nickname">
                                        {{ __('auto.当前每日累计消耗额度/每日总额度：') }}
                                    </label>
                                    {{ __('auto.添加') }} / {{ __('auto.减少额度') }}<input type="text" class="form-control required" name="total_quota"
                                                    placeholder="{{ __('auto.每日总额度设置') }}" value="{{ $info->total_quota }}">
                                </div>
                                <div class="form-group">
                                    <label for=""><h4>{{ __('auto.开关设置') }}</h4></label> <br>
                                    <label> <input type="radio" name="status" id="optionsRadios2" value="1" @if ($info->status == 1)
                                        checked
                                    @endif> {{ __('auto.开启') }}</label>
                                    <label> <input type="radio" name="status" id="optionsRadios2" value="2" @if ($info->status == 2)
                                        checked
                                    @endif> {{ __('auto.关闭') }}</label>
                                </div>
                                <div class="form-group" style="margin-top: 20px;">
                                    <label><h4>{{ __('auto.代付渠道配置') }}</h4></label>
                                    <p class="text-muted">{{ __('auto.以下为当前可用的代付渠道及其权重比例（从admin_configs表读取）') }}</p>
                                    
                                    <div class="table-responsive">
                                        <table class="table table-bordered table-striped">
                                            <thead class="bg-light">
                                                <tr>
                                                    <th width="30%">{{ __('auto.渠道名称') }}</th>
                                                    <th width="20%">{{ __('auto.渠道值') }}</th>
                                                    <th width="20%">{{ __('auto.权重比例') }}(%)</th>
                                                    <th width="15%">{{ __('auto.状态') }}</th>
                                                    <th width="15%">{{ __('auto.修改人') }}</th>
                                                </tr>
                                            </thead>
                                            <tbody>
                                                @php
                                                    $totalSort = 0;
                                                    $activeCount = 0;
                                                @endphp
                                                @foreach($agent as $val)
                                                    @php
                                                        if ($val->status == 1) {
                                                            $totalSort += $val->sort;
                                                            $activeCount++;
                                                        }
                                                    @endphp
                                                    <tr class="{{ $val->status == 1 ? 'table-success' : 'table-secondary' }}">
                                                        <td><strong>{{ $val->name }}</strong></td>
                                                        <td>{{ $val->config_value }}</td>
                                                        <td>
                                                            <span class="badge badge-{{ $val->status == 1 ? 'primary' : 'secondary' }} badge-pill" style="font-size: 14px;">
                                                                {{ $val->sort }}%
                                                            </span>
                                                        </td>
                                                        <td>
                                                            @if($val->status == 1)
                                                                <span class="badge badge-success">{{ __('auto.开启') }}</span>
                                                            @else
                                                                <span class="badge badge-danger">{{ __('auto.关闭') }}</span>
                                                            @endif
                                                        </td>
                                                        <td>{{ $val->admin_id ?? '-' }}</td>
                                                    </tr>
                                                @endforeach
                                            </tbody>
                                            <tfoot>
                                                <tr class="bg-light">
                                                    <td colspan="2" class="text-right"><strong>{{ __('auto.开启渠道权重总和') }}:</strong></td>
                                                    <td>
                                                        <strong class="text-{{ $totalSort == 100 ? 'success' : 'danger' }}" style="font-size: 16px;">
                                                            {{ $totalSort }}%
                                                            @if($totalSort == 100)
                                                                <i class="mdi mdi-check-circle text-success"></i>
                                                            @else
                                                                <i class="mdi mdi-alert-circle text-danger"></i>
                                                            @endif
                                                        </strong>
                                                    </td>
                                                    <td colspan="2">
                                                        <small class="text-muted">{{ __('auto.已开启') }}: {{ $activeCount }} {{ __('auto.个渠道') }}</small>
                                                    </td>
                                                </tr>
                                            </tfoot>
                                        </table>
                                    </div>
                                    
                                    <div class="alert alert-info" role="alert">
                                        <i class="mdi mdi-information"></i>
                                        <strong>{{ __('auto.说明') }}:</strong>
                                        <ul class="mb-0">
                                            <li>{{ __('auto.代付渠道配置从 admin_configs 表中读取，在') }} <a href="/admin/recharge/config/cash" target="_blank">{{ __('auto.代付渠道管理') }}</a> {{ __('auto.中进行修改') }}</li>
                                            <li>{{ __('auto.系统会根据开启渠道的权重比例自动分配代付任务') }}</li>
                                            <li>{{ __('auto.开启的渠道权重总和应为 100%') }}</li>
                                        </ul>
                                    </div>
                                </div>
                                <button type="button" onclick="commit({{ $info->id }})"
                                        class="btn btn-sm btn-gradient-primary btn-icon-text">
                                    <i class="mdi mdi-file-check btn-icon-prepend"></i>
                                    {{ __('auto.提交') }}
                                </button>
                                <button type="button" onclick="cancel()"
                                        class="btn btn-sm btn-gradient-warning btn-icon-text">
                                    <i class="mdi mdi-reload btn-icon-prepend"></i>
                                    {{ __('auto.取消') }}
                                </button>
                            </form>
                        </div>
                    </div>
                </div>
            </div>
        </div>
    </div>
    <script>

        function commit(id) {
            if (!checkForm()) {
                return false;
            }

            var data = $("#form").serializeObject();

            if (! data.draw_bi_checkbox){
                data.draw_bi = 0
            }

            if (! data.draw_gt_recharge_checkbox){
                data.draw_gt_recharge = 0
            }
            if (! data.draw_total_checkbox){
                data.draw_total = 0
            }
            if (! data.game_time_checkbox){
                data.game_time = 0
            }
            if (! data.recharge_checkbox){
                data.recharge_max = 0
                data.recharge_min = 0
            }
            if (! data.draw_checkbox){
                data.type = 0
            }

            let type = data.type

            if (type == 1 && data.quota == '') {
                layer.msg('{{ __('auto.固定额度不能为空') }}');
                return false;
            }
            if (type == 2 && data.multiple == '') {
                layer.msg('{{ __('auto.固定倍数不能为空') }}');
                return false;
            }

            if (parseInt(data.recharge_min) > parseInt(data.recharge_max)) {
                layer.msg('{{ __('auto.下限值不能大于上限') }}');
                return false;
            }

            if (data.quota != '' && (data.quota < 0 || data.quota > 1000000)) {
                layer.msg('{{ __('auto.固定额度区间为') }}0-1000000');
                return false;
            }

            if (data.multiple != '' && (data.multiple < 1 || data.multiple > 100)) {
                layer.msg('{{ __('auto.固定倍数区间为') }}1-100');
                return false;
            }

            if (data.multiple != '' && type == 2) {
                data.quota = data.multiple
            }

            myRequest("/admin/withdrawal/exempt_review_update/"+id, "post", data, function (res) {
                if (res.code == '200') {
                    layer.msg(res.msg)
                    setTimeout(function () {
                        parent.location.reload();
                    }, 1500)
                } else {
                    layer.msg(res.msg)
                }
            });
        }

        function cancel() {
            parent.location.reload();
        }
    </script>
@endsection
