{{ header }}
<div id="account-register" class="container-xl flex-grow-1">
	<nav aria-label="breadcrumb">
        <ul class="breadcrumb pr-breadcrumb fsz-12">
        {% for breadcrumb in breadcrumbs %}
            {% if loop.last %}
                <li class="breadcrumb-item pr-breadcrumb-item">{{ breadcrumb.text }}</li>
            {% else %}
                <li class="breadcrumb-item pr-breadcrumb-item"><a href="{{ breadcrumb.href }}">{{ breadcrumb.text }}</a></li>
            {% endif %}
        {% endfor %}
        </ul>
    </nav>
	<main>
		<div class="content-top-box">{{ content_top }}</div>
		{% if error_warning %}
			<script>
				prNotify('danger', '{{ error_warning }}');
			</script>
		{% endif %}
		<div class="row g-3 g-xxl-4 pr-account">
			{{ column_left }}
		    {% if column_left and column_right %}
		    	{% set class = 'col-xl-6' %}
		    {% elseif column_left or column_right %}
		    	{% set class = 'col-xl-9' %}
		    {% else %}
		    	{% set class = 'col-xl-12' %}
		    {% endif %}
		    <div id="content" class="{{ class }}">
				<div class="content-block pr-account-page">
					<form action="{{ action }}" method="post" enctype="multipart/form-data" class="form-horizontal">
						<fieldset id="account" class="p-0">
                            <div class="pr-page-title mb-3 mb-xl-4 pb-3 pb-xl-4 border-bottom d-flex align-items-center gap-3">
                                <h1>{{ heading_title }}</h1>
                            </div>
							<legend class="h4 mb-4">{{ text_your_details }}</legend>
							<div class="form-group mb-4 d-{% if customer_groups|length > 1 %}block {% else %}none {% endif %}">
								<label class="dark-text fsz-14 fw-500 mb-1"><span class="required">*</span> {{ entry_customer_group }}</label>
								{% for customer_group in customer_groups %}
									{% if customer_group.customer_group_id == customer_group_id %}
										<div class="form-check d-flex align-items-center mb-0">
											<input type="radio" name="customer_group_id" class="form-check-input" id="{{ customer_group.customer_group_id }}" value="{{ customer_group.customer_group_id }}" checked="checked" />
											<label class="form-check-label" for="{{ customer_group.customer_group_id }}">
												{{ customer_group.name }}
											</label>
										</div>
									{% else %}
										<div class="form-check d-flex align-items-center mb-0">
											<input type="radio" name="customer_group_id" class="form-check-input" id="{{ customer_group.customer_group_id }}" value="{{ customer_group.customer_group_id }}">
											<label class="form-check-label" for="{{ customer_group.customer_group_id }}">
												{{ customer_group.name }}
											</label>
										</div>
									{% endif %}
								{% endfor %}
							</div>
							<div class="form-floating mb-4">
								{% if error_firstname %}
									<script>
										prNotify('danger', '{{ error_firstname }}');
									</script>
								{% endif %}
								<input type="text" name="firstname" value="{{ firstname }}" placeholder="{{ entry_firstname }}" id="input-firstname" class="form-control" />
								<label class="px-3 secondary-text fsz-14" for="input-firstname"><span class="required">*</span> {{ entry_firstname }}</label>
							</div>
							<div class="form-floating mb-4">
								{% if error_lastname %}
									<script>
										prNotify('danger', '{{ error_lastname }}');
									</script>
								{% endif %}
								<input type="text" name="lastname" value="{{ lastname }}" placeholder="{{ entry_lastname }}" id="input-lastname" class="form-control" />
								<label class="px-3 secondary-text fsz-14" for="input-lastname"><span class="required">*</span> {{ entry_lastname }}</label>
							</div>
							<div class="form-floating mb-4">
								{% if error_email %}
									<script>
										prNotify('danger', '{{ error_email }}');
									</script>
								{% endif %}
								<input type="email" name="email" value="{{ email }}" placeholder="{{ entry_email }}" id="input-email" class="form-control" inputmode="email">
								<label class="px-3 secondary-text fsz-14" for="input-email"><span class="required">*</span> {{ entry_email }}</label>
							</div>
							<div class="form-floating mb-4">
								{% if error_telephone %}
									<script>
										prNotify('danger', '{{ error_telephone }}');
									</script>
								{% endif %}
								<input type="tel" name="telephone" value="{{ telephone }}" placeholder="{{ entry_telephone }}" id="input-telephone" class="form-control" inputmode="tel">
								<label class="px-3 secondary-text fsz-14" for="input-telephone"><span class="required">*</span> {{ entry_telephone }}</label>
							</div>
							{% for custom_field in custom_fields %}
								{% if custom_field.type == 'select' %}
									<div id="custom-field{{ custom_field.custom_field_id }}" class="form-floating mb-4 custom-field" data-sort="{{ custom_field.sort_order }}">
										{% if error_custom_field[custom_field.custom_field_id] %}
											<script>
												prNotify('danger', '{{ error_custom_field[custom_field.custom_field_id] }}');
											</script>
										{% endif %}
										<select name="custom_field[{{ custom_field.location }}][{{ custom_field.custom_field_id }}]" id="input-custom-field{{ custom_field.custom_field_id }}" class="form-select form-control">
											<option value="">{{ text_select }}</option>
											{% for custom_field_value in custom_field.custom_field_value %}
												{% if register_custom_field[custom_field.location][custom_field.custom_field_id] and custom_field_value.custom_field_value_id == register_custom_field[custom_field.custom_field_id] %}
													<option value="{{ custom_field_value.custom_field_value_id }}" selected="selected">{{ custom_field_value.name }}</option>
												{% else %}
													<option value="{{ custom_field_value.custom_field_value_id }}">{{ custom_field_value.name }}</option>
												{% endif %}
											{% endfor %}
										</select>
										<label class="px-3 secondary-text fsz-14" for="input-custom-field{{ custom_field.custom_field_id }}">{{ custom_field.name }}</label>
									</div>
								{% endif %}
								{% if custom_field.type == 'radio' %}
									<div id="custom-field{{ custom_field.custom_field_id }}" class="form-group mb-4 custom-field" data-sort="{{ custom_field.sort_order }}">
										<label class="dark-text fsz-14 fw-500 mb-1">{{ custom_field.name }}</label>
										{% if error_custom_field[custom_field.custom_field_id] %}
											<script>
												prNotify('danger', '{{ error_custom_field[custom_field.custom_field_id] }}');
											</script>
										{% endif %}
										<div>
											{% for custom_field_value in custom_field.custom_field_value %}
												<div class="form-check d-flex align-items-center gap-3 mb-3">
													{% if register_custom_field[custom_field.custom_field_id] and custom_field_value.custom_field_value_id == register_custom_field[custom_field.custom_field_id] %}
														<label class="form-check-label form-check-label fsz-12 dark-text d-flex align-items-center gap-2">
															<input type="radio" class="form-check-input" name="custom_field[{{ custom_field.location }}][{{ custom_field.custom_field_id }}]" value="{{ custom_field_value.custom_field_value_id }}" checked="checked" />
															{{ custom_field_value.name }}
														</label>
													{% else %}
														<label class="form-check-label form-check-label fsz-12 dark-text d-flex align-items-center gap-2">
															<input type="radio" class="form-check-input" name="custom_field[{{ custom_field.location }}][{{ custom_field.custom_field_id }}]" value="{{ custom_field_value.custom_field_value_id }}" />
															{{ custom_field_value.name }}
														</label>
													{% endif %}
												</div>
											{% endfor %}
										</div>
									</div>
								{% endif %}
								{% if custom_field.type == 'checkbox' %}
									<div id="custom-field{{ custom_field.custom_field_id }}" class="form-group mb-4 custom-field" data-sort="{{ custom_field.sort_order }}">
										<label class="dark-text fsz-14 fw-500 mb-1">{{ custom_field.name }}</label>
										{% if error_custom_field[custom_field.custom_field_id] %}
											<script>
												prNotify('danger', '{{ error_custom_field[custom_field.custom_field_id] }}');
											</script>
										{% endif %}
										<div class="form-checkbox-group">
											{% for custom_field_value in custom_field.custom_field_value %}
												<div class="form-check d-flex align-items-center mb-0">
													{% if register_custom_field[custom_field.custom_field_id] and custom_field_value.custom_field_value_id in register_custom_field[custom_field.custom_field_id] %}
														<label class="form-check-label">
															<input type="checkbox" name="custom_field[{{ custom_field.location }}][{{ custom_field.custom_field_id }}][]" value="{{ custom_field_value.custom_field_value_id }}" checked="checked" class="form-check-input" />
															{{ custom_field_value.name }}
														</label>
													{% else %}
														<label class="form-check-label">
															<input type="checkbox" name="custom_field[{{ custom_field.location }}][{{ custom_field.custom_field_id }}][]" value="{{ custom_field_value.custom_field_value_id }}" class="form-check-input" />
															{{ custom_field_value.name }}
														</label>
													{% endif %}
												</div>
											{% endfor %}
										</div>
									</div>
								{% endif %}
								{% if custom_field.type == 'text' %}
									<div id="custom-field{{ custom_field.custom_field_id }}" class="form-floating mb-4 custom-field" data-sort="{{ custom_field.sort_order }}">
										{% if error_custom_field[custom_field.custom_field_id] %}
											<script>
												prNotify('danger', '{{ error_custom_field[custom_field.custom_field_id] }}');
											</script>
										{% endif %}
										<input type="text" name="custom_field[{{ custom_field.location }}][{{ custom_field.custom_field_id }}]" value="{% if register_custom_field[custom_field.custom_field_id] %}{{ register_custom_field[custom_field.custom_field_id] }}{% else %}{{ custom_field.value }}{% endif %}" placeholder="{{ custom_field.name }}" id="input-custom-field{{ custom_field.custom_field_id }}" class="form-control" />
										<label class="px-3 secondary-text fsz-14" for="input-custom-field{{ custom_field.custom_field_id }}">{{ custom_field.name }}</label>
									</div>
								{% endif %}
								{% if custom_field.type == 'textarea' %}
									<div id="custom-field{{ custom_field.custom_field_id }}" class="form-group mb-4 custom-field" data-sort="{{ custom_field.sort_order }}">
										<label class="dark-text fsz-14 fw-500 mb-1" for="input-custom-field{{ custom_field.custom_field_id }}">{{ custom_field.name }}</label>
										{% if error_custom_field[custom_field.custom_field_id] %}
											<script>
												prNotify('danger', '{{ error_custom_field[custom_field.custom_field_id] }}');
											</script>
										{% endif %}
										<textarea name="custom_field[{{ custom_field.location }}][{{ custom_field.custom_field_id }}]" rows="5" placeholder="{{ custom_field.name }}" id="input-custom-field{{ custom_field.custom_field_id }}" class="form-control">{% if register_custom_field[custom_field.custom_field_id] %}{{ register_custom_field[custom_field.custom_field_id] }}{% else %}{{ custom_field.value }}{% endif %}</textarea>
									</div>
								{% endif %}
								{% if custom_field.type == 'file' %}
									<div id="custom-field{{ custom_field.custom_field_id }}" class="form-group mb-3 custom-field" data-sort="{{ custom_field.sort_order }}">
										<label class="dark-text fsz-14 fw-500 mb-1">{{ custom_field.name }}</label>
										{% if error_custom_field[custom_field.custom_field_id] %}
											<script>
												prNotify('danger', '{{ error_custom_field[custom_field.custom_field_id] }}');
											</script>
										{% endif %}
										<button type="button" id="button-custom-field{{ custom_field.custom_field_id }}" data-loading-text="{{ text_loading }}" class="button button-primary"><i class="fa fa-upload"></i> {{ button_upload }}</button>
										<input type="hidden" name="custom_field[{{ custom_field.location }}][{{ custom_field.custom_field_id }}]" value="{% if register_custom_field[custom_field.custom_field_id] %}  {{ register_custom_field[custom_field.custom_field_id] }} {% endif %}" />
									</div>
								{% endif %}
								{% if custom_field.type == 'date' %}
									<div id="custom-field{{ custom_field.custom_field_id }}" class="form-group mb-3 custom-field" data-sort="{{ custom_field.sort_order }}">
										<label class="dark-text fsz-14 fw-500 mb-1" for="input-custom-field{{ custom_field.custom_field_id }}">{{ custom_field.name }}</label>
										{% if error_custom_field[custom_field.custom_field_id] %}
											<script>
												prNotify('danger', '{{ error_custom_field[custom_field.custom_field_id] }}');
											</script>
										{% endif %}
										<div class="input-group date">
											<input type="date" name="custom_field[{{ custom_field.location }}][{{ custom_field.custom_field_id }}]" value="{% if register_custom_field[custom_field.custom_field_id] %}{{ register_custom_field[custom_field.custom_field_id] }}{% else %}{{ custom_field.value }}{% endif %}" placeholder="{{ custom_field.name }}" data-date-format="YYYY-MM-DD" id="input-custom-field{{ custom_field.custom_field_id }}" class="form-control" />
										</div>
									</div>
								{% endif %}
								{% if custom_field.type == 'time' %}
									<div id="custom-field{{ custom_field.custom_field_id }}" class="form-group mb-3 custom-field" data-sort="{{ custom_field.sort_order }}">
										<label class="dark-text fsz-14 fw-500 mb-1" for="input-custom-field{{ custom_field.custom_field_id }}">{{ custom_field.name }}</label>
										{% if error_custom_field[custom_field.custom_field_id] %}
											<script>
												prNotify('danger', '{{ error_custom_field[custom_field.custom_field_id] }}');
											</script>
										{% endif %}
										<div class="input-group time">
											<input type="time" name="custom_field[{{ custom_field.location }}][{{ custom_field.custom_field_id }}]" value="{% if register_custom_field[custom_field.custom_field_id] %}{{ register_custom_field[custom_field.custom_field_id] }}{% else %}{{ custom_field.value }}{% endif %}" placeholder="{{ custom_field.name }}" data-date-format="HH:mm" id="input-custom-field{{ custom_field.custom_field_id }}" class="form-control" />
										</div>
									</div>
								{% endif %}
								{% if custom_field.type == 'datetime' %}
									<div id="custom-field{{ custom_field.custom_field_id }}" class="form-group mb-3 custom-field" data-sort="{{ custom_field.sort_order }}">
										<label class="dark-text fsz-14 fw-500 mb-1" for="input-custom-field{{ custom_field.custom_field_id }}">{{ custom_field.name }}</label>
										{% if error_custom_field[custom_field.custom_field_id] %}
											<script>
												prNotify('danger', '{{ error_custom_field[custom_field.custom_field_id] }}');
											</script>
										{% endif %}
										<div class="input-group datetime">
											<input type="datetime-local" name="custom_field[{{ custom_field.location }}][{{ custom_field.custom_field_id }}]" value="{% if register_custom_field[custom_field.custom_field_id] %}{{ register_custom_field[custom_field.custom_field_id] }}{% else %}{{ custom_field.value }}{% endif %}" placeholder="{{ custom_field.name }}" data-date-format="YYYY-MM-DD HH:mm" id="input-custom-field{{ custom_field.custom_field_id }}" class="form-control" />
										</div>
									</div>
								{% endif %}
							{% endfor %}
						</fieldset>
						<fieldset class="p-0">
							<legend class="h4 mt-3 mb-2">{{ text_your_password }}</legend>
							<div class="form-floating mb-4">
								{% if error_password %}
									<script>
										prNotify('danger', '{{ error_password }}');
									</script>
								{% endif %}
								<input type="password" name="password" value="{{ password }}" placeholder="{{ entry_password }}" id="input-password" class="form-control" />
								<label class="px-3 secondary-text fsz-14" for="input-password"><span class="required">*</span> {{ entry_password }}</label>
							</div>
							<div class="form-floating mb-4">
								{% if error_confirm %}
									<script>
										prNotify('danger', '{{ error_confirm }}');
									</script>
								{% endif %}
								<input type="password" name="confirm" value="{{ confirm }}" placeholder="{{ entry_confirm }}" id="input-confirm" class="form-control" />
								<label class="px-3 secondary-text fsz-14" for="input-confirm"><span class="required">*</span> {{ entry_confirm }}</label>
							</div>
						</fieldset>
						<fieldset class="p-0">
							<legend class="h4 mt-3 mb-2">{{ text_newsletter }}</legend>
							<div class="form-group mb-3">
								<label class="dark-text fsz-14 fw-500 mb-2">{{ entry_newsletter }}</label>
								<div class="d-flex">
									{% if newsletter %}
										<div class="form-check d-flex align-items-center mb-0">
											<input type="radio" name="newsletter" class="form-check-input" id="checkYes" class="form-check-input" value="1" checked="checked" />
											<label class="form-check-label" for="checkYes">
												{{ text_yes }}
											</label>
										</div>
										<div class="form-check d-flex align-items-center my-0 ms-3">
											<input type="radio" name="newsletter" class="form-check-input" id="checkNo" class="form-check-input" value="0" />
											<label class="form-check-label" for="checkNo">
												{{ text_no }}
											</label>
										</div>
									{% else %}
										<div class="form-check d-flex align-items-center mb-0">
											<input type="radio" name="newsletter" class="form-check-input" id="checkYes" class="form-check-input" value="1" />
											<label class="form-check-label" for="checkYes">
												{{ text_yes }}
											</label>
										</div>
										<div class="form-check d-flex align-items-center my-0 ms-3">
											<input type="radio" name="newsletter" class="form-check-input" id="checkNo" class="form-check-input" value="0" checked="checked" />
											<label class="form-check-label" for="checkNo">
												{{ text_no }}
											</label>
										</div>
									{% endif %}
								</div>
							</div>
						</fieldset>
						{{ captcha }}
						{% if text_agree %}
							<div class="form-check d-flex align-items-center gap-2 mb-3">
								{% if agree %}
									<input type="checkbox" name="agree" id="registerAgree" class="form-check-input" value="1" checked="checked" />
								{% else %}
									<input type="checkbox" name="agree" id="registerAgree" class="form-check-input" value="1" />
								{% endif %}
								<label class="form-check-label fsz-12 dark-text" for="registerAgree">
									{{ text_agree }}
								</label>
							</div>
						{% endif %}
						<button type="submit" class="button button-secondary button-large">{{ button_continue }}</button>
					</form>
			    </div>
		    </div>
			{{ column_right }}
		</div>
		{{ content_bottom }}
	</main>
</div>
<script>
// Sort the custom fields
$('#account .form-group[data-sort]').detach().each(function() {
	if ($(this).attr('data-sort') >= 0 && $(this).attr('data-sort') <= $('#account .form-group').length) {
		$('#account .form-group').eq($(this).attr('data-sort')).before(this);
	}

	if ($(this).attr('data-sort') > $('#account .form-group').length) {
		$('#account .form-group:last').after(this);
	}

	if ($(this).attr('data-sort') == $('#account .form-group').length) {
		$('#account .form-group:last').after(this);
	}

	if ($(this).attr('data-sort') < -$('#account .form-group').length) {
		$('#account .form-group:first').before(this);
	}
});

$('input[name=\'customer_group_id\']').on('change', function() {
	$.ajax({
		url: 'index.php?route=account/register/customfield&customer_group_id=' + this.value,
		dataType: 'json',
		cache: false,
		success: function(json) {
			$('.custom-field').hide();
			$('.custom-field').removeClass('required');

			for (i = 0; i < json.length; i++) {
				custom_field = json[i];

				$('#custom-field' + custom_field['custom_field_id']).show();

				if (custom_field['required']) {
					$('#custom-field' + custom_field['custom_field_id']).addClass('required');
				}
			}
		},
		error: function(xhr, ajaxOptions, thrownError) {
			alert(thrownError + "\r\n" + xhr.statusText + "\r\n" + xhr.responseText);
		}
	});
});

$('input[name=\'customer_group_id\']:checked').trigger('change');

$('button[id^=\'button-custom-field\']').on('click', function() {
	var element = this;

	$('#form-upload').remove();

	$('body').prepend('<form enctype="multipart/form-data" id="form-upload" style="display: none;"><input type="file" name="file" /></form>');

	$('#form-upload input[name=\'file\']').trigger('click');

	if (typeof timer != 'undefined') {
    	clearInterval(timer);
	}

	timer = setInterval(function() {
		if ($('#form-upload input[name=\'file\']').val() != '') {
			clearInterval(timer);

			$.ajax({
				url: 'index.php?route=tool/upload',
				type: 'post',
				dataType: 'json',
				data: new FormData($('#form-upload')[0]),
				cache: false,
				contentType: false,
				processData: false,
				beforeSend: function() {
					$(element).button('loading');
				},
				complete: function() {
					$(element).button('reset');
				},
				success: function(json) {
					$(element).parent().find('.text-danger').remove();

					if (json['error']) {
						$(node).parent().find('input').after('<div class="text-danger">' + json['error'] + '</div>');
					}

					if (json['success']) {
						alert(json['success']);

						$(element).parent().find('input').val(json['code']);
					}
				},
				error: function(xhr, ajaxOptions, thrownError) {
					alert(thrownError + "\r\n" + xhr.statusText + "\r\n" + xhr.responseText);
				}
			});
		}
	}, 500);
});
</script>
{{ footer }}
