class ActionGroupControl {
	constructor(linked) {
		this.linked = false !== linked;
		this.groups = new DatasetControl({
			prefix:"group",
			prefixName:"기능그룹",
			keymapper:info => info.GRP_ID,
			dataGetter:obj => obj.groupList,
			formats: {
				REG_DT:datetimeFormat
			},
			urls:{
				load:wctx.url("/actionGroup/list.do"),
				getInfo:wctx.url("/actionGroup/info.do"),
				create:wctx.url("/actionGroup/create.do"),
				update:wctx.url("/actionGroup/update.do"),
				remove:wctx.url("/actionGroup/remove.do")
			}
		});
		
		this.actions = new DatasetControl({
			prefix:"action",
			prefixName:"기능",
			keymapper:info => info.GRP_ID + "-" + info.ACTION,
			dataGetter:obj => obj.actionList,
			formats: {
				REG_DT:datetimeFormat
			},
			urls:{
				load:wctx.url("/actionGroup/action/list.do")
			}
		});
		this.actions.urls.load = wctx.url("/actionGroup/action/list.do")
		
		this.groups.onDatasetChange = obj => this.onGroupListChange(obj);
		this.groups.onCurrentChange = item => {
			this.onCurrentGroupChange(item);
			if (!this.linked) return;
			
			let row = item ? item.data : null;
			if (row) {
				this.actions.query.groupIDs = row.GRP_ID;
				this.actions.load();
			} else
				this.actions.dataset.clear();
		};
		this.groups.onSelectionChange = selected => this.onGroupSelect(selected);

		this.actions.onDatasetChange = obj => this.onActionListChange(obj);
		this.actions.onCurrentChange = item => this.onCurrentActionChange(item);
		this.actions.onSelectionChange = selected => this.onActionSelect(selected);
	}
	
	onGroupListChange(obj) {}
	
	onCurrentGroupChange(item) {}
	
	onGroupSelect(selected) {}

	async selectActionGroup() {
		return new Promise((resolve, reject) => {
			ajax.get({
				url:wctx.url("/actionGroup/select.do"),
				data:{multiple:true},
				success:resp => {
					dialog.open({
						title:"기능그룹 선택",
						content:resp,
						size: "lg",
						getData:() => {return getSelectedActionGroup();},
						onOK:(selected) => resolve(selected)
					});
				}
			});
		});
	}
	
	onActionListChange(obj) {}
	
	onCurrentActionChange(item) {}
	
	onActionSelect(selected) {}
	
	async addActions() {
		let actions = await selectURL(true);
		actions = actions.filter(a => "/" == a || a.endsWith(".do"));
		let groupID = this.groups.dataset.getCurrent().GRP_ID;
		json.post({
			url:wctx.url("/actionGroup/action/add.do"),
			data:{
				groupID:groupID,
				actions:actions.join(",")
			},
			success:resp => {
				this.actions._load();
			}
		});
	}
	
	removeActions() {
		let selected = this.actions.dataset.getDataset("selected").map(info => info.ACTION);
		let groupID = this.groups.dataset.getCurrent().GRP_ID;
		json.post({
			url:wctx.url("/actionGroup/action/remove.do"),
			data:{
				groupID:groupID,
				actions:selected.join(",")
			},
			success:resp => {
				if (resp.saved)
					this.actions._load();
			}
		});
	}
}
