@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>
                            @php
                                $ratioMap = [];
                                foreach (($ratioConfig ?? []) as $pixType => $items) {
                                    foreach ($items as $item) {
                                        $ratioMap[$pixType][$item['agent']] = $item['ratio'];
                                    }
                                }
                            @endphp
                            <form class="forms-sample" id="form">
                                <div class="form-group" style="margin-top: 20px">
                                    <label for=""><h4>自动免审代付比例</h4></label>
                                    <div class="alert alert-info">
                                        CashApp、PayPal、CashApp小额（提现金额&lt;50）分别配置比例，每种提现方式下开启的比例总和必须为 100。
                                    </div>
                                    @foreach(($pixTypeOptions ?? [1 => 'CashApp', 2 => 'PayPal', 'cash_small' => 'CashApp小额(<50)']) as $pixType => $pixTypeName)
                                        <h5>{{ $pixTypeName }}</h5>
                                        <div class="row">
                                            @foreach($agent as $val)
                                                <div class="col-md-3" style="margin-bottom: 10px">
                                                    <label>{{$val->name}} ({{$val->config_value}}，费率：{{$val->withdraw_fee_texts[$pixType] ?? '-'}})</label>
                                                    <input class="form-control agent-ratio" type="number" min="0" max="100"
                                                           name="agent_ratio[{{$pixType}}][{{$val->config_value}}]"
                                                           data-pix-type="{{$pixType}}"
                                                           value="{{ $ratioMap[$pixType][$val->config_value] ?? 0 }}">
                                                </div>
                                            @endforeach
                                        </div>
                                        <div class="ratio-total" data-pix-type="{{$pixType}}" style="margin-bottom: 10px"></div>
                                    @endforeach
                                </div>
                                <button type="button" onclick="commit({{ $config->channel }})"
                                        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;
            }

            if (!checkAgentRatio()) {
                return false;
            }

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

            myRequest("/admin/channel/withdrawal_configall", "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();
        }

        function checkAgentRatio() {
            var pass = true;
            var pixTypes = @json(array_keys($pixTypeOptions ?? [1 => 'CashApp', 2 => 'PayPal', 'cash_small' => 'CashApp小额(<50)']));
            pixTypes.forEach(function (pixType) {
                var total = 0;
                $('.agent-ratio[data-pix-type="' + pixType + '"]').each(function () {
                    total += parseInt($(this).val()) || 0;
                });

                if (total !== 100) {
                    pass = false;
                }
            });

            if (!pass) {
                layer.msg('CashApp、PayPal、CashApp小额的比例总和都必须为100');
            }

            return pass;
        }

        $(document).ready(function () {
            function refreshRatioTotal() {
                var pixTypes = @json(array_keys($pixTypeOptions ?? [1 => 'CashApp', 2 => 'PayPal', 'cash_small' => 'CashApp小额(<50)']));
                pixTypes.forEach(function (pixType) {
                    var total = 0;
                    $('.agent-ratio[data-pix-type="' + pixType + '"]').each(function () {
                        total += parseInt($(this).val()) || 0;
                    });

                    $('.ratio-total[data-pix-type="' + pixType + '"]').text('当前总和：' + total + '%');
                });
            }

            $('.agent-ratio').on('input', refreshRatioTotal);
            refreshRatioTotal();
        });
    </script>
@endsection
