{% extends "_layouts/cp" %}
{% set title = 'Sitemap' %}
{% set selectedSubnavItem = 'sitemap' %}
{% set fullPageForm = true %}

{% import "_includes/forms" as forms %}

{% block saveButton %}
	<input type="submit" class="btn submit" value="{{ 'Save'|t }}">
{% endblock %}

{% set changeFrequencyOpts = {
	'always': 'Always',
	'hourly': 'Hourly',
	'daily': 'Daily',
	'weekly': 'Weekly',
	'monthly': 'Monthly',
	'yearly': 'Yearly',
	'never': 'Never',
} %}

{% set priorityOpts = {
	'1.0': '1.0 (High)',
	'0.9': '0.9',
	'0.8': '0.8',
	'0.7': '0.7',
	'0.6': '0.6',
	'0.5': '0.5 (Default)',
	'0.4': '0.4',
	'0.3': '0.3',
	'0.2': '0.2',
	'0.1': '0.1',
	'0.0': '0.0 (Low)',
} %}

{% block content %}
	{% namespace namespace %}

		{# Start: Sections #}
		<div class="field">
			<div class="heading">
				<label>Sections</label>
				<div class="instructions">How should we handle each section?</div>
			</div>
		</div>

		<input type="hidden" name="sections" value="">

		<table class="data fullwidth">
			<thead>
			<tr>
				<th>Section</th>
				<th style="white-space:nowrap">Change Frequency</th>
				<th>Priority</th>
				<th>Enabled?</th>
			</tr>
			</thead>
			<tbody>
			{% for section in sections %}
				<tr>
					<td style="text-align:left;">
						<input
							type="hidden"
							name="sections[{{ section.id }}]"
							value=""
						/>
						<input
							type="hidden"
							name="sections[{{ section.id }}][id]"
							value="{{ sitemap.sections is defined and sitemap.sections[section.id] is defined ? sitemap.sections[section.id].id : -1 }}"
						/>
						<input
							type="hidden"
							name="sections[{{ section.id }}][url]"
							value="{{ section.id }}"
						/>
						<strong>{{ section.name }}</strong>

						{% for siteSettings in section.getSiteSettings() %}
							<span class="seo--sitemap-tag">{{ siteSettings.site.name }}</span>
						{% endfor %}
					</td>
					<td class="thin" style="text-align:left;">
						{{ forms.selectField({
							name: "sections["~section.id~"][frequency]",
							options: changeFrequencyOpts,
							value: sitemap.sections[section.id].frequency|default('weekly')
						}) }}
					</td>
					<td class="thin">
						{{ forms.selectField({
							name: "sections["~section.id~"][priority]",
							options: priorityOpts,
							value: sitemap.sections[section.id].priority|default('0.5')
						}) }}
					</td>
					<td class="thin" style="text-align:center">
						{{ forms.checkbox({
							name: "sections["~section.id~"][enabled]",
							value: true,
							checked: (sitemap.sections is defined and sitemap.sections[section.id] is defined ? sitemap.sections[section.id].enabled : false)
						}) }}
					</td>
				</tr>
			{% endfor %}
			</tbody>
		</table>
		{# End: Sections #}

		<br>

		{# Start: Categories #}
		<div class="field">
			<div class="heading">
				<label>Categories</label>
				<div class="instructions">How should we handle each category?</div>
			</div>
		</div>

		<input type="hidden" name="categories" value="">

		<table class="data fullwidth">
			<thead>
			<tr>
				<th>Category</th>
				<th style="white-space:nowrap">Change Frequency</th>
				<th>Priority</th>
				<th>Enabled?</th>
			</tr>
			</thead>
			<tbody>
			{% for category in categories %}
				<tr>
					<td style="text-align:left;">
						<input
							type="hidden"
							name="categories[{{ category.id }}]"
							value=""
						/>
						<input
							type="hidden"
							name="categories[{{ category.id }}][id]"
							value="{{ sitemap.categories is defined and sitemap.categories[category.id] is defined ? sitemap.categories[category.id].id : -1 }}"
						/>
						<input
							type="hidden"
							name="categories[{{ category.id }}][url]"
							value="{{ category.id }}"
						/>
						<strong>{{ category.name }}</strong>
					</td>
					<td class="thin" style="text-align:left;">
						{{ forms.selectField({
							name: "categories["~category.id~"][frequency]",
							options: changeFrequencyOpts,
							value: sitemap.categories[category.id].frequency|default('weekly')
						}) }}
					</td>
					<td class="thin">
						{{ forms.selectField({
							name: "categories["~category.id~"][priority]",
							options: priorityOpts,
							value: sitemap.categories[category.id].priority|default('0.5')
						}) }}
					</td>
					<td class="thin" style="text-align:center">
						{{ forms.checkbox({
							name: "categories["~category.id~"][enabled]",
							value: true,
							checked: (sitemap.categories is defined and sitemap.categories[category.id] is defined ? sitemap.categories[category.id].enabled : false)
						}) }}
					</td>
				</tr>
			{% endfor %}
			</tbody>
		</table>
		{# End: Categories #}

		<br>

		{# Start: Product Types #}
		{% if productTypes|length %}
			<div class="field">
				<div class="heading">
					<label>Product Types</label>
					<div class="instructions">How should we handle each product type?</div>
				</div>
			</div>

			<input type="hidden" name="categories" value="">

			<table class="data fullwidth">
				<thead>
				<tr>
					<th>Product Type</th>
					<th style="white-space:nowrap">Change Frequency</th>
					<th>Priority</th>
					<th>Enabled?</th>
				</tr>
				</thead>
				<tbody>
				{% for productType in productTypes %}
					<tr>
						<td style="text-align:left;">
							<input
								type="hidden"
								name="productTypes[{{ productType.id }}]"
								value=""
							/>
							<input
								type="hidden"
								name="productTypes[{{ productType.id }}][id]"
								value="{{ sitemap.productTypes is defined and sitemap.productTypes[productType.id] is defined ? sitemap.productTypes[productType.id].id : -1 }}"
							/>
							<input
								type="hidden"
								name="productTypes[{{ productType.id }}][url]"
								value="{{ productType.id }}"
							/>
							<strong>{{ productType.name }}</strong>
						</td>
						<td class="thin" style="text-align:left;">
							{{ forms.selectField({
								name: "productTypes["~productType.id~"][frequency]",
								options: changeFrequencyOpts,
								value: sitemap.productTypes[productType.id].frequency|default('weekly')
							}) }}
						</td>
						<td class="thin">
							{{ forms.selectField({
								name: "productTypes["~productType.id~"][priority]",
								options: priorityOpts,
								value: sitemap.productTypes[productType.id].priority|default('0.5')
							}) }}
						</td>
						<td class="thin" style="text-align:center">
							{{ forms.checkbox({
								name: "productTypes["~productType.id~"][enabled]",
								value: true,
								checked: (sitemap.productTypes is defined and sitemap.productTypes[productType.id] is defined ? sitemap.productTypes[productType.id].enabled : false)
							}) }}
						</td>
					</tr>
				{% endfor %}
				</tbody>
			</table>

			<br>
		{% endif %}
		{# End: Product Types #}

		{# Start: Custom #}
		<div class="field">
			<div class="heading">
				<label>Custom URLs</label>
				<div class="instructions">Add additional URLs that may not appear in Craft (i.e. public templates). <strong>Do not include {{ siteUrl }}</strong></div>
			</div>
		</div>

		<input type="hidden" name="customUrls" value="">

		<style>
			.seo--custom-url-td {
				padding: 0 !important;
			}
			.seo--custom-url-input {
				width: 100%;
				padding: 12px 0;
			}
			.seo--custom-url-input:not(:focus) {
				border: 2px solid #fff;
			}
		</style>
		<table class="data fullwidth" id="customUrls">
			<thead>
			<tr>
				<th>URL</th>
				<th style="white-space:nowrap">Change Frequency</th>
				<th>Priority</th>
				<th>Enabled?</th>
				<th></th>
			</tr>
			</thead>
			<tbody>
			<tr class="hidden">
				<td class="seo--custom-url-td">
					<input
						type="hidden"
						name="customUrls[{i}]"
						value=""
					/>
					<input
						type="hidden"
						name="customUrls[{i}][id]"
						value="-1"
					/>
					<input
						class="seo--custom-url-input"
						placeholder="URL"
						name="customUrls[{i}][url]"
					/>
				</td>
				<td class="thin" style="text-align:left;">
					{{ forms.selectField({
						name: 'customUrls[{i}][frequency]',
						options: changeFrequencyOpts,
						value: 'weekly'
					}) }}
				</td>
				<td class="thin">
					{{ forms.selectField({
						name: 'customUrls[{i}][priority]',
						options: priorityOpts,
						value: '0.5'
					}) }}
				</td>
				<td class="thin" style="text-align:center">
					{{ forms.checkbox({
						name: 'customUrls[{i}][enabled]',
						value: true,
						checked: true
					}) }}
				</td>
				<td class="thin action">
					<a class="delete icon" title="Delete"></a>
				</td>
			</tr>

			{% if sitemap.customUrls is defined %}
				{% for custom in sitemap.customUrls %}
					<tr data-id="{{ loop.index }}">
						<td class="seo--custom-url-td">
							<input
								type="hidden"
								name="customUrls[{{ loop.index }}]"
							/>
							<input
								type="hidden"
								name="customUrls[{{ loop.index }}][id]"
								value="{{ custom.id }}"
							/>
							<input
								class="seo--custom-url-input"
								placeholder="URL"
								type="text"
								name="customUrls[{{ loop.index }}][url]"
								value="{{ custom.url }}"
							/>
						</td>
						<td class="thin" style="text-align:left;">
							{{ forms.selectField({
								name: 'customUrls['~loop.index~'][frequency]',
								options: changeFrequencyOpts,
								value: custom.frequency|default('weekly')
							}) }}
						</td>
						<td class="thin">
							{{ forms.selectField({
								name: 'customUrls['~loop.index~'][priority]',
								options: priorityOpts,
								value: custom.priority|default('0.5')
							}) }}
						</td>
						<td class="thin" style="text-align:center">
							{{ forms.checkbox({
								name: 'customUrls['~loop.index~'][enabled]',
								value: true,
								checked: custom.enabled
							}) }}
						</td>
						<td class="thin action">
							<a class="delete icon" title="Delete"></a>
						</td>
					</tr>
				{% endfor %}
			{% endif %}
			</tbody>
		</table>

		<br>

		<div class="btngroup">
			<input type="button" class="btn submit" value="Add a URL" id="addCustomUrl" />
		</div>
		{# End: Custom #}
	{% endnamespace %}
{% endblock %}
