<?php echo $__env->make('dashboard.include.header', array_except(get_defined_vars(), array('__data', '__path')))->render(); ?>

  
  <div class="content-wrapper">
    <div class="content-header">
      <div class="container-fluid">
        <div class="row mb-2">
          <div class="col-sm-6">
            <h1 class="m-0">Account Wholesale</h1>
          </div>
          <div class="col-sm-6">
            <ol class="breadcrumb float-sm-right">
              <li class="breadcrumb-item"><a href="/home">Home</a></li>
              <li class="breadcrumb-item active">Account Wholesale</li>
            </ol>
          </div>
        </div>
      </div>
    </div>
   
    <section class="content">
      <div class="container-fluid">

      <div class="card">
        <div class="card-body">
          <table id="example" class="table table-bordered" style="width:100%; font-size: 13px;">
            <thead>
                <tr>
                    <th>ID</th>
                    <th>Wholesaler Name</th>
                    <th>Total</th>
                    <th>Paid Amount</th>
                    <th>Balance</th>
                    <th>Status</th>
                    <th>Action</th>
                </tr>
            </thead>
            <tbody>
        <?php $__currentLoopData = $purce; $__env->addLoop($__currentLoopData); foreach($__currentLoopData as $data): $__env->incrementLoopIndices(); $loop = $__env->getLastLoop(); ?>
            <tr>
                <td><?php echo e($data->id); ?></td>
                <td><?php echo e($data->getseholsellername->name); ?></td>
                <td><?php echo e($data->total); ?></td>
                <td><?php echo e($data->amountpaid); ?></td>
                <td><span class="badge badge-danger"><?php echo e($data->total - $data->amountpaid); ?></span></td>
                <td class="text-center">
                <?php if($data->status=='0'): ?>
                <span class="badge badge-warning">Not paid</span>
                  <!-- <?php if($data->paymentmethod=='cash'): ?> -->
                
                  <!-- <?php else: ?>
                  <span class="badge badge-warning"><?php echo e(\Carbon\Carbon::parse( $data->ccdate )->diffInDays( $ldate )); ?> days overdue</span>
                  <?php endif; ?> -->
                <?php elseif($data->status=='1'): ?>
                <span class="badge badge-success">paid</span>
                <?php endif; ?>
                </td>
                <td class="text-center">   
                <?php if($data->status=='0'): ?>           
                
                    <button type="button" class="btn btn-primary btn-xs supmodel" data-total="<?php echo e($data->total); ?>" data-bank="<?php echo e($data->bankid); ?>" data-wholename="<?php echo e($data->getseholsellername->name); ?>" data-statuss="<?php echo e($data->status); ?>" data-id="<?php echo e($data->id); ?>" data-amountpaid="<?php echo e($data->amountpaid); ?>" data-cdate="<?php echo e($data->ccdate); ?>" data-pdate="<?php echo e($data->paiddate); ?>" data-id="<?php echo e($data->id); ?>" data-supid="<?php echo e($data->wholesaleuserid); ?>" data-payment="<?php echo e($data->paymentmethod); ?>"  data-toggle="modal" data-target="#supmodel">Edit</button>
                    <!-- <a href="" data-id="<?php echo e($data->id); ?>" class="btn btn-danger btn-xs button purchasingdelete" data-url=""><i class="far fa-trash-alt"></i> </a> -->
               <a data-id="<?php echo e($data->id); ?>" class="btn btn-success btn-xs" target="_blank" href="wholesalerprodetails/<?php echo e($data->id); ?>">Payment</a>       
                <?php elseif($data->status=='1'): ?>
                <button type="button" class="btn btn-primary btn-xs supmodel"  data-toggle="modal" data-target="#supmodel" disabled>Edit</button>
                <!-- <a href="" data-id="<?php echo e($data->id); ?>" style=" pointer-events: none;cursor: default;" class="btn btn-danger btn-xs button purchasingdelete" data-url="" disabled><i class="far fa-trash-alt"></i> </a> -->
                <a data-id="<?php echo e($data->id); ?>" class="btn btn-success btn-xs" target="_blank" href="wholesalerprodetails/<?php echo e($data->id); ?>">Payment</a>
                <?php endif; ?>
                </td>                        
            </tr>
        <?php endforeach; $__env->popLoop(); $loop = $__env->getLastLoop(); ?>
        </tbody>
        </table>
        </div>
      </div>

      </div>
    </section>
  </div>
 





<div class="modal fade" id="supmodel" tabindex="-1" role="dialog" aria-labelledby="exampleModalCenterTitle" aria-hidden="true">
  <div class="modal-dialog modal-dialog-centered" role="document">
      <div class="modal-content">
      <div class="modal-header">
          <h5 class="modal-title" id="exampleModalLongTitle">Purchasing Details Change</h5>
          <button type="button" class="close" data-dismiss="modal" aria-label="Close">
          <span aria-hidden="true">&times;</span>
          </button>
      </div>
      <form class="form-horizontal" method="POST" action="/addwholsalerupdate">
          <?php echo e(csrf_field()); ?>

      <div class="modal-body"> 
          <input type="hidden" name="id" id="id">   
          <input type="hidden" name="wholsesalename" id="wholsesalename">   
          <input type="hidden" name="total" id="total">   


                <!-- <div class="form-group">
                    <label>Wholesaler Name</label>
                    <select class="form-control select2" name="supplier_name" id="supplier_name" style="width: 100%;" required>
                        <option value="" selected="selected" disabled>Select</option>
                        <?php $__currentLoopData = $item; $__env->addLoop($__currentLoopData); foreach($__currentLoopData as $itemd): $__env->incrementLoopIndices(); $loop = $__env->getLastLoop(); ?>
                        <option value="<?php echo e($itemd->id); ?>"><?php echo e($itemd->name); ?></option>
                        <?php endforeach; $__env->popLoop(); $loop = $__env->getLastLoop(); ?> 
                    </select>
                </div> -->

            <div class="form-group">
                <!-- <label for="">Paid Amount</label> -->
                <input type="hidden" class="form-control" id="cost" name="cost" placeholder="Paid Amount" required>
            </div>
            <div class="form-group">
                <label for="">Paid Status</label>
                <div class="form-check">
                    <input class="form-check-input" type="checkbox" name="paidstatus" value="" id="flexCheckChecked" checked>
                    <label class="form-check-label" for="flexCheckChecked">Paid</label>
                </div>
            </div>



      </div>
      <div class="modal-footer">
          <button type="button" class="btn btn-secondary" data-dismiss="modal">Close</button>
          <button type="submit" class="btn btn-primary">Save changes</button>
      </div>
      </form>
      </div>
  </div>
</div>
  <?php echo $__env->make('dashboard.include.footer', array_except(get_defined_vars(), array('__data', '__path')))->render(); ?>;
  <style>
    table{
        width: 100%;
        margin: 20px 0;
        border-collapse: collapse;
    }
    table, th, td{
        border: 1px solid #cdcdcd;
    }
    table th, table td{
        padding: 5px;
        text-align: left;
    }
</style>

<script>
 $(document).ready(function() {
$('#flexCheckChecked').change(function() {
    if($(this).is(":checked")) {
        $("#pdate2").html("*Paid Date");
    }
    else{
        var valupricw = $('#payment2').val();
        if(valupricw=="cash"){
        $("#pdate2").html("Paid Date");
        }else if(valupricw=="cheque"){
        $("#pdate2").html("Cheque Date");
        }
        else if(valupricw=="credit"){
        $("#pdate2").html("Credit Date");
        } 
    }

});
});


$(function () {
    $('.select2').select2()
})

$('#reservationdate').datetimepicker({
format: 'YYYY-MM-DD HH:mm:ss'
});
$('#reservationdate2').datetimepicker({
format: 'YYYY-MM-DD HH:mm:ss'
});


$('#payment').on('change', function(){
  var id = $(this).val();

  if(id=="cash"){
    $("#pdate").html("Paid Date");
}else if(id=="cheque"){
  $("#pdate").html("Cheque Date");
}
else if(id=="credit"){
  $("#pdate").html("Credit Date");
}    
});

$(document).ready(function() {
    $('#example').DataTable({
        "order": [[ 0, "desc" ]]
    });
} );



$(document).on('click', '.purchasingdelete', function (e) {
    e.preventDefault();
    var id = $(this).data('id');
    let ele = this;
    console.log(id);

                swal({
                title: "Are you sure?",
                text: "Are you Sure to Remove Wholesaler Recipt",
                icon: "warning",
                showCancelButton: true,
    confirmButtonColor: '#3085d6',
    cancelButtonColor: '#d33',
    confirmButtonText: 'Yes, delete it!',
                dangerMode: true,
            }).then(function(isConfirm) {
                
                if (isConfirm.value===true) {

                        $.ajax({
                            url: "<?php echo e(url('/deletepurchassing')); ?>/"+id ,
                            method: "POST",
                            headers: {
                                'X-CSRF-TOKEN': $('meta[name="csrf-token"]').attr('content')
                            },
                            cache: false,
                            contentType: false,
                            processData: false,
                            dataType: 'JSON',
                            data:{
                                "id":id
                            },
                            success: function (result, code, ex) {
                                swal("Done!", "It was successfully deleted!", "success");
                                $(ele).closest('tr').hide();
                                // $("#here").load(location.href + " #here");
                                // window.location.href = "/supplier";
                            },
                            error: function (jqXHR, textStatus, errorThrown) {
                                swal("Warning", "Unable to Process your request", "error");
                            },
                        });

                } else {
                    swal("Cancelled", "No Removed", "error");
                }
            })
});

$('#supmodel').on('show.bs.modal', function (event) {

var button = $(event.relatedTarget) // Button that triggered the modal
var recipient = button.data('id') 
var supplier = button.data('supid') 
var product = button.data('proid') 
var qty1 = button.data('qtyss') 
var paymethod = button.data('payment') 
var paiddate = button.data('pdate') 
var cccdate = button.data('cdate') 
var cost1 = button.data('amountpaid') 
var staa = button.data('statuss') 
var bank = button.data('bank') 
var wholename = button.data('wholename') 
var total = button.data('total') 


var modal = $(this)
modal.find('.modal-body #id').val(recipient)
modal.find('.modal-body #Quantity').val(qty1)
modal.find('.modal-body #cost').val(cost1)
modal.find('.modal-body #wholsesalename').val(wholename)
modal.find('.modal-body #total').val(total)
$("#bank").val(bank).change();
$("#supplier_name").val(supplier).change();
$("#product_name").val(product).change();
$("#payment2").val(paymethod).change();

if(paymethod=="cash"){
    modal.find('.modal-body #paid_date').val(paiddate)
    $("#pdate2").html("Paid Date");
}else if(paymethod=="cheque"){
    modal.find('.modal-body #paid_date').val(cccdate)
    $("#pdate2").html("Cheque Date");
}else{
    modal.find('.modal-body #paid_date').val(cccdate)
    $("#pdate2").html("Credit Date");
}
if(staa==1){
    $( "#flexCheckChecked" ).prop( "checked", true );
}else{
    $( "#flexCheckChecked" ).prop( "checked", false  );
}

})

$('#payment2').on('change', function(){
  var id = $(this).val();

  if(id=="cash"){
    $("#pdate2").html("Paid Date");
}else if(id=="cheque"){
  $("#pdate2").html("Cheque Date");
}
else if(id=="credit"){
  $("#pdate2").html("Credit Date");
}    
});

$('#Quantity').on('input',function(e){
    var dis = $(this).val();
    var n1 = parseInt($('#Unit_Price').val());
    $('#Total').val(dis*n1);
    
});

$('#Unit_Price').on('input',function(e){
    var dis = $(this).val();
    var n1 = parseInt($('#Quantity').val());
    $('#Total').val(dis*n1);
});
</script>
 
<script>
    $(document).ready(function(){
    
        $(".add-row").click(function(){
            var name = $( "#product option:selected" ).text();
            var conceptName = $('#product').val();
            var qty = $("#Quantity").val();
            var uni = $("#Unit_Price").val();
            var tot = $("#Total").val();

            var markup = "<tr><td><input type='hidden' name='pro[]' value='"+conceptName+"'>"+name+"</td><td><input type='hidden' name='qty[]' value='"+qty+"'>"+qty+"</td><td><input type='hidden' name='unit[]' value='"+uni+"'>"+uni+"</td><td><input type='hidden' name='tot[]' id='tota' value='"+tot+"'>"+tot+"</td><td class='text-center'><a class='btn btn-danger btn-xs delete-row'><i class='far fa-trash-alt'></i></a></td></tr>";
            $("#tbUser tbody").append(markup);
            

            var values = $("input[name='tot[]']").map(function(){return $(this).val();}).get();
            var weeklyPay= getArraySum(values);
            $('#nettotal').val(weeklyPay);


            
        });
        
        // Find and remove selected table rows
        $(".delete-row").click(function(){

                if($(this).closest("tr")){
                    $(this).parents("tr").remove();
                    var values = $("input[name='tot[]']").map(function(){return $(this).val();}).get();
            var weeklyPay= getArraySum(values);
            $('#nettotal').val(weeklyPay);

                }
   
        });

        $("#tbUser").on('click', '.delete-row', function () {
        $(this).closest('tr').remove();

                    var values = $("input[name='tot[]']").map(function(){return $(this).val();}).get();
            var weeklyPay= getArraySum(values);
            $('#nettotal').val(weeklyPay);

});
    });    



function getArraySum(a){
    var total=0;
    for(var i in a) { 
        total += parseInt(a[i]);
    }
    return total;
}



<?php if(session('success')): ?>
        // swal("Success!","<?php echo e(session('success')); ?>","success");
        toastr.success('Your work has been successfully!')

<?php endif; ?>
<?php if(session('error')): ?>
    swal({
            title: "Error!",
            text: "<?php echo e(session('error')); ?>",
            icon: "error",
            button: "OK",
        });
<?php endif; ?>

</script>
