text: (function($, app) {
      'use strict';
  
      angular
          .module('ThachvuCMS')
          .controller('CategoryController', CategoryController);
  
      function CategoryController($rootScope, $scope, $http, $window, $timeout) {
  
          $scope.categories = [];
          $scope.totalPages = 0;
          $scope.currentPage = 1;
          $scope.range = [];
          $scope.enableSubmit = false;
  
          $scope.pullDownLists = {
              availableOption: [
                { value: 10, name: '10' },
                { value: 25, name: '25' },
                { value: 50, name: '50' },
                { value: 100, name: '100' }
              ],
              selectedOption: {value: 10, name: '10'}
          };
  
          $scope.getResultsPage = function (name, perPage, pageNumber) {
              $scope.loading = true;
              $scope.loaded = false;
  
              $http.get(app.vars.baseUrl + '/categories/getAllCategories?name=' + name + '&perPage=' + perPage + '&page=' + pageNumber, {cache: false})
                  .success(function(response) {
  
                      $scope.loading = false;
                      $scope.loaded = true;
  
                      $scope.name = name;
                      $scope.pullDownLists.selectedOption = { value: perPage, name: perPage };
                      $scope.perPage = perPage;
                      $scope.pageNumber = pageNumber;
                      $scope.totalPages = response.data.last_page;
                      $scope.currentPage = response.data.current_page;
                      $scope.from = response.data.from;
                      $scope.to = response.data.to;
                      $scope.total = response.data.total;
                      var pages = [];
                      for (var i = 1; i <= response.data.last_page; i++) {          
                          pages.push(i);
                      }
                      $scope.range = pages;
                      if ($scope.totalPages == 0) {
                          $scope.currentPage = 0;
                      }
                      $scope.categories = response.data.data;
                      $scope.totalItems = response.data.total;
  
                  });
          }
  
          $scope.loadInit = function () {
              $scope.getResultsPage('all-category', 10, 1);
          }
  
          $scope.searchCategoryName = function() {
              if ($scope.searchText.length >= 1) {
                  $scope.getResultsPage($scope.searchText, $scope.perPage, $scope.pageNumber);
              } else {
                  $scope.getResultsPage('all-category', $scope.perPage, $scope.pageNumber);
              }
          }
  
          $scope.previousPage = function () {
              $scope.pageNumber -= 1;
              $scope.getResultsPage($scope.searchText, $scope.perPage, $scope.pageNumber);
          }
  
          $scope.nextPage = function () {
              $scope.pageNumber += 1;
              $scope.getResultsPage($scope.searchText, $scope.perPage, $scope.pageNumber);
          }
  
          $scope.range = function(min, max, step) {
              step = step || 1;
              var input = [];
              for (var i = min; i <= max; i += step) input.push(i);
              return input;
          };
  
          $scope.process = function (type) {
              
              var title = (type == 'add') ? 'thêm' : 'cập nhật';
              var formData = new FormData($('#formProcess')[0]);
              formData.append('seo_content', CKEDITOR.instances.seo_content.document.getBody().getHtml());
              
              swal({
                  title: "Bạn chắc chắn muốn "+ title +" danh mục này ?",
                  type: "warning",
                  showCancelButton: true,
                  confirmButtonClass: "btn-success",
                  confirmButtonText: (type == 'add') ? 'Thêm' : 'Cập nhật' + ' ngay',
                  cancelButtonText: "Quay lại",
                  closeOnConfirm: false,
                  showLoaderOnConfirm: true
              }, function () {
                  $http({
                      method: 'POST',
                      url: app.vars.baseUrl + '/categories/' + type,
                      data: formData,
                      headers: { 'Content-Type': undefined },
                      transformRequest: angular.identity
                  }).success(function (response) {
                      swal({ title: '', text: response.message, type: response.type }, function (isConfirm) {
                          if (isConfirm) {
                              if (response.status) {
                                  // toastr.success(response.message, 'SUCCESS');
                                  window.location.href = app.vars.baseUrl + '/categories';
                              } else {
                                  // toastr.error(response.message, 'ERROR');
                              }
                          }
                      })
                  });
              });
          }
  
          $scope.delete = function (cate, index) {
              swal({
                  title: "Bạn chắc chắn muốn xóa danh mục này ? Tất cả các sản phẩm thuộc danh mục này sẽ bị xóa theo !",
                  type: "warning",
                  showCancelButton: true,
                  confirmButtonClass: "btn-danger",
                  confirmButtonText: 'Xóa ngay',
                  cancelButtonText: "Quay lại",
                  closeOnConfirm: false,
                  showLoaderOnConfirm: true
              }, function () {
                  $http({
                      url: app.vars.baseUrl + '/categories/delete',
                      method: 'POST',
                      data: {
                          cateId: cate.id
                      }
                  }).success(function (response) {
                      swal({ title: '', text: response.message, type: response.type }, function (isConfirm) {
                          if (isConfirm) {
                              if (response.status) {
                                  // toastr.success(response.message, 'SUCCESS');
                                  $scope.loadInit();
                              } else {
                                  // toastr.error(response.message, 'ERROR');
                              }
                          }
                      })
                  });
              });
          }
  
      }
  })($, $.app);