hino / app / controllers / role-management / edit.js
edit.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(),

	formConfig : {
		roleName : { isEditable : true }, 
		description : { isEditable : true }, 
		roleManagementDealer : { 
			roleManagement : { isEditable : true }, 
			dealer : { isEditable : true }, 
		},
		roleManagementDealerBranch : { 
			roleManagement : { isEditable : true }, 
			branch : { isEditable : true }, 
		},
	},
	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.roleManagement.addRow(data, "roleManagementDealer");
			}
			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") {
				this.roleManagement.deleteRow(data, "roleManagementDealer");
				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();
		});
	},
});