'use strict';
angular.module('app', ['ngRoute', 'ngAnimate', 'ngFileUpload', 'xeditable']);
angular.module('app')
    .run(['editableOptions', function(editableOptions) {
        editableOptions.theme = 'default'; // bootstrap3 theme. Can be also 'bs2', 'default'
    }])
    .factory('ClientService', function ($http, Upload)
    {
        return {
            import: function (data) {
                return Upload.upload({
                    url: 'ajax/import_clients.php',
                    data: {file: data},
                });
            }
        }
    })
    .factory('EstimationService', function ($http, Upload)
    {
        return {}
    })
    .factory('ReclamationService', function ($http, Upload)
    {
        var POST_URL = '/admin/ajax/ajax_estimations.php';
        return {
            getAll: function (data) {
                return Upload.upload({url: POST_URL, data: data});
            },
            getAppointmentPhotos: function (data) {
                return Upload.upload({url: POST_URL, data: data});
            },
            uploadPhoto: function (reclamation, photo) {
                return Upload.upload({
                    url: POST_URL,
                    data: {
                        action: 'uploadReclamationPhoto',
                        data: reclamation,
                        file: photo
                    }
                });
            },
            deleteReclamationPhoto: function(photo) {
                return Upload.upload({
                    url: POST_URL,
                    data: {
                        action: 'deleteReclamationPhoto',
                        data: photo
                    }
                });
            },
            getRecentlyCreatedReclamation: function (data) {
                return Upload.upload({
                    url: POST_URL,
                    data: {
                        action: 'CheckAndFetchRecentlyCreatedReclamationBySession',
                        data: {},
                    }
                });
            },
            createEstimateAndInvoice: function (invoice, estimation) {
                return Upload.upload({
                    url: POST_URL,
                    data: {
                        action: 'createEstimateAndInvoice',
                        invoice: invoice,
                        estimation: estimation
                    }
                });
            },
            updateEstimateAndInvoice: function (invoice, estimation, invoiceId) {
                invoice.id = invoiceId;
                return Upload.upload({
                    url: POST_URL,
                    data: {
                        action: 'updateEstimateAndInvoice',
                        invoice: invoice,
                        estimation: estimation
                    }
                });
            },
            changeInvoiceType: function(invoice) {
                return Upload.upload({
                    url: POST_URL,
                    data: {
                        action: 'changeInvoiceType',
                        invoice: invoice
                    }
                });
            },
            loadContentById: function (id) {
                return Upload.upload({
                    url: POST_URL,
                    data: {
                        action: 'loadContentById',
                        'id': id,
                    }
                });
            },
        }
    })
    .factory('InvoiceService', function ($http, Upload)
    {
        return {}
    })
    .config(function ($routeProvider, $locationProvider) {
        $routeProvider.when('/', {
            controller: 'estimationCtrl'
        }).when('/estimation/:id', {
            controller: 'estimationCtrl3'
        });
    })
    .controller("clientCtrl",
        ["$rootScope", "$scope", "$http", '$route', '$location', '$timeout', 'ClientService',
            function clientCtrl($rootScope, $scope, $http, $route, $location, $timeout, ClientService)
            {
                $scope.dataFile = '';
                this.$onInit = function () {
                    $scope.showUploadBox = false;
                };

                $scope.exportClients = function () {

                };

                $scope.importClients = function () {
                    ClientService.import($scope.dataFile).then(function (response) {
                        if ( ! response.data.success) {
                            alert(response.data.message);
                        }
                        else {
                            window.location.reload();
                        }
                    }, function (reason) {
                        alert(reason.data.message);
                    });
                };
            }
        ]
    )
    .controller("estimationCtrl",
        ["$rootScope", "$scope", "$http", "$route", "$routeParams", "$location", "$interval", "$timeout", 'ClientService', 'ReclamationService', 'InvoiceService', 'EstimationService',
            function estimationCtrl($rootScope, $scope, $http, $route, $routeParams, $location, $interval, $timeout, ClientService, ReclamationService, InvoiceService, EstimationService)
            {
                var SUCCESS = 'success';
                var WARNING = 'warning';
                var ERROR   = 'danger';
                var INFO    = 'info';
                $scope.user = {
                    name: 'awesome user'
                };
                /*Estimation object*/
                $scope.showCreateReclamationForm = false;
                $scope.showNewReclamationBlock = false;
                $scope.showCreateInvoiceForm = false;
                $scope.showReclamationChooseWidget = true;
                $scope.estimation = {
                    reclamation: null,
                    type_of_loss:'grêle'
                };

                this.$onInit = function () {
                    $scope.loadAllReclamations();
                    sessionStorage.setItem('reclamation', JSON.stringify($scope.estimation.reclamation));
                    if ( $location.path() !== '/' ) {
                        $scope.pid = $location.path().replace('/estimation/', '');
                        $scope.loadContentById($scope.pid);
                    }
                };
                $scope.setMessage = function (message, type) {
                    $scope.message = {
                        'type': type,
                        'text': message
                    };
                };
                $scope.isReclamationSetProperlyAlsoNotifyUser = function() {
                    if ( ! $scope.estimation.reclamation) {
                        $scope.setMessage('Choisir ou créer une réclamation en premier.', ERROR);
                        jQuery("html, body").animate({ scrollTop: 0 }, "slow");
                        return false;
                    }
                    return true;
                };

                $scope.showCreateReclamationBlock = function() {
                    $scope.showCreateReclamationForm = true;
                };

                $scope.createNewReclamation = function() {
                    $scope.showNewReclamationBlock = true;
                    $scope.lookup = $interval(function () {
                        ReclamationService.getRecentlyCreatedReclamation().then(function (value) {
                            if (typeof value.data.reclamation !== 'undefined') {
                                $scope.estimation.reclamation = value.data;
                                $scope.selectReclamation(value.data);
                                $interval.cancel($scope.lookup);
                                $scope.showNewReclamationBlock = false;
                                $scope.reclamations.push(value.data);
                            }
                        }, function (reason) {});
                    }, 5000);
                    window.open('/admin/reclamation.php','_blank', 'location=yes,height='+ (window.innerHeight - 100 )+',width='+ (window.innerWidth - 100 )+',scrollbars=yes,status=no');
                };

                $scope.editReclamation = function() {
                    $scope.showCreateReclamationForm = true;
                };

                $scope.loadContentById = function(id) {
                    ReclamationService.loadContentById(id).then(function (value) {
                        var data = value.data;
                        $scope.estimation  = data.estimation;
                        $scope.invoice     = data.invoice;
                        $scope.estimation.reclamation  = data.reclamation;
                        $scope.estimation.reclamation['photos'] = data.reclamationPhotos;
                        $scope.selectReclamation(data.reclamation);
                        $scope.loadInvoice();
                    }, function ( reason ) {
                        console.log( reason.message );
                    });
                };

                $scope.loadAllReclamations = function () {
                    ReclamationService.getAll({'action': 'loadAllReclamations'}).then(function (response) {
                        $scope.reclamations = response.data;
                    }, function (reason) {
                        $scope.reclamations = [];
                    });
                };

                $scope.selectReclamation = function ( reclamation ) {
                    $scope.estimation.reclamation = reclamation;
                    $scope.showReclamationChooseWidget = false;
                    $scope.reclamation_value = reclamation.reclamation;
                    sessionStorage.setItem('reclamation', JSON.stringify($scope.estimation.reclamation));
                    ReclamationService.getAppointmentPhotos({'action': 'getAppointmentPhotos', 'data': reclamation}).then(function (value) {
                        $scope.estimation.reclamation['photos'] = value.data;
                    }, function (reason) {
                        $scope.estimation.reclamation['photos'] = [];
                    });
                };

                $scope.uploadReclamationPhoto = function(picture) {
                    ReclamationService.uploadPhoto($scope.estimation.reclamation, picture).then(function (value) {
                        $scope.estimation.reclamation['photos'] = value.data;
                    }, function (reason) {});
                };

                $scope.deleteReclamationPhoto = function(photo) {
                    ReclamationService.deleteReclamationPhoto(photo).then(function (value) {
                        var index = $scope.estimation.reclamation.photos.indexOf(photo);
                        $scope.estimation.reclamation.photos.splice(index,1);
                    }, function (reason) {
                        alert('Photo not deleted');
                    });
                };

                $scope.editReclamationSelection = function () {
                    $scope.showReclamationChooseWidget = true;
                };

                $scope.ifReclamationSetProperlyThenContinue = function () {
                    if ( $scope.isReclamationSetProperlyAlsoNotifyUser ) {
                       $scope.showCreateReclamationForm = false;
                    }
                };

                $scope.closeReclamation = function () {
                    $scope.showCreateReclamationForm = false;
                };


            // ///////////////////////////////////////
            //    INVOICE
            //////////////////////////////////////////
                $scope.invoice = {
                    id: 0,
                };
                $scope.createInvoice = function () {
                    if ( $scope.isReclamationSetProperlyAlsoNotifyUser() ) {
                        //Reclamation set properly now what?

                        var damages = [1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,'stripping','other_fees','glazier','work_force','parts'];
                        var subs = ['tech','note','price'];
                        var invoiceData = {
                            rental_agreement: jQuery('#invoice_location').val(),
                            rental_car: jQuery('#rental_car').is(':checked') ? 1 : 0,
                            payment_status: jQuery('#payment_status').val(),
                            sub_total: jQuery('#sub_total').val(),
                            tps: jQuery('#tps').val(),
                            tvq: jQuery('#tvq').val(),
                            franchise: jQuery('#franchise').val(),
                            total: jQuery('#total').val(),
                            deposit: jQuery('#deposit').val(),
                            balance: jQuery('#balance').val(),
                            damages: jQuery('#damages').val(),
                            payment_method: jQuery('input[name="payment_method"]:checked').val(),
                            number_of_days: jQuery('#number_of_days').val(),
                            confirm_invoice: 1,
                        };

                        try {
                            var $apiSignaturePad = jQuery('#signature-pad').signaturePad();
                            if ( $apiSignaturePad.getSignature().length ) {
                                invoiceData['signature_img'] = $apiSignaturePad.getSignatureImage();
                                invoiceData['signature'] = $apiSignaturePad.getSignatureString();
                            }
                        } catch (e) {}

                        jQuery.each(damages, function (k, v) {
                            jQuery.each(subs, function (subKey, subValue) {
                                if ( jQuery('#inv_' + v + '_' + subValue).length ) {
                                    invoiceData['inv_' + v + '_' + subValue] = jQuery('#inv_' + v + '_' + subValue).val();
                                }
                            });
                        });


                        var parts = [];
                        for ( var p = 1; p <= 10; p++ ) {
                            var part = jQuery('#part_' + p).val();
                            var part_received_status = jQuery('#part_received_' + p).is(':checked');
                            var part_order_status = jQuery('#part_order_' + p).is(':checked');
                            var part_price = jQuery('#part_price_' + p).val();

                            if ( jQuery.trim(part) && jQuery.trim(part_price) && jQuery.trim(part_price) > 0  ) {
                                parts.push( {
                                    'name': part,
                                    'price': part_price,
                                    'ordered': part_order_status ? 1 : 0,
                                    'received': part_received_status ? 1 : 0,
                                } );
                            }
                        }
                        invoiceData['parts'] = parts;

                        if ($scope.dotInfoHolder) {
                            invoiceData['javascript_object'] = $scope.dotInfoHolder;
                        }

                        ReclamationService.createEstimateAndInvoice(invoiceData, $scope.estimation).then(function (value) {
                            $scope.setMessage(value.data.message, SUCCESS);
                            window.location.href = '/admin/estimation_report.php';
                        }, function (reason) {
                            $scope.setMessage(reason.message, ERROR);
                            jQuery("html, body").animate({ scrollTop: 0 }, "slow");
                        });
                    }
                };

                $scope.updateInvoice = function() {

                    if ( $scope.isReclamationSetProperlyAlsoNotifyUser() ) {
                        //Reclamation set properly now what?

                        var damages = [1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,'stripping','other_fees','glazier','work_force','parts'];
                        var subs = ['tech','note','price'];
                        var invoiceData = {
                            rental_agreement: jQuery('#invoice_location').val(),
                            rental_car: jQuery('#rental_car').is(':checked') ? 1 : 0,
                            payment_status: jQuery('#payment_status').val(),
                            sub_total: jQuery('#sub_total').val(),
                            tps: jQuery('#tps').val(),
                            tvq: jQuery('#tvq').val(),
                            franchise: jQuery('.invoice_franchise').val(),
                            total: jQuery('#total').val(),
                            deposit: jQuery('#deposit').val(),
                            balance: jQuery('#balance').val(),
                            damages: jQuery('#damages').val(),
                            payment_method: jQuery('input[name="payment_method"]:checked').val(),
                            number_of_days: jQuery('#number_of_days').val(),
                            confirm_invoice: 1,
                        };

                        if ($scope.dotInfoHolder) {
                            invoiceData['javascript_object'] = $scope.dotInfoHolder;
                        }

                        try {
                            var $apiSignaturePad = jQuery('#signature-pad').signaturePad();
                            if ( $apiSignaturePad.getSignature().length ) {
                                invoiceData['signature_img'] = $apiSignaturePad.getSignatureImage();
                                invoiceData['signature'] = $apiSignaturePad.getSignatureString();
                            }
                        } catch (e) {}

                        jQuery.each(damages, function (k, v) {
                            jQuery.each(subs, function (subKey, subValue) {
                                if ( jQuery('#inv_' + v + '_' + subValue).length ) {
                                    invoiceData['inv_' + v + '_' + subValue] = jQuery('#inv_' + v + '_' + subValue).val();
                                }
                            });
                        });


                        var parts = [];
                        for ( var p = 1; p <= 10; p++ ) {
                            var part = jQuery('#part_' + p).val();
                            var part_received_status = jQuery('#part_received_' + p).is(':checked');
                            var part_order_status = jQuery('#part_order_' + p).is(':checked');
                            var part_price = jQuery('#part_price_' + p).val();

                            if ( jQuery.trim(part) && jQuery.trim(part_price) && jQuery.trim(part_price) > 0  ) {
                                parts.push( {
                                    'name': part,
                                    'price': part_price,
                                    'ordered': part_order_status ? 1 : 0,
                                    'received': part_received_status ? 1 : 0,
                                } );
                            }
                        }
                        invoiceData['parts'] = parts;


                        ReclamationService.updateEstimateAndInvoice(invoiceData, $scope.estimation, $scope.invoice.id).then(function (value) {
                            $scope.setMessage(value.data.message, SUCCESS);
                            window.location.href = '/admin/estimation_report.php';
                        }, function (reason) {
                            $scope.setMessage(reason.message, ERROR);
                            jQuery("html, body").animate({ scrollTop: 0 }, "slow");
                        });
                    }

                };

                $scope.changeInvoiceType = function(invoice) {
                    ReclamationService.changeInvoiceType(invoice).then(function (value) {
                        window.location.reload();
                    });
                };

                $scope.loadInvoice = function() {

                    angular.forEach($scope.invoice, function (v,k) {
                        if ( jQuery('#' + k).length ) {
                            jQuery('#' + k).val(v);
                        }
                        if (k === 'franchise' && v ) {
                            jQuery('.invoice_franchise').val(v);
                        }
                        if ( k === 'payment_method' && v) {
                            jQuery('input[name="payment_method"]').each(function () {
                               if (jQuery(this).val() === v) {
                                   jQuery(this).attr('checked', true);
                               }
                            });
                        }
                    });


                    for (var j = 1; j <= $scope.invoice.parts.length; j++) {
                        var i = j-1;
                        jQuery('#part_' + j).val($scope.invoice.parts[i].name);
                        jQuery('#part_price_' + j).val($scope.invoice.parts[i].price);

                        if ($scope.invoice.parts[i].ordered == 1) {
                            jQuery('#part_order_' + j).attr('checked', true);
                        }

                        if ($scope.invoice.parts[i].received == 1) {
                            jQuery('#part_received_' + j).attr('checked', true);
                        }
                    }
                };

                $scope.dotInfo = {};
                $scope.dotInfoHolder = {};
                $scope.addDotInfo = function () {
                    var dotInfo = {
                        number: jQuery('#dot_number').val(),
                        tech:   jQuery('#dot_tech').val(),
                        desc:   jQuery('#dot_desc').val(),
                        price:  jQuery('#dot_price').val(),
                    };
                    $scope.dotInfo['number_' + dotInfo.number] = dotInfo;
                    $scope.dotInfoHolder[dotInfo.number] = {
                        price:  jQuery('#dot_price').val(),
                        description:   jQuery('#dot_desc').val(),
                        tech:   jQuery('#dot_tech').val(),
                    };
                };

            }
        ]
    )
;
