{%- extends "templates/saas/layout.html" -%}
{% set app_title = app_title %}
{% set image_path = image_path %}

{%- block content -%}
<input id="app" type="text" value="{{ app }}" hidden />
<input id="headless" type="check" value="{{ headless }}" hidden />

{% if headless %}

<div class="mx-auto text-center form-container col-xl-5 col-lg-6 col-md-7 col-sm-9" id="user-headless-verify">
	<form name="verify">
		{%- if jingrow.form_dict.key -%}
		<div class="mx-auto form-container col-xl-5 col-lg-6 col-md-7 col-sm-9">
			<button id="redirect" type="submit" style="width: 100%; background-color: #171717; color: white;" class="btn btn-primary" hidden>Redirect</button>
		</div>
		{%- else -%}
		<div class="m-0 alert alert-danger small" role="alert">
			Invalid or expired key
		</div>
		{%- endif -%}
	</form>
</div>

{% else %}
<div class="flex justify-center mx-auto form-container col-xl-5 col-lg-6 col-md-7 col-sm-9" id="user-signup-verify"
	data-validators="field_validators">
	<div class="card">
		<div class="card-body">
			{%- if jingrow.form_dict.key -%}
			<div class="alert alert-primary form-alert-info small" style="display: none;" role="alert"></div>
			<div class="alert alert-danger form-alert-error small" style="display: none;" role="alert"></div>
			<form class="form-step" data-step="1" data-action="business_settings">
				<div class="form-group">
					<p>Company Name</p>
					<input type="text" class="form-control" id="company_name" name="company">
				</div>
				<div class="form-row">
					<div class="form-group col">
						<p for="industry">Industry</p>
						<select id="industry" name="industry" class="custom-select">
							<option selected disabled></option>
							<option value="Distribution">Distribution</option>
							<option value="Manufacturing">Manufacturing</option>
							<option value="Retail">Retail</option>
							<option value="Education">Education</option>
							<option value="Services">Services</option>
							<option value="Agriculture">Agriculture</option>
							<option value="Non Profit">Non Profit</option>
							<option value="Non Profit">Others</option>
						</select>
					</div>
				</div>
				<div class="form-row">
					<div class="form-group col">
						<p for="no_of_employees">Number of employees</p>
						<select id="no_of_employees" name="no_of_employees" class="custom-select">
							<option selected disabled></option>
							<option value="1-10">1 - 10</option>
							<option value="11-50">11 - 50</option>
							<option value="51-200">51 - 200</option>
							<option value="201-500">201 - 500</option>
							<option value="501-1000">501 - 1000</option>
							<option value="1000+">1000+</option>
						</select>
					</div>
				</div>
				<div class="form-row">
					<div class="form-group col">
						<p for="designation">Your Designation</p>
						<select id="designation" name="designation" class="custom-select">
							<!-- <option disabled selected>Select Country</option> -->
							<option value=''></option>
							<option value='CEO/Founder/GM'>CEO/Founder/GM</option>
							<option value='CFO'>CFO</option>
							<option value='CTO/CIO'>CTO/CIO</option>
							<option value='Vice president'>Vice president</option>
							<option value='Director/Head of Department'>Director/Head of Department</option>
							<option value='IT/System manager'>IT/System manager</option>
							<option value='Manager (Sales/Marketing/Customer)'>Manager (Sales/Marketing/Customer)
							</option>
							<option value='Developer/Analyst'>Developer/Analyst</option>
							<option value='Student/Freelancer/Just looking'>Student/Freelancer/Just looking</option>
							<option value='Others'>Others</option>
						</select>
					</div>
				</div>
				<div class="form-group">
					<p for="phone">Phone Number</p>
					<input type="tel" id="phone" class="form-control" name="phone_number" autocomplete="tel">
				</div>
				<div class="form-group">
					<input type="checkbox" name="agreed_to_partner_consent" id="agreed_to_partner_consent">
					<p class="d-inline" for="agreed_to_partner_consent">I am okay if my details are shared with
						local partner</p>
				</div>
				<button type="submit" style="width: 100%; background-color: #171717; color: white;" class="btn btn-primary btn-step-4">Next</button>
			</form>
			{%- else -%}
			<div class="m-0 alert alert-danger small" role="alert">
				Invalid or expired key
			</div>
			{%- endif -%}
		</div>
	</div>
</div>

{% endif %}

{%- endblock -%}

{%- block script -%}
<script src="/assets/jcloud/js/form_controller.js"></script>
<script>
	let controller;
	let form_values = {};
	let key = jingrow.utils.get_url_arg('key');
	const app = document.getElementById('app').value;
	const headless = document.getElementById('headless').value;

	if (headless == 1) {
		jingrow.ready(() => {
			controller = new FormController("user-headless-verify");

			// call setup function after form loads
			jingrow.call({
				method: 'jcloud.api.saas.headless_setup_account',
				args: { key },
				type: "POST"
			})
				.then((r) => {
					window.location.href = '/prepare-site?key=' + key + '&app=' + app;
				})

		});
	} else {
		jingrow.ready(() => {
			controller = new FormController("user-signup-verify");

			// load country, currency, language select options
			load_dropdowns();
		});
	}

	// this needs to be on global scope
	var field_validators = {
		domain: (value) => {
			if (!value) {
				return "Please enter a domain";
			}
		},
		company: (value) => {
			if (!value) {
				return "Please enter company name";
			}
		},
		industry: (value) => {
			if (!value) {
				return "Please select an industry";
			}
		},
		designation: (value) => {
			if (!value) {
				return "Please enter your designation";
			}
		},
		referral_source: (value) => {
			if (!value) {
				return "Please select a value";
			}
		},
		no_of_employees: (value) => {
			if (!value) {
				return 'Please select a value';
			}
		},
		no_of_users: (value) => {
			if (isNaN(value)) {
				return 'Please enter a valid number';
			}
		},
	};

	function regional_settings($form, values) {
		Object.assign(form_values, values);
	}

	function business_settings($form, values) {
		Object.assign(form_values, values);
		return call('jcloud.api.saas.setup_account', { key, business_data: form_values }, $form)
			.then(() => {
				window.location.href = '/prepare-site?key=' + key + '&app=' + app;
			});
	}

	function load_dropdowns() {
		if (!key) return;

		jingrow.call({
			method: "jcloud.api.central.options_for_regional_data",
			args: { key },
			callback: function (r) {
				let $country_input = $('input[name=country]');
				$country_input.val(r.message.country);

				let $language_select = $("select[name=language]");
				r.message.languages.forEach((language) => {
					//language[0] is for language code and language[1] is for language name
					$language_select.append(
						$("<option />").val(language.language_code).text(language.language_name)
					);
				});

				let $timezone_select = $("select[name=timezone]");
				r.message.all_timezones.forEach((timezone) => {
					$timezone_select.append(
						$("<option />").val(timezone).text(timezone)
					);
				});

				let $currency_select = $("select[name=currency]");
				r.message.currencies.forEach((currency) => {
					$currency_select.append(
						$("<option />").val(currency).text(currency)
					);
				});

				let country_info = r.message.country_info;

				let country = r.message.country;
				$timezone_select.val(country_info[country].timezones[0]);
				$currency_select.val(country_info[country].currency);
				$language_select.val("en");
			},
		});
	}

	function call(method, args, $form) {
		return jingrow
			.call({
				method,
				args,
				type: "POST",
				btn: $form.find("button.btn-primary"),
			})
			.then((r) => {
				if (r.exc) {
					console.error("An error occurred", r.exc);
					return;
				}
				return r;
			});
	}

</script>
{%- endblock -%}
