<?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">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">Wholesale</li>
            </ol>
          </div>
        </div>
      </div>
    </div>
   
    <section class="content">
      <div class="container-fluid">

    <form class="form-horizontal" method="POST" action="/addwholesalepro">
    <?php echo e(csrf_field()); ?>


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

          <div class="row">

            <div class="col-md-3">
                <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>
            <div class="col-md-3">
                <div class="form-group">
                    <label for="">Wholesaler Balance Payment</label><input type="text" class="form-control" id="whobalance" name="whobalance" placeholder="Supplier Balance Payment" readonly required>
                </div>
            </div>
    



          
          </div>
          
          </div>

          </div>

        <div class="card">
        <div class="card-body">
            <body>       
                <div class="row">
                <div class="col-md-4">
                    <div class="form-group">
                        <label>Product Name</label>
                        <select class="form-control select2" name="product" id="product" style="width: 100%;">
                            <option value="0" selected="selected" disabled>Select</option>
                            <?php $__currentLoopData = $product; $__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>
                    <div class="col-md-2">
                        <div class="form-group">
                            <label for="">Quantity</label><input type="number" id="Quantity" class="form-control" name="Quantity" placeholder="Quantity" >
                        </div>
                    </div>
                    <div class="col-md-2">
                        <div class="form-group">
                            <label for="">Unit Price</label><input type="number"id="Unit_Price" class="form-control" name="Unit_Price" placeholder="Unit Price" >
                        </div>
                    </div>
                    <div class="col-md-2">
                        <div class="form-group">
                            <label for="">Total</label><input type="number" class="form-control" value="0" id="Total" name="Total" placeholder="Total"  readonly>
                        </div>
                    </div>

                    <div class="col-md-2" style="padding-top:30px;">
                    <button type="button" class="btn btn-block btn-primary btn-sm add-row">Add</button>
                    </div>
                    

                    </div>

                        <table id="tbUser">
                            <thead>
                                <tr>
                                    <th>Product Name</th>
                                    <th>Quantity</th>
                                    <th>Unit Price</th>
                                    <th>Total</th>
                                    <th>Action</th>
                                </tr>
                            </thead>
                            <tbody>
                                    
                            </tbody>
                        </table>
    
                </body> <br><br>
<div class="row">
<div class="col-md-8"></div>
<div class="col-md-2">
    <div class="form-group">
        <label for=""> Net Amount</label><input type="number" class="form-control" value="0" id="nettotal" name="nettotal" placeholder="Net Amount"  readonly>
    </div>
</div>
<!-- <div class="col-md-2">
    <div class="form-group">
        <label for=""> Paid Amount</label><input type="number" class="form-control" value="0" id="amountpaid" name="amountpaid" placeholder="Paid Amount">
    </div>
</div> -->
<div class="col-md-2" style="padding-top:30px;"><button type="submit" class="btn btn-block btn-primary">Submit</button> </div>
</div>

<div class="row">
<div class="col-md-10"></div>

</div>

        </div>
        </div>

</form>



      <div class="card">
        <div class="card-body">
          <table id="example" class="table table-bordered" style="width:100%;">
            <thead>
                <tr>
                    <th>ID</th>
                    <th>Wholesaler Name</th>
                    <th>Total</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 class="text-center">   
                <?php if($data->status=='0'): ?>           
                <a data-id="<?php echo e($data->id); ?>" class="btn btn-primary btn-xs" href="wholesalerprodetails/<?php echo e($data->id); ?>">View</a>       
                    <!-- <button type="button" class="btn btn-primary btn-xs supmodel" 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> -->
                    <!-- &nbsp;<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> -->
                <?php elseif($data->status=='1'): ?>
                <a data-id="<?php echo e($data->id); ?>" class="btn btn-primary btn-xs" href="wholesalerprodetails/<?php echo e($data->id); ?>">View</a>
                <!-- <button type="button" class="btn btn-primary btn-xs supmodel"  data-toggle="modal" data-target="#supmodel" disabled>Edit</button> -->
                <!-- &nbsp;<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> -->
                <?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">    
                <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>Payment Method</label>
                <select class="form-control" name="payment" id="payment2" style="width: 100%;" required>
                    <option value="" selected="selected" disabled>Select</option>
                    <option value="cash">Cash</option>
                    <option value="cheque">Cheque</option>
                    <option value="credit">Credit</option>
                </select>
            </div>
            <div class="form-group">
            <label id="pdate2">Paid Date</label>
                <div class="input-group date" id="reservationdate2" data-target-input="nearest">
                    <input type="text" class="form-control datetimepicker-input" id="paid_date" name="paid_date" data-target="#reservationdate2"/ required>
                    <div class="input-group-append" data-target="#reservationdate2" data-toggle="datetimepicker">
                        <div class="input-group-text"><i class="fa fa-calendar"></i></div>
                    </div>
                </div>
            </div>
            <div class="form-group">
                <label for="">Paid Amount</label><input type="number" 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=="bank transfer"){
        $("#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'
});

$('.ChequeNumber').hide();

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

  if(id=="cash"){
    $("#pdate").html("Paid Date");
    $('.ChequeNumber').hide();
}else if(id=="cheque"){
  $("#pdate").html("Cheque Date");
  $('.ChequeNumber').show();
}
else if(id=="bank transfer"){
    $("#pdate").html("Paid Date");
    $('.ChequeNumber').hide();
    }
else if(id=="credit"){
  $("#pdate").html("Credit Date");
  $('.ChequeNumber').hide();
}    
});

$(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 modal = $(this)
modal.find('.modal-body #id').val(recipient)
modal.find('.modal-body #Quantity').val(qty1)
modal.find('.modal-body #cost').val(cost1)
$("#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);
            //supplier_name
            

            $('#Quantity').val("");
            $('#Unit_Price').val("");
            $('#Total').val("");
            $('#product').val(''); 
        });
        
        // 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;
}


$('#supplier_name').on('change', function(){
    var id = $(this).val();
    $.ajax({
            url: "<?php echo e(url('/getwholesalebalance')); ?>/"+id ,
            method: "get",
            headers: {
                'X-CSRF-TOKEN': $('meta[name="csrf-token"]').attr('content')
            },
            cache: false,
            contentType: false,
            processData: false,
            dataType: 'JSON',
            data:{
                "id":id
            },
            success: function (data) {
                // swal("Done!", "It was successfully deleted!", "success");

                    jQuery.each(data, function(key,value){
                        for(i=0; i<value.length; i++){
                            var rid = value[i].id;
                            $('#whobalance').val(value[i].balance);
                            // $('#wholename').val(value[i].name);
                            // $("#branch").val(value[i].balance).change();
                        }

                    });

            },
            error: function (jqXHR, textStatus, errorThrown) {
                swal("Warning", "Unable to Process your request", "error");
            },
        });
});
</script>
