{extends file="layout.html"}
{block name=title}LMS: {$layout.pagetitle|striphtml}{/block}
{block name=module_content}
<!-- $Id$ -->

<style>

	#periodcont {
		display: flex;
	}

	#periodcont .scombobox {
		width: 12em;
		margin-right: 0.5em;
	}

</style>

<h1>{$layout.pagetitle}</h1>
<h3>{trans("Promotion:")} {$schema.promotionname}</h3>

<form method="post" name="schemaadd" id="schemaadd" action="?m=promotionschemaadd&id={$schema.promotionid}">
</form>

<table class="lmsbox">
	<colgroup>
		<col style="width: 1%;">
		<col style="width: 1%;">
		<col style="width: 98%;">
	</colgroup>
	<thead>
		<tr>
			<td>
				{icon name="money"}
			</td>
			<td>
				<strong>{trans("Name")}</strong>
			</td>
			<td>
				<input type="text" name="schema[name]" value="{$schema.name|escape}" size="40" form="schemaadd"
					{tip text="Enter schema name" trigger="name" bold=1} required>
			</td>
		</tr>
	</thead>
	<tbody>
		<tr>
			<td>
				{icon name="calendar"}
			</td>
			<td>
				<strong>{trans("Periods")}</strong>
			</td>
			<td id="periodcont">
				{foreach $schema.periods as $key => $period}
					<select name="schema[periods][{$key}]" {tip class="lms-ui-combobox" text="Select length of the period (in months)"}
						data-value="{$period}" form="schemaadd">
						{if !$key && count($schema.periods) == 1}
							<option value="0"{if !$period} selected{/if}>— {trans("unlimited")} —</option>
						{/if}
						{foreach $schema.selection as $item}
							<option value="{$item}"{if $period == $item} selected{/if}>{$item}</option>
						{/foreach}
					</select>
				{/foreach}
				{if count($schema.periods) < 2}
					{$visible = false}
				{else}
					{$visible = true}
				{/if}
				{button type="link" icon="delete" tip="Delete" id="perioddel" visible=$visible}
				{button type="link" icon="add" tip="Add" id="periodadd"}
			</td>
		</tr>
		<tr>
			<td>
				{icon name="period"}
			</td>
			<td>
				<strong>{trans("Period")}</strong>
			</td>
			<td>
				{trans("from")}
				<input type="text" name="schema[datefrom]" form="schemaadd"
					value="{if $schema.datefrom}{$schema.datefrom}{/if}"
					placeholder="{trans("yyyy/mm/dd")}" size="10"
					{tip class="lms-ui-date lms-ui-date-unix" text="Enter tariff start date in YYYY/MM/DD format. If you don't want to define 'From' date leave this field empty" trigger="datefrom"}>
				{trans("to")}
				<input type="text" name="schema[dateto]" form="schemaadd"
					value="{if $schema.dateto}{$schema.dateto}{/if}"
					placeholder="{trans("yyyy/mm/dd")}" size="10"
					{tip class="lms-ui-date lms-ui-date-unix" text="Enter tariff end date in YYYY/MM/DD format. Leave this field empty if you don't want to set expiration date" trigger="dateto"}>
			</td>
		</tr>
		<tr>
			<td>
				{icon name="info"}
			</td>
			<td>
				<strong>{trans("Description")}</strong>
			</td>
			<td>
				<textarea name="schema[description]" cols="50" rows="5" form="schemaadd"
					{tip text="Enter additional information (optional)"}>{$schema.description|escape}</textarea>
			</td>
		</tr>
		<tr>
			<td>
				{icon name="import"}
			</td>
			<td class="nobr">
				<strong>{trans("New files")}</strong>
			</td>
			<td>
				{$item_custom_contents = array()}
				{foreach $fileupload.attachments as $fileidx => $file}
					{capture assign="item_custom_content"}{strip}
						<label>
							<input type="checkbox" name="fileupload[attachments][{$fileidx}][checked]"
								value="1" form="schemaadd" {if $file.checked} checked{/if}>
							{trans("default checked")}
						</label>
						<input size="10" type="text" name="fileupload[attachments][{$fileidx}][label]"
							value="{$file.label}" form="schemaadd" placeholder="{trans("text label")}">
					{/strip}{/capture}
					{$item_custom_contents[] = $item_custom_content}
				{/foreach}
				{capture assign="new_item_custom_content"}{strip}
					<label>
						<input type="checkbox" name="fileupload[attachments][%idx%][checked]"
							value="1" form="schemaadd">
						{trans("default checked")}
					</label>
					<input size="10" type="text" name="fileupload[attachments][%idx%][label]"
						value="" form="schemaadd" placeholder="{trans("text label")}">
				{/strip}{/capture}
				{fileupload id="attachments" fileupload=$fileupload form="schemaadd"
					item_custom_contents=$item_custom_contents
					new_item_custom_content=$new_item_custom_content}
			</td>
		</tr>
		<tr>
			<td class="lms-ui-box-buttons" colspan="3">
				<div class="lms-ui-box-button-container">
					{button type="submit" icon="save" label="Submit" form="schemaadd"}
					{button icon="cancel" label="Cancel" href="?m=promotioninfo&amp;id={$schema.promotionid}"}
					<label>
						<input type="checkbox" name="schema[reuse]" form="schemaadd"
							value="ON"{if $schema.reuse} checked{/if}>
						{trans("Display this form again, when this schema is saved")}
					</label>
				</div>
			</td>
		</tr>
	</tbody>
</table>

<script>

	$('[name="schema[name]"]').focus();

	$(function() {
		$("#periodadd").click(function() {
			var comboboxes = $('#periodcont').children('.scombobox');

			if (comboboxes.length === 1) {
				var firstSelect = $(comboboxes[0]).find('select');
				firstSelect.find('option:first-child').remove();
				updateComboBoxes(firstSelect);
			}

			comboboxes = $('#periodcont').children('.scombobox');
			var select = $(comboboxes[comboboxes.length - 1]).find('select').clone().attr('name', 'schema[periods][' + comboboxes.length + ']');
			var del = $('#perioddel');
			select.insertBefore(del);

			init_comboboxes(select)

			del.show();
		});

		$("#perioddel").click(function() {
			var comboboxes = $('#periodcont .scombobox');
			if (comboboxes.length <= 2) {
				var select = $(comboboxes[0]).find('select');
				select.prepend('<option value="0">— ' + $t("unlimited") + ' —</option>');
				updateComboBoxes(select);
			}

			$('#perioddel').prev().remove();
			$('#perioddel').toggle(comboboxes.length > 2);
		});

		$('form[name="schemaadd"]').click(function() {
			$(".scombobox-value").each(function() {
				if ($(this).is("[name]")) {
					$(this).closest(".scombobox").find("select").removeAttr("name");
				}
			});
		});
	});

</script>
{/block}
