﻿app.run(function ($rootScope, $location, $route) {
    $rootScope.config = {};
    $rootScope.config.app_url = $location.url();
    $rootScope.config.app_path = $location.path();
    $rootScope.layout = {};
    $rootScope.layout.loading = false;

    $rootScope.$on('$routeChangeStart', function () {
        $rootScope.layout.loading = true;
    });
    $rootScope.$on('$routeChangeSuccess', function () {
        $rootScope.layout.loading = false;
    });
    $rootScope.$on('$routeChangeError', function () {
        $rootScope.layout.loading = false;
    });
});

app.config(['$routeProvider', function ($routeProvider) {
    $routeProvider.
        when('/IadeTaleplerim/:OrderID', {
            templateUrl: '/Scripts/apps/template/hesabim/IadeTaleplerim.html',
            controller: 'cntrlIadeTaleplerim',
            cache: false
        }).
        when('/IadeTaleplerim', {
            templateUrl: '/Scripts/apps/template/hesabim/IadeTaleplerim.html',
            controller: 'cntrlIadeTaleplerim',
            cache: false
        }).
        otherwise({
            redirectTo: '/'
        });
}]);

app.controller('cntrlKolayIade', function ($scope, $rootScope, $filter, $location, ngDialog, $timeout, api) {
    window.$scope = $scope;

    //Show Form
    $scope.isShowOrderVerification = true;
    $scope.isShowSmsVerification = false;
    $scope.isShowEasyReturnClaimForm = false;

    //Genel
    $scope.isVerified = false;
    $scope.submitted = false;
    $scope.defaultCountryCode = globalModel.countryCode.toLowerCase();
    $scope.IsLoaded = false;
    $scope.SmsDogrula = siteSettings.siparisAyar.iadeAyar.kolayIadedeSmsDogrulamaZorunlu;
    $scope.returnClaimModel = {};

    $scope.KolayIadeSiparisDogrula = function (operation) {
        $scope.submitted = true;
        $scope.IsLoaded = false;
        $scope.ShowCepTelefonValidation = false;

        var telefon = ($scope.returnClaimModel.txtGsm + '').replace("+", "").replace(/ /g, "");
        var request = {
            Operation: operation,
            OrderCode: $scope.returnClaimModel.txtSiparisNumarasi,
            PhoneNumber: telefon,
            EMail: $scope.returnClaimModel.txtMailAdresi,
            VerificationCode: $scope.returnClaimModel.txtVerifyCode
        };

        if (!request.OrderCode || request.OrderCode == '') {
            swal('', translateIt('Validation_SiparisNoGiriniz'));
            return;
        }

        if (request.EMail == '' && (request.PhoneNumber+'').length < 5) {
            swal('', translateIt('Validation_SiparisNumarasiVeyaCepTelefonuGiriniz'));
            return;
        }

        api.getEasyReturnClaimControl(request, function (response) {
            console.log(response);
            if (response.isError) {
                swal('', response.errorMessage);
            } else {

                if (response.nextAction === "smsverification") {
                    $scope.isShowSmsVerification = true;
                    $scope.isShowOrderVerification = false;
                    $scope.isShowEasyReturnClaimForm = false;
                    if (response.errorCode == 'newsms') {
                        SayacYenidenBaslat();
                    } else {
                        SayaciBaslat();
                    }
                } else if (response.nextAction === "order") {
                    $scope.isShowSmsVerification = false;
                    $scope.isShowOrderVerification = true;
                    $scope.isShowEasyReturnClaimForm = false;
                } else if (response.nextAction === "showclaims") {
                    $scope.GetReturnClaimsTemplate(response.orderID);
                }
            }
            $scope.IsLoaded = true;
        }, function (error) { $scope.Products = null; });
    };

    $scope.GetReturnClaimsTemplate = function (orderID) {
        $scope.isShowEasyReturnClaimForm = true;
        $scope.isShowOrderVerification = false;
        $scope.isShowSmsVerification = false;
        $scope.isVerified = true;
        //get-iade template
        $location.path("/IadeTaleplerim");
        setTimeout(function () {
            $rootScope.$emit("CallBindOrder", [orderID]);
            $scope.$apply();
        }, 500);
    };
});