

<div x-data="offline_orders_schedule_options">















































</div>

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

    <script>
        document.addEventListener("alpine:init", () => {

            Alpine.data("offline_orders_schedule_options", (params = {auto_init : true})=>({

                init(){
                    this.setSettings({...this.settings,...this.configs});
                },

                destroy(){
                    clearInterval(this.scheduleInterval);
                },

                scheduleInterval : null,
                settings :{
                    enable : true,
                    interval :  60, // en s
                    number_of_order : 5,
                },

                get time (){
                    const {interval} = this.settings;

                    return {
                        s : (interval === "") ? 0 : interval,
                        ms : interval * 1000,
                        min : arround(interval/60,2),
                    }
                },
                get desc (){
                    const configs = this.settings;
                    if('number_of_order' in configs){
                        const {number_of_order,enable} = configs;

                        const time = this.time;
                        const active = enable ? "<?php echo e(__("Enabled")); ?>" : "<?php echo e(__("Disabled")); ?>";

                        return formatNumber(number_of_order)+" order(s) was sent every "+formatNumber(time.s)+" sec " + "<< "+active+" >>";
                    }
                    return "<?php echo e(__("Schedule orders submission")); ?>"
                },



                get configs (){
                    return JSON.parse(localStorage.getItem('SYNCHRONISATION_SETTINGS')) ?? {};
                },

                setSettings(settings = {}){
                    let result = true
                    settings = {...this.settings, ...settings} ;
                    settings.time_ms = 1000 * settings.interval
                    localStorage.setItem('SYNCHRONISATION_SETTINGS',JSON.stringify(settings))
                    this.settings = settings;

                    if(result && params.auto_init){
                        this.stopSchedule()
                        this.startSchedule()
                    }
                    return result;
                },
                saveSetting(){
                    console.log("ici")
                    this.setSettings()
                },

                startSchedule(){
                    const alpine = this;
                    const {time_ms  } = alpine.settings;
                    if( alpine.settings.enable) {
                        alpine.scheduleInterval  = setInterval(() => {
                            alpine.sendOrders()
                        }, time_ms) ; /* rappel après 2 secondes = 2000 millisecondes */
                    }else{
                        this.stopSchedule();
                    }
                },

                getService (order) {
                    const that = this;
                    const {items, customer, config,ref,point_of_sale,allTotal,warehouse } = order;
                    const service =  OrderService({
                        data : {
                            order : {items , config,allTotal},
                            customer,
                            point_of_sale_id : point_of_sale?.id ?? null,
                            warehouse_id : warehouse?.id ?? null,
                        },
                        displayMessageOnfailed : false,
                        displayMessageOnCompleted : false ,
                    });

                    service.onDisconnected = (res)=>{
                        order.response = res;
                        order.sync.success = false;
                        order.sync.date = moment().format("<?php echo e(__('YYYY-MM-DD HH:mm:ss')); ?>");
                        that.$store.draft.updateOrder(ref,order);
                    }
                    service.onCompleted = (response) => {
                        console.log("[schedule-submit-order] '"+ref+"' ==> completed");

                        if ("success" in response){
                            const success = response.success ?? false

                            if (success){
                                that.$store.draft.remove(order,false);
                            }else{
                                order.sync.success = success ;
                                delete response.exception
                                order.response = response;
                                order.sync.date = moment().format("<?php echo e(__('YYYY-MM-DD HH:mm:ss')); ?>");
                                that.$store.draft.updateOrder(ref,order);
                            }

                        }

                    };

                    service.onError = (error) => {
                        console.log("[schedule-submit-order] '"+ref+"' ==> error");
                        console.log(error)
                    }
                    return service;
                },
                async sendOrders(){
                    const that = this;
                    const orders = that.$store.draft.offline_orders.filter(o => (o.sync.active && !o.sync.success));
                    const count = orders.length;
                    const {interval ,number_of_order,time_ms  } = that.settings;

                    if (count > 0){
                        console.log(formatNumber(count)+" Orders Scheduled")
                        if(number_of_order > 0){
                            for (let index = 0; index < number_of_order; index++) {
                                try {
                                    const order = orders[index];
                                    if (("items" in order) && ("customer" in order) && ("config" in order)){
                                        const service = that.getService(order);
                                        const response  = await service.sendOrder();
                                    }

                                } catch (error) {
                                    console.log(error)
                                }

                            }
                        }
                    }else{
                        console.log("No Scheduled Order.");
                    }
                },

                stopSchedule(){
                    clearInterval(this.scheduleInterval)
                },

            }))

        })
    </script>

<?php $__env->stopPush(); ?>
<?php /**PATH /home2/lekruu/public_html/resources/views/backend/orders/create/blocs/form-setting-offline-orders-schedule.blade.php ENDPATH**/ ?>