{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;
	}

	 .schemaattachrow a:nth-child(2) {
		 margin-left: 0.5em;
	 }

	.schema-attachment {
		display: flex;
		flex-direction: column;
		width: fit-content;
	}

	.schema-attachment-options {
		display: flex;
		align-items: center;
		margin-top: 0.5em;
		justify-content: space-between;
	}

	.schema-attachment-options input[type="text"] {
		margin-left: 1em;
		flex-grow: 1;
	}

</style>

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

<form method="post" name="schemaedit" id="schemaedit" action="?m=promotionschemaedit&id={$schema.id}">
</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="schemaedit"
					{if !empty($schema.assignmentcount) && !ConfigHelper::checkPrivilege('superuser')}
						readonly {tip text="Schema is assigned to liabilities, so name change is disabled!" trigger="name" bold=1}
					{else}
						{tip text="Enter schema name" trigger="name" bold=1}
					{/if} 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}]" form="schemaedit"
							{if !empty($schema.assignmentcount) && !ConfigHelper::checkPrivilege('superuser')}
								data-options='{ "disabled": true }'
								{tip class="lms-ui-combobox" text="Schema is assigned to liabilities, so periods change is disabled!" bold=1}
							{else}
								{tip class="lms-ui-combobox" text="Select length of the period (in months)"}
							{/if}
						data-value="{$period}">
						{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 empty($schema.assignmentcount) || ConfigHelper::checkPrivilege('superuser')}
					{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"}
				{/if}
			</td>
		</tr>
		<tr>
			<td>
				{icon name="period"}
			</td>
			<td>
				<strong>{trans("Period")}</strong>
			</td>
			<td>
				{trans("from")}
				<input type="text" name="schema[datefrom]" form="schemaedit"
					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="schemaedit"
					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="schemaedit"
					{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="schemaedit" {if $file.checked} checked{/if}>
							{trans("default checked")}
						</label>
						<input size="10" type="text" name="fileupload[attachments][{$fileidx}][label]"
							   value="{$file.label}" form="schemaedit" 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="schemaedit">
						{trans("default checked")}
					</label>
					<input size="10" type="text" name="fileupload[attachments][%idx%][label]"
						   value="" form="schemaedit" placeholder="{trans("text label")}">
				{/strip}{/capture}
				{fileupload id="attachments" fileupload=$fileupload form="schemaedit"
					item_custom_contents=$item_custom_contents
					new_item_custom_content=$new_item_custom_content}
			</td>
		</tr>
		{if !empty($schema.attachments)}
			<tr>
				<td class="valign-top">
					{icon name="import"}
				</td>
				<td class="valign-top nobr">
					<strong>{trans("Attached files")}</strong>
				</td>
				<td>
					{foreach $schema.attachments as $schemaattach}
						<div class="schema-attachment">
							<div class="schemaattachrow{if $schemaattach.type <= 0} schemaattach{if $schema.attachments[$schemaattach.id].deleted} blend crossed{/if}{/if}">
								<a href="#">
									<i class="fa-fw lms-ui-icon-{if $schema.attachments[$schemaattach.id].deleted}cancel{else}delete{/if}"
										{if $schemaattach.type == 1} style="visibility: hidden;"{/if}></i>
								</a>
								{$class = ""}
								{if $schema.attachments[$schemaattach.id].deleted}
									{$class = "blend"}
								{/if}
								{documentview
									id="{$schema.id}-{$schemaattach.id}"
									type=$schemaattach.contenttype
									name=$schemaattach.filename
									url="?m=promotionattachmentview&schemaid={$schema.id}&attachmentid={$schemaattach.id}"
									external=true
									class=$class
								}
								<input type="hidden" name="schema[attachments][{$schemaattach.id}][deleted]"
									value="{$schema.attachments[$schemaattach.id].deleted}" form="schemaedit">
							</div>
							<div class="schema-attachment-options">
								<label>
									<input type="checkbox" name="schema[attachments][{$schemaattach.id}][checked]"
										value="1"{if $schemaattach.checked} checked{/if} form="schemaedit">
									{trans("default checked")}
								</label>
								<input size="10" type="text" name="schema[attachments][{$schemaattach.id}][label]"
									value="{$schemaattach.label}" form="schemaedit" placeholder="{trans("text label")}">
							</div>
						</div>
					{/foreach}
				</td>
			</tr>
		{/if}
		<tr>
			<td class="lms-ui-box-buttons" colspan="3">
				{button type="submit" icon="save" label="Submit" form="schemaedit"}
				{button icon="cancel" label="Cancel" href="?m=promotionschemainfo&id={$schema.id}"}
			</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="schemaedit"]').click(function() {
			$(".scombobox-value").each(function() {
				if ($(this).is("[name]")) {
					$(this).closest(".scombobox").find("select").removeAttr("name");
				}
			});
		});

		$('.schemaattach').on('click', function() {
			var elem = $(this).closest('.schema-attachment');
			elem.toggleClass('blend crossed')
					.find('.lms-ui-button').toggleClass('blend').end()
					.find('input:not([type="hidden"])').prop('disabled', function() {
				return !$(this).prop('disabled');
			});
			elem.find('i').toggleClass('lms-ui-icon-delete').toggleClass('lms-ui-icon-cancel');
			elem.find('input[type="hidden"]').val(elem.hasClass('blend') ? 1 : 0);
			return false;
		});
	});

</script>
{/block}
