<?
	$this->title = 'Gift cards - Groups';
	$this->titleicon = 'gift';
	$this->titleLink = '/giftcards/groups';
	
	$this->title2 = 'Create new group';
	$this->title2icon = 'group';

	$group = $this->group;

	if( $group->id_promo_group ){
		$date_mkt = $group->date_mkt();
		if( $date_mkt == '00/00/0000' ){
			$date_mkt = date( 'm/d/Y' );
		}
	} else {
		$date_mkt = date( 'm/d/Y' );
	}

?>
<div class="container-fluid padded">
	<div class="row-fluid">
		<div class="span6">
			<div class="box">
				<div class="box-header">
					<span class="title">Group</span>
				</div>
				<div class="box-content ">
					<ul class="box-list">
						<li>
							<span>Name</span>
							<span class="pull-right">
								<input type="text" name="name" maxlength="250" id="name" value="<?php echo $group->name; ?>" />
							</span>
						</li>
						<?php 
						if( $group->id_promo_group ){?>
						<li>
							<span>Number of giftcards</span>
							<span class="pull-right">
								<?php echo $group->giftcards_total(); ?>
							</span>
						</li>
						<li>
							<span>Number of giftcards redeemed</span>
							<span class="pull-right">
								<?php echo $group->giftcards_redeemed_total(); ?>
							</span>
						</li>
						<?php } ?>
						<li>
							<span>Range of giftcards ids</span>
							<span class="pull-right">
								<input type="text" name="range" maxlength="250" id="range" value="<?php echo $group->range; ?>" />
								<div class="note large">
									Sample: 1 - 3, 10 - 12, 15
								</div>
							</span>
						</li>
					</ul>
				</div>
			</div>

			<div class="box">
				<div class="box-header">
					<span class="title">Marketing event</span>
				</div>
				<div class="box-content ">
					<ul class="box-list">
						<li>
							<span>Date</span>
							<span class="pull-right">
								<input type="text" name="date_mkt" id="date_mkt" class="date-picker"  maxlength="250" id="date_mkt" value="<?php echo $date_mkt; ?>" />
							</span>
						</li>
						<li>
							<span>Community</span>
							<span class="pull-right">
								<input type="text" name="community" maxlength="250" id="community" value="<?php echo $group->community; ?>" />
							</span>
						</li>
						<li>
							<span>Promotion</span>
							<span class="pull-right">
								<input type="text" name="promotion_type" maxlength="250" id="promotion_type" value="<?php echo $group->promotion_type; ?>" />
							</span>
						</li>
						<li>
							<span>Description</span>
							<span class="pull-right">
								<input type="text" name="description" id="description" value="<?php echo $group->description; ?>" />
							</span>
						</li>
						<li>
							<span>Man/Hour</span>
							<span class="pull-right">
								<input type="number" name="man_hours" id="man_hours" value="<?php echo $group->man_hours; ?>" />
							</span>
						</li>
						<li class="input">
							<button type="submit" class="btn btn-blue admin-giftcard-group-save"><i class="icon-save"></i> Save </button> 
						</li>
					</ul>
				</div>
			</div>

	</div>
</div>
<script>

$(function() {
	$( '.date-picker' ).datepicker( { dateFormat: 'mm/dd/yyyy' } );
	$(document).on('click', '.admin-giftcard-group-save', function() {
	App.giftcards.print = true;
	sendForm();
	} );
});

var processing = false;

function sendForm(){

	if( processing ){
		return;
	}

	var name = $.trim( $( '#name' ).val() );
	var range = $.trim( $( '#range' ).val() );
	var date_mkt = $.trim( $( '#date_mkt' ).val() );
	var community = $.trim( $( '#community' ).val() );
	var promotion_type = $.trim( $( '#promotion_type' ).val() );
	var description = $.trim( $( '#description' ).val() );
	var man_hours = $.trim( $( '#man_hours' ).val() );
	if( name == '' ){
		alert( 'Please type a name!' );
		$( '#name' ).focus();
		return;
	}

	var data = { 	'name' : name, 
								'range' : range,
								'date_mkt' : date_mkt,
								'community' : community,
								'promotion_type' : promotion_type,
								'description' : description,
								'man_hours' : man_hours,
							};
	
	processing = true;
	
	$( '.admin-giftcard-group-save' ).html( '<i class="icon-spinner icon-spin"></i> Please wait' );

	var url = App.service + 'giftcard/group/<?php echo $group->id_promo_group; ?>';
	$.ajax({
		type: "POST",
		dataType: 'json',
		data: data,
		url: url,
		success: function( json ) {
			processing = false;
			if( json.error ){
				alert( 'Error at saving the group!' );
			} else {
				alert( 'Group saved!' );
				location.href = '/giftcards/groups/';
			}
		},
		error: function( ){
			processing = false;
			alert( 'Error at saving the group!' );
		}
	});
}
</script>