@extends('client.layouts.app')
@section('title','تقرير الأرباح ')
@section('content')
<div class="app-content content ">
      <div class="content-overlay"></div>
      <div class="header-navbar-shadow"></div>
      <div class="content-wrapper container-xxl p-0">
        <div class="content-header row">
        </div>
    <div class="content-body">
	<!-- Dashboard Ecommerce Starts -->
		<section id="dashboard-ecommerce">
		<div class="content-header row">
          <div class="content-header-left col-md-9 col-12 mb-2">
            <div class="row breadcrumbs-top">
              <div class="col-12">
                <h2 class="content-header-title float-start mb-0"> الأرباح</h2>
                <div class="breadcrumb-wrapper">
                  <ol class="breadcrumb">
                    <li class="breadcrumb-item"><a href="{{route('client.home')}}">لوحة التحكم</a>
                    </li>
                    <li class="breadcrumb-item"><a href="{{route('client.reports')}}">التقارير </a>

                    <li class="breadcrumb-item active">الأرباح 
                    </li>
                  </ol>
                </div>
              </div>
            </div>
          </div>
          <div class="content-header-right text-md-end col-md-3 col-12 d-md-block d-none">
            <div class="mb-1 breadcrumb-right">
			
              <div class="dropdown">
               				
            </div>
          </div>
        </div>
            <div class="col-xl-12 col-md-12 col-12">
                <div class="card">
                    <form id="search" name="date" >
                    <div class="card-body row" align="center">
                        <div class="col-lg-2 col-sm-6-md-6"></div>

                        <div class="col-lg-6 col-sm-6-md-6 ">


                            <div class= "row">
                        <div class="col-lg-6 col-sm-6-md-6 mb-1">
                            <center> <label class="form-label" style="font-weight:bold " > من تاريخ</label></center>
                   <input type="date" name="from_date" id="from_date" class="form-control" >
                       
                </div>
                <div class="col-lg-6 col-sm-6-md-6 mb-1">
                    <center> <label class="form-label" style="font-weight:bold " > إلي تاريخ</label></center>
                    <input type="date" name="to_date" id="to_date" class="form-control" >
                   </div>
                            </div>
                </div>
                        <div class="col-lg-4 col-sm-6-md-6">
                            <center> <label class="form-label" style="font-weight:bold " ></label></center>
            
                            <button type="submit" class="btn btn-success me-1">بحث</button>
            
                         </div>
                      </div>
                    </form>
                </div>
              </div>

      <div class="col-xl-12 col-md-12 col-12" id="data-card" style="display: none">
          <div class="card card-statistics">

        <div class="card-body statistics-body">
          <div class="row">

            <div class="card-body" align="center" id="loader">
                <div class="spinner-border text-primary" style="width: 5rem; height: 5rem" role="status">
                </div>
              </div> 

                <div id="header" style="display: none">
              <center>
                  <h3 id="header-title"></h3>
                  <br>
              </center>
                </div>
                        <div class="card-datatable">
                            <div class="table-responsive">
                                <table class="table table-condensed table-striped table-bordered text-center table-hover " id="bills">
                                    <thead>
                                    <tr>
                                        <th>م</th>
                                        <th>رقم الفاتورة</th>
                                        <th>العميل</th>
                                        <th>تاريخ الفاتورة</th>
                                        <th> وقت الفاتورة</th>
                                        <th>الاجمالى النهائى</th>
                                        <th>الربح</th>
                                        <th>المسؤل</th>
                                        <th class=" no-print">عرض</th>
                                    </tr>
                                    </thead>
                                    <tbody class="tbody">
                                    </tbody>
                                </table>
                            </div>
                            
                        </div>

                        <div class="divider"></div>
          </div>
        </div>
      </div>
    </div>
    <center>
      <div class="col-lg-4 col-md-12" style="display: none" id="additionalData">
        <div class="card" >
          <div class="card-header"  >
            <h4 class="card-title"> ملخص التقرير </h4>
          </div>
          <div class="card-body">
            <p class="card-text">
             
            </p>
            <ul class="list-group">
              <li class="list-group-item list-group-item-primary">
                عدد الفواتير :
                <span class="badge bg-primary rounded-pill" id="total_count"></span>
              </li>
              <li class="list-group-item list-group-item-info">
                اجمالى المبيعات  : 
                <span class="badge bg-info rounded-pill" id="total_sales"></span>
                {{$company->currency}}
              </li>
              <li class="list-group-item list-group-item-warning">
                إجمالي الربح  : 
                <span class="badge bg-warning rounded-pill" id="total_profit"></span>
                {{$company->currency}}
              </li>
            </ul>
          </div>
        </div>
      </div>
      </center>
	</section>

	</div>
      </div>
    </div>
@endsection
@section('script')
<script>



$("#search").on('submit',(function(e) {
e.preventDefault();
  filter();
}));
  var table = $('.table') ;
  function filter() {

var safe_id = $('#safe_id').val();
var from_date = $('#from_date').val();
var to_date = $('#to_date').val();

if(from_date == '' || to_date == ''){$('#header-title').text('تقرير الأرباح')}else{$('#header-title').text('  تقرير الأرباح من تاريخ '+from_date+' إلي تاريخ '+to_date )}
$.ajax({
    url: "{{route('client.report13.post')}}",
    type: "POST",
    data: {
        "_token": "{{ csrf_token() }}",
        "safe_id": safe_id,
        "from_date": $("#from_date").val(),
        "to_date" : $("#to_date").val()
    },
    beforeSend : function (){table.hide();$('#additionalData').hide();$('#header').hide(); $('#data-card').show();$('#loader').show(); var ifConnected = window.navigator.onLine;if (!ifConnected) {toastr.info("يتم الآن إعادة الإتصال","لا يوجد إتصال بالإنترنت",{positionClass: "toast-top-right",tapToDismiss: !1,timeOut: 2e3,rtl: $("html").attr("data-textdirection")});}},
    success: function (data) {
      var profit = 0 ; 
        var sales = 0 ; 
        $('#loader').hide();
        table.show();
        $('.tbody').empty();
        $('#header').show();
        $.each( data.sale_bills, function( key, value ) {

            var url = "{{ route('client.sale_bills.print',['id'=>':id']) }}";
              url = url.replace(':id', value.sale_bill_number);
              profit +=  parseFloat(value.updated_at);
              sales +=  parseFloat(value.final_total);


            $('#bills').append(
                '<tr>'+
                '<td>'+(key+1)+'</td>'+
                '<td>'+(value.sale_bill_number)+'</td>'+
                '<td>'+(value.outer_client_id == null ? 'عميل مبيعات نقدية' : value.outer_client.client_name)+'</td>'+
                '<td>'+value.date+'</td>'+
                '<td>'+(value.time)+'</td>'+
                '<td>'+formatMoney(parseFloat(value.final_total))+'</td>'+
                '<td>'+formatMoney(parseFloat(value.updated_at))+'</td>'+
                '<td>'+value.client.name+'</td>'+
                '<td class="no-print"><a href="'+url+'"><button class="btn btn-success">عرض</button></a></td>'+
                '</tr>'
            );

           
        });

        if(data.sale_bills == ''){ $('#bills').append('<tr style="font-weight:bold;">'+'<td colspan="9">لا يوجد أي عمليات  </td>'+'</tr>')}
        
        $('#additionalData').show();
        $('#total_count').text(formatMoney(data.sale_bills.length));
        $('#total_sales').text(formatMoney(sales));
        $('#total_profit').text(formatMoney(profit));



          },
   error: function (data) {

    }
});
};


var selectForm = $('#search');

if (selectForm) {
    selectForm.validate({

        rules: {
      'safe_id': {
        required: true,
      }
    }
  });
}
   </script>
@endsection
