/**
 * Created by Jossef on 23/09/2014.
 */

var app = angular.module('app');

app.factory('GlobalService', function ($modal, $http) {
    var GlobalService = function () {
    };


    GlobalService.slides = [
        {
            image: '/img/truck14.jpg',
            text: ''
        },
        {
            image: '/img/truck14.jpg',
            text: ''
        }
    ];

    GlobalService.sendEmail = function(subject, message)
    {
        var data = {
            "subject":subject,
            "body":message
        };

        var json = angular.toJson(data);

        $http.post('/contact/sendmail.php', json);
    };

    GlobalService.contactModal = function()
    {
        var modalInstance = $modal.open({
            templateUrl: '/templates/contact-modal.html',
            controller: 'ContactModalCtrl'
        });

        modalInstance.result.then(function (message) {
            // Ok
            var subject = 'פריינטי שאיבות - יצירת קשר';
            GlobalService.sendEmail(subject, message);

        }, function () {
            // Cancel
        });
    };


    return GlobalService;
});



app.controller('ContactModalCtrl', function ($scope, $modalInstance) {

    $scope.name = '';
    $scope.emailAddress = '';
    $scope.phone = '';
    $scope.notes = 'אני מעוניין ליצור קשר, \n';

    $scope.ok = function () {
        var message = 'קוראים לי: '+ $scope.name +
            '\nהאימייל שלי: ' + $scope.emailAddress +
            '\nמספר הטלפון שלי: ' + $scope.phone +
            '\nהערות: ' + $scope.notes;
        $modalInstance.close(message);
    };

    $scope.cancel = function () {
        $modalInstance.dismiss('cancel');
    };

});


app.controller('OrderModalCtrl', function ($scope, $modalInstance, item) {

    $scope.name = '';
    $scope.emailAddress = '';
    $scope.phone = '';
    $scope.item = item;
    $scope.notes = '';

    $scope.ok = function () {
        var message = 'קוראים לי: '+ $scope.name +
            '\nהאימייל שלי: ' + $scope.emailAddress +
            '\nמספר הטלפון שלי: ' + $scope.phone +
            '\nהמוצר שאני מתעניין בו: ' + $scope.item +
            '\nהערות: ' + $scope.notes;
        $modalInstance.close(message);
    };

    $scope.cancel = function () {
        $modalInstance.dismiss('cancel');
    };

});