@extends('client.layouts.app')
@section('title',' دفع بنكي إلي '.$suppliers->supplier_name )
@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 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">
             دفع بنكي إلي {{$suppliers->supplier_name}}
              </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.suppliers.index')}}">الموردين</a>
                  </li>
                  <li class="breadcrumb-item active">{{$suppliers->supplier_name}}
                  </li>
                  <li class="breadcrumb-item active">الدفعات
                </li>
                <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">
                @can('قائمة الموردين الحاليين')
                  <a  href="{{ route('client.suppliers.index') }}">
                        <button class="btn-icon btn btn-primary" type="button"><i data-feather='eye'> </i></button>
                    </a>
                @endcan

                </div>
              </div>
        </div>
      </div>
      <div class="content-body">
        <!-- Basic Horizontal form layout section start -->
        <!-- Basic multiple Column Form section start -->
<section id="multiple-column-form">
<div class="row">
  <div class="col-12">
    <div class="card">
      <div class="card-header">

        <h4 class="card-title">البيانات الاساسية</h4>

          <h4><a href="{{route('client.add.cash.suppliers',$suppliers->id)}}"><button class="btn-icon btn btn-warning waves-effect waves-float waves-light" type="button">دفع نقدي</button></a></h4>

      </div>

      <div class="card-body">
        <form class="form" id="addForm" action="#">
            <input type="hidden" name="company_id" value="{{$company_id}}">
            <input type="hidden" name="cash_number" value="{{$pre_buy_cash}}" class="form-control">
            @csrf
          <div class="row">
            <div class="col-md-4 col-12">
                <div class="mb-1">                    
                    <b class="form-label">  المورد </b>
                    <select  name="supplier_id" class="form-control selectpicker" id="supplier_id" readonly>
                            <option value="{{$suppliers->id}}" selected>{{$suppliers->supplier_name}}</option>
                    </select>
                    <center><span id="balance" style="font-size:15px"> المديونية :  @if($suppliers->balance > 0 )    له  {{number_format($suppliers->balance)}} @elseif($suppliers->balance < 0)   علية  {{number_format(abs($suppliers->balance))}} @else 0 @endif</span></center>
                  </div>
            </div>
            <div class="col-md-4 col-12">
                <div class="mb-1">  
                   <b class="form-label"> المبلغ المدفوع </b>
                    <input  class="form-control"
                           name="amount" type="number" step="any" placeholder="المبلغ المدفوع" >
                </div>
            </div>
            <div class="col-md-4 col-12">
                <div class="mb-1">  
                    <b class="form-label"> التاريخ </b>
                    <input  class="form-control"
                           name="date" type="date"  value="{{date('Y-m-d')}}">
                </div>
             </div>
                <div class="col-md-4 col-12">
                    <div class="mb-1">  
                    <b class="form-label"> الوقت </b>
                    <input  class="form-control"
                           name="time" type="time"  value="{{date('H:i:s')}}">
                           
                </div>
            </div>
            <div class="col-md-4 col-12">
                <div class="mb-1">  
                    <b class="form-label"> بنك الدفع </b>
                    <select name="bank_id" class="form-control">
                        <option value="">اختر بنك الدفع</option>
                        @foreach($banks as $bank)
                            <option
                                value="{{$bank->id}}">{{$bank->bank_name}}</option>
                        @endforeach
                    </select>
                    </div>
                </div>  
                
                <div class="col-md-4 col-12">
                  <div class="mb-1">  
                    <b class="form-label"> رقم المعاملة </b>
                  <input type="text" class="form-control" name="bank_check_number" placeholder="رقم المعاملة"/>
                  </div>
                </div>
                <div class="col-md-4 col-12">
                  <div class="mb-1">  
                  <b class="form-label"> ملاحظات </b>
                  <input type="text" class="form-control" name="notes" placeholder="ملاحظات الدفع">
                </div>
                 </div>


            <div class="col-12" align="center">
            <br>
              <button type="submit" class="btn btn-success me-1">إضافة</button>
              <button type="button" class="btn btn-outline-danger" onclick="window.history.back()">رجوع</button>
            </div>
          </div>
          
        </form>
      </div>
    </div>
  </div>
</div>
@can('سجل الدفعات إلي المورد')
<div class="row">
  <div class="col-12">
    <div class="card">
      <div class="card-header">
        <h4 class="card-title">
            سجل الدفعات البنكية للعميل : {{$suppliers->supplier_name}}
        </h4>
      </div>
      <div class="card-body">
        <div class="table-responsive">
            <table id="example2" class="datatables-basic table dataTable no-footer dtr-column collapsed" aria-describedby="example2_info">

                                 <thead>
                            <tr>
                              <th>تحكم</th>
                                <th>كود</th>
                                <th>المبلغ</th>
                                <th>مديونية قبل</th>
                                <th>مديونية بعد</th>
                                <th>فاتورة</th>
                                <th>التاريخ</th>
                                <th>الوقت</th>
                                <th>البنك</th>
                                <th>المسؤول</th>
                            </tr>
                            </thead>
                            <tbody>
                            @php
                                $i=0;
                            @endphp
                            @foreach ($buy_cashs as $key => $cash)
                                <tr id="id-{{$cash->id}}">

                                  <td>
                                    <a class="pe-1 dropdown-toggle -arrow text-primary" data-bs-toggle="dropdown">
                                      <i data-feather='more-vertical'style="width: 20;height: 20;"></i>
                                    </a>
                                       <div class="dropdown-menu dropdown-menu-end">
                                        
                                        {{-- <a href="{{ route('client.edit.cash.clients', [$suppliers->id,$cash->id]) }}" class="dropdown-item"><b> تعديل <i data-feather='edit'></b></i></a> --}}
                                        <a onclick="deleteRow({{$cash->id}})" class="dropdown-item"><b> حذف <i data-feather='trash-2'></b></i></a>
                                        </div>                        
                                  </td>
                                    <td>{{ $cash->cash_number }}</td>
                                    <td>
                                        {{number_format($cash->amount)}}
                                    </td>
                                    <td>
                                        {{number_format($cash->balance_before)}}
                                    </td>
                                    <td>
                                        {{number_format($cash->balance_after)}}
                                    </td>
                                    <td>@if(!empty($cash->bill_id)) <a href="{{route('client.sale_bills.print',$cash->bill_id)}}"> فاتورة # {{$cash->bill_id}} </a> @else دفع بنكي  @endif</td>
                                    <td>{{ $cash->date }}</td>
                                    <td>{{ $cash->time }}</td>
                                    <td>{{ $cash->bank->bank_name }}</td>
                                    <td>{{ $cash->client->name }}</td>
                                </tr>
                            @endforeach
                            </tbody>
            </table>
        </div>
      </div>
    </div>
  </div>
</div>
@endcan
</section>
<!-- Basic Floating Label Form section end -->

      </div>
    </div>
  </div>
  <!-- END: Content-->
@endsection
@section('script')
<script src="{{asset('app-assets/js/jquery.dataTables.min.js')}}"></script>
<script src="{{asset('app-assets/js/dataTables.bootstrap5.min.js')}}"></script>
<script src="{{asset('app-assets/js/sweetalert.min.js')}}"></script>
<script>
       

    $(function () {
      $("#example2").DataTable();
     
    });
   $('#example2').DataTable({"language": { "url": "{{asset('app-assets/data/locales/Arabic.json')}}"}}); 

   
   
        var selectForm = $('#addForm');

if (selectForm) {
    selectForm.validate({

        rules: {
      'client_id': {
        required: true,
      },
      'bank_id': {
        required: true
      },
      'amount': {
        required: true
      },
      'time': {
        required: true
      },
      'date': {
        required: true
      }
    
    
    }
  });
}

$("#addForm").on('submit',(function(e) {

e.preventDefault();

$.ajax({

  url: "{{route('client.store.cashbank.suppliers')}}",

  type: "POST",

  data:  new FormData(this),

  dataType:  'json',

  contentType: false,

  cache: false,

  processData:false,
  
  beforeSend : function (){var ifConnected = window.navigator.onLine;if (!ifConnected) {toastr.info("يتم الآن إعادة الإتصال","لا يوجد إتصال بالإنترنت",{positionClass: "toast-top-right",tapToDismiss: !1,timeOut: 2e3,rtl: $("html").attr("data-textdirection")});}},

  success: function(response) {

    if(response.status){

            toastr.success("",response.message,{
			positionClass: "toast-top-right",
			tapToDismiss: !1,
			timeOut: 2e3,
			rtl: $("html").attr("data-textdirection")
		    });      
            document.getElementById("addForm").reset();

    }else{
        toastr.warning("",response.message,{
			positionClass: "toast-top-right",
			tapToDismiss: !1,
			timeOut: 2e3,
			rtl: $("html").attr("data-textdirection")
		    });   
    } 

  },
  error: function(response){
          $.each(response.responseJSON.errors, function (key, item) 
          {
            toastr.error("",item,{
			positionClass: "toast-top-right",
			tapToDismiss: !1,
			timeOut: 2e3,
			rtl: $("html").attr("data-textdirection")
		    });

          });
  },

 });

}));

function deleteRow(id){

swal({

title: "هل أنت متأكد من حذف هذه العملية؟",

type: "warning",

showCancelButton: true,

confirmButtonColor: "#DD6B55",

confirmButtonText: "تأكيد",

cancelButtonText: "إلغاء",

closeOnConfirm: false

}, function (isConfirm) {

if (!isConfirm) return;

$.ajax({

 url: "{{route('client.destroy.cashbank.suppliers')}}",

   type: "post",

   data: {

       id: id,
       '_token':'{{csrf_token()}}'
   },

   dataType: "json",
   beforeSend : function (){var ifConnected = window.navigator.onLine;if (!ifConnected) {toastr.info("يتم الآن إعادة الإتصال","لا يوجد إتصال بالإنترنت",{positionClass: "toast-top-right",tapToDismiss: !1,timeOut: 2e3,rtl: $("html").attr("data-textdirection")});}},

   success: function (response) {
            
            if(response.status){
             swal("تم الحذف بنجاح", "", "success");
      
               $("#id-" +id).remove();
            }else{
              
              swal(response.message,response.sol, "error");
      
            }
      
         },

   error: function (xhr, ajaxOptions, thrownError) {

       swal("خطأ في الحذف ", "الرجاء المحاولة مرة آخري", "error");

   }

});

});
}
        </script>
@endsection

