var autoApp = angular.module('autoApp', ['ngTouch', 'ui.grid', 'ui.grid.selection', 'ui.grid.pagination', 'ui.grid.autoResize']);
autoApp.controller('AutoUsedController', ['$scope', '$http', '$interval', '$timeout', 'uiGridConstants', function ($scope, $http, $interval, $timeout, $uiGridConstants) {
$scope.Message = "";
var stockNumber;
var paginationOptions = {
pageNumber: 1,
pageSize: 25,
sort: null
};
$scope.gridOptions = {
enableRowSelection: true,
enableRowHeaderSelection: false,
enableColumnMenus: false,
paginationPageSizes: [25, 50, 75],
paginationPageSize: 25,
useExternalPagination: true,
useExternalSorting: true,
columnDefs: [
{ field: 'ID', displayName: 'Item ID', width: 100, enableFiltering: false },
{ field: 'bodyType', displayName: 'Body Type', width: 100, height: 35, enableFiltering: false },
{ field: 'autoMake', displayName: 'Make', width: 150, height: 35 },
{ field: 'autoModel', displayName: 'Model', width: 150, height: 35 },
{ field: 'autoYear', displayName: 'Year', width: 60, height: 35 },
{ field: 'autoDoor', displayName: 'Doors', width: 60, height: 35, enableFiltering: false },
{ field: 'exteriorColor', displayName: 'Ext Color', width: 100, height: 35, enableFiltering: false },
{ field: 'interiorColor', displayName: 'Int Color', width: 100, height: 35, enableFiltering: false },
// { field: 'autoMileage', displayName: 'Mileage', width: 100, height: 35, cellClass: 'grid-align-center', enableFiltering: false },
{ field: 'autoStatus', displayName: 'Status', width: 100, height: 35, cellClass: 'grid-align-center', enableFiltering: false },
{ field: 'salePrice', displayName: 'Price', width: 100, height: 35, cellFilter: 'currency', cellClass: 'grid-align-right', enableFiltering: false },
//{ field: 'picture', displayName: 'Picture', width: 80, height: 35, cellTemplate: "<img width=\"60px\" ng-src=\"{{grid.getCellValue(row, col)}}\" lazy-src>", enableFiltering: false },
{ field: 'picture', displayName: 'Picture', width: 70, cellTemplate: "<img width=\"60px\" ng-src=\"{{grid.getCellValue(row, col)}}\" lazy-src>", enableFiltering: false },
{ field: 'Edit', width: 70, height: 35, cellTemplate: '<button class="btn primary" ng-click="grid.appScope.showDetail(row)">Edit</button>', enableFiltering: false }
],
onRegisterApi: function (gridApi) {
$scope.gridApi = gridApi;
$scope.gridApi.core.on.sortChanged($scope, function (grid, sortColumns) {
if (sortColumns.length === 0) {
paginationOptions.sort = null;
} else {
paginationOptions.sort = sortColumns[0].sort.direction;
}
getPage(userAcct);
});
gridApi.pagination.on.paginationChanged($scope, function (newPage, pageSize) {
paginationOptions.pageNumber = newPage;
paginationOptions.pageSize = pageSize;
getPage(userAcct);
});
$scope.gridOptions.multiSelect = false;
$scope.gridOptions.modifierKeysToMultiSelect = false;
$scope.gridOptions.noUnselect = true;
$scope.gridOptions.onRegisterApi = function (gridApi) {
$scope.gridApi = gridApi;
};
$scope.toggleRowSelection = function () {
$scope.gridApi.selection.clearSelectedRows();
$scope.gridOptions.enableRowSelection = !$scope.gridOptions.enableRowSelection;
$scope.gridApi.core.notifyDataChange(uiGridConstants.dataChange.OPTIONS);
};
}
};
var getPage = function (uAccount) {
$http.get('http://localhost:8080/AutoWcfService/Service1.svc/AutoUsedMaint/kc-6')
.success(function (data) {
$("#imgLoading").hide();
var totalItems = data.length;
$scope.gridOptions.totalItems = totalItems;
var firstRow = (paginationOptions.pageNumber - 1) * paginationOptions.pageSize;
$scope.gridOptions.data = data.slice(firstRow, firstRow + paginationOptions.pageSize);
// $interval(function () { $scope.gridApi.selection.selectRow($scope.gridOptions.data[0]); }, 0, 1);
});
};
$scope.showDetail = function (row) {
var vRecordId = row.entity.ID;
$scope.stockNumber = row.entity.stockNumber
$scope.userAccount = row.entity.accountNumber
$scope.recordId = row.entity.ID
$scope.bodyType = row.entity.bodyType
$scope.autoMake = row.entity.autoMake
$scope.autoModel = row.entity.autoModel
$scope.autoYear = row.entity.autoYear
$scope.exteriorColor = row.entity.exteriorColor
$scope.interiorColor = row.entity.interiorColor
$scope.autoEngine = row.entity.autoEngine
$scope.autoDoor = row.entity.autoDoor
$scope.autoMileage = row.entity.autoMileage
$scope.salePrice = row.entity.salePrice
$scope.newPrice = row.entity.newPrice
$scope.autoStatu = row.entity.autoStatus
$scope.userState = row.entity.userState
$scope.userCity = row.entity.userCity
$scope.imgDisplay = row.entity.picture
$scope.notes = row.entity.notes
$scope.VIN = row.entity.VIN
};
autoApp.filter('bodyType', function () {
var genderHash = {
1: 'Van',
2: 'Truck',
3: 'SUV',
4: 'Wagons',
5: 'Sedans',
6: 'Any',
7: 'Convetiples',
8: 'Couples'
};
return function (input) {
if (!input) {
return 'this';
} else {
return "this is"; //genderHash[input];
}
};
});
$scope.showImage = function (image) {
var value = row.entity.picture;
return value;
};
$scope.newPrice = "0";
$scope.autoStatu = "0";
//Pad given value to the left with "0"
function AddZero(num) {
return (num >= 0 && num < 10) ? "0" + num : num + "";
}
function getStockNumber(vUserAcct) {
//debugger;
var now = new Date();
var DD = AddZero(now.getDate());
var MM = AddZero(now.getMonth());
var YY = AddZero(now.getYear());
$http({
method: 'Get',
url: 'UsedCar/GetNextStockNumber/',
data: JSON.stringify(vUserAcct),
dataType: "json"
}).success(function (data) {
$scope.stockNumber = vUserAcct + DD.toString() + "-" + MM.toString() + "-" + YY.toString();
}).error(function (data) {
stockNumber = '';
});
}
GetAutoMakes();
function GetAutoMakes() {
$http.get("http://localhost:8080/AutoWcfService/Service1.svc/GetAutoMake/")
.success(function (data, status, headers, config) {
$scope.autoMakes = data;
}).error(function (data, status, headers, config) {
$scope.message = 'Unexpected Error';
});
}
$scope.GetAutoModels = function () {
var makeId = $scope.autoMake;
$http.get("http://localhost:8080/AutoWcfService/Service1.svc/GetAutoModels/" + makeId)
.success(function (data, status, headers, config) {
$scope.autoModels = data;
}).error(function (data, status, headers, config) {
$scope.message = 'Unexpected Error';
});
}
GetAutoYears();
function GetAutoYears() {
$http.get("http://localhost:8080/AutoWcfService/Service1.svc/GetYears/")
.success(function (data, status, headers, config) {
$scope.autoYears = data;
}).error(function (data, status, headers, config) {
$scope.message = 'Unexpected Error';
});
}
EngineList();
function EngineList() {
$http.get("http://localhost:8080/AutoWcfService/Service1.svc/GetAutoEngine/")
.success(function (data, status, headers, config) {
$scope.autoEngines = data;
}).error(function (data, status, headers, config) {
$scope.message = 'Unexpected Error';
});
}
BodyTypeList();
function BodyTypeList() {
$http.get("http://localhost:8080/AutoWcfService/Service1.svc/GetBodyType/")
.success(function (data, status, headers, config) {
$scope.autoBodyType = data;
}).error(function (data, status, headers, config) {
$scope.message = 'Unexpected Error';
});
}
ColorList();
function ColorList() {
$http.get("http://localhost:8080/AutoWcfService/Service1.svc/GetAutoColor/")
.success(function (data, status, headers, config) {
$scope.autoColors = data;
}).error(function (data, status, headers, config) {
$scope.message = 'Unexpected Error';
});
}
AutoStatusList();
function AutoStatusList() {
$http.get("http://localhost:8080/AutoWcfService/Service1.svc/GetAutoStatus/")
.success(function (data, status, headers, config) {
$scope.autoStatus = data;
}).error(function (data, status, headers, config) {
$scope.message = 'Unexpected Error';
});
}
AutoDoorList();
function AutoDoorList() {
$http.get("http://localhost:8080/AutoWcfService/Service1.svc/GetAutoDoor/")
.success(function (data, status, headers, config) {
$scope.autoDoors = data;
}).error(function (data, status, headers, config) {
$scope.message = 'Unexpected Error';
});
}
GetStates();
function GetStates() {
$http({
method: 'Get',
url: 'http://localhost:8080/AutoWcfService/Service1.svc/GetState/'
}).success(function (data) {
$scope.States = data;
}).error(function (data) {
$scope.Message = 'Get State Unexpected Error';
});
}
var stateId;
$scope.GetCities = function () {
stateId = $scope.userState;
if (stateId) {
$http.get("http://localhost:8080/AutoWcfService/Service1.svc/GetCity/" + stateId)
.success(function (data, status, headers, config) {
$scope.Cities = data;
}).error(function (data, status, headers, config) {
$scope.Message = 'Get Cities Unexpected Error';
});
}
else {
$scope.custCity = null;
}
};
SaleStatusList();
function SaleStatusList() {
$http.get("http://localhost:8080/AutoWcfService/Service1.svc/SaleStatus")
.success(function (data, status, headers, config) {
$scope.SaleStatus = data;
}).error(function (data, status, headers, config) {
$scope.message = 'Unexpected Error';
});
}
}
]);
// allow you to format a text input field.
// <input type="text" ng-model="test" format="number" />
// <input type="text" ng-model="test" format="currency" />
autoApp.directive('format', ['$filter', function ($filter) {
return {
require: '?ngModel',
link: function (scope, elem, attrs, ctrl) {
if (!ctrl) return;
ctrl.$formatters.unshift(function (a) {
return $filter(attrs.format)(ctrl.$modelValue);
});
elem.bind('blur', function (event) {
var plainNumber = elem.val().replace(/[^\d|\-+|\.+]/g, '');
elem.val($filter(attrs.format)(plainNumber));
});
}
};
}]);