<x-app-layout>

<div class="row mt-2">
    <div class="col">
        <div class="card">
            <div class="card-header">
                <h3 class="page-title">{{ __('Reports Ticket') }}</h3>
            </div>
            <div class="card-body">
                <form method="get">
                    <div class="row">
                        <div class="col-3 form-group">
                            <label class="control-label" for="from">{{ __('From') }}</label>
                            <input type="date"  name="from" for="from" class="form-control date" value={{ old('from', Request::get('from', date('Y-m-d')))}}>
                        </div>
                        <div class="col-3 form-group">
                            <label class="control-label" for="to">{{ __('To') }}</label>
                            <input type="date"  name="to" for="to" class="form-control date" value={{ old('to', Request::get('to', date('Y-m-d')))}}>
                        </div>
                        @if(auth()->user()->office->is_admin==1)
                        <div class="col-3 form-group">
                            <label class="control-label" for="o">{{ __('Office') }}</label>
                            <select name="office_id" for="o" class="form-control">
                                @foreach(App\Models\Office::all() as $office)
                                    <option value="{{ $office->id }}" @if($office->id ===intval(Request::get('office_id'))) selected @endif>
                                        {{ $office->name }}
                                    </option>
                                @endforeach
                            </select>
                        </div>
                        @endif
                        <div class="col-3">
                            <label class="control-label">&nbsp;</label><br>
                            <button class="btn btn-primary" type="submit">{{ __('filter by date') }}</button>
                        </div>
                    </div>
                </form>
            </div>
        </div>

    </div>
</div>

<div class="card">
    <div class="card-header">
        <a href="{{ route('ticket_reports_export',Request::all()) }}" class="btn btn-primary float-right"> {{ __('Export Excel') }}</a>
    </div>

    <div class="card-body">
        <div class="row">
             <table class="table table-bordered table-hover">
                 <tr>
                    <th>{{ __('Date') }}</th>
                    <th>{{ __('Booking Number') }}</th>
                    <th>{{ __('Ticket Number') }}</th>
                    <th>{{ __('Passenger Name') }}</th>
                    <th>{{ __('Office') }}</th>
                    <th>{{ __('balance') }}</th>
                    <th>{{ __('Status') }}</th>
                    <th>{{ __('Fare') }}</th>
                 </tr>
               
                    @foreach ($tickets as $ticket)
                                @foreach($ticket->transactions as $transaction)
                                <tr class="{{ statusBg($ticket->ticket_status) }}">
                                    <td>{{ $transaction->created_at->format('Y/m/d') }}</td>
                                    <td>{{ $ticket->segment->booking->number }}</td>
                                    <td>{{ $ticket->ticket_number }}</td>
                                    <td>{{ $ticket->full_name}}</td>
                                    <td>{{ $ticket->segment->booking->office->name }}</td>
                                     <?php 
                                           $amount=App\Models\Transaction::where('ticket_id', optional($transaction->ticket)->id)->where('amount','!=',0)->first();
                                          $commission=$transaction->account->office->booking_commission /100;
                                          $price=$ticket->price-( $ticket->price *$commission);
                                          
                                       
                                           
                                      ?>
                                   @if($transaction->amount !=0)
                                   <td>{{formatPrice($transaction->balance )}}</td>
                                   @else
                                   <td>{{ formatPrice($transaction->balance) }}</td>
                                   @endif
                                   
                                    @if($ticket->ticket_status=="R"&& $transaction->type=="expense")
                                     <td>O </td>
                                      @elseif($ticket->ticket_status=="V"&& $transaction->type=="expense")
                                       <td>O </td>
                                    
                                       
                                      @else
                                      <td>{{ $ticket->ticket_status }}</td>
                                    @endif
                                    <?php 
                                           $amount=App\Models\Transaction::where('ticket_id', optional($transaction->ticket)->id)->where('amount','!=',0)->first();
                                           
                                        
                                       ?>
                                        
                                    @if($ticket->ticket_status =='R'  || $ticket->ticket_status=="V" && $transaction->type!="expense")
                                      @if($ticket->hasRetour())
                                              @if($transaction->amount==0)
                                              
                                         <td>{{FormatPrice($price *-1)  }}</td>
                                               @else 
                                               <td>{{ formatPrice($transaction->amount *-1 )}}</td>
                                               @endif

                                       @elseif($transaction->amount==0)
                                        
                                         <td>{{FormatPrice($price *-1  )  }}</td>
                                       @else
                                       <td>{{ formatPrice($transaction->amount*-1 )}}</td>

                                      @endif
                                    @else
                                    <td>{{ formatPrice($transaction->amount*-1 )}}</td>
                                    @endif
                                    
                                 
                                    
                              
                                </tr>   
                                @endforeach
                    @endforeach
             </table>
             
        </div>
        <div>
            {{$tickets->withQueryString()->links()}}
        </div>



    </div>
</div>

<x-slot name="after_script">
<script>
                    var app = new Vue({
                    el: '#app',
                    mounted:function(){
                       this.$root.load = false;
                    }
                })
   
</script>
</x-slot>
</x-app-layout>