var customerApp = angular.module("customerApp", []);

customerApp.controller("customerController", ["$scope", "$http", function ($scope, $http) {
    $scope.baseUrl = "";
    $scope.ShowCustomerGrid = false;
    $scope.CustomerModel = [];

    $scope.IsPurchaseVisible = false;
    $scope.IsReturnVisible = false;
    $scope.EnteredEsn = "";
    $scope.IsThankYouVisible = false;
    $scope.IsEsnMismatch = false;
    $scope.IsDeviceReturnAcknowledged = false;
    
    $scope.DeviceReturnedOrPurchased = function()
    {
        return $scope.CustomerModel.IsDevicePurchased || $scope.CustomerModel.IsDeviceReturned;
    };

    $scope.init = function (baseUrl, customerModel) {
        $scope.baseUrl = baseUrl;
        $scope.CustomerModel = customerModel;
    };

    // Return Device Funcitons
    $scope.ReturnDevice = function() {
        $scope.IsReturnVisible = true;
        $scope.IsEsnMismatch = false;
        $scope.IsDeviceReturnAcknowledged = false;
    };

    $scope.CloseDeviceReturn = function () {
        $scope.IsReturnVisible = false;
    };

    $scope.SubmitDeviceReturn = function () {
        if ($scope.EnteredEsn == $scope.CustomerModel.ESN) {

            var getUrl = $scope.baseUrl + "/ProcessLeaseReturn/";
            
            //$http({
            //    url: getUrl,
            //    method: "GET",
            //    params: {
            //        leasingCustomerId: $scope.CustomerModel.CustomerId,
            //        amountCollected: $scope.CustomerModel.EarlyTerminationFee
            //    }
            //}).success(function (data, status, headers, config) {
            //    $scope.CustomerModel = data;
            //    $scope.IsReturnVisible = false;
            //    $scope.IsThankYouVisible = true;
            //}).error(function (data, status, headers, config) {
            //    $scope.ProcessingError = "Exception: " + data;
            //});
            $scope.IsReturnVisible = false;
            $scope.IsThankYouVisible = true;
            $scope.CustomerModel.IsDeviceReturned = true;
        } else {
            $scope.IsEsnMismatch = true;
            $scope.EnteredEsn = "";
        }
    };

    // Purchase Device Functions
    $scope.PurchaseDevice = function() {
        $scope.IsPurchaseVisible = true;
        $scope.IsEsnMismatch = false;
    };

    $scope.ClosePurchaseDevice = function() {
        $scope.IsPurchaseVisible = false;
    };

    $scope.SubmitPurchaseDevice = function() {

        // need to validate esn here
        if ($scope.EnteredEsn == $scope.CustomerModel.ESN) {

            //var getUrl = $scope.baseUrl + "/ProcessLeasePurchase/";
            
            //$http({
            //    url: getUrl,
            //    method: "GET",
            //    params: {
            //        leasingCustomerId: $scope.CustomerModel.CustomerId,
            //        amountCollected: $scope.CustomerModel.TotalCostToPurchase
            //    }
            //}).success(function (data, status, headers, config) {
            //    $scope.CustomerModel = data;
            //    $scope.IsPurchaseVisible = false;
            //    $scope.IsThankYouVisible = true;
            //}).error(function (data, status, headers, config) {
            //    $scope.ProcessingError = "Exception: " + data;
            //});

            $scope.IsPurchaseVisible = false;
            $scope.IsThankYouVisible = true;
            $scope.CustomerModel.IsDevicePurchased = true;
            
        } else {
            
            $scope.IsEsnMismatch = true;
            $scope.EnteredEsn = "";
        }
        
    };

    // close thank You screen
    $scope.CloseThankYou = function() {
        $scope.IsThankYouVisible = false;
    };

}]);



angular.element(document).ready(function () {
    //var template = document.getElementById("myTemplate");
    var appDiv = document.getElementById("customerApp");
    setTimeout(function () {
        //appDiv.innerHTML = template.innerHTML;
        angular.bootstrap(angular.element(appDiv), ['customerApp']);
    }, 1000);
});