supersamApp.controller('SmsOrdersReadyController', [ '$scope', '$http', '$timeout', 'config', '$filter', 'Upload', function($scope, $http, $timeout, config, $filter, Upload) { var SmsOrdersReady; SmsOrdersReady = (function() { class SmsOrdersReady { constructor() { this.processSearchResponse = this.processSearchResponse.bind(this); this.searchSms = this.searchSms.bind(this); this.uploadSmsList = this.uploadSmsList.bind(this); $scope.utilities.showContentOnLoad(); $timeout(() => { return angular.element("#smsOrdersReadySearch").focus(); }); config.sortFields = { smsList: { field: 'sentDate', direction: 'desc' } }; } processSearchResponse(data) { this.errors = []; this.smsList = data.smsList; this.totalSms = data.totalSms; this.pagination.totalItems = data.totalSms; this.pagination.totalPages = this.pagination.numPerPage < 1 ? 1 : Math.ceil(this.pagination.totalItems / this.pagination.numPerPage); this.pagination.totalPages = Math.max(this.pagination.totalPages || 0, 1); $scope.utilities.setUpdateDates("smsOrdersReady", data, config.updates); return $timeout(() => { return angular.element("body").scrollTo('#table-sms-orders-ready', 50, { offset: { top: -300 } }); }); } searchSms() { var dataToSend; this.search = angular.copy(this.search.trim()); $timeout(() => { return angular.element("#smsOrdersReadySearch").val(this.search); }); if (!this.isSubmitting) { this.isSubmitting = true; dataToSend = { action: "searchSms", search: this.search, smsType: this.smsType, smsStatus: this.smsStatus, currentPage: this.pagination.currentPage, numPerPage: this.pagination.numPerPage, sortFields: config.sortFields.smsList }; return $http.post(config.urls.smsOrdersReady, dataToSend).then((response) => { var data; data = response.data; this.isSubmitting = false; this.usedSearch = angular.copy(this.search); if (data) { if (data.status && (data.status === "success")) { return this.processSearchResponse(data); } else { if (data.errors && data.errors.length) { return this.errors = data.errors; } else { return toastr.error(config.global_labels.defaultErrorMessage.value); } } } }).catch((response) => { if (response.data && response.data.errors) { this.errors = response.data.errors; } else { toastr.error(config.global_labels.defaultErrorMessage.value); } this.usedSearch = angular.copy(this.search); return this.isSubmitting = false; }); } } uploadSmsList() { if (!this.isSubmitting && this.importSmsFile) { this.isSubmitting = true; this.errors = []; return Upload.upload({ url: config.urls.smsOrdersReady, data: { file: this.importSmsFile, action: 'importSms' } }).then((response) => { var data; data = response.data; this.isSubmitting = false; if (data.status && (data.status === "success")) { toastr.success('Импорт выполнен успешно, загружено ' + data.importCount + ' новых SMS'); this.pagination.setPage(1); return $scope.utilities.setUpdateDates("smsOrdersReady", data, config.updates); } else { if (data.errors) { return this.errors = data.errors; } else { return toastr.error(config.global_labels.defaultErrorMessage.value); } } }).catch((response) => { if (response.data && response.data.errors) { this.errors = response.data.errors; } else { this.errors = ['Ошибка импорта, повторите позднее!']; } return this.isSubmitting = false; }); } } }; SmsOrdersReady.prototype.isSubmitting = false; SmsOrdersReady.prototype.errors = []; SmsOrdersReady.prototype.importSmsFile = null; SmsOrdersReady.prototype.search = ""; SmsOrdersReady.prototype.smsType = "order"; SmsOrdersReady.prototype.smsStatus = ""; SmsOrdersReady.prototype.usedSearch = null; SmsOrdersReady.prototype.smsList = []; SmsOrdersReady.prototype.pagination = { currentPage: 1, numPerPage: 25, totalItems: 0, onWatchChange: (newValue, oldValue) => { if (newValue !== oldValue) { return $scope.sms.searchSms(); } }, setPage: (page) => { if (page !== $scope.sms.pagination.currentPage) { $scope.sms.pagination.currentPage = page; } $scope.sms.pagination.totalPages = 0; $scope.sms.smsList = []; return $scope.sms.searchSms(); } }; return SmsOrdersReady; }).call(this); $scope.config = config; $scope.utilities = new UtilitiesClass(config, $filter, $timeout, $http); $scope.sms = new SmsOrdersReady(); $scope.$watch("sms.pagination.currentPage", $scope.sms.pagination.onWatchChange); $scope.$watch("config.sortFields.smsList.field", $scope.sms.pagination.onWatchChange); return $scope.$watch("config.sortFields.smsList.direction", $scope.sms.pagination.onWatchChange); } ]);