<?php $__env->startSection('title', 'Purchase'); ?>
<?php $__env->startPush('css'); ?>
    <link rel="stylesheet" href="<?php echo e(asset('backend/plugins/select2.min.css')); ?>">
    <link rel="stylesheet" href="<?php echo e(asset('myfile/backend/assets/datepicker/zebra_datepicker.min.css')); ?>" type="text/css">
    <link rel="stylesheet" href="<?php echo e(asset('myfile/backend/assets/datepicker/examples.css')); ?>" type="text/css">
    <style>
        input[readonly] {
            background-color: #eee;
            opacity: 1;
            border: 1px solid #c7c5c5;
        }
        .required{
            color:red;
            font-weight:bold;
        }
    </style>
<?php $__env->stopPush(); ?>
<!-- page content -->
<?php $__env->startSection('content'); ?>
    <div class="right_col" role="main">
        <div class="">
            <div class="clearfix"></div>
            <?php if(Session::has('success')): ?>
                <div class="alert alert-success">
                    <?php echo e(Session::get('success')); ?>

                </div>
            <?php endif; ?>
            <?php if(Session::has('error')): ?>
                <div class="alert alert-danger">
                    <?php echo e(Session::get('error')); ?>

                </div>
            <?php endif; ?>
            <div class="row">
                <div class="col-md-12 col-sm-12 col-xs-12">
                    <div class="x_panel">
                        <div class="x_title">
                            <h2>
                                Purchase Create 

                                 <a href="<?php echo e(route('purchase.list')); ?>" class="btn btn-xs btn-primary">Purchase List</a>
                            </h2> 
                            <ul class="nav navbar-right panel_toolbox">
                                <li>
                                    <a class="collapse-link"><i class="fa fa-chevron-up"></i></a>
                                </li>
                                <li class="dropdown">
                                    <a href="#" class="dropdown-toggle" data-toggle="dropdown" role="button"
                                       aria-expanded="false"><i class="fa fa-wrench"></i></a>
                                    <ul class="dropdown-menu" role="menu">
                                        <li><a href="#">Settings 1</a>
                                        </li>
                                        <li><a href="#">Settings 2</a>
                                        </li>
                                    </ul>
                                </li>
                                <li><a class="close-link"><i class="fa fa-close"></i></a>
                                </li>
                            </ul>
                            <div class="clearfix"></div>
                        </div>
                        <div class="x_content">
                            <form action="<?php echo e(route('purchase.store')); ?>" method="post" class="form-horizont" role="form" autocomplete="off" >
                             <?php echo e(csrf_field()); ?>

                                <div class="row">
                                    <div class="col-sm-6">
                                        <div class="form-group">
                                            <label class="col-sm-3 control-label no-padding-right" for="form-field-1"> Supplier <span class="required">*</span></label>
                                            <div class="col-sm-9">
                                                <select name="supplier_name" id="supplier_id" class="form-control input-sm select2" required="required">
                                                    <option value="">-Select Supplier-</option>
                                                    <?php $__currentLoopData = $suppliers; $__env->addLoop($__currentLoopData); foreach($__currentLoopData as $sup): $__env->incrementLoopIndices(); $loop = $__env->getLastLoop(); ?>
                                                        <option value="<?php echo e($sup->id); ?>" <?php echo e($sup->id == old('supplier_name') ? 'Selected' : ''); ?>><?php echo e($sup->name); ?></option>
                                                    <?php endforeach; $__env->popLoop(); $loop = $__env->getLastLoop(); ?>
                                                </select> 
                                                <p class="error-sms"><?php echo e($errors->first('supplier_name')); ?></p>
                                            </div>
                                        </div>
                                        <div class="form-group">
                                            <label class="col-sm-3 control-label no-padding-right" for="form-field-1"> Chalan No <span class="required">*</span></label>
                                            <div class="col-sm-9">
                                                <input type="text" name="chalan_no"  class="form-control" value="<?php echo e(old('chalan_no') ?? $order_no); ?>" readonly placeholder="Order No">
                                                <p class="error-sms"><?php echo e($errors->first('chalan_no')); ?></p>
                                            </div>
                                        </div>
                                    </div>
                                    <div class="col-sm-6">
                                        <div class="form-group">
                                            <label class="col-sm-3 control-label no-padding-right" for="form-field-1"> Purchase Date <span class="required">*</span></label>
                                            <div class="col-sm-9">
                                                <input id="datepicker" type="text" name="purchase_date"  class="form-control" data-zdp_readonly_element="true" value="<?php echo e(date('d-m-Y')); ?>" placeholder="dd-mm-yyyy">
                                                <p class="error-sms"><?php echo e($errors->first('purchase_date')); ?></p>
                                            </div>
                                        </div>
                                        <div class="form-group">
                                            <label class="col-sm-3 control-label no-padding-right" for="form-field-1"> Note</label>
                                            <div class="col-sm-9">
                                                <input type="text" name="note"  class="form-control" placeholder="Note About this Purchase">
                                                <p class="error-sms"><?php echo e($errors->first('note')); ?></p>
                                            </div>
                                        </div>
                                    </div>
                                </div>
                                <hr/>
                                <div class="row">
                                    <div class="col-sm-3"></div>
                                    <div class="col-sm-3">
                                        <div class="form-group ">
                                            <label>Select Category <span class="required">*</span></label>
                                            <select class="form-control select2" name="category_id" id="category">
                                                <option value="">- Select Category -</option>
                                                <?php $__currentLoopData = $categories; $__env->addLoop($__currentLoopData); foreach($__currentLoopData as $category): $__env->incrementLoopIndices(); $loop = $__env->getLastLoop(); ?>
                                                <option value="<?php echo e($category->id); ?>"> <?php echo e($category->name); ?> </option>
                                                <?php endforeach; $__env->popLoop(); $loop = $__env->getLastLoop(); ?>
                                            </select>
                                        </div>     
                                    </div>
                                    <div class="col-sm-3">
                                        <div class="form-group">
                                            <label>Select Product <span class="required">*</span></label>
                                            <select class="form-control select2" name="product_name" id="product">
                                                <option value="">-Select Product-</option>
                                            </select>
                                        </div>  
                                    </div>
                                    <div class="col-sm-3"></div>
                                </div>
                                <table class="table table-condensed table-bordered table-hover table-header-fixed dt-responsive green" id="sale">
                                    <thead class="info">
                                        <tr>
                                            <th class="all">SL </th>
                                            <th class="all">Product </th>
                                            <th class="all">Category </th>
                                            <th class="all">Unit </th>
                                            <th class="all">Code No </th>
                                            <th class="all">Quantity </th>
                                            <th class="all">Square Feet</th>
                                            <th class="all" title="Total Square Foot" >TSQF</th>
                                            <th class="all">MRP Rate</th>
                                            <th class="all">Sub Total</th>
                                           <th class="all">Delete</th>
                                        </tr>
                                    </thead>
                                    <tbody>
                                    </tbody>
                                    <tfoot>
                                        <tr>
                                            <th>-</th>
                                            <th class="all" colspan="4">Total</th>
                                            <th id="total_ordered">0</th>
                                            <th>-</th>
                                             <th>-</th>
                                            <th>-</th>
                                            <th id="total_sub">0</th>
                                            <th>-</th>
                                        </tr>
                                    </tfoot>
                                </table>
                                <div class="row">
                                    <div class="col-sm-7">
                                        <div class="form-group">
                                            <label class="control-label col-sm-4" for="discount">Discount</label>
                                            <div class="col-sm-8 ">
                                              <input type="text" class="form-control" min="0" value="0" name="discount" id="discount">
                                                <p class="error-sms"><?php echo e($errors->first('discount')); ?></p>
                                            </div>
                                        </div>
                                        <div class="form-group">
                                            <label class="control-label col-sm-4">Discount Type</label>
                                            <div class="col-sm-8 ">          
                                                <select class="form-control select2" id="discount_type" name="discount_type">
                                                    <option> Fixed</option>
                                                    <option>Percentage </option>
                                                </select>
                                                <p class="error-sms"><?php echo e($errors->first('discount_type')); ?></p>
                                            </div>
                                        </div>
                                        <!--<div class="form-group">
                                            <label class="control-label col-sm-4">Payment Status</label>
                                            <div class="col-sm-8 "> 
                                                <select class="form-control select2" name="payment_status">
                                                    <option>Paid </option>                                                   
                                                    <option>Due </option>
                                                </select>
                                            </div>    
                                        </div>-->
                                        <div class="form-group">
                                            <label class="control-label col-sm-4">Payment Amount</label>
                                            <div class="col-md-8">
                                                <input type="number" class="form-control" id="payment" min="0" name="payment" >
                                                <p class="error-sms"><?php echo e($errors->first('payment')); ?></p>
                                            </div>  
                                        </div>
                                        <!--<div class="form-group">-->
                                        <!--    <label class="control-label col-sm-4">Check Information</label>-->
                                        <!--    <div class="col-sm-4">-->
                                        <!--        <div class="checkbox">-->
                                        <!--            <label><input type="checkbox" class="ch" name="sms"> Send SMS </label>-->
                                        <!--        </div>-->
                                        <!--    </div>-->
                                        <!--</div>-->
                                    </div>
                                    <div class="col-sm-5">
                                        <table style="max-width:80%;margin:0 auto" class="table table-striped table-bordered table-hover table-header-fixed" id="sample_2" >
                                            <tfoot>
                                                <tr>
                                                    <th> Name: </th>
                                                    <th>Amount:</th>
                                                </tr>
                                                <tr>
                                                    <th> Items: </th>
                                                    <th><span class="pull-right" id="total_items">0</span></th>
                                                </tr>
                                                <tr>
                                                    <th> Total: </th>
                                                    <th><span class="pull-right" id="total_value">0</span></th>
                                                </tr>
                                                <tr>    
                                                    <th> Discount: </th>
                                                    <th> <span class="pull-right" id="total_discount">0</span></th>
                                                </tr>
                                                <!--<tr>
                                                    <th> Tax </th><th>  <span class="pull-right" id="total_tax">0</span></th>
                                                </tr>
                                                <tr>
                                                    <th> Shipping </th><th>  <span class="pull-right" id="total_shipping">0</span></th>
                                                </tr>-->
                                                <tr>
                                                    <th> Grand Total: </th>
                                                    <th>  <span class="pull-right" id="grand_total">0</span></th>
                                                </tr>
                                                <tr>
                                                    <th> Due: </th>
                                                    <th>  <span class="pull-right" id="due">0</span></th>
                                                </tr>
                                            </tfoot>
                                        </table>
                                        <input type="hidden" id="total_price" name="total_price"  />
                                        <input type="hidden" class="grand_total" name="grand_total"  />
                                        <input type="hidden" class="due" name="due"  />
                                    </div>
                                </div>
                                <hr>
                                <div class="form-group">
                                    <div class="col-md-6 col-sm-6 col-xs-12 col-md-offset-5">
                                      <button class="btn btn- btn-warning" type="reset">Reset</button>
                                      <button type="submit" class="btn btn- btn-success">Save</button>
                                    </div>
                                </div>
                                <br><br>
                            </form>
                        </div>
                    </div>
                </div>
            </div>
        </div>
    </div>
    <!-- /page content -->
<?php $__env->stopSection(); ?>
<?php $__env->startPush('js'); ?>
    <script src="<?php echo e(asset('backend/plugins/select2.min.js')); ?>"></script>
    <!-- Jquery Plugin For Date Calender-->
    <script type="text/javascript" src="<?php echo e(asset('myfile/backend/assets/datepicker/zebra_datepicker.src.js')); ?>"></script>
    <!-- jQuery autocomplete -->
    <script type="text/javascript">
        $(document).ready(function(){
            $('#datepicker').Zebra_DatePicker({
                format: 'd-m-Y',
                direction: 1,
            });
            /*-------------*/
            $(".select2").select2();
            /*---------------*/
            $("#category").change(function () {
                var category = $(this).val();
                //var supplier = $('#supplier_id').val();
                var token = "<?php echo e(csrf_token()); ?>";
                if(category != ''){
                    $.ajax({
                        url: '<?php echo e(route("purchase.productList")); ?>',
                        type: 'Get',
                        data: {category:category, '_token' : token},
                        dataType: 'json',
                        success: function (response) {
                        //console.log(response)
                            var len = response.length;
                            $("#product").empty();
                            $("#product").append("<option value=''> Select Product</option>");
                            if (len == 0) {
                                $("#product").empty();
                                var id = "";
                                var name = "";
                                $("#product").append("<option value='" + id + "'>" + name + "</option>");
                            }
                            for (var i = 0; i < len; i++) {
                                var id = response[i]['id'];
                                var name = response[i]['product_name'];
                                var code = response[i]['code'];
                                var disable = "";
                                $("#product").append("<option " + disable + " value='" + id + "'>"+name+" ( "+code+" )</option>");
                            }
                        }
                    });
                }
                else{
                    alert('Please Select Supplier ans Category!!')
                }
            });

        var array = [];
        var total_tax = 0;
        var tax_type = "Fixed";
        var sl = 1
        $("#product").change(function () {
            var id = $(this).val();
           // $("#supplier").attr('disabled', true);
            var token = "<?php echo e(csrf_token()); ?>";

            $.ajax({
                url: '<?php echo e(route("purchase.productDetails")); ?>',
                type: 'Get',
                data: {id: id, '_token' : token},
                dataType: 'json',
                success: function (response) {
            //alert(response)
            //console.log(response)
                    var len = response.length;
                   // $("#product").empty();
                   // $("#product").append("<option value=''> Select Product</option>");
                    for (var i = 0; i < len; i++) {
                        var id = response[1][i]['id'];
                        var name = response[1][i]['name'];
                        var disable = "";
                        $("#product").append("<option " + disable + " id='product_" + id + "' value='" + id + "'>" + name + "</option>");
                    }

                    var id = response['id'];  
                    var name = response['product_name'];                    
                    var category_name = response['category']['name'];               
                    //var brand_name = response['brand']['brand_name'];               
                    var unit_name = response['unit']['name'];               
                    var code =  response['code'];
                    var supplier_price =  response['supplier_price'];
                    var sale_price =  response['sale_price'] + 10;
                    var square_foot =  response['square_foot'];
                    var ordered_quantity =  1;
                    var tsqf = ordered_quantity * square_foot;
                    var sub_total =  parseFloat(tsqf*supplier_price).toFixed(2);
                    sl++
                    $("#sale tbody").append(
                        "<tr id='"+sl+"'>"
                          +"<td>"+sl+"</td>"
                          +"<td><input type='hidden' name='product_id[]' value='"+id+"' />"+name+" </td>"
                          +"<td><input type='hidden' name='category_name[]' value='"+category_name+"' />"+category_name+" </td>"
                          +"<td><input type='hidden' name='unit_name[]' value='"+unit_name+"' />"+unit_name+" </td>"
                          +"<td><input type='hidden' name='code[]' value='"+code+"' />"+code+" </td>"
                          +"<td><input type='text' id='"+sl+"_ordered_quantity' class='ordered_quantity' name='ordered_quantity[]' min='1' value='"+ordered_quantity+"' size='6' /></td>"
                          +"<td><input type='text'  id='"+sl+"_square_foot' name='square_foot[]' value='"+square_foot+"' readonly size='6'></td>"
                          +"<td><input type='text'  id='"+sl+"_tsqf' name='tsqf[]' value='"+tsqf+"' readonly size='6'></td>"
                          +"<td><input id='"+sl+"_price' type='text' name='supplier_price[]' class='supplier_price' value='"+supplier_price+"' size='6'/></td>"
                          +"<td class='sub_total' id='"+sl+"_sub_total'>"+sub_total+"</td>"
                          +"<td class='text-center'><i class='red fa fa-trash fa-lg'></i></td>"
                        +"</tr>");

                    array.push(id);
                    var idLength = array.length;
                    for(j = 0; j<idLength; j++){
                        $('#product_'+array[j]).attr('disabled','disabled').select2({placeholder:"",width:"auto",allowClear:!0});
                    }

                    var total_ordered = parseFloat($('#total_ordered').html());
                    total_ordered = total_ordered + 1;
                    $('#total_ordered').html(total_ordered);

                    calculate_sub_total();
                    calculate_total();
                    calculate_due();
                }
            });
        });
        
        $(document).on('click','.fa-trash',function(){
            var id = $(this).parent().parent().attr('id');
            $('#'+id).remove();

            $('#product_'+id).removeAttr('disabled');
            array.splice($.inArray(id, array), 1);

           // $('#product').select2("destroy").select2({placeholder:"",width:"auto",allowClear:!0});

            calculate_ordered_quantity();
            calculate_sub_total();
            calculate_total();
            calculate_due();
        });
        $(document).on('keyup','.ordered_quantity',function(){
            var id = $(this).parent().parent().attr('id');
            var price = $('#'+id+'_price').val();
            var quantity = $('#'+id+'_ordered_quantity').val();
            var squarefoot = $('#'+id+'_square_foot').val();
            var tsqf = squarefoot * quantity;
            var sub_total =  parseFloat(tsqf*price).toFixed(2);
            
            $('#'+id+'_tsqf').val(tsqf);
            $('#'+id+'_sub_total').html(sub_total);

            calculate_ordered_quantity();
            calculate_sub_total();
            calculate_total();
            calculate_due();
        });
        $(document).on('keyup','.supplier_price',function(){
            var id = $(this).parent().parent().attr('id');
            var price = $('#'+id+'_price').val();
            var quantity = $('#'+id+'_ordered_quantity').val();
            var squarefoot = $('#'+id+'_square_foot').val();
            var tsqf = squarefoot * quantity;
            var sub_total =  parseFloat(tsqf*price).toFixed(2);
            
            $('#'+id+'_sub_total').html(sub_total);
            
            calculate_ordered_quantity();
            calculate_sub_total();
            calculate_total();
            calculate_due();
        });
        $(document).on('keyup','#discount',function(){
            calculate_discount();
            calculate_grand_total();
            calculate_due();
        });
        $(document).on('change','#discount_type',function(){
            calculate_discount();
            calculate_grand_total();
            calculate_due();
        });
        $(document).on('keyup','#payment',function(){
            calculate_discount();
            calculate_grand_total();
            calculate_due();
        });
        function calculate_ordered_quantity(){
            var total_ordered = 0;
            //iterate through each td based on class and add the values
            $(".ordered_quantity").each(function() {
                var value = parseFloat($(this).val());
                total_ordered = total_ordered + value;
            });
            $('#total_ordered').html(total_ordered);
        }
        function calculate_discount(){
            var discount = $('#discount').val();
            var type = $('#discount_type').val();
            if (type != 'Fixed') {
                discount = parseFloat($('#total_value').html()) * discount / 100;
            }
            
            $('#total_discount').html(discount);
        }
        function calculate_sub_total(){
            var total_sub = 0;
            //iterate through each td based on class and add the values
            $(".sub_total").each(function() {
                var value = parseFloat($(this).html());
                total_sub = total_sub + value;
            });
            $('#total_sub').html(total_sub);
        }

        function calculate_grand_total(){
            
           // var grand_total = parseFloat($('#total_value').html())-parseFloat($('#total_discount').html())+parseFloat($('#total_tax').html())+parseFloat($('#total_shipping').html());
            var grand_total = parseFloat($('#total_value').html())-parseFloat($('#total_discount').html());
            $('#grand_total').html(grand_total);
            $('.grand_total').val(grand_total);
            $('#due').html(grand_total);
            //alert(grand_total)
        }

        function calculate_total(){
            var total_items = 0;            
            var total_sub = 0;
            //iterate through each td based on class and add the values
            $(".sub_total").each(function() {
                total_items++;
            });

            $('#total_items').html(total_items);
            $('#total_value').html($('#total_sub').html());
            $('#total_price').val($('#total_sub').html());

            calculate_discount();
           // calculate_tax();
            //calculate_shipping();
            calculate_grand_total();
        }
        // due section
        function calculate_due(){
            var pay = $('#payment').val() || 0;
            var grand_total = $('.grand_total').val() || 0;
            var due = parseInt(grand_total) - parseInt(pay);
            $('.due').val(due);
            $('#due').html(due);
        }
        })
    </script>
<?php $__env->stopPush(); ?>
<?php echo $__env->make('backend.layouts.master', array_except(get_defined_vars(), array('__data', '__path')))->render(); ?>