@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      
        }
        ?>

<center>
    <a href="{{route('customers.create')}}" class="btn btn-primary">اضافة عميل  جديد</a>
</cemter>
<div class="card">
    <div class="card-body">
        <div class="row">
            <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 style="display: none;">البائع</th>
                    <th>الاجمالي</th>
                    <th>المطلوب بعد الخصم </th>
                    <th>المدفوع</th>
                    <th>المتبقي</th>

                    <th>الحالة</th>
                    <th>-</th>
                    <th>التاريخ والزمن</th>
                </tr>

      @else
                <tr>
                    <th>ID</th>
                    <th>Customer Name</th>
                    <th>Invoices</th>
                    <th>User</th>
                    <th>Total</th>
                    <th>Paid</th>
                    <th>Reamining</th>

                    <th>Status</th>
                    <th>To Pay</th>
                    <th>Created At</th>
                </tr>

      @endif


            </thead>
            <tbody>

                <?php 
                
                $customers = \App\Models\Customer::where('id','<>','4')
                ->where('type','sale')
                ->get();

                ?>

                @foreach ($customers as $r_customer)



                <?php       

                   $total = 0;
                   $the_discount = 0;

                   $order = \App\Models\Order::where('customer_id',$r_customer->id)
                   ->where('id','>','1')
                   ->where('status','not_done')
                   ->where('order_status','active')
                   ->get();
                ?>


                @if($order->count() > 0) 
                <tr>
                @else
                <tr style="display: none;">

                @endif
                    <td>{{$r_customer->id}}</td>
                    <td>
                        <a href="{{url('customer_payments')}}/{{$r_customer->id}}"> 
                        {{$r_customer->first_name}} {{$r_customer->last_name}}

                         </td>



                    <?php 
                    

                        $mm = array();

                        foreach($order as $r2) {


                        array_push($mm, $r2->id);


                        if($order->count() > 0) {


                        $the_orders = \App\Models\Order::where('id',$r2->id)->get();

                        foreach($the_orders as $the_order) {
                           $the_discount = $the_discount + $the_order->discount;
                        }




                        $order_items = \App\Models\OrderItem::where('order_id',$r2->id)
                        ->where('status','not_done')
                        ->get();


                            foreach($order_items as $r) {
                            $price = $r->price * $r->quantity;
                            $total = $total + $price;
                            }

                        }





                        }

                    
                    ?>


       
                    <td> 


                        <?php 

                        foreach($mm as $key=>$value) { 
                        ?>
                        <a href="#" onclick="window.open('<?php echo url('admin/ticket'); ?>/<?php echo $value; ?>','','width=400,height=500');">

                        {{$value}}
                        </a>

                    <?php } ?>


                    </td>

                

                    <td style="display: none;">

                        @if($order->count() > 0) 
                        
                        {{$r2->first()->user->first_name}} {{$r2->first()->user->last_name}}
                        
                        @endif

                         </td>

                    <td>{{ config('settings.currency_symbol') }} {{number_format($total,0)}}</td>

                    <td>{{ config('settings.currency_symbol') }} {{number_format($total - $the_discount,0)}}</td>

                    <td>{{ config('settings.currency_symbol') }} {{number_format($r_customer->paid,0)}}</td>
       


                    <td >{{ config('settings.currency_symbol') }} {{number_format($total - $r_customer->paid - $the_discount,0)}}

                        <input type="hidden" id="remaining<?php echo $r_customer->id; ?>" value="{{number_format($total - $r_customer->paid - $the_discount,0)}}" name="">
                    </td>


                    <td id="payment_status<?php echo $r_customer->id; ?>" style="color: red; font-size: 13px; font-weight: bolder;" >


  <img style="display: none;" src="" onerror=" if(document.getElementById('remaining<?php echo $r_customer->id; ?>').value <= 0) {
                            document.getElementById('payment_status<?php echo $r_customer->id; ?>').innerHTML = ' اكتمل الدفع ';
                        }
                        else
                        {
                            document.getElementById('payment_status<?php echo $r_customer->id; ?>').innerHTML = 'غير مدفوع';
                        }

                   ">

                    </td>

                    <td>

                        <a onclick="return confirm('<?php echo __('دقعيه جديده ؟') ?>');" href="{{url('customers/paid')}}/{{$r_customer->id}}">
                          <span class="badge badge-info"><i class="fas fa-plus"></i> دفعيه جديده </span>
                        </a>


                        <a onclick="

                        if(document.getElementById('remaining<?php echo $r_customer->id; ?>').value <= 0)
                        {
                        return confirm('<?php echo __('هل انت متأكد أنه تم الدفع كامل ؟') ?>');
                        }
                        else
                        {
                            alert('ما زال هناك مبلغ متبقي  على العميل , الرجاء اضافته ثم عمل اكتمل الدفع ');
                            return false;
                        }



                        " href="{{url('done_pay')}}/{{$r_customer->id}}">
                          <span class="badge badge-success"><i class="fas fa-check"></i> تم الدفع  </span>
                        </a>



                    </td>
                    <td>

                         @if($order->count() > 0) 
                        {{$r2->created_at}}
                        @endif

                    </td>
                </tr>
                @endforeach
            </tbody>
      
        </table>
      
    </div>
</div>
@endsection

