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

<style>

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

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

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

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

</style>

<form method="post" name="promotionedit" id="promotionedit" action="?m=promotionedit&id={$promotion.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="promotion[name]" value="{$promotion.name|escape}" size="40" form="promotionedit"
					{if !empty($promotion.assignmentcount) && !ConfigHelper::checkPrivilege('superuser')}
						readonly {tip text="Promotion is indirectly assigned to liabilities, so name change is disabled!" trigger="name" bold=1}
					{else}
						{tip text="Enter promotion name" trigger="name" bold=1}
					{/if} required>
			</td>
		</tr>
	</thead>
	<tbody>
		<tr>
			<td>
				{icon name="period"}
			</td>
			<td>
				<strong>{trans("Period")}</strong>
			</td>
			<td>
				{trans("from")}
				<input type="text" name="promotion[datefrom]" form="promotionedit"
					value="{if $promotion.datefrom}{$promotion.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="promotion[dateto]" form="promotionedit"
					value="{if $promotion.dateto}{$promotion.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="optional-info"}
			</td>
			<td>
				<strong>{trans("Description")}</strong>
			</td>
			<td>
				<textarea name="promotion[description]" cols="50" rows="5" form="promotionedit"
					{tip text="Enter additional information (optional)"}>{$promotion.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="promotionedit" {if $file.checked} checked{/if}>
							{trans("default checked")}
						</label>
						<input size="10" type="text" name="fileupload[attachments][{$fileidx}][label]"
							value="{$file.label}" form="promotionedit" 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="promotionedit">
						{trans("default checked")}
					</label>
					<input size="10" type="text" name="fileupload[attachments][%idx%][label]"
						value="" form="promotionedit" placeholder="{trans("text label")}">
				{/strip}{/capture}
				{fileupload id="attachments" fileupload=$fileupload form="promotionedit"
					item_custom_contents=$item_custom_contents
					new_item_custom_content=$new_item_custom_content}
			</td>
		</tr>
		{if !empty($promotion.attachments)}
			<tr>
				<td class="valign-top">
					{icon name="import"}
				</td>
				<td class="valign-top nobr">
					<strong>{trans("Attached files")}</strong>
				</td>
				<td>
					{foreach $promotion.attachments as $promoattach}
						<div class="promotion-attachment">
							<div class="promoattachrow{if $promoattach.type <= 0} promoattach{if $promotion.attachments[$promoattach.id].deleted} blend crossed{/if}{/if}">
								<a href="#">
									<i class="fa-fw lms-ui-icon-{if $promotion.attachments[$promoattach.id].deleted}cancel{else}delete{/if}"
										{if $promoattach.type == 1} style="visibility: hidden;"{/if}></i>
								</a>
								{$class = ""}
								{if $promotion.attachments[$promoattach.id].deleted}
									{$class = "blend"}
								{/if}
								{documentview
									id="{$promotion.id}-{$promoattach.id}"
									type=$promoattach.contenttype
									name=$promoattach.filename
									url="?m=promotionattachmentview&promotionid={$promotion.id}&attachmentid={$promoattach.id}"
									external=true
									class=$class
								}
								<input type="hidden" name="promotion[attachments][{$promoattach.id}][deleted]"
									value="{$promotion.attachments[$promoattach.id].deleted}" form="promotionedit">
							</div>
							<div class="promotion-attachment-options">
								<label>
									<input type="checkbox" name="promotion[attachments][{$promoattach.id}][checked]"
										value="1"{if $promoattach.checked} checked{/if} form="promotionedit">
									{trans("default checked")}
								</label>
								<input size="10" type="text" name="promotion[attachments][{$promoattach.id}][label]"
									value="{$promoattach.label}" form="promotionedit" 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="promotionedit"}
				{button icon="cancel" label="Cancel" href="?m=promotionlist"}
			</td>
		</tr>
	</tbody>
</table>

<script>

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

	$(function() {
		$('.promoattach').on('click', function() {
			var elem = $(this).closest('.promotion-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}
