hino / app / controllers / role-management / create.js
create.js
Raw
import Controller from '@ember/controller';
import { inject as service } from '@ember/service';

export default Controller.extend({
	roleManagement : service(),
	roleManagementData : service(),
	dealerData : service(),
	roleManagementDealerData : service(),
	branchData : service(),
	roleManagementDealerBranchData : service(),
	session : service(),
	errors : service(),
	didValidate: false,
	rightActionInProgress: false,

	dealers: Array(),
	branchs: Array(),

	roleManagementDealerDataArray: Array(),
	roleManagementDealerBranchDataArray: Array(),

	init() {
		this._super(...arguments);
		var dealers = this.dealerData.getList();
		this.set("dealers", dealers);

		// var branchs = this.branchData.getList();
		// this.set("branchs", branchs);

	},

	actions: {
		onChangeCombo(propertyName, itemCollection) {
			// this.set(propertyName, itemCollection);
			if(propertyName == "dealer") {
				this.reloadBranch();
			}
		},

		async saveRoleManagement() {
			this.set("rightActionInProgress", true);
			this.get('model')
				.validate()
				.then(async ({ validations }) => {
					this.set('didValidate', true); 
					if (validations.get('isValid')) {
						this.set('roleManagement.model.isApprovalData', false);
						this.roleManagementData.save(this.roleManagement.model).then(response => {
							if (response != undefined) {
								this.transitionToRoute('role-management.detail', response.id); 
								this.set("rightActionInProgress", false);
							}
						}) 
						.catch((e) => {
							this.set("rightActionInProgress", false);
							this.set('errorMessages', this.errors.getErrorMessages(e));
							window.scrollTo(0, 0);
						});
					} else {
						this.set("rightActionInProgress", false);
					}
			});
		},

		addRowData(modal_type) {
			if (modal_type == "role-management-dealer") {
				let data = this.roleManagementDealerData.createRecord();
				// this.roleManagementDealerDataArray.pushObject(data);
				this.roleManagement.addRow(data, "roleManagementDealer");

				// this.roleManagementDealerDataArray.forEach(function(data){
				// 	console.log("Data Dealer", data.dealer);
				// });
				// this.get('model').then(function(data){
					console.log("DATA MODEL", this.get('model'));
				// });
			}
			if (modal_type == "role-management-dealer-branch") {
				let data = this.roleManagementDealerBranchData.createRecord();
				this.roleManagement.addRow(data, "roleManagementDealerBranch");
			}
		},
		deleteRowData(modal_type, data) {
			if (modal_type == "role-management-dealer") {
				// console.log("BEFORE DELETE", this.roleManagementDealerDataArray);
				// this.roleManagementDealerDataArray.removeObject(data);
				this.roleManagement.deleteRow(data, "roleManagementDealer");
				// console.log("AFTER DELETE", this.roleManagementDealerDataArray);
				this.roleManagement.refreshBranches();
				this.reloadBranch();
			}
			if (modal_type == "role-management-dealer-branch") {
				this.roleManagement.deleteRow(data, "roleManagementDealerBranch");
			}
		},
	},

	reloadBranch() {
		let filter = this.roleManagement.buildFilter()
		if (filter != null && filter != '') {
			filter = '{' + filter + '}';
			filter = JSON.parse(filter);
		}

		let branchs = this.branchData.getList({filter: filter}).then(result=>{
			this.set("branchs", null);
			this.set("branchs", result);
			this.roleManagement.refreshBranches();
		});
	},
});