<script>

    //Check product list...
    function checkProductList() {
    event.preventDefault();
        toastr.error('Please select product first.!');
    }

    //To fetch all the product data with category and brand...
    $("#category_id").change(function () {
        var categoryId = $(this).val();
        var url = "<?php echo e(route('filter.product-list')); ?>";
        if (categoryId !== '' && categoryId !== null) {
            $.ajaxSetup({
                headers: {
                    'X-CSRF-TOKEN': $('meta[name="csrf-token"]').attr('content')
                }
            });
            $.ajax({
                type: 'post',
                url: url,
                data: {
                    categoryId: categoryId,
                },
                success: function (data) {
                    $("#filterProductList").html(data);
                }
            });
        }
    });
    
    //To search product data...
    function searchProduct() {
        event.preventDefault();
        var searchValue = $("#search").val();
        var url = "<?php echo e(route('search.product-list')); ?>";

        if(searchValue != ''){
            $.ajaxSetup({
                headers: {
                    'X-CSRF-TOKEN': $('meta[name="csrf-token"]').attr('content')
                }
            });
            $.ajax({
                type: 'post',
                url: url,
                data: {
                    searchValue: searchValue,
                },
                success: function (data) {
                    if(data.error){
                        toastr.error(data.error);
                    }else{
                        $("#filterProductList").html(data);
                    }
                }
            });
        }else{
            toastr.error('Please enter product code or name.!');
        }
    }

    //To reset search product...
    function resetSearch() {
        event.preventDefault();
        var searchValue = $("#search").val('');
        $('#category_id').val(null).trigger("change");
        $('#brand_id').val(null).trigger("change");

        var url = "<?php echo e(route('reset.product-list')); ?>";

        $.ajaxSetup({
            headers: {
                'X-CSRF-TOKEN': $('meta[name="csrf-token"]').attr('content')
            }
        });

        $.ajax({
            type: 'GET',
            url: url,
            success: function (data) {
                $("#filterProductList").html(data);
            }
        });
    }

    //To add new Client...
    function addNewClient() {
        event.preventDefault();
        let clientName = $("#client_name").val();
        let clientEmail = $("#client_email").val();
        let clientPhone = $("#client_mobile").val();
        let clientAddress = $("#client_address").val();
        let selectedSellId = $("#sellId").val();
        if (clientPhone != '' && clientPhone.length != 11) { 
            event.preventDefault();
            toastr.error("Mobile number must be 11 digit."); 
            return false;
        }

        let url = "<?php echo e(route('sell.add-new-client')); ?>";
        if (clientName != '') {
            $.ajaxSetup({
                headers: {
                    'X-CSRF-TOKEN': $('meta[name="csrf-token"]').attr('content')
                }
            });
            $.ajax({
                type: 'post',
                url: url,
                data: {
                    clientName: clientName,
                    clientEmail: clientEmail,
                    clientPhone: clientPhone,
                    clientAddress: clientAddress,
                    sellId: selectedSellId
                },
                success: function (data) {
                    $("#clientId").html(data);

                    $("#client_name").val('');
                    $("#client_email").val('');
                    $("#client_mobile").val('');
                    $("#client_address").val('');

                    $("#addNewClient").modal('hide');
                }
            });
        }
    }

    //For modal open purchase product add to the sell product list...
    function sellProductDetailsModal(id) {
        event.preventDefault();
        var singleProductId =id;
        var url = "<?php echo e(route('sell.get-single-product-info')); ?>";
        $.ajaxSetup({
            headers: {
                'X-CSRF-TOKEN': $('meta[name="csrf-token"]').attr('content')
            }
        });

        $.ajax({
            type: 'POST',
            url: url,
            data:{
                singleProductId: singleProductId
            },
            success: function (data) {
                $("#purchase_serial_batch_id"+id).select2({
                    dropdownParent: $('#sellProductDetailsModal'+id)
                });
                $("#sellProductDetailsModal"+id).modal('show');
                $("#singleProductInformation"+id).html(data);
            }
        });
    }

    //For purchase product add to the sell product list...
    function addSellProductDetails(id) {
        event.preventDefault();

        var sellId = $("#sellId").val();
        var productId = $("#productId" + id).val();
        var purchaseSerialBatchId = $("#purchase_serial_batch_id" + id).val();

        var url = "<?php echo e(route('add.sell.product-deatils')); ?>";
        if(sellId != ''){
            $.ajaxSetup({
                headers: {
                    'X-CSRF-TOKEN': $('meta[name="csrf-token"]').attr('content')
                }
            });
            $.ajax({
                type: 'post',
                url: url,
                data: {
                    sellId: sellId,
                    productId: productId,
                    currentPurchaseProductId: purchaseSerialBatchId,
                },

                success: function (data) {
                    if(data.error){
                        toastr.error(data.error);
                    }else{
                        $("#updateSellList").html(data);
                        $("#sellProductDetailsModal"+id).modal('hide');
                    }
                }
            });
        }else{
            //
        }
    }

    //To add sell product quantity...
    function addSellProductQty(id) {
        event.preventDefault();
        var sellProductId = id;
        var sellProductQty = $("#sellProductQty"+sellProductId).val();
        var url = "<?php echo e(route('add-product-qty.sell-product')); ?>";
        if (sellProductId != '') {
            $.ajaxSetup({
                headers: {
                    'X-CSRF-TOKEN': $('meta[name="csrf-token"]').attr('content')
                }
            });
            $.ajax({
                type: 'post',
                url: url,
                data: {
                    sellProductId: sellProductId,
                    sellProductQty: sellProductQty,
                },
                success: function (data) {
                    if(data.error){
                        toastr.error(data.error);
                        $("#sellProductQty"+sellProductId).val(data.sell_product);
                    }else{
                        $("#updateSellProData"+sellProductId).modal('hide');
                        $("#updateSellList").html(data);
                    }
                }
            });
        }
    }

    //To remove the sell product data from product list....
    function deleteSellProduct(id) {
        event.preventDefault();
        var sellProductId = id;
        var url = "<?php echo e(route('sell-list.remove-product')); ?>";
        if (sellProductId != '') {
            $.ajaxSetup({
                headers: {
                    'X-CSRF-TOKEN': $('meta[name="csrf-token"]').attr('content')
                }
            });
            $.ajax({
                type: 'post',
                url: url,
                data: {
                    sellProductId: sellProductId,
                },
                success: function (data) {
                    $("#updateSellList").html(data);
                }
            });
        }
    }

    //To add product details...
    function addProductDetailsFMP(id) {
        event.preventDefault();
        alert(id);
        var purchaseId = $("#purchaseId").val();
        var productId = $("#productId" + id).val();
        var productQty = $("#product_qty_fmp" + id).val(); 
        var mrpPrice = $("#mrp_price_fmp" + id).val();
        var sellingPrice = $("#selling_price_fmp" + id).val();
        var totalProductPrice = $("#total_product_price_fmp" + id).val();

        var url = "<?php echo e(route('add.product-deatils')); ?>";
        if(purchaseId != ''){
            $.ajaxSetup({
                headers: {
                    'X-CSRF-TOKEN': $('meta[name="csrf-token"]').attr('content')
                }
            });
            $.ajax({
                type: 'post',
                url: url,
                data: {
                    purchaseId: purchaseId,
                    productId: productId,
                    productQty: productQty,
                    mrpPrice: mrpPrice,
                    sellingPrice: sellingPrice,
                    totalProductPrice: totalProductPrice,
                },

                success: function (data) {
                    if (data.error) {
                        toastr.error(data.error);
                    }else{
                        toastr.success('Success product added to purchase list.');

                        //To purchase modal to hide...
                        $("#purchaseProductCreate" + productId).modal('hide');
                        $("#updatePurchaseList").html(data);

                        //To purchase-product modal data null...
                        var singleUnitPrice = $("#unit_price" + id).val();
                        $("#product_qty_fmp" + id).val('1');
                        $("#mrp_price_fmp" + id).val(singleUnitPrice);
                        $("#selling_price_fmp" + id).val(0);
                        $("#total_product_price_fmp" + id).val(singleUnitPrice);
                    }
                },
                error: function (jqXHR) {
                    if (jqXHR.status === 422) {
                        event.preventDefault();
                        toastr.error('Validation error occurred. Please check the required fields.');
                    } else {
                        toastr.error('An unexpected error occurred.');
                    }
                }
            });
        }
    }

    //productQuantity calculation singleProductPrice and totalProduct
    function productQuantityFMP(id) {
        let singleProductPrice = parseFloat($("#mrp_price_fmp" + id).val());
        let productQty = parseFloat($("#product_qty_fmp" + id).val());
    
        if (productQty >= 0) {
            let totalProductPrice = singleProductPrice * productQty;
            $("#total_product_price_fmp" + id).text(totalProductPrice);
            $("#total_product_price_fmp" + id).val(totalProductPrice);

        }else {
            $("#total_product_price_fmp" + id).text(0);
            $("#total_product_price_fmp" + id).val(0);
        }

    }

    //To purchase product modal show...
    function purchaseProductCreate(productId) {
        var purchaseUniqueId = $("#purchaseId").val();

        var url = "<?php echo e(route('single-purchase-product-data')); ?>";
        if (purchaseUniqueId != null) {
            $.ajaxSetup({
                headers: {
                    'X-CSRF-TOKEN': $('meta[name="csrf-token"]').attr('content')
                }
            });
            $.ajax({
                type: 'post',
                url: url,
                data: {
                    purchaseUniqueId: purchaseUniqueId,
                    productId: productId
                },
                success: function (data) {
                   if(data.error){
                        toastr.error(data.error);
                    }else{
                        $("#purchaseProductCreate"+productId).modal('show');
                    }
                }
            });
        }
    }
</script><?php /**PATH E:\Project\Personal-Project\Office-Project\update\skin-care\resources\views/admin/productManage/sellProduct/partial/script.blade.php ENDPATH**/ ?>