<?php echo $__env->make('Admin.header', \Illuminate\Support\Arr::except(get_defined_vars(), ['__data', '__path']))->render(); ?>

<div class="main-content">
  <div class="container-fluid">

    <br>
    <br>
    <br>
    <br>
    <?php if($errors->any()): ?>
    <div class="alert alert-danger">
      <ul>
        <?php $__currentLoopData = $errors->all(); $__env->addLoop($__currentLoopData); foreach($__currentLoopData as $error): $__env->incrementLoopIndices(); $loop = $__env->getLastLoop(); ?>
        <li><?php echo e($error); ?></li>
        <?php endforeach; $__env->popLoop(); $loop = $__env->getLastLoop(); ?>
      </ul>
    </div>
    <?php endif; ?>
    <div class="row">
      <div class="col-md-12">

        <div class="card card-shadow invoice-form-container mb-4">
          <div class="card-header">
            <div class="card-title" style="float: left;">
             Add Chalan
            </div> 
            <a href="<?php echo e(route('chalan.index')); ?>"><button class="btn btn-info" style="float: right;">Chalan List</button></a>
          </div>
          <div class="card-body">
            <form method="post" action="<?php echo e(route('chalan.store')); ?>">
             <?php echo csrf_field(); ?>
            <div class="col-md-12">
                <div class="row">
                    <div class="col-md-4">
                        <div class="form-group ">
                            <label class="control-label">Company</label>
                            <div class="controls">
                                <select name="company_id" class="form-control ciq_company_select2" id="" onchange="getCompanyDetails(this.value)">
                                    <?php $__currentLoopData = $companies; $__env->addLoop($__currentLoopData); foreach($__currentLoopData as $company): $__env->incrementLoopIndices(); $loop = $__env->getLastLoop(); ?>
                                        <option value="" selected="">Select</option>
                                        <option value="<?php echo e($company->id); ?>"><?php echo e($company->name); ?></option>
                                    <?php endforeach; $__env->popLoop(); $loop = $__env->getLastLoop(); ?>
                                </select>
                            </div>
                        </div>
                    </div>
                    <div class="col-md-4">
                        <div class="form-group ">
                            <label class="control-label">City</label>
                            <div class="controls">
                                <input type="text" class="form-control" name="city" id="city" readonly="">
                            </div>
                        </div>
                    </div>
                    <div class="col-md-4">
                        <div class="form-group ">
                            <label class="control-label">Address</label>
                            <div class="controls">
                                <input type="text" class="form-control" name="address" id="address" readonly="">
                            </div>
                        </div>
                    </div>
                    <div class="col-md-6">
                        <div class="form-group ">
                            <label class="control-label">Chalan No</label>
                            <div class="controls">
                                <input type="text" class="form-control" name="chalan_no" placeholder="Chalan No" required="">
                            </div>
                        </div>
                    </div>
                    <div class="col-md-6">
                        <div class="form-group ">
                            <label class="control-label">Date</label>
                            <div class="controls">
                                <input type="text" class="form-control" id="datepicker" name="date" value="<?php echo date('d-m-Y') ?>" required="" readonly>
                            </div>
                        </div>
                    </div>
                    <div class="col-md-12">
                        <div class="form-group ">
                            <label class="control-label">Items</label>
                            <div class="controls">
                                <table class="table" id="invoice_item_table">
                                    <thead>
                                        <tr>
                                            <th>QTY</th>
                                            <th>Produt Name</th>
                                            <th>Description</th>
                                            <th>unit Price</th>
                                            <th>Action</th>
                                        </tr>
                                    </thead>
                                    <tbody>
                                        <tr class="invoice-row">
                                            <td style="width: 140px;"><input type="number" name="qty[]" class="form-control"></td>
                                            <td style="width: 250px;">
                                                <select name="product_id[]" id="" class="form-control get-product-details">
                                                    <option value="">Select</option>
                                                    <?php $__currentLoopData = $products; $__env->addLoop($__currentLoopData); foreach($__currentLoopData as $product): $__env->incrementLoopIndices(); $loop = $__env->getLastLoop(); ?>
                                                        <option value="<?php echo e($product->id); ?>"><?php echo e($product->product_name); ?></option>
                                                    <?php endforeach; $__env->popLoop(); $loop = $__env->getLastLoop(); ?>
                                                </select>
                                                <!-- <input type="text"  name="product_name[]" id="product_id" style="text-align:right;" class="form-control getProducts">

                                                <div class="filter_products">
                                                    
                                                </div> -->
                                            </td>
                                            <td><input type="text" name="description[]" class="form-control"></td>

                                            <td style="width: 150px;"><input type="number" step="0.01" name="unit_price[]" class="form-control" ></td>


                                            <td>
                                                <button id="add_invoice_row" type="button"
                                                    class="btn btn-success add_item_row btn-sm "><i
                                                        class="fa fa-plus-circle"></i> </button>
                                            </td>
                                        </tr>
                                        <tr></tr>
                                    </tbody>
                                </table>
                            </div>
                        </div>
                    </div>

                    <div class="col-md-4"></div>
                    <div class="col-md-4"></div>

                    <div class="col-md-4">
                        <div class="form-group ">
                            <label class="control-label">Total</label>
                            <div class="controls">
                                <input type="text" class="form-control" name="total" placeholder="Total" value="">
                            </div>
                        </div>
                    </div>


                    <!--<div class="col-md-6">
                        <div class="form-group ">
                            <label class="control-label">Status</label>
                            <div class="controls">
                                <select name="status" class="form-control">
                                    <option value="1">Active</option>
                                    <option value="0">Inaction</option>
                                </select>
                            </div>
                        </div>
                    </div> -->

                </div>
            </div>
            <br>
            <div align="center">
             <input type="submit" name="submit" class="btn btn-success">
           </div>
         </form>
       </div>
     </div>
   </div>
 </div>
</div>
</div>
<?php echo $__env->make('Admin.footer', \Illuminate\Support\Arr::except(get_defined_vars(), ['__data', '__path']))->render(); ?>


<!--<script src="<?php echo e(URL::to('/')); ?>/public/editor3/ckeditor.js"></script> -->
<link href="https://cdn.jsdelivr.net/npm/summernote@0.8.18/dist/summernote-bs4.min.css" rel="stylesheet">
<script src="https://cdn.jsdelivr.net/npm/summernote@0.8.18/dist/summernote-bs4.min.js"></script>

<script>
    $('#description').summernote({
        focus: true,
        toolbar: [
            // [groupName, [list of button]]
            ['style', ['bold', 'italic', 'underline', 'clear']],
            //['font', ['strikethrough', 'superscript', 'subscript']],
            ['fontsize', ['fontsize']],
            ['fontname', ['fontname']],
            ['color', ['color']],
            ['para', ['ul', 'ol', 'paragraph']],
            ['height', ['height']],
            ['table', ['table']],
            ['insert', ['link', 'picture', 'video']],
            ['view', ['fullscreen', 'codeview', 'help']],
        ],
        fontSizes: ['8', '9', '10', '11', '12', '13', '14', '15', '16', '17', '18', '24', '36', '48', '64',
            '82', '150'
        ],
        popover: {
            air: [
                ['color', ['color']],
                ['font', ['bold', 'underline', 'clear']]
            ]
        }
    });

    $(document).on('click', '.add_item_row', function(){
        var html = '';
        html += '<tr class="invoice-row">'
            +'<td style="width: 140px;"><input type="number" name="qty[]" class="form-control"></td>'                   
            // +'<td><input type="text"  name="product_name[]" style="text-align:right;" class="form-control getProductDetails"></td>'                   
            +'<td style="width: 250px;"><select name="product_id[]" id="" class="form-control get-product-details"><option value="">Select</option><?php $__currentLoopData = $products; $__env->addLoop($__currentLoopData); foreach($__currentLoopData as $product): $__env->incrementLoopIndices(); $loop = $__env->getLastLoop(); ?><option value="<?php echo e($product->id); ?>"><?php echo e($product->product_name); ?></option><?php endforeach; $__env->popLoop(); $loop = $__env->getLastLoop(); ?></select></td>'
            +'<td><input type="text" name="description[]" class="form-control"></td>'                   
            +'<td style="width: 150px;"><input type="number" step="0.01" name="unit_price[]" class="form-control"></td>'                   

         +'<td><button type="button" name="remove" class="btn btn-danger btn-sm remove mr-1"><span class="fa fa-minus-circle"></span></button><button id="add_item_row" type="button" class="btn btn-success add_item_row btn-sm "><i class="fa fa-plus-circle"></i> </button></td></tr>';
        $('#invoice_item_table').append(html);
    });

    $(document).on('click', '.remove', function(){
        $(this).closest('tr').remove();
    });

    $(document).on('change', '.get-product-details', function() {
        $this = $(this).val();

        var datastr = "product_id=" + $this + "&token=" + token;
        $.ajax({
            type: "post",
            url: "<?php echo route('get-product-details'); ?>",
            data: datastr,
            cache: false,
            success: (data) => {
                // data.in_qt_ch_des
                // console.log($('.get-product-details').closest('td'));
                $(this).parents('.invoice-row').find('[name^=unit_price]').val(data.current_price);
                calculateTotal();
            },
            error: function(jqXHR, status, err) {
                alert(status);
            },
            complete: function() {
                // alert("Complete");
            }
        });
    });

    // manage add invoice page calculation
    const calculateTotal = () => {
        let total = 0;
        let paid = 0;
        let due = 0;

        // get all information from invoice item table
        $('.invoice-row').each((index, row) => {
            const quantity = $(row).find('[name^=qty]').val();
            const unit_price = $(row).find('[name^=unit_price]').val();
            if (!quantity || !unit_price) {
                return;
            }

            total += (unit_price * quantity);
        });

        paid = +$('[name=paid]').val() || 0;
        due = total - paid;

        // put total and due in the respective fields
        $('[name=total]').val(total);
        $('[name=due]').val(due);
    };
    $('.invoice-form-container').on('input', 'input', function() {
        calculateTotal();
    });
</script>


<?php /**PATH D:\wamp64\www\electronics\resources\views/Admin/company_chalan/create.blade.php ENDPATH**/ ?>