<?php
if (isset($tpl['status']))
{
	$status = __('status', true);
	switch ($tpl['status'])
	{
		case 2:
			pjUtil::printNotice(NULL, $status[2]);
			break;
	}
} else {
	$week_start = isset($tpl['option_arr']['o_week_start']) && in_array((int) $tpl['option_arr']['o_week_start'], range(0,6)) ? (int) $tpl['option_arr']['o_week_start'] : 0;
	$jqDateFormat = pjUtil::jqDateFormat($tpl['option_arr']['o_date_format']);
	$week_start = isset($tpl['option_arr']['o_week_start']) && in_array((int) $tpl['option_arr']['o_week_start'], range(0,6)) ? (int) $tpl['option_arr']['o_week_start'] : 0;
	$jqDateFormat = pjUtil::jqDateFormat($tpl['option_arr']['o_date_format']);
	$months = __('months', true);
	$short_months = __('short_months', true);
	ksort($months);
	ksort($short_months);
	$days = __('days', true);
	$short_days = __('short_days', true);
	?>
	<div class="ui-tabs ui-widget ui-widget-content ui-corner-all b10">
		<ul class="ui-tabs-nav ui-helper-reset ui-helper-clearfix ui-widget-header ui-corner-all">
			<li class="ui-state-default ui-corner-top"><a href="<?php echo $_SERVER['PHP_SELF']; ?>?controller=pjAdminVouchers&amp;action=pjActionIndex"><?php __('tabCoupons'); ?></a></li>
			<li class="ui-state-default ui-corner-top"><a href="<?php echo $_SERVER['PHP_SELF']; ?>?controller=pjAdminDiscounts&amp;action=pjActionIndex"><?php __('tabDiscounts'); ?></a></li>
		</ul>
	</div>
	<?php
	pjUtil::printNotice(__('infoUpdateDiscountTitle', true, false), __('infoUpdateDiscountDesc', true, false)); 
	?>
	
	<?php if ((int) $tpl['option_arr']['o_multi_lang'] === 1 && count($tpl['lp_arr']) > 1) : ?>
	<div class="multilang"></div>
	<?php endif; ?>
	
	<div class="clear_both">
		<form action="<?php echo $_SERVER['PHP_SELF']; ?>?controller=pjAdminDiscounts&amp;action=pjActionUpdate" method="post" id="frmUpdateDiscount" class="form pj-form" autocomplete="off">
			<input type="hidden" name="discount_update" value="1" />
			<input type="hidden" name="id" value="<?php echo $tpl['arr']['id'];?>" />
			<?php
			foreach ($tpl['lp_arr'] as $v)
			{
			?>
				<p class="pj-multilang-wrap" data-index="<?php echo $v['id']; ?>" style="display: <?php echo (int) $v['is_default'] === 0 ? 'none' : NULL; ?>">
					<label class="title"><?php __('lblDiscountName'); ?></label>
					<span class="inline_block">
						<input type="text" id="i18n_name_<?php echo $v['id'];?>" name="i18n[<?php echo $v['id']; ?>][name]" class="pj-form-field w300<?php echo (int) $v['is_default'] === 0 ? NULL : ' required'; ?>" value="<?php echo htmlspecialchars(stripslashes(@$tpl['arr']['i18n'][$v['id']]['name'])); ?>" lang="<?php echo $v['id']; ?>" />
						<?php if ((int) $tpl['option_arr']['o_multi_lang'] === 1 && count($tpl['lp_arr']) > 1) : ?>
						<span class="pj-multilang-input"><img src="<?php echo PJ_INSTALL_URL . PJ_FRAMEWORK_LIBS_PATH . 'pj/img/flags/' . $v['file']; ?>" alt="" /></span>
						<?php endif; ?>
					</span>
				</p>
				<?php
			}
			?>
			<?php
			$date_from = date($tpl['option_arr']['o_date_format'], strtotime($tpl['arr']['date_from']));
			$date_to = date($tpl['option_arr']['o_date_format'], strtotime($tpl['arr']['date_to']));
			?>
			<p>
				<label class="title"><?php __('lblDateFrom'); ?></label>
				<span class="inline_block">
					<span class="pj-form-field-custom pj-form-field-custom-after float_left r5">
						<input type="text" name="date_from" id="date_from" class="pj-form-field pointer w80 datepick required" value="<?php echo $date_from;?>" readonly="readonly" rel="<?php echo $week_start; ?>" rev="<?php echo $jqDateFormat; ?>" data-months="<?php echo join(',', $months);?>" data-shortmonths="<?php echo join(',', $short_months);?>" data-day="<?php echo join(',', $days);?>" data-daymin="<?php echo join(',', $short_days);?>"/>
						<span class="pj-form-field-after"><abbr class="pj-form-field-icon-date"></abbr></span>
					</span>
				</span>
			</p>
			<p>
				<label class="title"><?php __('lblDateTo'); ?></label>
				<span class="inline_block">
					<span class="pj-form-field-custom pj-form-field-custom-after float_left r5">
						<input type="text" name="date_to" id="date_to" class="pj-form-field pointer w80 datepick required" value="<?php echo $date_to;?>" readonly="readonly" rel="<?php echo $week_start; ?>" rev="<?php echo $jqDateFormat; ?>" data-months="<?php echo join(',', $months);?>" data-shortmonths="<?php echo join(',', $short_months);?>" data-day="<?php echo join(',', $days);?>" data-daymin="<?php echo join(',', $short_days);?>"/>
						<span class="pj-form-field-after"><abbr class="pj-form-field-icon-date"></abbr></span>
					</span>
				</span>
			</p>
			<?php
			$apply_conditions = pjUtil::sortArrayByArray(__('apply_conditions'), array('period','made','both'));
			?>
			<p>
				<label class="title">&nbsp;</label>
				<span class="block overflow float_left">
					<?php
					foreach($apply_conditions as $k => $v)
					{
						?>
						<span class="block overflow b5"><input type="radio" name="condition" id="condition_<?php echo $k;?>"<?php echo $k=='period' ? ' checked="checked"' : null;?><?php echo $k==$tpl['arr']['condition'] ? ' checked="checked"' : null;?> value="<?php echo $k;?>" class="block float_left r5"/><label for="condition_<?php echo $k;?>"><?php echo $v;?></label></span>
						<?php
					} 
					?>
				</span>
			</p>
			<p>
				<label class="title"><?php __('lblDiscount'); ?></label>
				<span class="inline_block">
					<span class="block float_left r10">
						<span class="pj-form-field-custom pj-form-field-custom-before">
							<span class="pj-form-field-before"><abbr id="icon_type" class="pj-form-field-icon-text"><?php echo $tpl['arr']['type'] == 'amount' ? pjUtil::formatCurrencySign(NULL, $tpl['option_arr']['o_currency'], "") : '%'; ?></abbr></span>
							<input type="text" id="discount" name="discount" class="pj-form-field w80 required<?php echo $tpl['arr']['type'] == 'amount' ? (isset($tpl['option_arr']['o_currency_format']) ? ' pjNumber' : ' number') : ' number';?>" value="<?php echo $tpl['arr']['type'] == 'amount' ? pjUtil::converToCurrencyFormat($tpl['arr']['discount'], $tpl['option_arr']) : $tpl['arr']['discount'];?>" data-currency_format="<?php echo isset($tpl['option_arr']['o_currency_format']) ? $tpl['option_arr']['o_currency_format'] : 0;?>" data-msg-number="<?php __('pj_number_validation');?>" data-msg-pjNumber="<?php __('lblValidNumberMessage');?>"/>
						</span>
					</span>
					
					<select name="type" id="type" class="pj-form-field w150 required">
						<?php
						foreach (__('voucher_types', true, false) as $k => $v)
						{
							?><option value="<?php echo $k; ?>"<?php echo $k == $tpl['arr']['type'] ? ' selected="selected"' : NULL;?> data-sign="<?php echo $k == 'amount' ? pjUtil::formatCurrencySign(NULL, $tpl['option_arr']['o_currency'], "") : '%'; ?>"><?php echo $v; ?></option><?php
						}
						?>
					</select>
				</span>
			</p>
			
			<p>
				<label class="title"><?php __('lblCalendars'); ?></label>
				<span class="inline_block">
					<?php
					if(!empty($tpl['calendar_arr']))
					{ 
						?>
						<select name="calendar_id[]" id="calendar_id" multiple="multiple" size="5" class="pj-form-field required w300">
							<?php
							foreach ($tpl['calendar_arr'] as $v)
							{
								?><option value="<?php echo $v['id']; ?>"<?php echo in_array($v['id'], $tpl['calendar_id_arr']) ? ' selected="selected"' : NULL;?>><?php echo stripslashes($v['name']); ?></option><?php
							}
							?>
						</select>
						<?php
					}
					?>
				</span>
			</p>
			<?php
			$discount_options = pjUtil::sortArrayByArray(__('discount_options'), array('early','persons','family', 'duration'));
			?>
			<p>
				<label class="title"><?php __('lblDiscountOptions'); ?></label>
				<span class="inline_block">
					<select name="options" id="options" class="pj-form-field required w300">
						<option value="">-- <?php __('lblChoose'); ?>--</option>
						<?php
						foreach ($discount_options as $k => $v)
						{
							?><option value="<?php echo $k; ?>"<?php echo $k == $tpl['arr']['options'] ? ' selected="selected"' : NULL;?>><?php echo stripslashes($v); ?></option><?php
						}
						?>
					</select>
				</span>
			</p>
			<p class="optionsBox earlyBox">
				<label class="title"><?php echo $discount_options['early']; ?></label>
				<span class="inline_block">
					<input type="text" id="early_days" name="early_days" value="<?php echo $tpl['arr']['early_days'];?>" class="pj-form-field abOptions w80"/>
				</span>
			</p>
			<p class="optionsBox personsBox">
				<label class="title"><?php echo $discount_options['persons']; ?></label>
				<span class="inline_block">
					<span class="block float_left r10">
						<input type="text" id="min_persons" name="min_persons" value="<?php echo $tpl['arr']['min_persons'];?>" class="pj-form-field abOptions w80 block float_left r5"/><label class="block float_left t5"><?php __('lblMinimum');?></label>
					</span>
					<span class="block float_left r10">
						<input type="text" id="max_persons" name="max_persons" value="<?php echo $tpl['arr']['max_persons'];?>" class="pj-form-field abOptions w80 block float_left r5"/><label class="block float_left t5"><?php __('lblMaximum');?></label>
					</span>
				</span>
			</p>
			<p class="optionsBox durationBox">
				<label class="title"><?php echo $discount_options['duration']; ?></label>
				<span class="inline_block">
					<span class="block float_left r10">
						<input type="text" id="min_duration" name="min_duration" value="<?php echo $tpl['arr']['min_duration'];?>" class="pj-form-field abOptions w80 block float_left r5"/><label class="block float_left t5"><?php __('lblMinimum');?></label>
					</span>
					<span class="block float_left r10">
						<input type="text" id="max_duration" name="max_duration" value="<?php echo $tpl['arr']['max_duration'];?>" class="pj-form-field abOptions w80 block float_left r5"/><label class="block float_left t5"><?php __('lblMaximum');?></label>
					</span>
				</span>
			</p>
			<p>
				<label class="title">&nbsp;</label>
				<input type="submit" value="<?php __('btnSave', false, true); ?>" class="pj-button" />
				<input type="button" value="<?php __('btnCancel'); ?>" class="pj-button" onclick="window.location.href='<?php echo PJ_INSTALL_URL; ?>index.php?controller=pjAdminDiscounts&action=pjActionIndex';" />
			</p>
		</form>
	</div>
	
	<script type="text/javascript">
	var pjLocale = pjLocale || {};
	var myLabel = myLabel || {};
	myLabel.choose = "-- <?php __('lblChoose'); ?> --";
	var locale_array = new Array(); 
	pjLocale.langs = <?php echo $tpl['locale_str']; ?>;
	pjLocale.flagPath = "<?php echo PJ_FRAMEWORK_LIBS_PATH; ?>pj/img/flags/";
	
	myLabel.field_required = "<?php __('pj_field_required'); ?>";
	<?php
	foreach ($tpl['lp_arr'] as $v)
	{
		?>locale_array.push(<?php echo $v['id'];?>);<?php
	} 
	?>
	myLabel.locale_array = locale_array;
	(function ($) {
		$(function() {
			$(".multilang").multilang({
				langs: pjLocale.langs,
				flagPath: pjLocale.flagPath,
				select: function (event, ui) {
					
				}
			});
		});
	})(jQuery_1_8_2);
	</script>
	<?php
}
?>