
<div class="card block" x-data="block_products">
    <div class="card-body" >
        <div class="row mb-2">
            <div class="col-lg-12">
                <span class="float-left">
                    <h4 class="block-title">
                         <i class="fa fa-list-alt text-success"></i>
                        <?php echo e(__("Products")); ?>

                    </h4>
                </span>
            </div>
        </div>
        <div class="row mt-10">

            <div class="col-lg-12 m-b-5">
                <div class="pull-right">
                    <div class="form-group">
                        <input type="text" x-model="search" class="form-control form-control-sm"
                               placeholder="Product Name, Ref ...">
                         <small class="float-right">
                              <strong class="fs-10 text-blue">
                                <strong x-text="'Found : '+ formatNumber(filteredProducts.length)+' / '"></strong>
                               <strong x-text="'Total : '+formatNumber(products.length)">/</strong>
                            </strong>
                         </small>
                    </div>
                </div>
            </div>

            <div class="col-lg-12">
                <template x-if="blank(filteredProducts)">
                    <?php if (isset($component)) { $__componentOriginal13e1f50f0bc62a8194c32460ab51a9dc01c30e0e = $component; } ?>
<?php $component = App\View\Components\NotFound::resolve([] + (isset($attributes) && $attributes instanceof Illuminate\View\ComponentAttributeBag ? (array) $attributes->getIterator() : [])); ?>
<?php $component->withName('not-found'); ?>
<?php if ($component->shouldRender()): ?>
<?php $__env->startComponent($component->resolveView(), $component->data()); ?>
<?php if (isset($attributes) && $attributes instanceof Illuminate\View\ComponentAttributeBag && $constructor = (new ReflectionClass(App\View\Components\NotFound::class))->getConstructor()): ?>
<?php $attributes = $attributes->except(collect($constructor->getParameters())->map->getName()->all()); ?>
<?php endif; ?>
<?php $component->withAttributes([]); ?>
                         <?php $__env->slot('tittle', null, []); ?> <?php echo e(__("No Product found.")); ?> <?php $__env->endSlot(); ?>
                         <?php $__env->slot('desc', null, []); ?> '<?php echo e(__('Make sure your typing a correct value filter, or click to refresh products list.')); ?>' <?php $__env->endSlot(); ?>
                     <?php echo $__env->renderComponent(); ?>
<?php endif; ?>
<?php if (isset($__componentOriginal13e1f50f0bc62a8194c32460ab51a9dc01c30e0e)): ?>
<?php $component = $__componentOriginal13e1f50f0bc62a8194c32460ab51a9dc01c30e0e; ?>
<?php unset($__componentOriginal13e1f50f0bc62a8194c32460ab51a9dc01c30e0e); ?>
<?php endif; ?>
                </template>
            </div>
            <div class="col-lg-12">
                <div class="row" style="max-height:500px; overflow-y:scroll">
                    <template x-for="product in filteredProducts" :key="product.id">

                        <div class="col-lg-4 col-xl-4 col-xxl-3 col-6 form-group"
                             x-data="product_availability(product)">
                            <div class="product-card cursor-pointer rounded "
                                @click="addToCart(product,is_available);">
                                <div x-transition x-show="!is_available" class="badge badge-danger fs-8 badge-nav"><?php echo e(__("Unavailable")); ?></div>
                                <div class="product-tumb"
                                :styleliste="'background: no-repeat center url('+product.image+');'">
                                    <img class="rounded" :src="`${product.image}`" :alt="product.name">
                                </div>
                                <div class="product-details p-2 text-center">
                                    <div class="font-weight-bold fs-12 text-uppercase product-name" x-text="product.name"></div>
                                    <div class="product-price text-success">
                                        <div class="font-weight-bold" x-text="formatNumber(product?.price ?? 0,currency)"></div>
                                    </div>
                                </div>
                                
                            </div>
                        </div>
                    </template>
                </div>
            </div>
        </div>
    </div>
</div>



<?php $__env->startPush("js"); ?>
    <script>

        document.addEventListener('alpine:init', () => {

            // document.dispatchEvent(new CustomEvent('open-modal', {}));

            document.addEventListener("open-modal", ()=>{

            })

            Alpine.data('product_availability' , (product) => ({

                product : product,

                get is_available(){
                    const product = this.product;
                    let available = true;
                    <?php if(getSettingsOf("check_stock_when_ordering_backend")): ?>
                        if ( product){
                            const stocks = this.$store.data_page?.stocks?.filter(s => (s.product_id === product.id) ) ?? [];
                            if (stocks.length){
                                available = (getSum(stocks,"qty") > 0)
                            }else{
                                available = false;
                            }

                        }
                    <?php endif; ?>
                    return available;
                },

                productIsAvailable(order_qty){
                    const warehouse_id = this.warehouse_id;
                    let available = true;

                    <?php if(getSettingsOf("check_stock_when_ordering_backend")): ?>
                        if (warehouse_id && product){
                            const stocks = this.$store.data_page.stocks?.filter(s => ( (s.warehouse_id === warehouse_id) && (s.product_id === product.id) ) );
                            if (stocks.length){
                                const stock_qty = getSum(stocks,"qty")
                                console.log("id = "+product.id+" stock_qty = "+stock_qty)
                                available = (order_qty <= stock_qty)
                            }else{
                                available = false;
                            }
                        }
                    <?php endif; ?>


                    return available;

                },
            }))

            Alpine.data('block_products', () => ({

                async init (){
                //    await this.refresh();
                    const alpine = this;

                },

                search : "",
                // products : [],

                get products(){
                    return this.$store.data_page?.products ?? [];
                },

                get filteredProducts (){
                    const search = this.search.toLowerCase()
                    if(search){
                        return this.products.filter(
                                        product =>
                                            ( product.id.toString().startsWith(search) ||
                                            product.name.toLowerCase().startsWith(search) ||
                                            product.full_name.toLowerCase().includes(search)

                                            )
                                )
                    }
                    return this.products;
                },

                addToCart(item,is_available = true) {
                    //document.dispatchEvent(new CustomEvent('cart.item.added', { detail : item}));
                    if (is_available) {
                      return  this.$dispatch('cart.order.items.added', item);
                    }
                    return swal.fire({
                        icon : "warning",
                        title : item.full_name,
                        text : "<?php echo e(__("Not Available")); ?>",
                    })
                }

            }));

        });
    </script>
<?php $__env->stopPush(); ?>
<?php /**PATH /home2/lekruu/public_html/resources/views/backend/orders/create/blocs/products-list.blade.php ENDPATH**/ ?>