@extends('layouts.app')
@section('title', __('vrc::lang.check_in'))

@section('content')
<section class="content-header"><h1>@lang('vrc::lang.check_in')</h1></section>

<section class="content">
    @component('components.widget', ['class' => 'box-primary'])
        {!! Form::open(['route' => 'vrc.repair-orders.store', 'id' => 'vrc_ro_form']) !!}
        <div class="row">
            <div class="col-md-4"><div class="form-group">
                {!! Form::label('erp_customer_id', __('vrc::lang.customer') . ':*') !!}
                {!! Form::select('erp_customer_id', $customers, null, ['class' => 'form-control select2 vrc-ro-customer', 'required', 'placeholder' => __('messages.please_select'), 'data-vehicles-url' => url('vrc/customers/__ID__/vehicles')]) !!}
            </div></div>
            <div class="col-md-4"><div class="form-group">
                {!! Form::label('vehicle_id', __('vrc::lang.vehicle') . ':*') !!}
                {!! Form::select('vehicle_id', [], null, ['class' => 'form-control select2 vrc-ro-vehicle', 'required', 'placeholder' => __('messages.please_select')]) !!}
            </div></div>
            <div class="col-md-2"><div class="form-group">
                {!! Form::label('priority', __('vrc::lang.priority') . ':') !!}
                {!! Form::select('priority', ['low'=>__('vrc::lang.prio_low'),'normal'=>__('vrc::lang.prio_normal'),'high'=>__('vrc::lang.prio_high'),'urgent'=>__('vrc::lang.prio_urgent')], 'normal', ['class' => 'form-control']) !!}
            </div></div>
            <div class="col-md-2"><div class="form-group">
                {!! Form::label('bay_id', __('vrc::lang.bay') . ':') !!}
                {!! Form::select('bay_id', [''=>'--'] + $bays, null, ['class' => 'form-control']) !!}
            </div></div>
        </div>
        <div class="row">
            <div class="col-md-3"><div class="form-group">
                {!! Form::label('odometer_in', __('vrc::lang.odometer') . ':') !!}
                {!! Form::number('odometer_in', null, ['class' => 'form-control', 'min' => 0]) !!}
            </div></div>
            <div class="col-md-3"><div class="form-group">
                {!! Form::label('fuel_level_in', __('vrc::lang.fuel_level') . ' (%):') !!}
                {!! Form::number('fuel_level_in', null, ['class' => 'form-control', 'min' => 0, 'max' => 100]) !!}
            </div></div>
            <div class="col-md-3"><div class="form-group">
                {!! Form::label('promised_at', __('vrc::lang.promised') . ':') !!}
                {!! Form::input('datetime-local', 'promised_at', null, ['class' => 'form-control']) !!}
            </div></div>
            @if(!empty($locations))
            <div class="col-md-3"><div class="form-group">
                {!! Form::label('location_id', __('business.business_location') . ':') !!}
                {!! Form::select('location_id', $locations, null, ['class' => 'form-control', 'placeholder' => __('messages.please_select')]) !!}
            </div></div>
            @endif
        </div>
        <div class="form-group">
            {!! Form::label('customer_concerns', __('vrc::lang.customer_concerns') . ':') !!}
            {!! Form::textarea('customer_concerns', null, ['class' => 'form-control', 'rows' => 3]) !!}
        </div>

        <div class="form-group" style="border:1px solid #eee;border-radius:6px;padding:10px;background:#fafafa;">
            <label style="font-weight:400;">
                <input type="checkbox" name="is_rework" value="1" id="vrc-is-rework"> @lang('vrc::lang.is_rework')
            </label>
            <div id="vrc-rework-fields" style="display:none;margin-top:8px;">
                <div class="row">
                    <div class="col-md-5"><div class="form-group">
                        {!! Form::label('rework_of_id', __('vrc::lang.original_repair_order') . ':') !!}
                        {!! Form::select('rework_of_id', [''=>'--'], null, ['class' => 'form-control', 'id' => 'vrc-rework-of', 'data-prior-url' => url('vrc/vehicles/__ID__/repair-orders')]) !!}
                    </div></div>
                    <div class="col-md-7"><div class="form-group">
                        {!! Form::label('rework_reason', __('vrc::lang.reason') . ':') !!}
                        {!! Form::text('rework_reason', null, ['class' => 'form-control', 'maxlength' => 255]) !!}
                    </div></div>
                </div>
            </div>
        </div>

        <button type="submit" class="btn btn-primary"><i class="fa fa-sign-in"></i> @lang('vrc::lang.check_in')</button>
        {!! Form::close() !!}
    @endcomponent
</section>
@endsection

@section('javascript')
<script>
$(function () {
    $('.vrc-ro-customer').on('change', function () {
        var url = $(this).data('vehicles-url').replace('__ID__', $(this).val());
        var $veh = $('.vrc-ro-vehicle'); $veh.html('<option value="">--</option>');
        if (!$(this).val()) return;
        $.get(url, function (vehicles) {
            $.each(vehicles, function (id, label) { $veh.append($('<option>', { value: id, text: label })); });
            $veh.trigger('change.select2');
        });
    });

    // Rework: reveal fields and load this vehicle's prior repair orders.
    $('#vrc-is-rework').on('change', function () {
        $('#vrc-rework-fields').toggle(this.checked);
    });
    $('.vrc-ro-vehicle').on('change', function () {
        var $sel = $('#vrc-rework-of'); $sel.html('<option value="">--</option>');
        var vid = $(this).val();
        if (!vid) return;
        $.get($sel.data('prior-url').replace('__ID__', vid), function (orders) {
            $.each(orders, function (id, label) { $sel.append($('<option>', { value: id, text: label })); });
        });
    });
    $('#vrc_ro_form').on('submit', function (e) {
        e.preventDefault();
        var $f = $(this), $b = $f.find('button[type=submit]').prop('disabled', true);
        $.post($f.attr('action'), $f.serialize(), function (resp) {
            if (resp.success && resp.redirect) { window.location = resp.redirect; }
            else { toastr.error(resp.msg || 'Error'); $b.prop('disabled', false); }
        }).fail(function (xhr) {
            $b.prop('disabled', false);
            if (xhr.status === 422) { Object.values(xhr.responseJSON.errors).forEach(function (e) { toastr.error(e[0]); }); }
            else { toastr.error('Error'); }
        });
    });
});
</script>
@endsection
