@extends('layouts.app')
@section('title', ($estimate->number ?: 'Estimate'))
@section('content')
@php $base = url('vrc/estimates/'.$estimate->id); $editable = in_array($estimate->status, ['draft','sent']); @endphp
<section class="content-header">
    <h1>{{ $estimate->number ?: ('EST #'.$estimate->id) }}
        <span class="label label-info">{{ __('vrc::lang.est_status_'.$estimate->status) }}</span>
        <a href="{{ route('vrc.estimates.pdf', $estimate->id) }}" target="_blank" class="btn btn-default btn-sm pull-right"><i class="fa fa-file-pdf-o"></i> PDF</a>
    </h1>
</section>

<section class="content" data-base="{{ $base }}">
    <div class="row">
        <div class="col-md-8">
            @component('components.widget', ['class' => 'box-primary', 'title' => __('vrc::lang.line_items')])
                @if($editable)
                @can('vrc.estimate.manage')
                <form class="form-inline vrc-est-add" style="margin-bottom:10px;">
                    <select name="kind" class="form-control input-sm"><option value="service">@lang('vrc::lang.service')</option><option value="part">@lang('vrc::lang.part')</option></select>
                    <input type="text" name="description" class="form-control input-sm" placeholder="@lang('vrc::lang.description')" required>
                    <input type="number" name="qty" class="form-control input-sm" style="width:70px;" value="1" step="0.5" min="0">
                    <input type="number" name="unit_price" class="form-control input-sm" style="width:100px;" value="0" step="0.01" min="0">
                    <select name="tax_rate_id" class="form-control input-sm">{!! collect($taxRates)->map(fn($n,$i)=>"<option value='$i'>$n</option>")->implode('') !!}</select>
                    <button class="btn btn-sm btn-primary"><i class="fa fa-plus"></i> @lang('messages.add')</button>
                </form>
                @endcan
                @endif
                <table class="table table-condensed table-bordered">
                    <thead><tr><th>@lang('vrc::lang.description')</th><th>@lang('vrc::lang.qty')</th><th>@lang('vrc::lang.price')</th><th>@lang('vrc::lang.line_total')</th><th></th></tr></thead>
                    <tbody>
                    @foreach ($estimate->items as $it)
                        <tr><td>{{ $it->description }} <small class="text-muted">({{ __('vrc::lang.'.$it->kind) }})</small></td><td>{{ $it->qty }}</td><td>{{ $it->unit_price }}</td><td>{{ $it->line_total }}</td>
                            <td>@if($editable)@can('vrc.estimate.manage')<button class="btn btn-xs btn-danger vrc-est-del" data-item="{{ $it->id }}"><i class="fa fa-trash"></i></button>@endcan @endif</td></tr>
                    @endforeach
                    </tbody>
                </table>
            @endcomponent
        </div>
        <div class="col-md-4">
            @component('components.widget', ['class' => 'box-success', 'title' => __('vrc::lang.totals')])
                <table class="table table-condensed">
                    <tr><th>@lang('vrc::lang.subtotal')</th><td class="text-right">{{ $estimate->subtotal }}</td></tr>
                    <tr><th>@lang('vrc::lang.discount')</th><td class="text-right">{{ $estimate->discount_total }}</td></tr>
                    <tr><th>@lang('vrc::lang.tax')</th><td class="text-right">{{ $estimate->tax_total }}</td></tr>
                    <tr><th>@lang('vrc::lang.grand_total')</th><td class="text-right"><strong>{{ $estimate->grand_total }}</strong></td></tr>
                </table>
            @endcomponent
            @can('vrc.estimate.manage')
            <div class="box box-default"><div class="box-body">
                @if($estimate->status === 'draft' || $estimate->status === 'sent')
                    <button class="btn btn-primary btn-block" id="vrc-est-send"><i class="fa fa-paper-plane"></i> @lang('vrc::lang.send_estimate')</button>
                @endif
                @if(in_array($estimate->status, ['draft','sent']))
                    @can('vrc.estimate.approve')<button class="btn btn-success btn-block" id="vrc-est-approve"><i class="fa fa-check"></i> @lang('vrc::lang.record_approval')</button>@endcan
                    <button class="btn btn-default btn-block" id="vrc-est-decline"><i class="fa fa-times"></i> @lang('vrc::lang.record_decline')</button>
                @endif
                @if($estimate->approval_token)
                    <hr><small class="text-muted">@lang('vrc::lang.approval_link'):</small>
                    <input type="text" class="form-control input-sm" readonly value="{{ route('vrc.portal.estimate', $estimate->approval_token) }}">
                @endif
            </div></div>
            @endcan
        </div>
    </div>
</section>
@endsection
@section('javascript')
<script>
$(function () {
    var base = $('section.content[data-base]').data('base'), token = $('meta[name=csrf-token]').attr('content');
    function obj($f){ var o={}; $f.serializeArray().forEach(function(f){o[f.name]=f.value;}); o._token=token; return o; }
    $('.vrc-est-add').on('submit', function (e) { e.preventDefault(); $.post(base + '/items', obj($(this)), function(r){ r.success?location.reload():toastr.error(r.msg); }); });
    $(document).on('click', '.vrc-est-del', function () {
        $.ajax({ url: base + '/items/' + $(this).data('item'), method: 'DELETE', data: { _token: token } }).done(function(r){ r.success?location.reload():toastr.error(r.msg); });
    });
    $('#vrc-est-send').on('click', function () { $.post(base + '/send', { _token: token }, function (r) { r.success ? (toastr.success(r.msg), location.reload()) : toastr.error(r.msg); }); });
    $('#vrc-est-approve').on('click', function () { $.post(base + '/approve', { method: 'in_person', _token: token }, function (r) { r.success ? (toastr.success(r.msg), location.reload()) : toastr.error(r.msg); }); });
    $('#vrc-est-decline').on('click', function () { var reason = prompt('{{ __('vrc::lang.reason') }}:')||''; $.post(base + '/decline', { method: 'in_person', reason: reason, _token: token }, function (r) { r.success ? (toastr.success(r.msg), location.reload()) : toastr.error(r.msg); }); });
});
</script>
@endsection
