@extends('layouts.app')

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

@section('content')

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

<section class="content">
    {!! Form::open(['route' => 'bms.booking-policy.update', 'method' => 'PUT', 'id' => 'bms-policy-form']) !!}

    {{-- Cancellation policy --}}
    @component('components.widget', ['class' => 'box-primary', 'title' => __('bms::lang.cancellation_policy')])
        <div class="row">
            <div class="col-sm-4">
                <div class="checkbox">
                    <label><input type="checkbox" name="enabled" value="1" {{ ($policy['enabled'] ?? true) ? 'checked' : '' }}> @lang('bms::lang.policy_enabled')</label>
                </div>
            </div>
            <div class="col-sm-4">
                <div class="checkbox">
                    <label><input type="checkbox" name="reschedule_uses_policy" value="1" {{ ($policy['reschedule_uses_policy'] ?? true) ? 'checked' : '' }}> @lang('bms::lang.reschedule_uses_policy')</label>
                </div>
            </div>
            <div class="col-sm-4">
                <div class="form-group">
                    <label>@lang('bms::lang.default_penalty_percent')</label>
                    <input type="number" min="0" max="100" step="0.5" name="default_penalty_percent" class="form-control"
                        value="{{ $policy['default_penalty_percent'] ?? 0 }}">
                </div>
            </div>
        </div>

        <p class="text-muted">@lang('bms::lang.tiers_help')</p>
        <table class="table table-bordered" id="tier-table" style="max-width:520px">
            <thead><tr>
                <th>@lang('bms::lang.days_before')</th><th>@lang('bms::lang.penalty_percent')</th><th style="width:40px"></th>
            </tr></thead>
            <tbody>
                @php $tiers = $policy['tiers'] ?? []; @endphp
                @forelse($tiers as $t)
                    <tr>
                        <td><input type="number" min="1" name="tier_days[]" class="form-control" value="{{ $t['days_before'] }}"></td>
                        <td><input type="number" min="0" max="100" step="0.5" name="tier_pct[]" class="form-control" value="{{ $t['penalty_percent'] }}"></td>
                        <td><button type="button" class="btn btn-xs btn-danger row-del">&times;</button></td>
                    </tr>
                @empty
                    <tr>
                        <td><input type="number" min="1" name="tier_days[]" class="form-control" value="7"></td>
                        <td><input type="number" min="0" max="100" step="0.5" name="tier_pct[]" class="form-control" value="25"></td>
                        <td><button type="button" class="btn btn-xs btn-danger row-del">&times;</button></td>
                    </tr>
                @endforelse
            </tbody>
        </table>
        <button type="button" class="btn btn-default btn-sm" id="add-tier"><i class="fa fa-plus"></i> @lang('bms::lang.add_tier')</button>
    @endcomponent

    {{-- Payment schedule template --}}
    @component('components.widget', ['class' => 'box-primary', 'title' => __('bms::lang.payment_schedule_template')])
        <table class="table table-bordered" id="sched-table" style="max-width:760px">
            <thead><tr>
                <th>@lang('bms::lang.deposit') / @lang('bms::lang.installment')</th>
                <th style="width:120px">@lang('bms::lang.kind')</th>
                <th style="width:90px">%</th>
                <th style="width:140px">@lang('bms::lang.offset_days')</th>
                <th style="width:40px"></th>
            </tr></thead>
            <tbody>
                @foreach($schedule as $s)
                    <tr>
                        <td><input type="text" name="sched_label[]" class="form-control" value="{{ $s['label'] ?? '' }}"></td>
                        <td>
                            <select name="sched_kind[]" class="form-control">
                                @foreach(['deposit','installment','balance'] as $k)
                                    <option value="{{ $k }}" {{ ($s['kind'] ?? 'installment') === $k ? 'selected' : '' }}>@lang('bms::lang.'.$k)</option>
                                @endforeach
                            </select>
                        </td>
                        <td><input type="number" min="0" max="100" step="0.5" name="sched_pct[]" class="form-control" value="{{ $s['percent'] ?? 0 }}"></td>
                        <td><input type="number" name="sched_offset[]" class="form-control" value="{{ $s['offset_days'] ?? 0 }}"></td>
                        <td><button type="button" class="btn btn-xs btn-danger row-del">&times;</button></td>
                    </tr>
                @endforeach
            </tbody>
        </table>
        <button type="button" class="btn btn-default btn-sm" id="add-sched"><i class="fa fa-plus"></i> @lang('bms::lang.add_row')</button>
        <p class="text-muted" style="margin-top:8px;">@lang('bms::lang.schedule_help')</p>
    @endcomponent

    {{-- Cancellation reasons --}}
    @component('components.widget', ['class' => 'box-primary', 'title' => __('bms::lang.cancellation_reasons')])
        <table class="table table-bordered" id="reason-table" style="max-width:760px">
            <thead><tr>
                <th>@lang('bms::lang.code')</th><th>@lang('bms::lang.title') (EN)</th><th>@lang('bms::lang.title') (AR)</th><th style="width:40px"></th>
            </tr></thead>
            <tbody>
                @forelse(($reasons ?? []) as $r)
                    <tr>
                        <td><input type="text" name="reason_code[]" class="form-control" value="{{ $r['code'] ?? '' }}"></td>
                        <td><input type="text" name="reason_en[]" class="form-control" value="{{ $r['en'] ?? '' }}"></td>
                        <td><input type="text" name="reason_ar[]" class="form-control" value="{{ $r['ar'] ?? '' }}" dir="rtl"></td>
                        <td><button type="button" class="btn btn-xs btn-danger row-del">&times;</button></td>
                    </tr>
                @empty
                    <tr>
                        <td><input type="text" name="reason_code[]" class="form-control"></td>
                        <td><input type="text" name="reason_en[]" class="form-control"></td>
                        <td><input type="text" name="reason_ar[]" class="form-control" dir="rtl"></td>
                        <td><button type="button" class="btn btn-xs btn-danger row-del">&times;</button></td>
                    </tr>
                @endforelse
            </tbody>
        </table>
        <button type="button" class="btn btn-default btn-sm" id="add-reason"><i class="fa fa-plus"></i> @lang('bms::lang.add_row')</button>
    @endcomponent

    {{-- Deposit gate --}}
    @component('components.widget', ['class' => 'box-primary'])
        <div class="checkbox">
            <label><input type="checkbox" name="enforce_deposit_gate" value="1" {{ $enforceDeposit ? 'checked' : '' }}> @lang('bms::lang.enforce_deposit_gate')</label>
        </div>
        <button type="submit" class="tw-dw-btn tw-dw-btn-primary tw-text-white">
            <i class="fa fa-save"></i> @lang('messages.update')
        </button>
    @endcomponent

    {!! Form::close() !!}
</section>

@push('scripts')
<script>
$(function () {
    function rowFrom(tableId) {
        var $tr = $('#' + tableId + ' tbody tr:first').clone();
        $tr.find('input').val('');
        return $tr;
    }
    $('#add-tier').on('click', function () { $('#tier-table tbody').append(rowFrom('tier-table')); });
    $('#add-sched').on('click', function () { $('#sched-table tbody').append(rowFrom('sched-table')); });
    $('#add-reason').on('click', function () { $('#reason-table tbody').append(rowFrom('reason-table')); });
    $(document).on('click', '.row-del', function () {
        var $tb = $(this).closest('tbody');
        if ($tb.find('tr').length > 1) { $(this).closest('tr').remove(); }
        else { $(this).closest('tr').find('input').val(''); }
    });
});
</script>
@endpush

@endsection
