<?
	$community = $this->community;
	// echo '<pre>';var_dump( $community );exit();
	$this->title = 'Community';
	$this->titleicon = 'group';
	$this->titleLink = '/community';

	$this->title2 = $community[ 'name' ];
?>

<!-- content -->
<div class="container-fluid padded">
	<div class="row-fluid">
		<div class="container-fluid">
			<div class="row-fluid">

				<div class="area-top clearfix">
					<div class="pull-left header">
						<h3 class="title">
							<i class="icon-dashboard"></i>
							<?php echo $community[ 'name' ]; ?>
							<?php if( $community[ 'community' ]->close_all_restaurants ) { ?>
								<span style="color:red"> - Community Closed</span>
							<?php } ?>
							<?php if( $community[ 'community' ]->close_3rd_party_delivery_restaurants ) { ?>
								<span style="color:red"> - 3rd Party Delivery Restaurants Closed</span>
							<?php } ?>
						</h3>
						<h5> Summary of the last 7 days </h5>
					</div>
					<ul class="inline pull-right sparkline-box">
						<li class="sparkline-row">
							<h4 class="blue"><span>Orders</span> <?php echo $community[ 'ordersLastWeek' ][ 'total' ]; ?></h4>
							<span class="mini-chart" color="#6e97aa"><?php echo $community[ 'ordersLastWeek' ][ 'week' ];  ?></span>
						</li>
						<li class="sparkline-row">
							<h4 class="green"><span>New users</span> <?php echo $community[ 'newUsersLastWeek' ][ 'total' ]; ?></h4>
							<span class="mini-chart" color="#8fae53"><?php echo $community[ 'newUsersLastWeek' ][ 'week' ];  ?></span>
						</li>
					</ul>
				</div>
			</div>
		</div>
	</div>


	<div class="box">
		<div class="box-header">
			<div class="title">
				<?php echo $community[ 'name' ]; ?> in numbers
			</div>
		</div>

		<div class="box-content padded">
			<div class="row-fluid">
				<div class="separate-sections" style="margin-top: 5px;">

					<div class="row-fluid">

						<?php foreach( $community[ 'numbers' ] as $numbers ) { ?>

						<div class="span3">
							<div class="dashboard-stats small">
								<ul class="inline">
									<li class="glyph"><i class="icon-<?php echo $numbers[ 'icon' ]; ?>"></i></li>
									<li class="count"><?php echo $numbers['values'][ 'community' ]; ?></li>
								</ul>
								<div class="progress progress-blue"><div class="bar tip" title="<?php echo $numbers['values'][ 'percent' ]; ?>% of total (<?php echo $numbers['values'][ 'all' ]; ?>)" data-percent="<?php echo $numbers['values'][ 'percent' ]; ?>"></div></div>
								<div class="stats-label">Total <?php echo $numbers[ 'title' ]; ?></div>
							</div>
						</div>
						<?php } ?>
				</div>
			</div>
		</div>
	</div>
</div>

<div class="box">
	<div class="box-header">
			<ul class="nav nav-tabs nav-tabs-left">
				<li class="active"><a href="#hours" data-toggle="tab"><i class="icon-time"></i> <span>
				Closing Time</span></a></li>
				<?php if( $this->restaurantsPermissions ) { ?>
					<li><a href="#restaurants" data-toggle="tab"><i class="icon-food"></i> <span>Restaurants</span></a></li>
				<?php } ?>
				<?php if( $this->driversPermissions ) {?>
					<li><a href="#drivers" data-toggle="tab"><i class="icon-road"></i> <span>Drivers</span></a></li>
				<?php } ?>
				<? if ( c::admin()->permission()->check( [ 'global','drivers-all', 'drivers-working-hours' ] ) ) { ?>
				<li><a href="#shifts" data-toggle="tab"><i class="icon-truck"></i> <span>Shifts</span></a></li>
				<li><a href="#charts" data-toggle="tab"><i class="icon-bar-chart"></i> <span>Delivery Report</span></a></li>
				<?php } ?>
				<li><a href="#drivers-sms" data-toggle="tab"><i class="icon-cog"></i> <span>SMS to Drivers</span></a></li>
				<li><a href="#more" data-toggle="tab"><i class="icon-cog"></i> <span>More actions</span></a></li>
			</ul>
	</div>

	<div class="box-content padded">
		<div class="tab-content">
			<div class="tab-pane active" id="hours">

<div class="box-content padded">
			<div class="row-fluid">
				<div class="separate-sections" style="margin-top: 5px;">
					<div class="row-fluid">
						<div class="span6">
							<strong>Close all restaurants</strong>
							<ul class="padded separate-sections">
								<li class="input">
									<label style="text-align:left;">
										<input type="checkbox" name="close_all_restaurants" id="close_all_restaurants" <?php if( $community[ 'community' ]->close_all_restaurants ){ echo 'checked="checked"'; } ?> > Close all restaurants
									</label>
								</li>
								<li class="input">
									<input style="width:100%" type="text" name="close_all_restaurants_note" id="close_all_restaurants_note" value="<?php echo $community[ 'community' ]->close_all_restaurants_note; ?>" placeholder="Notes displayed when restaurant is clicked">
								</li>
								<li>
									<button type="button" onclick="community.closeAll();" class="btn close_all_restaurants_save btn-blue">Save changes</button>
								</li>
								<li>
									<?php
									if( $community[ 'community' ]->close_all_restaurants && $community[ 'community' ]->close_all_restaurants_id_admin  ){
										$admin = Admin::o( $community[ 'community' ]->close_all_restaurants_id_admin );
										if( $admin->id_admin ){
											echo 'Closed by ' . $admin->name;
										}
										if( $community[ 'community' ]->close_all_restaurants ){
											$date = new DateTime( $community[ 'community' ]->close_all_restaurants_date, new DateTimeZone(c::config()->timezone));
											echo ' at ' . $date->format( 'M jS Y g:i:s A T' );
										}
									} ?>
								</li>
							</ul>
							<br>
							<br>
							<strong>If it is closed and it has drivers doesn't warn CS till (PST):</strong>
							<ul class="padded separate-sections">
								<?php
									$dont_warn_till = $community[ 'community' ]->dont_warn_till;
									if( $dont_warn_till ){
										$dont_warn_till = $community[ 'community' ]->dontWarnTill()->format( 'Y-m-d\TH:i' );
										$active = ( $community[ 'community' ]->dontWarnTill()->format( 'Y' ) > 0 );
									}
									?>
								<li class="input">
									<label style="text-align:left;">
										<input type="checkbox" onchange="community.dontWarnTillActive();" name="dont_warn_till_active" id="dont_warn_till_active" <?php if( $active ){ echo 'checked="checked"'; } ?> > Actived
									</label>
								</li>
								<li class="input">
									<input style="width:100%" <?php if( !$active ){ echo 'disabled="disabled"'; } ?> type="datetime-local" name="dont_warn_till" id="dont_warn_till" value="<?php echo $dont_warn_till; ?>">
								</li>
								<li>
									<button type="button" onclick="community.dontWarnTill();" class="btn dont_warn_till_save btn-blue">Save changes</button>
								</li>
							</ul>
						</div>
						<div class="span6">
							<strong>Close 3rd party delivery restaurants</strong>
							<ul class="padded separate-sections">
								<li class="input">
									<label style="text-align:left;">
										<input type="checkbox" name="close_3rd_party_delivery_restaurants" id="close_3rd_party_delivery_restaurants" <?php if( $community[ 'community' ]->close_3rd_party_delivery_restaurants ){ echo 'checked="checked"'; } ?> > Close 3rd party delivery restaurants
									</label>
								</li>
								<li class="input">
									<input style="width:100%" type="text" name="close_3rd_party_delivery_restaurants_note" id="close_3rd_party_delivery_restaurants_note" value="<?php echo $community[ 'community' ]->close_3rd_party_delivery_restaurants_note; ?>" placeholder="Notes displayed when restaurant is clicked">
								</li>
								<li>
									<button type="button" onclick="community.close3rdParty();" class="close_3rd_party_delivery_restaurants_save btn btn-blue">Save changes</button>
								</li>
								<li>
									<?php
									if( $community[ 'community' ]->close_3rd_party_delivery_restaurants && $community[ 'community' ]->close_3rd_party_delivery_restaurants_id_admin  ){
										$admin = Admin::o( $community[ 'community' ]->close_3rd_party_delivery_restaurants_id_admin );
										if( $admin->id_admin ){
											echo 'Closed by ' . $admin->name;
										}
										if( $community[ 'community' ]->close_3rd_party_delivery_restaurants_date ){
											$date = new DateTime( $community[ 'community' ]->close_3rd_party_delivery_restaurants_date, new DateTimeZone(c::config()->timezone));
											echo ' at ' . $date->format( 'M jS Y g:i:s A T' );
										}
									} ?>
								</li>
							</ul>


						<?php
							$is_auto_closed = $community[ 'community' ]->is_auto_closed;
							if( $is_auto_closed ){
								$current_message = $community[ 'community' ]->driver_restaurant_name;

						?>
													<br>
							<br>
						<strong>Auto closed message</strong>
							<ul class="padded separate-sections">
								<li class="input">
									<input style="width:100%" type="text" name="auto_closed_message" id="auto_closed_message" maxlenght="100" value="<?php echo $current_message; ?>" placeholder="Auto closed message">
								</li>
								<li>
									<button type="button" onclick="community.autoClosedMessage();" class="close_3rd_party_delivery_restaurants_save btn btn-blue">Save changes</button>
								</li>

							</ul>
							<?php
							}
							?>
						</div>
				</div>
			</div>
		</div>
	</div>

			</div>
			<?php if( $this->restaurantsPermissions ) { ?>
				<div class="tab-pane" id="restaurants"></div>
			<?php } ?>
			<?php if( $this->driversPermissions ) { ?>
				<div class="tab-pane" id="drivers"></div>
				<div class="tab-pane" id="drivers-sms"></div>
			<?php } ?>
			<div class="tab-pane" id="more">
				<?php if (c::admin()->permission()->check(['global','permissions-all', 'permission-users'])) { ?>
					<a href="/permissions/users/new"> <i class="icon-user"></i> Create a new driver</a>
					<br />
					<br />
				<?php } ?>

				<?php if (c::admin()->permission()->check(['global','drivers-assign', 'drivers-all'])) { ?>
				<a href="/drivers/assign/community/<?php echo $community[ 'community' ]->id_community;?>"> <i class="icon-magnet"></i> Assign drivers to <?php echo $community[ 'name' ]; ?></a>
				<br />
				<br />
				<?php } ?>
				<?php if (c::admin()->permission()->check(['global','drivers-working-hours', 'drivers-all', 'drivers-working-hours-view'])) { ?>
				<a href="/drivers/schedule"> <i class="icon-calendar"></i> Schedule</a>
				<?php } ?>



				<br>
				<br>
				<div>


</div>



			</div>
			<div class="tab-pane" id="shifts">
				<div class="row-fluid">
					<div id="shifts-content"></div>
				</div>
			</div>
			<div class="tab-pane" id="charts">
				<div class="row-fluid">
					<div id="charts-content">
						<button class="btn btn-default" onclick="community.deliveryChats();">Load charts</button>
					</div>
				</div>
			</div>
		</div>
	</div>
</div>

<div class="modal hide fade" id="modal-hour_override" style="display: none;">
	<div class="modal-header">
		<button class="close" data-dismiss="modal" type="button">×</button>

		<h6 id="modal-tablesLabel">Close times (holidays)</h6>
	</div>

	<div class="modal-body modal-hour_override-body"></div>

	<div class="modal-footer">
		<button class="btn btn-default" data-dismiss="modal">Close</button>
	</div>
</div>
<script type="text/javascript">

var community = { id_community : '<?php echo $community[ 'community' ]->id_community; ?>' };

var _id_restaurant = false;

$( document ).ready( function(){

	/* mini charts */
	$( '.mini-chart' ).each( function(){
		var color = $( this ).attr( 'color' );
		$( this ).sparkline('html', { type: 'bar', barColor: color, 'height' : '40px', barWidth : '5px'} );
	} );

	community.restaurants();
	community.drivers();
	community.driversSMS();
	<?php
	if ( c::admin()->permission()->check( [ 'global','drivers-all', 'drivers-working-hours' ] ) ) {

		$now = new DateTime( 'now', new DateTimeZone( c::config()->timezone  ) );
		if( $now->format( 'l' ) == 'Friday' ){
			$friday = $now;
		} else {
			$friday = new DateTime( 'last friday', new DateTimeZone( c::config()->timezone  ) );
		}
		$start_date = $friday->format( 'Y/m/d' );

	?>
	community.shifts( '/drivers/shift/community/<?php echo $community[ 'community' ]->id_community; ?>/<?php echo $start_date; ?>?ajax=1' );
	<?php } ?>

} );

community.deliveryChats = function(){
	var url = '/charts/community/delivered-orders?id_community=' + community.id_community;
	$.ajax( {
		url: url,
		complete: function(content) {
			$('#charts-content').html( content.responseText );
		}
	} );
};

community.shifts = function( url ){
	$.ajax( {
		url: url,
		complete: function(content) {
			$('#shifts-content').html( content.responseText );
			$( '.shift-previous, .shift-next' ).click( function(e) {
				e.preventDefault();
				var url = $( this ).attr( 'href' ) + '?ajax=1';
				community.shifts( url );
			} );
		}
	} );
};

community.drivers = function(){
	$.ajax( {
		url: '/community/' + community.id_community + '/drivers',
		complete: function(content) {
			$('#drivers').html( content.responseText );
		}
	} );
};

community.driversSMS = function(){
	$.ajax( {
		url: '/community/' + community.id_community + '/drivers-sms',
		complete: function(content) {
			$('#drivers-sms').html( content.responseText );
		}
	} );
};

community.restaurants = function(){
	$.ajax( {
		url: '/community/' + community.id_community + '/restaurants',
		complete: function(content) {
			$('#restaurants').html( content.responseText );
		}
	} );
};

community.quickClose = function( id_restaurant, status ){
	var tagline = $( '#force_close_tagline-' + id_restaurant );
	var status = ( status == 'open' ) ? 'open' : 'close';
	$.ajax( {
		type: 'POST',
		data: { 'force_close_tagline' : tagline.val() },
		url: '/api/restauranthours/' + id_restaurant + '/' + status,
		complete: function( request ) {
			if( request.success ){
				community.restaurants();
			} else {
				alert( 'Oops! Please try again!' );
			}
		}
	} );
};
community.dont_warn_till_old_val = null;
community.dontWarnTillActive = function(){
	var input = $('#dont_warn_till_active');
	if( !input.is(':checked') ){
		community.dont_warn_till_old_val = $('#dont_warn_till').val();
		$('#dont_warn_till').val( '' );
		$('#dont_warn_till').prop('disabled', true);
	} else {
		$('#dont_warn_till').val( community.dont_warn_till_old_val );
		$('#dont_warn_till').prop('disabled', false);
	}
};

community.dontWarnTill = function(){
	var dont_warn_till = $('#dont_warn_till');
	var save = true;
	var data = {
			'id_community': '<?php echo $community[ 'community' ]->id_community; ?>',
			'dont_warn_till': dont_warn_till.val()
		};
	$.ajax( {
		type: 'POST',
		url: '/api/community/dont-warn-till',
		data: data,
		complete: function( request ) {
			if( request.success ){
				alert( 'Saved!' )
			} else {
				alert( 'Oops! Please try again!' );
			}
		}
	} );
};

community.closeAll = function(){
	var input = $('#close_all_restaurants');
	var notes = $('#close_all_restaurants_note');
	var save = true;
	if( input.is(':checked') ){
		if( notes.val() == '' ){
			alert( 'Plese type the reason (notes)!' );
			notes.focus();
			return;
		}
	}
	var data = {
			'id_community': '<?php echo $community[ 'community' ]->id_community; ?>',
			'close_all_restaurants': ( input.is(':checked') ? 1 : 0 ),
			'close_all_restaurants_note': notes.val()
		};
	$.ajax( {
		type: 'POST',
		url: '/api/community/close-all',
		data: data,
		complete: function( request ) {
			console.log('request',request);
			if( request.success ){
				alert( 'Saved!' )
			} else {
				alert( 'Oops! Please try again!' );
			}
		}
	} );
};


community.autoClosedMessage = function(){
	var input = $('#auto_closed_message');
	var save = true;

	if( input.val() == '' ){
		alert( 'Plese type message!' );
		input.focus();
		return;
	}

	var data = {
			'id_community': '<?php echo $community[ 'community' ]->id_community; ?>',
			'auto_closed_message': input.val()
		};
	$.ajax( {
		type: 'POST',
		url: '/api/community/auto-closed-message',
		data: data,
		complete: function( request ) {
			console.log('request',request);
			if( request.success ){
				alert( 'Saved!' )
			} else {
				alert( 'Oops! Please try again!' );
			}
		}
	} );
};

community.close3rdParty = function(){
	var input = $('#close_3rd_party_delivery_restaurants');
	var notes = $('#close_3rd_party_delivery_restaurants_note');
	var save = true;
	if( input.is(':checked') ){
		if( notes.val() == '' ){
			alert( 'Plese type the reason (notes)!' );
			notes.focus();
			return;
		}
	}
	var data = {
			'id_community': '<?php echo $community[ 'community' ]->id_community; ?>',
			'close_3rd_party_delivery_restaurants': ( input.is(':checked') ? 1 : 0 ),
			'close_3rd_party_delivery_restaurants_note': notes.val()
		};
	$.ajax( {
		type: 'POST',
		url: '/api/community/close-3rd',
		data: data,
		complete: function( request ) {
			console.log('request',request);
			if( request.success ){
				alert( 'Saved!' )
			} else {
				alert( 'Oops! Please try again!' );
			}
		}
	} );
};

community.holidays = function( id_restaurant ){
	_id_restaurant = id_restaurant;
	hour_override.setCallback( function(){
		community.restaurants();
	} );
	hour_override.load();
};

</script>
<style type="text/css">
	.restaurant-grid{
		width: 100%;
	}
	.restaurant-grid td, .restaurant-grid th{
		padding:10px 0 10px 10px;
	}
	.restaurant-grid tr:nth-of-type(odd){
		background:#F5F5F5;
	}
	.restaurant-grid th{
		background: #F5F5F5;
	}
	.restaurant-grid .inactive{
		color:#CCC;
		text-decoration:line-through;
	}
	.drivers-grid{
		width: 100%;
	}
	.drivers-grid td, .drivers-grid th{
		vertical-align:text-top;
		padding:10px 0 10px 10px;
	}
	.drivers-grid tr:nth-of-type(odd){
		background:#F5F5F5;
	}
	.drivers-grid th{
		background: #F5F5F5;
	}
	.drivers-grid .deleted{
		color:#CCC;
		text-decoration:line-through;
	}
	.drivers-grid .icon{
		margin-right: 5px;
	}
</style>