"use strict";
angular.module('mdtbackoffice')
    .controller('CustomerQuoteCtrl',['$scope','$routeParams','cusDataSvc','$timeout','$http','FIREBASEMDTBACKOFFICEDB','$firebaseObject','$firebaseArray','$location',
        function ($scope, $routeParams, cusDataSvc, $timeout, $http,FIREBASEMDTBACKOFFICEDB,$firebaseObject,$firebaseArray,$location) {
            
            var CUSTOMERS = 'customers2';
            $scope.customers = [];
            $scope.customerTpl = {
                id:   '',
                name: '',
                description: '',
                industry: '',
                tags: '',
                website: '',
                billingAddress: '',
                billingAddress2: '',
                billingCity: '',
                billingState: '',
                billingZipcode: '',
                billingCountry: '',
                billingPhone: '',
                billingFax: '',
                comments : [ ]
            };
            
            $scope.selectedCustomers = [];
            $scope.customerSelected = false;
            $scope.value = [];
            $scope.allCustomers = false;
            $scope.label = {id:''};
            $scope.data = {dataLoading:''};
            $scope.orderByField="id";
            $scope.itemPerPage = 15;
            $scope.currentPage = 1; 
            $scope.totalPage;
            $scope.editmode = false;
            $scope.showID = true;
            $scope.showDel= true;
           
            // Setup Firebase Refernce URLs
            var fbRef = new Firebase(FIREBASEMDTBACKOFFICEDB);
            var customerRef = fbRef.child(CUSTOMERS);
            
            $scope.modalShown = false;
            $scope.toggleModal = function() {
                $scope.modalShown = !$scope.modalShown;
            };

            $scope.usPhoneMask = '(999) 999-9999';

            $scope.go = function (path) {
                $location.path(path);
            };

            $scope.reFreshCustomers = function(){
                $scope.data.dataLoading  = true;
                $scope.customers = [];
                $scope.customers=$firebaseArray(customerRef);
                $scope.customers.$loaded().then(function(){
                    $scope.data.dataLoading  = false;
                });
            };
            
            $scope.addNew = function() {
                $scope.editmode = false;
                $scope.newCustomer = angular.copy($scope.customerTpl);
                $scope.toggleModal();
                $scope.label.id='';
                $('.simple-form input').removeClass('ng-dirty ng-valid-parse ng-valid ng-valid-required ng-touched');
            };
            
            $scope.saveCustomer = function(customerObj){
                $scope.toggleModal();
                if ($scope.editmode==true) {
                    var itm = $scope.customers.$getRecord(customerObj.$id);
                    for(var key in itm){
                        itm[key]=customerObj[key];
                    }
                    $scope.customers.$save(itm).then(function(){
                        swal("Updated!", "Customer has been updated.", "success");
                    });   
                }
                else
                {
                    $scope.customers.$add(customerObj).then(function(){
                        swal("Added!", "New Customer has been added.", "success");    
                    });
                }
            };
             
            $scope.edit = function(cus) {
                $scope.editmode = true;
                $scope.newCustomer = angular.copy(cus);
                $scope.toggleModal();
                $scope.label.id=cus.$id;
                $('.simple-form input').removeClass('ng-dirty ng-valid-parse ng-valid ng-valid-required ng-touched');
            };

            $scope.deleteCustomers = function (cust) {
                swal({
                        title: "Are you sure?",
                        text: "You will be deleting a customer,  Once deleted, you will not be able to recover the data!",
                        type: "warning",
                        showCancelButton: true,
                        confirmButtonClass: "btn-danger",
                        confirmButtonText: "Yes, delete it!",
                        cancelButtonText: "No, cancel please!",
                        closeOnConfirm: false,
                        closeOnCancel: false
                    },
                    function(isConfirm) {
                        if (isConfirm) {
                            $scope.customers.$remove(cust)
                            swal("Deleted", "Customer has been deleted.", "success");
                            $scope.reFreshCustomers();
                        } else {
                            swal("Cancelled", "Customer is Safe", "error");
                        }
                    });

            };
            
            /*pagination*/
             $scope.getPage = function(idx) {
                idx=idx+1;
                $scope.totalPage = Math.ceil($scope.customers.length / $scope.itemPerPage);
                return Math.ceil(idx / $scope.itemPerPage) == $scope.currentPage;
            }

            $scope.nextPage = function () {
                if($scope.currentPage == $scope.totalPage) {
                    $('.next').attr('disabled');
                } else {
                    $('.next').removeAttr('disabled');
                    $scope.currentPage++;
                }
            };
            
            $scope.prevPage = function () {             
                if($scope.currentPage == 1) {
                    $('.prev').attr('disabled');
                } else {
                    $('.prev').removeAttr('disabled');
                    $scope.currentPage--;
                }
            };
            
            /*initialize*/
            $scope.newCustomer = angular.copy($scope.customerTpl);
            $scope.reFreshCustomers();
}]);
