<?php $__currentLoopData = $getBillingProductData; $__env->addLoop($__currentLoopData); foreach($__currentLoopData as $key=>$singleBillingProductData): $__env->incrementLoopIndices(); $loop = $__env->getLastLoop(); ?>
<?php if(isset($singleBillingProductData) && $singleBillingProductData != null): ?>
<div class="mb-3 invoice-add-item custom-invoice-common-padding">
    <div class="d-md-flex d-none row pe-5 custom-service-item-header-body">
        <p class="col-6">Service Item</p>

        <div class="custom-service-item-header-right">
            <span class="material-symbols-outlined me-0 removeInvoiceItem"
                onclick="removeBillingProduct(<?php echo e($singleBillingProductData->billing_id); ?>,<?php echo e($singleBillingProductData->id); ?>)" role="button">
                close
            </span>
        </div>
    </div>
    <div class="row border p-2 rounded ">
        <div class="w-90">
            <div class="row">
                <div class="col-md-6 mb-2">
                    <div class="form-group custom-select2-form">
                        <label for="category_id"> Service Category <span class=" text-danger">(required)</span>
                        </label>
                        <select name="category_id" id="category_id<?php echo e($singleBillingProductData->id); ?>" class="form-select select2"
                            onchange="serviceCWSFU(<?php echo e($singleBillingProductData->id); ?>)" required>
                            <option value="" selected disabled>Select Category</option>
                            <?php $__currentLoopData = $serviceCategoryData; $__env->addLoop($__currentLoopData); foreach($__currentLoopData as $singleCategory): $__env->incrementLoopIndices(); $loop = $__env->getLastLoop(); ?>
                            <option 
                            value="<?php echo e($singleCategory->id); ?>" 
                            <?php echo e($singleCategory->id == $singleBillingProductData->service_category_id ? 'selected' : ''); ?>>
                                <?php echo e($singleCategory->category_name); ?>

                            </option>
                            <?php endforeach; $__env->popLoop(); $loop = $__env->getLastLoop(); ?>
                        </select>
                    </div>

                    <?php $__errorArgs = ['category_id'];
$__bag = $errors->getBag($__errorArgs[1] ?? 'default');
if ($__bag->has($__errorArgs[0])) :
if (isset($message)) { $__messageOriginal = $message; }
$message = $__bag->first($__errorArgs[0]); ?>
                    <span class=text-danger><?php echo e($message); ?></span>
                    <?php unset($message);
if (isset($__messageOriginal)) { $message = $__messageOriginal; }
endif;
unset($__errorArgs, $__bag); ?>
                </div>
                <div class="col-md-6 mb-2">
                    <div class="form-group custom-select2-form">
                        <label for="service_id"> Service Select <span class=" text-danger">(required)</span>
                        </label>
                        <?php
                            //To get services data with service id...
                            $getServicesData = App\Models\Service::getSDWSID($singleBillingProductData->id);
                            // dd($getServicesData);
                        ?>
                        <select name="service_id" id="service_id<?php echo e($singleBillingProductData->id); ?>" class="form-select select2" 
                            onchange="addSIFU(<?php echo e($singleBillingProductData->id); ?>)" required>
                            <option value="" selected disabled>Select Service</option>
                            
                            <?php if(isset($getServicesData) && $getServicesData != null): ?>
                                <?php $__currentLoopData = $getServicesData; $__env->addLoop($__currentLoopData); foreach($__currentLoopData as $singleServiceData): $__env->incrementLoopIndices(); $loop = $__env->getLastLoop(); ?>
                                    <option 
                                    <?php echo e($singleServiceData->id == $singleBillingProductData->service_id ? 'selected' : ''); ?>

                                    value="<?php echo e($singleServiceData->id); ?>"><?php echo e($singleServiceData->service_name); ?> / <?php echo e($singleServiceData->service_price); ?></option>
                                <?php endforeach; $__env->popLoop(); $loop = $__env->getLastLoop(); ?>
                            <?php endif; ?>
                         
                        </select>
                    </div>

                    <?php $__errorArgs = ['service_id'];
$__bag = $errors->getBag($__errorArgs[1] ?? 'default');
if ($__bag->has($__errorArgs[0])) :
if (isset($message)) { $__messageOriginal = $message; }
$message = $__bag->first($__errorArgs[0]); ?>
                    <span class=text-danger><?php echo e($message); ?></span>
                    <?php unset($message);
if (isset($__messageOriginal)) { $message = $__messageOriginal; }
endif;
unset($__errorArgs, $__bag); ?>
                </div>
                <div class="col-12 mb-2">
                    <div class="row">
                        <div class="col-md-3">
                            <label for="product_price">Service Cost<span class=" text-danger">(required)</span></label>
                            <input class="form-control custom-cursor-pointer custom-readonly-bg-color" 
                                value="<?php echo e($singleBillingProductData->product_price !== null ? $singleBillingProductData->product_price : 0); ?>" type="number" name="product_price" 
                                id="product_price<?php echo e($singleBillingProductData->id); ?>" readonly required>
                        </div>
                        <div class="col-md-3">
                            <label for="product_qty">Quantity<span class=" text-danger">(required)</span></label>
                            <input class="form-control" 
                                value="<?php echo e($singleBillingProductData->product_qty !== null ? $singleBillingProductData->product_qty : 1); ?>" type="number" 
                                name="product_qty" id="product_qty<?php echo e($singleBillingProductData->id); ?>" 
                                onblur="billingPQFU(<?php echo e($singleBillingProductData->id); ?>)" required>
                        </div>
                        <div class="col-md-3">
                            <label for="product_discount">Discount<span class=" text-danger">(required)</span></label>
                            <input class="form-control" 
                                value="<?php echo e($singleBillingProductData->product_discount !== null ? $singleBillingProductData->product_discount : 0); ?>" 
                                type="number" name="product_discount" id="product_discount<?php echo e($singleBillingProductData->id); ?>" 
                                onblur="billingPDFU(<?php echo e($singleBillingProductData->id); ?>)" required>
                        </div>
                        <div class="col-md-3">
                            <label for="product_total_price">Total Price<span class=" text-danger">(required)</span></label>
                            <input class="form-control custom-cursor-pointer custom-readonly-bg-color" 
                                value="<?php echo e($singleBillingProductData->grand_total_price !== null ? $singleBillingProductData->grand_total_price : 0); ?>" 
                                type="number" name="product_total_price" id="product_total_price<?php echo e($singleBillingProductData->id); ?>" readonly required>
                        </div>
                    </div>
                </div>
                <div class="col-12">
                    <label for="description">Description</label>
                    <textarea placeholder="Enter Description" class="w-100 p-2 form-control" name="description"
                        id="product_description<?php echo e($singleBillingProductData->id); ?>" cols="3" rows="3"
                        onblur="billingPDCFU(<?php echo e($singleBillingProductData->id); ?>)"><?php echo e($singleBillingProductData->description); ?></textarea>
                </div>
            </div>
        </div>
    </div>
</div>

<script>
    $(document).ready(function(){
        $("#category_id<?php echo e($singleBillingProductData->id); ?>").select2();
    });

    $(document).ready(function(){
        $("#service_id<?php echo e($singleBillingProductData->id); ?>").select2();
    });

    //To get category wise service data...
    function serviceCWSFU(billingProductId){
        var serviceCategoryId = $("#category_id"+billingProductId).val();
        var url = "<?php echo e(route('service-list-with-category')); ?>";
        if (serviceCategoryId != '') {
            $.ajaxSetup({
                headers: {
                    'X-CSRF-TOKEN': $('meta[name="csrf-token"]').attr('content')
                }
            });
            $.ajax({
                type: 'post',
                url: url,
                data: {
                    service_category_id: serviceCategoryId,
                    billing_product_id: billingProductId
                },
                success: function (data) {
                    if(data.error){
                        toastr.error(data.error);
                    }else{
                        $("#service_id"+billingProductId).empty();
                        $("#service_id"+billingProductId).append('<option value="" selected disabled>Select Serivce</option>');
                        $.each(data.service_data,function(key,value){
                            $("#service_id"+billingProductId).append('<option value="'+value.id+'">'+value.service_name+' / '+value.service_price+'tk</option>');
                        });

                        $("#product_price"+billingProductId).val(0);
                        $("#product_qty"+billingProductId).val(1);
                        $("#product_discount"+billingProductId).val(0);
                        $("#product_total_price"+billingProductId).val(0);

                        //To update sub total price, qty, discount & grand...
                        $("#sub_total_price").val(data.sub_total_price);
                        $("#sub_total_discount").val(data.sub_total_product_discount);
                        $("#sub_total_quantity").val(data.sub_total_product_qty);
                        $("#grand_total_price").val(data.grand_total_product_price);
                    }
                }
            });
        }
    };

    //To add service id...
    function addSIFU(billingProductId){
        var serviceId = $("#service_id"+billingProductId).val();
        var url = "<?php echo e(route('add-service-id')); ?>";
        if (serviceId != '') {
            $.ajaxSetup({
                headers: {
                    'X-CSRF-TOKEN': $('meta[name="csrf-token"]').attr('content')
                }
            });
            $.ajax({
                type: 'post',
                url: url,
                data: {
                    service_id: serviceId,
                    billing_product_id: billingProductId
                },
                success: function (data) {
                    if(data.error){
                        toastr.error(data.error);
                    }else{
                        $("#product_price"+billingProductId).val(data.service_data.service_price);
                        $("#product_qty"+billingProductId).val(1);
                        $("#product_discount"+billingProductId).val(0);
                        $("#product_total_price"+billingProductId).val(data.service_data.service_price);

                        //To update sub total price, qty, discount & grand...
                        $("#sub_total_price").val(data.sub_total_price);
                        $("#sub_total_discount").val(data.sub_total_product_discount);
                        $("#sub_total_quantity").val(data.sub_total_product_qty);
                        $("#grand_total_price").val(data.grand_total_product_price);
                    }
                }
            });
        }
    };
    
    //To add product qty...
    function billingPQFU(billingProductId){
        var productPrice = parseFloat($("#product_price"+billingProductId).val());
        var productQty = parseFloat($("#product_qty"+billingProductId).val());
        var productDiscount = parseFloat($("#product_discount"+billingProductId).val());

        //To calculate total product price & subtract with discount...
        var totalProductPrice = productPrice * productQty;
        var totalPPAS = totalProductPrice - productDiscount;

        var url = "<?php echo e(route('add-product-details')); ?>";
        if (billingProductId != '') {
            $.ajaxSetup({
                headers: {
                    'X-CSRF-TOKEN': $('meta[name="csrf-token"]').attr('content')
                }
            });
            $.ajax({
                type: 'post',
                url: url,
                data: {
                    billing_product_id: billingProductId,
                    product_price: productPrice,
                    product_qty: productQty,
                    product_discount: productDiscount,
                },
                success: function (data) {
                    if(data.error){
                        toastr.error(data.error);
                    }else{
                        if(totalPPAS > 0){
                            $("#product_total_price"+billingProductId).val(totalPPAS);
                        }else{
                            $("#product_total_price"+billingProductId).val(0);
                        }

                        //To update sub total price, qty, discount & grand...
                        $("#sub_total_price").val(data.sub_total_price);
                        $("#sub_total_discount").val(data.sub_total_product_discount);
                        $("#sub_total_quantity").val(data.sub_total_product_qty);
                        $("#grand_total_price").val(data.grand_total_product_price);
                    }
                }
            });
        }
    };

    //To add product discount...
    function billingPDFU(billingProductId){
        var productPrice = parseFloat($("#product_price"+billingProductId).val());
        var productQty = parseFloat($("#product_qty"+billingProductId).val());
        var productDiscount = parseFloat($("#product_discount"+billingProductId).val());

        //To calculate total product price & subtract with discount...
        var totalProductPrice = productPrice * productQty;
        //To check product discount with total price...
        if(productDiscount > totalProductPrice){
            toastr.error('Sorry, Product discount can not greater than total price.!');
            var productDiscount = totalProductPrice;
            $("#product_discount"+billingProductId).val(productDiscount);
            var totalPPAS = totalProductPrice - productDiscount;
        }else{
            var totalPPAS = totalProductPrice - productDiscount;
        }

        var url = "<?php echo e(route('add-product-details')); ?>";
        if (billingProductId != '') {
            $.ajaxSetup({
                headers: {
                    'X-CSRF-TOKEN': $('meta[name="csrf-token"]').attr('content')
                }
            });
            $.ajax({
                type: 'post',
                url: url,
                data: {
                    billing_product_id: billingProductId,
                    product_price: productPrice,
                    product_qty: productQty,
                    product_discount: productDiscount,
                },
                success: function (data) {
                    if(data.error){
                        toastr.error(data.error);
                    }else{
                        if(totalPPAS > 0){
                            $("#product_total_price"+billingProductId).val(totalPPAS);
                        }else{
                            $("#product_total_price"+billingProductId).val(0);
                        }

                        //To update sub total price, qty, discount & grand...
                        $("#sub_total_price").val(data.sub_total_price);
                        $("#sub_total_discount").val(data.sub_total_product_discount);
                        $("#sub_total_quantity").val(data.sub_total_product_qty);
                        $("#grand_total_price").val(data.grand_total_product_price);
                    }
                }
            });
        }
    };

    //To add product description...
    function billingPDCFU(billingProductId){
        var productDescription = $("#product_description"+billingProductId).val();
        var url = "<?php echo e(route('add-product-description')); ?>";
        if (billingProductId != '') {
            $.ajaxSetup({
                headers: {
                    'X-CSRF-TOKEN': $('meta[name="csrf-token"]').attr('content')
                }
            });
            $.ajax({
                type: 'post',
                url: url,
                data: {
                    billing_product_id: billingProductId,
                    product_description: productDescription,
                },
                success: function (data) {
                    if(data.error){
                        toastr.error(data.error);
                    }else{
                        //
                    }
                }
            });
        }
    };
</script>
<?php endif; ?>
<?php endforeach; $__env->popLoop(); $loop = $__env->getLastLoop(); ?><?php /**PATH E:\Project\Personal-Project\Office-Project\update\skin-care\resources\views/admin/billingInvoice/updateBillingProductList.blade.php ENDPATH**/ ?>