@extends('layouts.app')

@section('title', __('bms::lang.matching_settings'))

@section('content')

<section class="content-header">
    <h1>@lang('bms::lang.matching_settings')
        <small>@lang('bms::lang.matching')</small>
    </h1>
</section>

<section class="content">
    {!! Form::open(['route' => 'bms.matching.update', 'method' => 'PUT']) !!}
    <div class="row">
        <div class="col-md-8">
            @component('components.widget', ['class' => 'box-primary', 'title' => __('bms::lang.weights')])
                <p class="text-muted">
                    Score = Σ(weight × criterion match), normalised to 100%. A hall is a
                    <strong>match</strong> when its score ≥ the threshold; halls below it are
                    listed last with their failed criteria shown as explicit reasons.
                </p>

                <table class="table table-bordered">
                    <thead>
                        <tr>
                            <th style="width:60%">@lang('bms::lang.weights')</th>
                            <th>%</th>
                        </tr>
                    </thead>
                    <tbody>
                        @php $weights = $setting->weights ?? []; @endphp
                        @foreach($criteria as $key => $label)
                            <tr>
                                <td><label class="tw-font-normal" style="margin:0">{{ $label }}</label></td>
                                <td>
                                    {!! Form::number("weights[$key]", $weights[$key] ?? 0, ['class' => 'form-control input-sm bms-weight', 'min' => 0, 'max' => 100]) !!}
                                </td>
                            </tr>
                        @endforeach
                        <tr class="bg-gray-light">
                            <th class="text-right">Total</th>
                            <th><span id="bms-weight-total">0</span></th>
                        </tr>
                    </tbody>
                </table>
                <small class="text-muted">
                    Weights don't need to total 100 — scores are normalised. Higher weight = the
                    criterion counts more toward a match.
                </small>
            @endcomponent
        </div>

        <div class="col-md-4">
            @component('components.widget', ['class' => 'box-solid', 'title' => __('bms::lang.threshold')])
                <div class="form-group">
                    {!! Form::label('threshold', __('bms::lang.threshold')) !!}
                    <div class="input-group">
                        {!! Form::number('threshold', $setting->threshold, ['class' => 'form-control', 'min' => 0, 'max' => 100, 'required']) !!}
                        <span class="input-group-addon">%</span>
                    </div>
                    <small class="text-muted">Default 60%. Halls at or above this score are shown as matches.</small>
                </div>
                <button type="submit" class="tw-dw-btn tw-dw-btn-primary tw-text-white btn-block">
                    <i class="fa fa-save"></i> @lang('messages.update')
                </button>
            @endcomponent
        </div>
    </div>
    {!! Form::close() !!}
</section>

@endsection

@section('javascript')
<script>
$(function () {
    function recalc() {
        var total = 0;
        $('.bms-weight').each(function () { total += parseInt($(this).val() || 0, 10); });
        $('#bms-weight-total').text(total);
    }
    $(document).on('input', '.bms-weight', recalc);
    recalc();
});
</script>
@endsection
