@extends('client.layouts.app')
@section('title', 'تعديل سلفة نقدية إلي عميل '.$outer_clients->client_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">
              <h3 class="content-header-title float-start mb-0">
                تعديل سلفة نقدي إلي  {{$outer_clients->client_name}}
              </h3>
              <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.outer_clients.index')}}">العملاء</a>
                  </li>
                  <li class="breadcrumb-item active">{{$outer_clients->client_name}}
                  </li>
                  <li class="breadcrumb-item active">السلف
                </li>
                <li class="breadcrumb-item active">{{$cash->id}}
                </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">
                    <a  href="{{ route('client.add.cash.clients',$outer_clients->id) }}">
                        <button class="btn-icon btn btn-primary" type="button"><i data-feather='eye'> </i></button>
                    </a>
                </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>

      </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="{{$cash->cash_number}}" class="form-control" >
            <input type="hidden" name="id" value="{{$cash->id}}" 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="outer_client_id" class="form-control selectpicker" id="outer_client_id" readonly>
                            <option value="{{$outer_clients->id}}" selected>{{$outer_clients->client_name}}</option>
                    </select>
                    <center><span id="balance" style="font-size:15px"> المديونية :  @if($outer_clients->balance > 0 )    عليه  {{number_format($outer_clients->balance)}} @elseif($outer_clients->balance < 0)   له  {{number_format(abs($outer_clients->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="مبلغ الدفعة"  value="{{abs($cash->amount)}}">
                  </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="{{$cash->date}}">
                </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="{{$cash->time}}">
                           
                </div>
            </div>
            <div class="col-md-4 col-12">
                <div class="mb-1">  
                    <b class="form-label"> خزنة الدفع </b>
                    <select name="safe_id" class="form-control">
                        <option value="">اختر خزنة الدفع</option>
                        @foreach($safes as $safe)
                            <option
                                value="{{$safe->id}}" @if($safe->id == $cash->safe_id) selected @endif >{{$safe->safe_name}}</option>
                        @endforeach
                    </select>
                    </div>
                </div>  
                
                <div class="col-md-4 col-12">
                  <div class="mb-1">  
                  <b class="form-label"> ملاحظات </b>
                  <input  class="form-control"
                         name="notes" 
                         type="text" 
                            value="{{$cash->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>
</section>
      </div>
    </div>
  </div>
@endsection
@section('script')
<script>
    
 var selectForm = $('#addForm');

if (selectForm) {
    selectForm.validate({

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

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

e.preventDefault();

$.ajax({

  url: "{{route('client.update.cash.borrow.clients')}}",

  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")
		    });      

    }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")
		    });

          });
  },

 });

}));

        </script>
@endsection