﻿app.controller('cartCtrl',
    [
        '$scope',
        'httpService',
        'helperService',
        'cartService',
        'breadCrumbService',
        '$timeout',
        function ($scope, httpService, helperService, cartService, breadCrumbService, $timeout) {
            helperService.addSpinner();
            helperService.announcePageIdentity('checkout');

            $scope.lang = mvcProduct.lang;
            $scope.currencyID = mvcProduct.currency;
            $scope.user = null;
            $scope.localStorageCartItems = cartService.getCartItems();
            $scope.cartItems = null;
            $scope.cartProcessing = true;
            $scope.itemProcessing = false;
            $scope.checkout = {
                processing: false,
                error: false,
                status: null
            };
            $scope.cartTotal = {
                price: 0,
                isClosed: -1
            };
            $scope.firstTimeLaunch = 0;
            $scope.checkoutStep = 1;
            $scope.tap = {
                response: {
                    formLoaded: false,
                    formError: "",
                    formBIN: null,
                    formResult: null,
                    extraValue: null
                }
            };
            $scope.network = {
                response: null,
                request: null
            };
            $scope.checkoutPayload = {
                method: '',
                currency: mvcProduct.currency
            };

            //Marwan:12102020 Add Domain to enable qnb for training.qatarchamber.com
            $scope.sub_domain = mvcProduct.subdomain;
            // country id for sub domain
            $scope.countryID = mvcProduct.countryID;

            $scope.$on('userDataRecieved', function () {
                $scope.user = helperService.getUser();
                var userID = $scope.user.logedin ? $scope.user.user.StudentID : 0;

                /*comparing between localStorageCartItems and user's activeCourses*/
                if (userID !== 0) {
                    if ($scope.user.activeCourses.length !== 0) {

                        for (var i = 0; i < $scope.user.activeCourses.length; i++) {
                            var index = -1;
                            for (var t = 0; t < $scope.localStorageCartItems.length; t++) {
                                if ($scope.localStorageCartItems[t].levelID === $scope.user.activeCourses[i].id) {
                                    index = t;
                                    break;
                                }
                            }

                            if (index !== -1) {
                                if ($scope.localStorageCartItems[index].trainingMethod === 'classroom' || $scope.localStorageCartItems[index].trainingMethod === 'live') {
                                    if ($scope.user.activeCourses[i].registerType === 'classroom' || $scope.user.activeCourses[i].registerType === 'live') {
                                        cartService.updateCart($scope.localStorageCartItems[index], 'remove');
                                    }
                                } else if ($scope.localStorageCartItems[index].trainingMethod === 'recorded') {
                                    if ($scope.user.activeCourses[i].registerType === 'recorded') {
                                        cartService.updateCart($scope.localStorageCartItems[index], 'remove');
                                    }
                                }
                                $scope.localStorageCartItems = cartService.getCartItems();
                            }
                        }
                    }
                }

                /*request cart items info from server*/
                if ($scope.firstTimeLaunch === 0) {
                    $scope.firstTimeLaunch = 1;
                    $scope.requestCartItemsDetails();
                }

                helperService.getParameterByName('reason', function (response) {
                    $scope.cartfailMessage = response;
                });

                helperService.removeSpinner();

            });

            $scope.$on('cartUpdated', function (value) {
                $scope.localStorageCartItems = cartService.getCartItems();
                $scope.calculateCart();
            });

            $scope.$on('conflictSolved', function () {
                $scope.requestCartItemsDetails();
            });

            breadCrumbService.identifyProduct('checkout', null);

            $scope.requestCartItemsDetails = function () {
                $scope.localStorageCartItems = cartService.getCartItems();
                $scope.user = helperService.getUser();
                var userID = $scope.user.logedin ? $scope.user.user.StudentID : 0;

                if ($scope.localStorageCartItems.length > 0) {
                    httpService.httpPost(mvcProduct.lang, '/CartItems/' + userID, $scope.localStorageCartItems, function (response) {

                        switch (response.status) {
                            case 200:
                                $scope.refactorCartItems(response.data);
                                break;
                            default:
                                break;
                        }
                    });
                } else {
                    $scope.cartItems = [];
                    $scope.cartProcessing = false;
                }
            };

            $scope.refactorCartItems = function (itemsArray) {
                $scope.cartItems = [];
                for (var i = 0; i < itemsArray.length; i++) {

                    if (!itemsArray[i].IsPurchased) {
                        var item = {
                            CourseName: itemsArray[i].CourseName,
                            CourseID: itemsArray[i].CourseID,
                            ItemType: itemsArray[i].ItemType,
                            TrainingMethod: itemsArray[i].TrainingMethod,
                            SectionID: itemsArray[i].Schedules.SectionID,
                            IsClosed: itemsArray[i].Schedules.IsClosed,
                            StartDate: itemsArray[i].Schedules.startDate,
                            Times: itemsArray[i].Schedules.times,
                            FromTime: itemsArray[i].Schedules.fromTime,
                            ToTime: itemsArray[i].Schedules.toTime,
                            IsShowSumPriceLevel: itemsArray[i].IsShowSumPriceLevel,
                            Price: itemsArray[i].Offer.offerID != 0 ? itemsArray[i].Offer.originalPrice : itemsArray[i].Price,
                            IsValidOffer: itemsArray[i].Offer.IsValid === true ? true : false,
                            OfferID: itemsArray[i].Offer.IsValid ? itemsArray[i].Offer.offerID : 0,
                            OfferPrice: itemsArray[i].Offer.offerPrice,
                            IsSectionOffer: itemsArray[i].Offer.IsSectionOffer,
                            Hours: itemsArray[i].Hours,
                            Thumb: itemsArray[i].Thumb,
                            Certificates: [],
                            OptionalCertificates: [],
                            totalPrice: function () {
                                var certificatesPrice = 0;
                                for (var c = 0; c < this.OptionalCertificates.length; c++) {
                                    if (this.OptionalCertificates[c].isChecked) {
                                        certificatesPrice += this.OptionalCertificates[c].Amount;
                                    }
                                }
                                if (this.OfferID !== null && this.OfferID !== 0) {
                                    return this.OfferPrice + certificatesPrice;
                                } else {
                                    return this.Price + certificatesPrice;
                                }
                            },
                            totalOriginalPrice: function () {
                                var certificatesPrice = 0;
                                for (var c = 0; c < this.OptionalCertificates.length; c++) {
                                    if (this.OptionalCertificates[c].isChecked) {
                                        certificatesPrice += this.OptionalCertificates[c].Amount;
                                    }
                                }
                                return this.Price + certificatesPrice;
                            },
                            includedCertsExpanded: false,
                            optionalCertsExpanded: true,
                            agentID: itemsArray[i].agentID
                        };

                        for (var r = 0; r < itemsArray[i].Certificates.length; r++) {
                            var certificate = {
                                CertificateID: itemsArray[i].Certificates[r].CertificateID,
                                type: itemsArray[i].Certificates[r].type,
                                vendor: itemsArray[i].Certificates[r].vendor,
                                Amount: itemsArray[i].Certificates[r].Amount,
                                certImage: itemsArray[i].Certificates[r].certImage,
                                desc: itemsArray[i].Certificates[r].desc,
                                FeeID: itemsArray[i].Certificates[r].FeeID,
                                IsOptional: itemsArray[i].Certificates[r].IsOptional ? true : false,
                                isChecked: itemsArray[i].Certificates[r].IsOptional ? false : true
                            };

                            if (certificate.IsOptional) {
                                item.OptionalCertificates.push(certificate);
                            } else {
                                item.Certificates.push(certificate);
                            }

                        }

                        $scope.cartItems.push(item);
                    }

                }
                $scope.calculateCart();
            };

            $scope.calculateCart = function () {
                var i;
                var c;
                var totalAmountsArray = [];
                var totalAmount = 0;

                for (i = 0; i < $scope.cartItems.length; i++) {
                    totalAmountsArray.push($scope.cartItems[i].totalPrice());
                }

                for (i = 0; i < totalAmountsArray.length; i++) {
                    totalAmount += Number(totalAmountsArray[i]);
                }

                $scope.cartTotal.price = totalAmount;

                $scope.cartTotal.isClosed = -1;
                for (i = 0; i < $scope.cartItems.length; i++) {
                    if ($scope.cartItems[i].IsClosed === true) {
                        $scope.cartTotal.isClosed = i;
                        { break; }
                    }
                }

                $scope.cartProcessing = false;
            };

            $scope.getTrainingMethod = function (method, lang) {
                return helperService.getTrainingMethod(method, lang);
            };

            $scope.getCartItemType = function (type, isPackageCourse, lang) {
                if (!type) return null;

                switch (type) {
                    case 1:
                        if (isPackageCourse) {
                            return lang === 'en' ? 'Training Program' : 'برنامج تدريبي';
                        } else {
                            return lang === 'en' ? 'Training Course' : 'دورة تدريبية';
                        }
                    case 2:
                        return lang === 'en' ? 'Training Diploma' : 'دبلوم تدريبي';
                    case 3:
                        return lang === 'en' ? 'Training Package' : 'باقة تدريبية';
                }
            };

            $scope.removeCartItem = function (index) {
                cartService.updateCart($scope.localStorageCartItems[index], 'remove');
                $scope.cartItems.splice(index, 1);
                $scope.calculateCart();
            };

            $scope.updateCartItemDetails = function (index) {
                $scope.selectedItemIndex = index;
                $scope.selectedItemDetails = null;
                $scope.itemProcessing = true;
                $scope.editItemStep = 1;
                $scope.selectedItemDetails = {
                    ItemType: $scope.cartItems[index].ItemType,
                    CourseID: $scope.cartItems[index].CourseID,
                    CourseName: $scope.cartItems[index].CourseName,
                    TrainingMethod: $scope.cartItems[index].TrainingMethod,
                    SectionID: $scope.cartItems[index].SectionID,
                    Price: $scope.cartItems[index].Price,
                    OfferID: $scope.cartItems[index].OfferID,
                    OfferPrice: $scope.cartItems[index].OfferPrice,
                    IsSectionOffer: $scope.cartItems[index].IsSectionOffer,
                    StartDate: $scope.cartItems[index].StartDate,
                    Times: $scope.cartItems[index].Times,
                    IsClassRoom: null,
                    IsLiveOnLine: null,
                    Schedule: null
                };
                httpService.httpGet(mvcProduct.lang, '/Schedule/' + $scope.cartItems[index].CourseID, function (response) {
                    $scope.selectedItemDetails.IsClassRoom = response.data.IsClassRoom;
                    $scope.selectedItemDetails.IsLiveOnLine = response.data.IsLiveOnLine;
                    $scope.selectedItemDetails.Schedule = response.data.Schedule;
                    $scope.itemProcessing = false;
                });
            };

            $scope.updateItemSection = function (index, SectionID) {
                $scope.selectedItemDetails.SectionID = SectionID;
                $scope.selectedItemDetails.StartDate = $scope.selectedItemDetails.Schedule[index].startDate;
                $scope.selectedItemDetails.Times = $scope.selectedItemDetails.Schedule[index].times;

                if (!$scope.cartItems[$scope.selectedItemIndex].OfferID || ($scope.cartItems[$scope.selectedItemIndex].OfferID && $scope.cartItems[$scope.selectedItemIndex].IsSectionOffer)) {
                    if ($scope.selectedItemDetails.Schedule[index].offerID && $scope.selectedItemDetails.Schedule[index].offerID !== 0) {
                        $scope.selectedItemDetails.OfferID = $scope.selectedItemDetails.Schedule[index].offerID;
                        $scope.selectedItemDetails.OfferPrice = $scope.selectedItemDetails.Schedule[index].offerPrice;
                        $scope.selectedItemDetails.IsSectionOffer = true;
                    } else {
                        $scope.selectedItemDetails.IsSectionOffer = false;
                    }
                }
            };

            $scope.submitItemUpdate = function () {
                $scope.itemProcessing = true;
                $scope.cartItems[$scope.selectedItemIndex].TrainingMethod = $scope.selectedItemDetails.TrainingMethod;
                $scope.cartItems[$scope.selectedItemIndex].SectionID = $scope.selectedItemDetails.SectionID;
                $scope.cartItems[$scope.selectedItemIndex].StartDate = $scope.selectedItemDetails.StartDate;
                $scope.cartItems[$scope.selectedItemIndex].Times = $scope.selectedItemDetails.Times;

                if (!$scope.cartItems[$scope.selectedItemIndex].OfferID || ($scope.cartItems[$scope.selectedItemIndex].OfferID && $scope.cartItems[$scope.selectedItemIndex].IsSectionOffer)) {
                    $scope.cartItems[$scope.selectedItemIndex].OfferID = $scope.selectedItemDetails.IsSectionOffer ? $scope.selectedItemDetails.OfferID : 0;
                    $scope.cartItems[$scope.selectedItemIndex].OfferPrice = $scope.selectedItemDetails.IsSectionOffer ? $scope.selectedItemDetails.OfferPrice : 0;
                    $scope.cartItems[$scope.selectedItemIndex].IsSectionOffer = $scope.selectedItemDetails.IsSectionOffer;
                }

                $scope.localStorageCartItems[$scope.selectedItemIndex].trainingMethod = $scope.cartItems[$scope.selectedItemIndex].TrainingMethod;
                $scope.localStorageCartItems[$scope.selectedItemIndex].sectionID = $scope.cartItems[$scope.selectedItemIndex].SectionID;
                $scope.localStorageCartItems[$scope.selectedItemIndex].offerID = $scope.cartItems[$scope.selectedItemIndex].OfferID;
                $scope.localStorageCartItems[$scope.selectedItemIndex].offerPrice = $scope.cartItems[$scope.selectedItemIndex].OfferPrice;
                $scope.localStorageCartItems[$scope.selectedItemIndex].isSectionOffer = $scope.cartItems[$scope.selectedItemIndex].IsSectionOffer;

                cartService.updateCart($scope.localStorageCartItems[$scope.selectedItemIndex], 'update');
                $scope.calculateCart();
                $scope.editItemStep = 2;
                $scope.itemProcessing = false;

            };

            $scope.recalculateItem = function (itemIndex, certIndex) {
                $timeout(function () {
                    $scope.calculateCart();
                }, 5);
            };

            $scope.slideStatus = 0;
            $scope.switchCertsSlider = function (type, index) {
                switch (type) {
                    case 'included':
                        if ($scope.slideStatus === 0) {
                            $scope.slideStatus = 1;
                            $scope.cartItems[index].includedCertsExpanded = !$scope.cartItems[index].includedCertsExpanded;
                            $timeout(function () {
                                $scope.slideStatus = 0;
                            }, 300);
                        }
                        break;
                    case 'optional':
                        if ($scope.slideStatus === 0) {
                            $scope.slideStatus = 1;
                            $scope.cartItems[index].optionalCertsExpanded = !$scope.cartItems[index].optionalCertsExpanded;
                            $timeout(function () {
                                $scope.slideStatus = 0;
                            }, 300);
                        }
                        break;
                }

            };

            $scope.prepareCheckout = function (method) {
                $scope.checkoutStep = 1;
                $scope.checkoutPayload.method.currency = mvcProduct.currency;

                if ($scope.cartTotal.price <= 0) {
                    $scope.checkoutStep = 2;
                    $scope.checkout.error = false;
                    $scope.checkout.status = null;
                    $scope.checkout.processing = true;

                    let enrolls = $scope.cartItems.map(function (x) {
                        return {
                            CourseID: x.CourseID,
                            SectionID: x.SectionID,
                            TrainingMethod: x.TrainingMethod,
                            agent: x.agentID,
                            StudentID: $scope.user.user.StudentID,
                            CurrencyID: mvcProduct.currency
                        }
                    });

                    if (enrolls.length > 0) {
                        httpService.httpPost('', '/multiple/enroll', enrolls, function (response) {
                            switch (response.status) {
                                case 200:
                                    window.location.href = `${mvcUrl + mvcProduct.lang}/PurchaseSuccessfull`
                                    break;
                                default:
                                    $scope.checkout.error = true;
                                    $scope.checkout.status = response.status;
                                    $scope.checkout.processing = false;
                                    break;
                            }
                        });
                    } else {
                        $scope.checkoutStep = 1;
                        $scope.checkout.processing = false;
                    }
                }
                else {
                    if (mvcProduct.subdomain === "eprostudy") {
                        $scope.checkoutPayload.method = "NETWORK";
                        $scope.proceedCheckoutObj.PaymentMethod = 10;
                        $scope.proceedCheckout();
                    }
                }
            };

            $scope.proceedCheckoutObj = {
                CartID: 0,
                access_token: '',
                lang: mvcProduct.lang,
                MediaID: mvcProduct.mediaID,
                PaymentMethod: 1
            };

            $scope.proceedCheckout = function () {
                $scope.checkout.error = false;
                $scope.checkout.status = null;
                $scope.checkout.processing = true;
                $scope.checkoutStep = 1;

                switch ($scope.checkoutPayload.method) {
                    case 'payfort':
                        $scope.proceedCheckoutObj.PaymentMethod = 1;
                        break;
                    case 'paypal':
                        $scope.proceedCheckoutObj.PaymentMethod = 2;
                        break;
                    case 'KW_Knet':
                        $scope.proceedCheckoutObj.PaymentMethod = 1;
                        break;
                    case 'KW_Visa':
                        $scope.proceedCheckoutObj.PaymentMethod = 2;
                        break;
                    case 'Efawatercom':
                        $scope.proceedCheckoutObj.PaymentMethod = 3;
                        break;
                    case 'QNB':
                        $scope.proceedCheckoutObj.PaymentMethod = 4;
                        break;
                    case 'PayMob':
                        $scope.proceedCheckoutObj.PaymentMethod = 6;
                        break;
                    case 'myfatoorah':
                        $scope.proceedCheckoutObj.PaymentMethod = 5;
                        break;
                    case "CASH":
                        break;
                    case "TAP":
                        $scope.proceedCheckoutObj.PaymentMethod = 9;
                        break;
                    case "NETWORK":
                        $scope.proceedCheckoutObj.PaymentMethod = 10;
                        break;
                    default:
                        $scope.proceedCheckoutObj.PaymentMethod = 1;
                        break;
                }

                httpService.httpPost('', '/Checkout', {
                    StudentID: $scope.user.user.StudentID,
                    MyCart: $scope.cartItems,
                    CurrencyID: mvcProduct.currency,
                    PaymentMethodID: $scope.proceedCheckoutObj.PaymentMethod,
                    AgentID: Number(mvcProduct.agentID)
                }, function (response) {
                    switch (response.status) {
                        case 200:
                            $scope.proceedCheckoutObj.CartID = response.data.CartID;
                            $scope.proceedCheckoutObj.access_token = $scope.user.access_token;
                            switch ($scope.checkoutPayload.method) {
                                case 'paypal':
                                    helperService.createFormSubmit(mvcProduct.PaymentHandlerLink + 'Home/CheckOutPayPal', 'post', $scope.proceedCheckoutObj);
                                    break;
                                case 'payfort':
                                    helperService.createFormSubmit(mvcProduct.PaymentHandlerLink + 'Home/checkout', 'post', $scope.proceedCheckoutObj);
                                    break;
                                case 'Efawatercom':
                                    helperService.createFormSubmit(mvcProduct.PaymentHandlerLink + 'Home/CheckOutEFwaterCom', 'post', $scope.proceedCheckoutObj);
                                    break;
                                case 'QNB':
                                    helperService.createFormSubmit(mvcProduct.PaymentHandlerLink + 'Home/CheckOutQNB', 'post', $scope.proceedCheckoutObj);
                                    break;
                                case 'PayMob':
                                    helperService.createFormSubmit(mvcProduct.PaymentHandlerLink + 'Home/CheckOutPayMob', 'post', $scope.proceedCheckoutObj);
                                    break;
                                case 'KW_Visa':
                                    helperService.createFormSubmit(mvcProduct.PaymentHandlerLink + 'Home/CheckOutKNET', 'post', $scope.proceedCheckoutObj);
                                    break;
                                case 'KW_Knet':
                                    helperService.createFormSubmit(mvcProduct.PaymentHandlerLink + 'Home/CheckOutKNET', 'post', $scope.proceedCheckoutObj);
                                    break;
                                case 'myfatoorah':
                                    helperService.createFormSubmit(mvcProduct.PaymentHandlerLink + 'Home/CheckOutMyFatoora', 'post', $scope.proceedCheckoutObj);
                                    break;
                                case 'zaincash':
                                    break;
                                case "CASH":
                                    $scope.checkout.processing = false;
                                    break;
                                case "TAP":
                                    $scope.tap.response.extraValue = $scope.proceedCheckoutObj;
                                    $scope.checkoutStep = 2;
                                    $scope.checkout.processing = false;
                                    break;
                                case "NETWORK":
                                    $scope.NetworkPaymentCreateSession({
                                        apiOperation: "INITIATE_CHECKOUT",
                                        interaction: {
                                            operation: "PURCHASE",
                                            merchant: {
                                                "name": "test20112023TS",
                                            },
                                            "returnUrl": window.location.href
                                        },
                                        order: {
                                            amount: $scope.cartTotal.price,
                                            currency: mvcProduct.currency == 2 ? "USD" : "JOD",
                                            id: response.data.CartID,
                                            description: "AUTHORIZE"
                                        }
                                    }, function (res) {
                                        switch (res.status) {
                                            case 200:
                                                $scope.network.request = {
                                                    session: {
                                                        id: res.data.session.id
                                                    }
                                                };
                                                $scope.network.response = res.data;
                                                $scope.checkout.processing = false;
                                                break;
                                            default:
                                                $scope.checkout.processing = false;
                                                break;
                                        }
                                    });
                                    break;
                                default:
                                    break;
                            }
                            break;
                        default:
                            $scope.checkout.error = true;
                            $scope.checkout.status = response.status;
                            $scope.checkout.processing = false;
                            break;
                    }
                });
            };

            $scope.tapProceedCheckout = function (req, callback) {
                httpService.httpPost('', '/tab/checkout', req, function (response) {
                    switch (response.status) {
                        case 200:
                            callback(response.data);
                            break;
                        default:
                            console.log(req);
                            $scope.checkout.processing = false;
                            break;
                    }

                });
            };

            $scope.switchToZainCash = function () {
                $scope.checkoutPayload.method = 'zaincash';
                $scope.zainObj = {
                    step: 1,
                    countryID: 1,
                    mobile: '',
                    pin: '',
                    processing: false,
                    error: false,
                    status: null,
                    cartID: null
                };

                if (!$scope.countries) {
                    $scope.countries = helperService.getCountries();
                }
            };

            $scope.submitZainPhone = function (step) {
                $scope.zainObj.error = false;
                $scope.zainObj.status = null;
                $scope.zainObj.processing = true;
                var obj;

                switch (step) {
                    case 1:
                        obj = {
                            StudentID: $scope.user.user.StudentID,
                            MyCart: $scope.cartItems,
                            AgentID: Number(mvcProduct.agentID)
                        };

                        httpService.httpPost('', '/Checkout', obj, function (response) {
                            switch (response.status) {
                                case 200:
                                    $scope.zainObj.cartID = response.data.CartID;
                                    var countryCode = $scope.returnCountryCode($scope.zainObj.countryID).replace('+', '00');
                                    obj = {
                                        CartID: $scope.zainObj.cartID,
                                        MobileNumber: countryCode.concat($scope.zainObj.mobile)
                                    };

                                    httpService.PaymentPost('', '/Home/ValidateZainCash', obj, function (res) {
                                        switch (res.status) {
                                            case 200:
                                                $scope.zainObj.step = 2;
                                                $scope.zainObj.error = false;
                                                $scope.zainObj.status = null;
                                                break;

                                            default:
                                                $scope.zainObj.error = true;
                                                $scope.zainObj.status = res.status;
                                                break;
                                        }
                                    });
                                    break;

                                default:
                                    $scope.zainObj.error = true;
                                    $scope.zainObj.status = res.status;
                                    break;
                            }
                            $scope.zainObj.processing = false;
                        });
                        break;
                    case 2:

                        var countryCode = $scope.returnCountryCode($scope.zainObj.countryID).replace('+', '');
                        obj = {
                            CartID: $scope.zainObj.cartID,
                            MobileNumber: countryCode.concat($scope.zainObj.mobile),
                            pin: $scope.zainObj.pin
                        };

                        httpService.PaymentPost('', '/Home/SubmitPaymentZainCash', obj, function (response) {
                            switch (response.status) {
                                case 200:
                                    window.location = mvcProduct.mediaID === 0 ? window.location = mvcUrl + mvcProduct.lang + '/purchaseSuccessfull/' : window.location = mvcUrl + mvcProduct.lang + '/purchaseSuccessfull/' + mvcProduct.mediaID;
                                    break;
                                default:
                                    $scope.zainObj.error = true;
                                    $scope.zainObj.status = response.status;
                                    $scope.zainObj.processing = false;
                                    break;
                            }

                        });
                        break;
                    default:
                }
            };

            $scope.prepareLoginModal = function () {
                helperService.prepareLoginModal();
            };

            //#region certificate modal
            $scope.loadCertificate = function (certificate) {

                $scope.certificateData = {
                    IsOptional: certificate.IsOptional,
                    certImage: certificate.certImage,
                    type: certificate.type,
                    Amount: certificate.Amount,
                    desc: certificate.desc,
                    vendor: certificate.vendor
                };

                helperService.setCertificateDate($scope.certificateData);

            };

            $scope.returnCountryCode = function (value) {
                if (!value || !$scope.countries) return null;

                var index = -1;
                for (var i = 0; i < $scope.countries.length; i++) {
                    if ($scope.countries[i].countryID === value) {
                        index = i;
                        break;
                    }
                }
                return '+' + $scope.countries[index].code.slice(2);
            };

            $scope.skipLeadingZero = function (method, value) {
                $scope.zainObj.mobile = helperService.skipLeadingZero(value);
            };
            //#endregion

            //#region timezone
            $scope.clientLocalTime = function (time, date) {

                if (!time || !date) return;
                var _time = time;
                var result;

                helperService.clientLocalTime(time, date, function (hours, minutes) {
                    if (hours === null || minutes === null) {
                        result = _time;
                        return;
                    }

                    result = (hours < 10 ? "0" + hours.toString() : hours) + ":" + (minutes < 10 ? "0" + minutes.toString() : minutes);
                });

                return result;

            };

            $scope.clientLocalDate = function (time, date) {
                if (!time || !date) return;
                var _date = date;
                var result;

                helperService.clientLocalDate(time, date, function (date) {
                    if (date === null) {
                        result = _date;
                        return;
                    }

                    result = date;
                });
                return result;
            };

            $scope.timingName = function () {
                return helperService.timingName();
            };
            //#endregion

            $scope.returnTimeFormate = function (time) {
                if (!time) return;

                var fixed_hours = parseInt(time);
                var fixed_minutes = parseInt((time - fixed_hours) * 60);

                return fixed_hours + ":" + (fixed_minutes < 10 ? '0' + fixed_minutes.toString() : fixed_minutes);
            };

            $scope.$watch("cartItems", function (nVal, oVal, scope) {
                if (nVal && nVal.length > 0 && $scope.sub_domain == "main") {
                    if (mvcProduct.currency == 1 || mvcProduct.currency == 2)
                        gtag_addCart_conversion(scope.cartTotal.price, mvcProduct.currency == 1 ? "JOD" : "USD");
                }
            }, true);

            //#region network payment
            $scope.NetworkPaymentCreateSession = function (obj, callback) {
                httpService.httpPost('', 'network/create/session', obj, function (response) {
                    callback(response);
                });
            };
            //#endregion
        }
    ]
);