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

{% include 'xhiveerp/selling/sales_common.js' %}
xhiveframework.provide("xhiveerp.crm");

cur_frm.email_field = "contact_email";
xhiveframework.ui.form.on("Opportunity", {
	setup: function(frm) {
		frm.custom_make_buttons = {
			'Quotation': 'Quotation',
			'Supplier Quotation': 'Supplier Quotation'
		};

		frm.set_query("opportunity_from", function() {
			return{
				"filters": {
					"name": ["in", ["Customer", "Lead", "Prospect"]],
				}
			}
		});
	},

	validate: function(frm) {
		if (frm.doc.status == "Lost" && !frm.doc.lost_reasons.length) {
			frm.trigger('set_as_lost_dialog');
			xhiveframework.throw(__("Lost Reasons are required in case opportunity is Lost."));
		}
	},

	onload_post_render: function(frm) {
		frm.get_field("items").grid.set_multiple_add("item_code", "qty");
	},

	party_name: function(frm) {
		frm.trigger('set_contact_link');

		if (frm.doc.opportunity_from == "Customer") {
			xhiveerp.utils.get_party_details(frm);
		} else if (frm.doc.opportunity_from == "Lead") {
			xhiveerp.utils.map_current_doc({
				method: "xhiveerp.crm.doctype.lead.lead.make_opportunity",
				source_name: frm.doc.party_name,
				frm: frm
			});
		}
	},

	onload_post_render: function(frm) {
		frm.get_field("items").grid.set_multiple_add("item_code", "qty");
	},

	status:function(frm){
		if (frm.doc.status == "Lost"){
			frm.trigger('set_as_lost_dialog');
		}

	},

	customer_address: function(frm, cdt, cdn) {
		xhiveerp.utils.get_address_display(frm, 'customer_address', 'address_display', false);
	},

	contact_person: xhiveerp.utils.get_contact_details,

	opportunity_from: function(frm) {
		frm.trigger('setup_opportunity_from');

		frm.set_value("party_name", "");
	},

	setup_opportunity_from: function(frm) {
		frm.trigger('setup_queries');
		frm.trigger("set_dynamic_field_label");
	},

	refresh: function(frm) {
		var doc = frm.doc;
		frm.trigger('setup_opportunity_from');
		xhiveerp.toggle_naming_series();

		if(!frm.is_new() && doc.status!=="Lost") {
			if(doc.items){
				frm.add_custom_button(__('Supplier Quotation'),
					function() {
						frm.trigger("make_supplier_quotation")
					}, __('Create'));

				frm.add_custom_button(__('Request For Quotation'),
					function() {
						frm.trigger("make_request_for_quotation")
					}, __('Create'));
			}

			if (frm.doc.opportunity_from != "Customer") {
				frm.add_custom_button(__('Customer'),
					function() {
						frm.trigger("make_customer")
					}, __('Create'));
			}

			frm.add_custom_button(__('Quotation'),
				function() {
					frm.trigger("create_quotation")
				}, __('Create'));
		}

		if(!frm.doc.__islocal && frm.perm[0].write && frm.doc.docstatus==0) {
			if(frm.doc.status==="Open") {
				frm.add_custom_button(__("Close"), function() {
					frm.set_value("status", "Closed");
					frm.save();
				});
			} else {
				frm.add_custom_button(__("Reopen"), function() {
					frm.set_value("lost_reasons",[])
					frm.set_value("status", "Open");
					frm.save();
				});
			}
		}

		if (!frm.is_new()) {
			xhiveframework.contacts.render_address_and_contact(frm);
			// frm.trigger('render_contact_day_html');
		} else {
			xhiveframework.contacts.clear_address_and_contact(frm);
		}

		if (frm.doc.opportunity_from && frm.doc.party_name) {
			frm.trigger('set_contact_link');
		}
	},

	set_contact_link: function(frm) {
		if(frm.doc.opportunity_from == "Customer" && frm.doc.party_name) {
			xhiveframework.dynamic_link = {doc: frm.doc, fieldname: 'party_name', doctype: 'Customer'}
		} else if(frm.doc.opportunity_from == "Lead" && frm.doc.party_name) {
			xhiveframework.dynamic_link = {doc: frm.doc, fieldname: 'party_name', doctype: 'Lead'}
		} else if (frm.doc.opportunity_from == "Prospect" && frm.doc.party_name) {
			xhiveframework.dynamic_link = {doc: frm.doc, fieldname: 'party_name', doctype: 'Prospect'}
		}
	},

	currency: function(frm) {
		let company_currency = xhiveerp.get_currency(frm.doc.company);
		if (company_currency != frm.doc.company) {
			xhiveframework.call({
				method: "xhiveerp.setup.utils.get_exchange_rate",
				args: {
					from_currency: frm.doc.currency,
					to_currency: company_currency
				},
				callback: function(r) {
					if (r.message) {
						frm.set_value('conversion_rate', flt(r.message));
						frm.set_df_property('conversion_rate', 'description', '1 ' + frm.doc.currency
						+ ' = [?] ' + company_currency);
					}
				}
			});
		} else {
			frm.set_value('conversion_rate', 1.0);
			frm.set_df_property('conversion_rate', 'hidden', 1);
			frm.set_df_property('conversion_rate', 'description', '');
		}

		frm.trigger('opportunity_amount');
		frm.trigger('set_dynamic_field_label');
	},

	opportunity_amount: function(frm) {
		frm.set_value('base_opportunity_amount', flt(frm.doc.opportunity_amount) * flt(frm.doc.conversion_rate));
	},

	set_dynamic_field_label: function(frm){
		if (frm.doc.opportunity_from) {
			frm.set_df_property("party_name", "label", frm.doc.opportunity_from);
		}
		frm.trigger('change_grid_labels');
		frm.trigger('change_form_labels');
	},

	make_supplier_quotation: function(frm) {
		xhiveframework.model.open_mapped_doc({
			method: "xhiveerp.crm.doctype.opportunity.opportunity.make_supplier_quotation",
			frm: frm
		})
	},

	make_request_for_quotation: function(frm) {
		xhiveframework.model.open_mapped_doc({
			method: "xhiveerp.crm.doctype.opportunity.opportunity.make_request_for_quotation",
			frm: frm
		})
	},

	change_form_labels: function(frm) {
		let company_currency = xhiveerp.get_currency(frm.doc.company);
		frm.set_currency_labels(["base_opportunity_amount", "base_total"], company_currency);
		frm.set_currency_labels(["opportunity_amount", "total"], frm.doc.currency);

		// toggle fields
		frm.toggle_display(["conversion_rate", "base_opportunity_amount", "base_total"],
			frm.doc.currency != company_currency);
	},

	change_grid_labels: function(frm) {
		let company_currency = xhiveerp.get_currency(frm.doc.company);
		frm.set_currency_labels(["base_rate", "base_amount"], company_currency, "items");
		frm.set_currency_labels(["rate", "amount"], frm.doc.currency, "items");

		let item_grid = frm.fields_dict.items.grid;
		$.each(["base_rate", "base_amount"], function(i, fname) {
			if(xhiveframework.meta.get_docfield(item_grid.doctype, fname))
				item_grid.set_column_disp(fname, frm.doc.currency != company_currency);
		});
		frm.refresh_fields();
	},

	calculate_total: function(frm) {
		let total = 0, base_total = 0;
		frm.doc.items.forEach(item => {
			total += item.amount;
			base_total += item.base_amount;
		})

		frm.set_value({
			'total': flt(total),
			'base_total': flt(base_total)
		});
	},
});
xhiveframework.ui.form.on("Opportunity Item", {
	calculate: function(frm, cdt, cdn) {
		let row = xhiveframework.get_doc(cdt, cdn);
		xhiveframework.model.set_value(cdt, cdn, "amount", flt(row.qty) * flt(row.rate));
		xhiveframework.model.set_value(cdt, cdn, "base_rate", flt(frm.doc.conversion_rate) * flt(row.rate));
		xhiveframework.model.set_value(cdt, cdn, "base_amount", flt(frm.doc.conversion_rate) * flt(row.amount));
		frm.trigger("calculate_total");
	},
	qty: function(frm, cdt, cdn) {
		frm.trigger("calculate", cdt, cdn);
	},
	rate: function(frm, cdt, cdn) {
		frm.trigger("calculate", cdt, cdn);
	}
})

// TODO commonify this code
xhiveerp.crm.Opportunity = class Opportunity extends xhiveframework.ui.form.Controller {
	onload() {

		if(!this.frm.doc.status) {
			frm.set_value('status', 'Open');
		}
		if(!this.frm.doc.company && xhiveframework.defaults.get_user_default("Company")) {
			frm.set_value('company', xhiveframework.defaults.get_user_default("Company"));
		}
		if(!this.frm.doc.currency) {
			frm.set_value('currency', xhiveframework.defaults.get_user_default("Currency"));
		}

		this.setup_queries();
		this.frm.trigger('currency');
	}

	refresh() {
		this.show_notes();
		this.show_activities();
	}

	setup_queries() {
		var me = this;

		me.frm.set_query('customer_address', xhiveerp.queries.address_query);

		this.frm.set_query("item_code", "items", function() {
			return {
				query: "xhiveerp.controllers.queries.item_query",
				filters: {'is_sales_item': 1}
			};
		});

		me.frm.set_query('contact_person', xhiveerp.queries['contact_query'])

		if (me.frm.doc.opportunity_from == "Lead") {
			me.frm.set_query('party_name', xhiveerp.queries['lead']);
		}
		else if (me.frm.doc.opportunity_from == "Customer") {
			me.frm.set_query('party_name', xhiveerp.queries['customer']);
		} else if (me.frm.doc.opportunity_from == "Prospect") {
			me.frm.set_query('party_name', function() {
				return {
					filters: {
						"company": me.frm.doc.company
					}
				};
			});
		}
	}

	create_quotation() {
		xhiveframework.model.open_mapped_doc({
			method: "xhiveerp.crm.doctype.opportunity.opportunity.make_quotation",
			frm: cur_frm
		})
	}

	make_customer() {
		xhiveframework.model.open_mapped_doc({
			method: "xhiveerp.crm.doctype.opportunity.opportunity.make_customer",
			frm: cur_frm
		})
	}

	show_notes() {
		const crm_notes = new xhiveerp.utils.CRMNotes({
			frm: this.frm,
			notes_wrapper: $(this.frm.fields_dict.notes_html.wrapper),
		});
		crm_notes.refresh();
	}

	show_activities() {
		const crm_activities = new xhiveerp.utils.CRMActivities({
			frm: this.frm,
			open_activities_wrapper: $(this.frm.fields_dict.open_activities_html.wrapper),
			all_activities_wrapper: $(this.frm.fields_dict.all_activities_html.wrapper),
			form_wrapper: $(this.frm.wrapper),
		});
		crm_activities.refresh();
	}
};

extend_cscript(cur_frm.cscript, new xhiveerp.crm.Opportunity({frm: cur_frm}));

cur_frm.cscript.item_code = function(doc, cdt, cdn) {
	var d = locals[cdt][cdn];
	if (d.item_code) {
		return xhiveframework.call({
			method: "xhiveerp.crm.doctype.opportunity.opportunity.get_item_details",
			args: {"item_code":d.item_code},
			callback: function(r, rt) {
				if(r.message) {
					$.each(r.message, function(k, v) {
						xhiveframework.model.set_value(cdt, cdn, k, v);
					});
					refresh_field('image_view', d.name, 'items');
				}
			}
		})
	}
}
