// Copyright (c) 2015, SolutionERP LLC and Contributors
// License: GNU General Public License v3. See license.txt

xhiveframework.ui.form.on('Sales Person', {
	refresh: function(frm) {
		if(frm.doc.__onload && frm.doc.__onload.dashboard_info) {
			let info = frm.doc.__onload.dashboard_info;
			frm.dashboard.add_indicator(__('Total Contribution Amount Against Orders: {0}',
				[format_currency(info.allocated_amount_against_order, info.currency)]), 'blue');

			frm.dashboard.add_indicator(__('Total Contribution Amount Against Invoices: {0}',
				[format_currency(info.allocated_amount_against_invoice, info.currency)]), 'blue');
		}
	},

	setup: function(frm) {
		frm.fields_dict["targets"].grid.get_field("distribution_id").get_query = function(doc, cdt, cdn){
			var row = locals[cdt][cdn];
			return {
				filters: {
					'fiscal_year': row.fiscal_year
				}
			}
		};

		frm.make_methods = {
			'Sales Order': () => xhiveframework.new_doc("Sales Order")
				.then(() => frm.add_child("sales_team", {"sales_person": frm.doc.name}))
		}
	}
});

cur_frm.cscript.refresh = function(doc, cdt, cdn) {
	cur_frm.cscript.set_root_readonly(doc);
}

cur_frm.cscript.set_root_readonly = function(doc) {
	// read-only for root
	if(!doc.parent_sales_person && !doc.__islocal) {
		cur_frm.set_read_only();
		cur_frm.set_intro(__("This is a root sales person and cannot be edited."));
	} else {
		cur_frm.set_intro(null);
	}
}

//get query select sales person
cur_frm.fields_dict['parent_sales_person'].get_query = function(doc, cdt, cdn) {
	return{
		filters: [
			['Sales Person', 'is_group', '=', 1],
			['Sales Person', 'name', '!=', doc.sales_person_name]
		]
	}
}

cur_frm.fields_dict.employee.get_query = function(doc, cdt, cdn) {
	return { query: "xhiveerp.controllers.queries.employee_query" }
}
