Ext.define("Portfolio.ux.navigation.controller.UserRole", {
	extend: "Ext.app.ViewController",

	alias: "controller.navigation.userrole",

	alternateClassName: [
		"Ext.ux.navigation.controller.UserRole"
	],

	doSave: function(f_callback) {
		var me = this;
		var view = me.getView();
		var i_record = view.record;
		var array = [];
		var a_checkboxes = view.query("checkbox");
		Ext.Array.each(a_checkboxes, function(i_checkbox) {
			if (i_checkbox.getValue() != i_checkbox.selected) {
				if (i_checkbox.getValue()) {
					array.push({
						token: i_checkbox.token,
						action: "insert"
					});
					i_checkbox.selected = true;
				}
				else {
					array.push({
						token: i_checkbox.token,
						action: "remove"
					});
					i_checkbox.selected = false;
				}
			}
		});

		if (array.length > 0) {
			var o_data = {
				action: "ip2NavProxy.updateroles",
				token: i_record.get(i_record.idProperty),
				rows: array,
				entity: view.entity
			};
			Ext.Ajax.request({
				url: "/ip2-services/ip2transaction.aspx.json",
				method: "POST",
				jsonData: o_data,
				success: function() {
					view.fireEvent("savecomplete", view, true);
					if (f_callback) {
						f_callback();
					}
				},
				faliure: function(err) {
					if (f_callback) {
						f_callback();
					}
				}
			});
		}
		else {
			if (f_callback) {
				f_callback();
			}
		}
	},
	doDelete: function() {

		var i_self = this;
		var i_view = i_self.getView();
		var i_record = i_self.getRecord();

		Ext.Ajax.request({
			url: "/ip2-services/ip2transaction.aspx.json",
			method: "POST",
			jsonData: {
				action: "ip2NavProxy.deleteroles",
				token: i_record.get(i_record.idProperty),
				entity: i_view.getEntity()
			},
			success: function() {
				i_view.fireEvent("savecomplete", i_view, true);
			}
		});
	},

	getRoles: function() {

		var i_controller = this;
		var i_view = i_controller.getView();
		var a_checkboxes = i_view.query("checkbox");
		var a_result = [];
		Ext.Array.each(a_checkboxes, function(i_checkbox) {
			if (i_checkbox.getValue()) {
				a_result.push(i_checkbox.token);
			}
		});
		return a_result;
	}
});
