@extends('base.base')
@section('base')
    <!-- 内容区域 -->
    <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-settings"></i>
                    </span>
                    {{ __('auto.咖啡渠道修改') }}
                </h3>
                <nav aria-label="breadcrumb">
                    <ol class="breadcrumb">
                        <li class="breadcrumb-item"><a href="#">{{ __('auto.咖啡管理') }}</a></li>
                        <li class="breadcrumb-item active" aria-current="page">{{ __('auto.咖啡渠道修改') }}</li>
                        <li class="breadcrumb-item active" aria-current="page">{{ __('auto.查看渠道') }}</li>
                    </ol>
                </nav>
            </div>
            <div class="row">
                <div class="col-lg-12 grid-margin stretch-card">
                    <div class="card">
                        <div class="card-body">
                            <h4 class="card-title">{{ __('auto.查看渠道') }}</h4>
                            <div class="mb-2">
                                <button type="button" class="btn btn-sm btn-outline-secondary" id="toggle-closed-btn">
                                    显示已关闭渠道
                                </button>
                            </div>

                                <form action="" method="post" class="form-ajax">
                                    {!! csrf_field() !!}
                                    <div class="table-responsive">
                                        <table class="table table-bordered table-striped">
                                            <thead>
                                            <tr>
                                                <th style="min-width: 80px;">ID</th>
                                                <th style="min-width: 180px;">{{ __('auto.渠道名称') }}</th>
                                                <th style="min-width: 120px;">Cash(1)</th>
                                                <th style="min-width: 120px;">PayPal(2)</th>
                                                <th style="min-width: 120px;">Apple(4)</th>
                                                <th style="min-width: 120px;">Google(8)</th>
                                                <th style="min-width: 150px;">{{ __('auto.当前状态') }}</th>
                                            </tr>
                                            </thead>
                                            <tbody>
                                            @foreach($list as $k => $v)
                                                @php
                                                    $remarks = [];
                                                    if (!empty($v->remarks)) {
                                                        $decoded = \GuzzleHttp\json_decode($v->remarks, true);
                                                        if (is_array($decoded)) {
                                                            $remarks = $decoded;
                                                        }
                                                    }
                                                    $weights = $remarks['weight'] ?? ($remarks['weights'] ?? []);
                                                    $payTypes = (int)($v->pay_types ?? 0);
                                                    $payRate = config('pay.' . ($v->config_key ?? '') . '.pay_rate', []);
                                                    $rateText = function ($method) use ($payRate) {
                                                        $methodRate = $payRate[$method] ?? null;
                                                        if (is_array($methodRate) && count($methodRate) >= 2) {
                                                            return $methodRate[0] . '%+' . $methodRate[1];
                                                        }
                                                        return '-';
                                                    };
                                                @endphp
                                                <tr data-status="{{ (int)$v->status }}">
                                                    <td>{{ $v->id }}</td>
                                                    <td>{{ $v->name }}</td>
                                                    <input type="hidden" name="config[{{$v->id}}][sort]" value="{{$v->sort}}">
                                                    <td>
                                                        <div style="display:flex; align-items:center; gap:6px;">
                                                            <input class="form-control form-control-sm" type="number" min="0"
                                                                   style="width:72px;"
                                                                   name="config[{{$v->id}}][weight][1]"
                                                                   value="{{ $weights['type_1'] ?? '' }}"
                                                                   placeholder="{{ (($payTypes & 1) === 1) ? '0' : 'N/A' }}"
                                                                   {{ (($payTypes & 1) === 1) ? '' : 'disabled' }}>
                                                            <small style="white-space:nowrap; color:#495057; font-size:13px;">{{ $rateText(1) }}</small>
                                                        </div>
                                                    </td>
                                                    <td>
                                                        <div style="display:flex; align-items:center; gap:6px;">
                                                            <input class="form-control form-control-sm" type="number" min="0"
                                                                   style="width:72px;"
                                                                   name="config[{{$v->id}}][weight][2]"
                                                                   value="{{ $weights['type_2'] ?? '' }}"
                                                                   placeholder="{{ (($payTypes & 2) === 2) ? '0' : 'N/A' }}"
                                                                   {{ (($payTypes & 2) === 2) ? '' : 'disabled' }}>
                                                            <small style="white-space:nowrap; color:#495057; font-size:13px;">{{ $rateText(2) }}</small>
                                                        </div>
                                                    </td>
                                                    <td>
                                                        <div style="display:flex; align-items:center; gap:6px;">
                                                            <input class="form-control form-control-sm" type="number" min="0"
                                                                   style="width:72px;"
                                                                   name="config[{{$v->id}}][weight][4]"
                                                                   value="{{ $weights['type_4'] ?? '' }}"
                                                                   placeholder="{{ (($payTypes & 4) === 4) ? '0' : 'N/A' }}"
                                                                   {{ (($payTypes & 4) === 4) ? '' : 'disabled' }}>
                                                            <small style="white-space:nowrap; color:#495057; font-size:13px;">{{ $rateText(4) }}</small>
                                                        </div>
                                                    </td>
                                                    <td>
                                                        <div style="display:flex; align-items:center; gap:6px;">
                                                            <input class="form-control form-control-sm" type="number" min="0"
                                                                   style="width:72px;"
                                                                   name="config[{{$v->id}}][weight][8]"
                                                                   value="{{ $weights['type_8'] ?? '' }}"
                                                                   placeholder="{{ (($payTypes & 8) === 8) ? '0' : 'N/A' }}"
                                                                   {{ (($payTypes & 8) === 8) ? '' : 'disabled' }}>
                                                            <small style="white-space:nowrap; color:#495057; font-size:13px;">{{ $rateText(8) }}</small>
                                                        </div>
                                                    </td>
                                                    <td>
                                                        @component('components.select', [
                                                            'name' => "config[{$v->id}][status]",
                                                            'class' => $v->status == 1 ? 'btn-primary' : 'btn-danger',
                                                            'options' => [1 => __('auto.开启'), -1 => __('auto.关闭')],
                                                            'default' => $v->status])
                                                        @endcomponent
                                                    </td>
                                                </tr>
                                            @endforeach
                                            </tbody>
                                        </table>
                                    </div>
                                    <div class="mt-3">
                                        <button class="btn btn-sm btn-primary">{{ __('auto.提交') }}</button>
                                    </div>
                                </form>
                        </div>
                    </div>
                </div>
            </div>
        </div>
    </div>
    <script>
        (function () {
            var showClosed = false;
            var btn = document.getElementById('toggle-closed-btn');
            if (!btn) return;

            function renderRows() {
                var rows = document.querySelectorAll('tr[data-status]');
                for (var i = 0; i < rows.length; i++) {
                    var row = rows[i];
                    var status = parseInt(row.getAttribute('data-status') || '0', 10);
                    if (!showClosed && status === -1) {
                        row.style.display = 'none';
                    } else {
                        row.style.display = '';
                    }
                }
                btn.textContent = showClosed ? '隐藏已关闭渠道' : '显示已关闭渠道';
            }

            btn.addEventListener('click', function () {
                showClosed = !showClosed;
                renderRows();
            });

            renderRows();
        })();
    </script>
@endsection
