@extends('layouts.admin')

@section('title', 'كشف الحساب  ')
@section('content-header', 'كشف الحساب  ')


 

@section('content-actions')

@endsection

@section('content')

        <?php
        if(isset($user_id)) { ?>
        <h3> 
            User : 
        <?php 
            echo \App\Models\User::where('id',$user_id)->first()->first_name." ";
            echo \App\Models\User::where('id',$user_id)->first()->last_name;
        ?>

  </h3>

       <?php      
        }
        ?>

  

<div class="card">
    <div class="card-body">
        <div class="row" style="display: none;">
            <div class="col-md-7"></div>
            <div class="col-md-5">
                <form action="{{route('orders.index')}}">
                    <div class="row">
                        <div class="col-md-5">
                            <input type="date" name="start_date" class="form-control" value="{{request('start_date')}}" />
                        </div>
                        <div class="col-md-5">
                            <input type="date" name="end_date" class="form-control" value="{{request('end_date')}}" />
                        </div>
                        <div class="col-md-2">
                            <button class="btn btn-outline-primary" type="submit">Submit</button>
                        </div>
                    </div>
                </form>
            </div>
        </div>

        <table class="table">
            <thead>


      @if(Config::get('app.locale') == "ar")
                <tr>
                    <th>#</th>
                    <th> التاريخ والزمن</th>
                    <th> العميل  </th>
                    <th>طريقة الدفع </th>
                    <th>المبلغ </th>
                    <th>ملاحظات</th>

                </tr>

      @else


      @endif


            </thead>
            <tbody>

                
            <?php 
            $cust_id = Request::segment('2');

            $the_customer = \App\Models\Customer::where('id',$cust_id)->first();
            $customers_payments = \App\customers_payments::where('customer_id',$cust_id)
            ->where('status','not_done')
            ->Orderby('id','desc')
            ->get();

            $paid = 0;
            $amount = 0;
		
            $customer_orders = \App\Models\Order::where('customer_id',$cust_id)
            ->where('order_status','active')
            ->where('status','not_done')
            ->get();

            ?>

            @foreach($customer_orders as $order)

            <tr>
                    <td>  
<a href="#" onclick="window.open('<?php echo url('admin/ticket/'); ?>/<?php echo $order->id; ?>','','width=400,height=500');">{{$order->id}}</a>
                    </td>
                    <td>  {{ $order->created_at }}</td>
                   
                    <td>  فاتورة مبيعات   {{$order->id}} -  {{ \App\Models\Customer::find($cust_id)->first_name . " ".\App\Models\Customer::find($cust_id)->last_name }}
                    </td>
                    <td>
                    </td>
                    
                    <td style="color: red;"> <?php 
		
		$order_items = \App\Models\OrderItem::where('order_id',$order->id)->get();
		
		$invoice_amount = 0;

		foreach($order_items as $order_item) {
			$amount = $amount + ($order_item->price * $order_item->quantity);
			$invoice_amount = $invoice_amount + ($order_item->price * $order_item->quantity);
		}

		echo number_format($invoice_amount,0);


                    


                     ?></td>
                    <td>  
                        <?php $payment = \App\Models\Payment::where('order_id',$order->id)->first(); ?>
                        المدفوع  بنكك  {{ number_format($payment->bankak,0) }}  -  
                        المدفوع   كاش  {{ number_format($payment->cash,0) }}  -  
                        المدفوع   شيك    {{ number_format($payment->shake,0) }}   


                    </td>

            </tr>


            @endforeach


            @foreach($customers_payments as $r)

                <tr>
                    <td>  {{ $r->id }}</td>
                    <td>  {{ $r->created_at }}</td>
                    <td> سداد  - {{ \App\Models\Customer::find($cust_id)->first_name . " ".\App\Models\Customer::find($cust_id)->last_name }}</td>
                    <td>  

                    <?php

                    if($r->payment_method == "shake") {
                        echo "شيك ";
                    }   

                    if($r->payment_method == "cash") {
                        echo " كاش  ";
                    }   
                    if($r->payment_method == "bankak") {
                        echo "بنكك ";
                    }   


                    ?>

                    </td>
                    <td style="color: green;">  
                        <?php $paid = $paid + $r->amount; ?>
                        {{ number_format($r->amount,0) }}</td>
                    <td>  {{ $r->notes }}</td>
   

     
                </tr>
                @endforeach
            </tbody>
      
        </table>

        <center>
        <h2> الاجمالي  :  {{number_format($amount,0)}}  -   المدفوع  :  {{number_format($paid,0)}}  -     المتبقي   :  {{number_format($amount - $paid,0)}}</h2>
        </center>

    </div>
</div>
@endsection

