demo20230512/Browser_Local/js/Product/ProductCreateController.js
2023-05-12 10:20:28 +08:00

70 lines
2.4 KiB
JavaScript

var arr = window.location.href.split("/");
if (arr[3].indexOf('app') == 0)
baseUrl = '';
else
baseUrl = '/' + arr[3];
angular.module('CarbonFootprint').config(
function ($routeProvider) {
$routeProvider.when('/Product/Create', {
templateUrl: baseUrl+'/Partials/Product-Create',
controller: 'ProductCreateController'
});
}
)
.controller('ProductCreateController', [
'$scope', '$http', '$location', 'UploadFile',
function ($scope, $http, $location) {
$scope.product = {};
// set $scope.baseUrl
var baseUrl = $scope.baseUrl;
if (typeof (baseUrl) === 'undefined' || baseUrl == null) {
var arr = window.location.href.split("/");
if (arr[3].indexOf('app') == 0)
baseUrl = '';
else
baseUrl = '/' + arr[3];
$scope.baseUrl = baseUrl;
}
$scope.saveProduct = function () {
if ($scope.form.$invalid) return;
//console.log('save product', $scope.product);
$http.post($scope.baseUrl+'/api/Product/Save', $scope.product)
.success(function (data, status, headers, config) {
$location.path('/Product/Index');
})
.error(function (data, status, headers, config) {
if (data?.Message != undefined)
alert(data.Message)
else
alert('Oops! This is something happen. Maybe you can try it later');
});
};
$scope.cancel = function () {
$location.path('/Product/Index');
};
$scope.uploadFile = function () {
$('#product-photo').click();
};
$scope.selectPhoto = function (photo) {
var formData = new FormData();
formData.append('file', photo);
$http.post($scope.baseUrl+'/api/Upload', formData, {
transformRequest: angular.identity,
headers: { 'Content-Type': undefined }
})
.success(function (data) {
$scope.product.PhotoUrl = $scope.baseUrl + data[0];
//console.log(' product PhotoUrl', $scope.product.PhotoUrl);
})
.error(function () {
console.log('500 error');
});
};
}
]);