<h7 class="col col-12 m-1 p-1"> <i class="text-info"> {{ __('settings.financial_hint') }} </i> </h7>

<ul class="nav nav-pills container" role="tablist">
    @for($i=3; $i<=12; $i++)
    <li class="nav-item">
        <a class="nav-link {{ $i == 3 ? 'active' : '' }}" data-toggle="tab" href="#group_{{$i}}">{{ $i. str_plural(__('settings.month')) }}</a>
    </li>
    @endfor
</ul>

<div class="alert alert-info col col-12 m-1 p-1">
    {{ __('settings.group_setting_error') }}
</div>
<div class="tab-content container ">
    @for($i=3; $i<=12; $i++)
    <div id="group_{{$i}}" class="tab-pane  {{ $i == 3 ? 'active' : '' }}"><br/>
        @php
            $groupSettings= old('group_settings', $final_settings->group_settings ?? [$i=> ['period'=> $i, 'max_budget'=> 1000, 'months'=> [] ] ])[$i];
            $perfixName="group_settings[{$i}]";
            $name= $perfixName."[period]";
            $period= $i;

        @endphp
        <input type="hidden" class="form-control {{ $errors->has('group_settings') && !$period ? 'is-invalid' : '' }}" name='{{ $name }}' placeholder="{{ __('settings.period') }}" value='{{ $period }}'/>

        <fieldset class="col col-12 m-1 border p-2">
            <legend  class="w-auto">{{ __('settings.max_budget') }}</legend>

            <div class="form-row">
                <div class="form-group col-lg-12">
                    <label>{{ __('settings.max_budget') }}</label>
                    <div class="input-group">
                        @php
                            $name= $perfixName."[max_budget]";
                            $max_budget= (float) $groupSettings['max_budget'] ?? 0;
                        @endphp
                        <input type="number" class="form-control {{ $errors->has('group_settings') && !$max_budget ? 'is-invalid' : '' }}" name='{{ $name }}' placeholder="{{ __('settings.max_budget') }}" value='{{ $max_budget }}'/>
                        <div class="input-group-append">
                            <div class="input-group-text">SAR</div>
                        </div>
                    </div>
                    <small id="emailHelp" class="form-text text-muted"> {{ __('settings.max_budget_hint') }} </small>
                </div>

            </div>
        </fieldset>

        @for($j=0; $j< $i; $j++)
        <fieldset class="col col-12 m-1 border p-2">
            @php
                // algorithms
                $step= 1 / ($i - 1);
                $release = round(100 * $j * $step);
                $hold = 100 - $release;
                $fees = round($hold / ($i * 10)) * 2 + 1;

                $groupSettingsMonths= $groupSettings['months'][$j] ?? [
                    'hold'=> $hold,
                    'release'=> $release,
                    'fees'=> $fees,
                    'score'=> ($hold+1)*10
                ];

                $perfixName="group_settings[{$i}][months][{$j}]";

                $hold_name= $perfixName."[hold]";
                $release_name= $perfixName."[release]";
                $fees_name= $perfixName."[fees]";
                $score_name= $perfixName."[score]";

                $release= $j == 0 ? 0 : (float) $groupSettingsMonths['release'] ?? "";

                $hold= $i == $j+1 ? 0 : (float) $groupSettingsMonths['hold'] ?? "";

                $fees= (float) $groupSettingsMonths['fees'] ?? "";

                $score= (float) $groupSettingsMonths['score'] ?? "";
                //$score= ($hold * $i + 1 )*10;

            @endphp
            <legend  class="w-auto">{{ __('settings.month') . ($j+1) }}</legend>
            <div class="form-row">

                <div class="form-group col-lg-3">
                    <label>{{ __('settings.hold') }}</label>
                    <div class="input-group">
                        <input type="number" min="0" max="{{ $i == $j+1 ? '0' : '100' }}" {{ $i == $j+1 ? 'readonly' : '' }} class="form-control {{ $errors->has('group_settings') && $hold < 0  ? 'is-invalid' : '' }}" name='{{ $hold_name }}' placeholder="{{ __('settings.hold') }}" value='{{ $hold }}'/>
                        <div class="input-group-append">
                            <div class="input-group-text">%</div>
                        </div>
                    </div>
                </div>

                <div class="form-group col-lg-3">
                    <label>{{ __('settings.release') }}</label>
                    <div class="input-group">
                        <input type="number" min="0" max="{{ 0 == $j ? '0' : '100' }}" {{ 0 == $j ? 'readonly' : '' }} class="form-control {{ $errors->has('group_settings') && $release < 0  ? 'is-invalid' : '' }}" name='{{ $release_name }}' placeholder="{{ __('settings.release') }}" value='{{ $release }}'/>
                        <div class="input-group-append">
                            <div class="input-group-text">%</div>
                        </div>
                    </div>
                </div>

                <div class="form-group col-lg-3">
                    <label>{{ __('settings.fees') }}</label>
                    <div class="input-group">
                        <input type="number" class="form-control {{ $errors->has('group_settings') && $fees < 0 ? 'is-invalid' : '' }}" name='{{ $fees_name }}' placeholder="{{ __('settings.fees') }}" value='{{ $fees }}'/>
                        <div class="input-group-append">
                            <div class="input-group-text">%</div>
                        </div>
                    </div>
                    <small id="emailHelp" class="form-text text-muted"> {{ __('settings.fees_hint') }} </small>
                </div>

                <div class="form-group col-lg-3">
                    <label>{{ __('settings.minimum_score') }}</label>
                    <input type="number" class="form-control {{ $errors->has('group_settings') && $score < 0 ? 'is-invalid' : '' }}" name='{{ $score_name }}' placeholder="{{ __('settings.score') }}" value='{{ $score }}'/>
                </div>

            </div>
        </fieldset>
        @endfor
    </div>
    @endfor
</div>
