import NavActions from '../actions/NavActions';
import DBUtils from '../../../../../brain/tools/utilities/DBUtils';
export default class NavIndex {
	//--------------------------
	// constructor
	//--------------------------
	constructor() {
		this.start();
		//this.dataUtils = new DataUtils();
		this.dbutils = new DBUtils();
	}
	//--------------------------
	// methods
	//--------------------------
	start() {
		Sortable.create(document.getElementById('nav-pages'), {
			onUpdate: () => {
				new NavActions()
					.save()
					.then(() => {
						//console.log(r);
					})
					.catch(() => {
						//console.log(err);
					});
			}
		});
		var nav = document.querySelectorAll('.nav-btn');
		for (var i = 0, length = nav.length; i < length; i++) {
			nav[i].addEventListener('click', e => this.handleNavButton(e), false);
		}
	}
	//--------------------------
	// event handlers
	//--------------------------
	handleNavButton(e) {
		let id = '';
		switch (e.target.id) {
			case 'remove-item':
				id = e.target.getAttribute('data-id');
				new NavActions().removeItem(id);
				break;
			case 'edit-item':
				window.location = '/@/dashboard/posts/edit/' + e.target.getAttribute('data-id');
				break;
		}
	}
}
