'use strict';

/** 
 * @controller PageCreateController
 * @property scope 
 * @property location
 * @property filter
 * @property System
 * @property PageService
 */

var PageCreateController = angular.module('pages.controllers.create', [
    'system'
])
    .controller('PageCreateController', [
        '$scope',
        '$route',
        '$location',
        '$filter',
        'System',
        'PageService',
        function ($scope, $route, $location, $filter, System, PageService) {
            $scope.errors = {};

            /**
            * @property page
            * @description default values for page propertyes.
            */
            $scope.page = {
                ParentID: $route.current.params["id"],
                AllowAnonymous: true,
                PageType: "Normal",
                Navigable: true,
                Status: 2,
                InternalRedirectID: "",
                Title: ""
            };
            
            if($scope.page.ParentID) {
                PageService.getEdit($scope.page.ParentID).then(function (data) {
                    $scope.parent = data;
                });
            }
            
            /**
            * @method getPages
            * @description PageService load pages.
            */
            $scope.getPages = function() {
                PageService.top().then(function(data) {
                    $scope.pages = qusion.list.jsTree(data.pages);
                });
            };

            /**
            * @method create
            * @description PageService creates new page
            */
            $scope.create = function () {
                PageService.create($scope.page)
                    .then(function (response) {
                        if (response.Errors) {
                            $scope.errors = response.Errors;
                            System.Message.error($scope.$resource('utility.changeNotSave'));
                        } else {
                            $location.url('/pages');
                            System.Message.success($scope.$resource('pages.PageCreated'));
                        }
                    });
            };

            /**
            * @action back returns to location /pages
            */
            $scope.back = function () {
                $location.url('/pages');
            };

            /**
            *@property options for tree widget.
            */
            $scope.treeOptions = {
                selectMode: 1,
                dialogTitle: $scope.$resource('pages.select_dialog_title')
            };
            
            $scope.getPages();
        }
    ]);