 
 <?php $__env->startSection('content'); ?>
 <?php echo $__env->make('administrator.deposit.form-cancel', array_except(get_defined_vars(), array('__data', '__path')))->render(); ?>
 <?php echo $__env->make('administrator.deposit.form-add-deposit', array_except(get_defined_vars(), array('__data', '__path')))->render(); ?>
 <?php echo $__env->make('administrator.deposit.form-approve-add-deposit', array_except(get_defined_vars(), array('__data', '__path')))->render(); ?>
 <?php echo $__env->make('administrator.deposit.form-reject-add-deposit', array_except(get_defined_vars(), array('__data', '__path')))->render(); ?>
 <?php echo $__env->make('administrator.deposit.form-approve-instalment', array_except(get_defined_vars(), array('__data', '__path')))->render(); ?>
 <?php echo $__env->make('administrator.deposit.form-reject-installment', array_except(get_defined_vars(), array('__data', '__path')))->render(); ?>
 <?php echo $__env->make('administrator.deposit.form-installment-info', array_except(get_defined_vars(), array('__data', '__path')))->render(); ?>
 <?php echo $__env->make('administrator.deposit.form-approve-cancel-deposit', array_except(get_defined_vars(), array('__data', '__path')))->render(); ?>
 <?php echo $__env->make('administrator.deposit.form-reject-cancel-deposit', array_except(get_defined_vars(), array('__data', '__path')))->render(); ?>
 <?php echo $__env->make('administrator.deposit.form-add-client-relationship', array_except(get_defined_vars(), array('__data', '__path')))->render(); ?>
 <?php Fun::lang() ?>
 <style>
  td.details-control {
      background: url(' <?php echo e(asset('images/details_open.png')); ?> ') no-repeat center center !important;
      cursor: pointer;
  }
  tr.shown td.details-control {
      background: url(' <?php echo e(asset('images/details_close.png')); ?> ') no-repeat center center !important;
  }
  .content-wrapper{
    height: 100% !important;
  }
</style>
 <div class="page-wrapper">
     <div class="top-content">
         <div class="row">
             <div class="col-sm-12 col-xs-12">
                 <div class="pull-left">
                     <h3>Deposit</h3>
                 </div>

             </div>
         </div>
     </div>
     <div class="row">
         <div class="box-body table-responsive">
             <table id="datatable_item_deposit" class="table table-striped table-bordered table-hover nowrap" width="100%">
                 <thead>
                     <tr>
                         <th></th>
                         <th>No</th>
                         <th>Sale Date</th>
                         <th>Name</th>
                         <th>Phone</th>
                         <th>Project Name</th>
                         <th>Category</th>
                         <th>Item Code</th>
                         <th>Contract Date</th>
                         <th>Payment Policy</th>
                         <th>Total Sale Price</th>
                         <th>Sale Discount</th>
                         <th>Sale Amount</th>
                         <th>Deposit Amount</th>
                         <th>Status</th>
                         <th>Action</th>
                     </tr>
                 </thead>
             </table>
         </div>
     </div>
 </div>
 <?php $__env->stopSection(); ?> <?php $__env->startSection('js'); ?>
 <script type="text/javascript">
 var data_client_relationship = <?php echo json_encode($client_relationship); ?>;
    function remove_relationship(obj){
        $(obj).closest("tr").remove();
    }
    function add_more_relationship(obj){
        var opt=`<option value="">Please Select</option>`;
        console.log(data_client_relationship);
        $.each(data_client_relationship,function(index, el) {
            opt+=`<option value="${el.id}">${el.clientship_fullname}</option>`;   
        });
        $('#client_relationship').empty();
        $("#client_relationship").append(opt);

        var tr = `<tr>
                    <td>
                        <div class="input-group" style='width:200px !important'>
                            <select class="form-control form-select select2" name="client_relationship[]" width="100%" required>
                                ${opt}
                            </select>
                        </div>
                    </td>
                    <td>
                        <div class="input-group">
                            <input type="text" name="relation_type[]" class="form-control" required>
                        </div>
                    </td>
                    <td><button class="btn btn-md btn-danger" type="button" onclick="remove_relationship(this)"><i class="fa fa-remove"></i></button></td>
                </tr>`;
        $(obj).closest('tbody').append(tr);
        $(".select2").select2();
}
    function add_client_relationship(sale_id)
    {
        var route = "<?php echo e(route('get_sale_client_relationship',':id')); ?>";
        $("#main-spinner").show();
        route = route.replace(":id", sale_id);
        $.ajax({
            url: route,
            type: 'GET',
            dataType: 'JSON',
        })
        .done(function(data) {
            $("#main-spinner").hide();
            $("#form-client-relationship").modal("show");
            console.log(data);
            var opt = "";
            
            
            if(data!=''){
                $.each(data,function(index, el) {
                    var child_opt = '<option value="">Please Select</option>';
                    $.each(data_client_relationship,function(i,v){
                        if(v.id==el.clientship_id){
                            child_opt+=`<option value="${v.id}" selected>${v.clientship_fullname}</option>`;
                        }else{
                            child_opt+=`<option value="${v.id}">${v.clientship_fullname}</option>`;
                        }
                    });
                    if(index==0){
                        opt += `<tr>
                        <td>
                            <div class="input-group" style='width:200px !important'>
                                <select class="form-control form-select select2" name="client_relationship[]" width="100%" required>
                                    ${child_opt}
                                </select>
                            </div>
                        </td>
                        <td>
                            <div class="input-group">
                                <input type="text" name="relation_type[]" class="form-control" value="${el.relationship_status}" required>
                            </div>
                        </td>
                        <td><button class="btn btn-md btn-primary" type="button" onclick="add_more_relationship(this)"><i class="fa fa-plus"></i></button></td>
                    </tr>`;
                    }else{
                        opt += `<tr>
                        <td>
                            <div class="input-group" style='width:200px !important'>
                                <select class="form-control form-select select2" name="client_relationship[]" width="100%" required>
                                    ${child_opt}
                                </select>
                            </div>
                        </td>
                        <td>
                            <div class="input-group">
                                <input type="text" name="relation_type[]" class="form-control" value="${el.relationship_status}" required>
                            </div>
                        </td>
                        <td><button class="btn btn-md btn-danger" type="button" onclick="remove_relationship(this)"><i class="fa fa-remove"></i></button></td>
                    </tr>`;
                    }
                    
                });
                $("table#client-relationship").find('tbody').empty();
                $("table#client-relationship").find('tbody').append(opt);
            }else{
                $("table#client-relationship").find('tbody').empty();
                var child_opt='<option value="">Please Select</option>';
                $.each(data_client_relationship,function(i,v){
                    child_opt+=`<option value="${v.id}">${v.clientship_fullname}</option>`;
                });
                var tr = `<tr>
                    <td>
                        <div class="input-group" style='width:200px !important'>
                            <select class="form-control form-select select2" name="client_relationship[]" width="100%" required>
                                ${child_opt}
                            </select>
                        </div>
                    </td>
                    <td>
                        <div class="input-group">
                            <input type="text" name="relation_type[]" class="form-control" required>
                        </div>
                    </td>
                    <td><button class="btn btn-md btn-primary" type="button" onclick="add_more_relationship(this)"><i class="fa fa-plus"></i></button></td>
                </tr>`;
                $("table#client-relationship").find('tbody').append(tr);
                $(".select2").select2();
            };
            $("#sale_id_client_relationship").val(sale_id);
            $(".select2").select2();
        })
        .fail(function() {
            console.log("error");
        })
        .always(function() {
            console.log("complete");
        });
        
    }
    /* Formatting function for row details - modify as you need */
    function format (sale_id) {
        var table_detail="";
        var route="<?php echo e(route('datatable.get_deposit_info')); ?>";
        $.ajax({
          url:route,
          type:"POST",
           async: false,
          dataType:"JSON",
          data:{
            "_token":"<?php echo e(csrf_token()); ?>",
            "sale_id":sale_id
          },
          success:function(data){
            var tr =`
                    <tr><th colspan="6" style = 'text-align:center;'><u>Deposit Detail</u></th></tr>
                    <tr>
                      <th>No</th>
                      <th>Date</th>
                      <th>Amount($)</th>
                      <th>Descrption</th>
                      <th>Action</th>
                    </tr>`;
            $.each(data,function(index,val){
              var date = val.deposit_date.split("-")['2'];
              var month=val.deposit_date.split("-")['1'];
              var year =val.deposit_date.split("-")['0'];
              var route = "<?php echo e(route('items-deposit-show.show',':id')); ?>";
              route = route.replace(":id",val.id);
              var button = `
              <a style="color:white;margin-left:0;" href="${route}" class="btn btn-success btn-sm"><i class="fa fa-print" title="Print Deposit"></i></a>

              `;
              
              tr += `
                    <tr id="${val.id}">
                      <td>${index+1}</td>
                      <td>${date}-${month}-${year}</td>
                      <td>${val.deposit_amount}</td>
                      <td>${val.deposit_des==null?"":val.deposit_des}</td>
                      <td>${button}</th>
                    </tr>
                    `;
            });
            table_detail= `<table id="${sale_id}" width="100%" class="table-bordered table">${tr}</table>`;
          }
        });
        return table_detail;
    }
     $(document).ready(function() {
         $('#main-spinner').show();
         var table = $('#datatable_item_deposit').DataTable({
             ordering: false,
             "serverSide": true,
             ajax: "<?php echo e(route('datatable.get_client_deposit')); ?>",
             columns: [
                  {
                  "className": 'details-control',
                  "orderable": false,
                  "searchable": false,
                  "data": null,
                  "defaultContent": ' ',
                },
                  {
                     data: 'DT_RowIndex'
                 },
                 {
                     data: 'sale_date'
                 },
                 {
                     data: 'client_fullname'
                 },
                 {
                     data: 'client_phone1'
                 },
                 {
                     data: 'project_name'
                 },
                 {
                     data: 'category_name',
                     width: '5px'
                 },
                 {
                     data: 'item_name'
                 },
                 {
                     data: 'contract_date'
                 },
                 {
                    data:'payment_policy'
                 },
                 {
                     data: 'item_price'
                 },
                 {
                    data: 'sale_discount'
                 },
                 {
                     data: 'sale_amount'
                 },
                 {
                     data: 'sale_receive'
                 },
                 {
                     data: 'other'
                 },
                 {
                     data: 'action',
                     width: '5px'
                 },
             ],

             "initComplete": function(settings, json) {
                 $('#main-spinner').hide();
             }
         });
        $('#datatable_item_deposit tbody').on('click', 'td.details-control', function () {
            var tr = $(this).closest('tr');
            var sale_id = tr.data("sale-id");
            var row = table.row( tr );
            if( row.child.isShown() ) {
                row.child.hide();
                    tr.removeClass('shown');
                }
            else {
                row.child( format(sale_id) ).show();
                tr.addClass('shown');
            }
        });
     });

     function cancel_deposit(id) {
        $("#cancel_deposit").modal("show");
         $("#ps_id").val(id);

     }

     function deposit(obj) {
        $('#main-spinner').show();
         var id = obj;
         $.ajax({
             url: "/realty/get-deposit/" + id,
             type: "get",
             success: function(data) {
                $('#main-spinner').hide();
                $("#deposit").modal("show");
                 var d = $.parseJSON(data);
                 $("#sale_id").val(d.id);
                 $("#deposit_amount").val(d.sale_receive);

             }
         });
     }

     function save_deposit(obj) {
         $(obj).attr("disabled", "disabled");
         var sale_id = $('#sale_id').val();
         var date = $('#date').val();
         var deposit_amount = $('#deposit_amount').val();
         var payamount = $('#payamount').val();
         if (!payamount) {
             $("#_msg_paymentPopup").html('Deposit amount is empty, Please enter your deposit amount!');
             $("#_div_paymentPopup").attr("style", "display:block");
         } else {
            $('#main-spinner').show();
             $.ajax({
                 type: 'POST',
                 url: "<?php echo e(route('submit_deposit_data')); ?>",
                 data: {
                     "_token": "<?php echo e(csrf_token()); ?>",
                     "sale_id": sale_id,
                     "date": date,
                     "deposit_amount": deposit_amount,
                     "payamount": payamount,
                     'date_contract':$("#date_contract").val()

                 },
                 success: function(data) {
                    $('#main-spinner').hide();
                     var d = $.parseJSON(data);
                     if (d.message == "success") {
                         window.location.reload();
                         $('#done').show();
                     }
                 }
             });
         }
     }

     function approvedInstalment(id) {
         $("#sid_instalment").val(id);
     }

     function rejectInstalment(obj) {
         $('#sid_reject_instalment').val(obj);
     }

     function installment_info(obj) {
         var route = "<?php echo e(route('get-installment-info',':id')); ?>";
         route = route.replace(':id', obj);
         $('#main-spinner').show();
         $.ajax({
             type: 'GET',
             url: route,
             dataType: 'text',
             data: {
                 id: obj,
                 "_token": "<?php echo e(csrf_token()); ?>",
             },
             success: function(data) {
                $('#main-spinner').hide();
                 $('#installment_detail').empty().html(data);
                 $("#installment_info").modal('show');
             }
         });
     }

     function approvedAddDe(obj) {
         var id = obj;
         $('#main-spinner').show();
         $.ajax({
             url: "/realty/approve-deposit/" + id,
             type: "get",
             success: function(data) {
                $('#main-spinner').hide();
                $("#approve-add-deposit").modal("show");
                 var d = $.parseJSON(data);
                 $('#app-date').val(d.app_date);
                 $("#sale_id-app").val(d.id);
                 $("#deposit_amount-app").val(d.sale_receive);
                 $("#payamount-app").val(d.app_payamount);
                 $("#date_contract-app").val(d.app_date_contract);

             }
         });
     }

     function rejectAddDe(id) {
        $("#reject-add-deposit").modal("show");
         $("#saleadd_id").val(id);
     }

     function save_approve_deposit(obj) {
         $(obj).attr("disabled", "disabled");
         var sale_id = $('#sale_id-app').val();
         var date = $('#app-date').val();
         var date_contract = $('#date_contract-app').val();
         var deposit_amount = $('#deposit_amount-app').val();
         var payamount = $('#payamount-app').val();

         if (!payamount) {
             $("#_msg_paymentPopup").html('Deposit amount is empty, Please enter your deposit amount!');
             $("#_div_paymentPopup").attr("style", "display:block");
         } else {
            $('#main-spinner').show();
             $.ajax({
                 type: 'POST',
                 url: "<?php echo e(route('approve_submit_deposit_data')); ?>",
                 data: {
                     "_token": "<?php echo e(csrf_token()); ?>",
                     "sale_id": sale_id,
                     "date": date,
                     "date_contract": date_contract,
                     "deposit_amount": deposit_amount,
                     "payamount": payamount

                 },
                 success: function(data) {
                    $('#main-spinner').hide();
                     var d = $.parseJSON(data);
                     if (d.message == "success") {
                         window.location.reload();
                         $('#done').show();
                     }
                 }
             });
         }
     }

     function rejectPOS(id) {
         $.confirm({
             title: 'Confirm!',
             content: 'Your edit request will be rejected!',
             buttons: {
                cancel: function() {
                 },
                 confirm: function() {
                    $('#main-spinner').show();
                    var route = "<?php echo e(route('external-deposit-list.destroy',':id')); ?>";
                    route = route.replace(':id',id);
                    $.ajax({
                        type:"DELETE",
                        dataType:"JSON",
                        url:route,
                        data:{
                            "_token": "<?php echo e(csrf_token()); ?>"
                        },
                        success:function(response){
                            $('#main-spinner').hide();
                            if(response.success==true){
                                $('#datatable_item_deposit').DataTable().ajax.reload();
                            }
                            $.alert(response.msg);
                            $('.overlay').hide();
                        }
                    });
                 }
             }
         });
     }
     function approvedCanPOS(obj) {
        var id = obj;
        $("#sid_can").val(id);
        var route = "<?php echo e(route('request-cancel-deposit',':id')); ?>";
        route = route.replace(':id',id);
        $.ajax({
            type:"GET",
            url:route,
            data:{
                '_token':"<?php echo e(csrf_token()); ?>"
            },
            success:function(response){
                var data = response.data;
                $('#app_des').val('').val(data.deposit_remark);

                $('#amount_return').val('').val(data.app_payamount);
            }
        });
    }

    function rejectCanPOS(obj) {
        var id = obj;
        $("#sid_rej").val(id);
        var route = "<?php echo e(route('request-cancel-deposit',':id')); ?>";
        route = route.replace(':id',id);
        $.ajax({
            type:"GET",
            url:route,
            data:{
                '_token':"<?php echo e(csrf_token()); ?>"
            },
            success:function(response){
                var data = response.data;
                $('#reject_des').val('').val(data.deposit_remark);
            }
        });
    }

 </script>

 <?php $__env->stopSection(); ?>
 <style type="text/css">
     .select2{
        width: 100% !important;
      }
 </style>
<?php echo $__env->make('adminlte::page', array_except(get_defined_vars(), array('__data', '__path')))->render(); ?>