<?
	die('#5430 deprecated');

	$this->title = 'Drivers';
	$this->titleicon = 'road';
	$this->titleLink = '/drivers';
	$this->title2 = 'Working hours';
	$this->title2icon = 'time';

	$hasEditPermission = $this->hasEditPermission;
?>
<!-- content -->
<div class="container-fluid padded">
	<div class="row-fluid">
		<div class="box">
			<div class="box-header">
				<div class="row-fluid">
					<div class="span2 padded">
						<a href="/drivers/hours/<?php echo $this->weekPrev; ?>"><i class="icon icon-angle-left"></i> previous</a>
					</div>
						<div class="span8 padded" style="text-align:center;">
							<strong>Week <?php echo $this->week; ?> / <?php echo $this->year; ?></strong>
						</div>
					<div class="span2 padded" style="text-align:right;">
						<a href="/drivers/hours/<?php echo $this->weekNext; ?>">next <i class="icon icon-angle-right"></i></a>
					</div>
				</div>
			</div>
				<div class="box-content">
					<table width="100%">
						<tr>
							<?php foreach( $this->days as $day ) {
								$dark = ( $day->format( 'N' ) == 7 || $day->format( 'N' ) % 2 == 0 ) ? '#F5F5F5' : '';
							?>
								<th width="14%" style="background:<?php echo $dark; ?>">
									<div class="row-fluid padded">
										<div class="span12">
											<strong style="font-size:20px;color:#666;">
												<?php echo $day->format( 'D' ); ?>
												<div style="font-size:12px;font-weight:normal;color:#CCC;"><?php echo $day->format( 'M' ); ?> <?php echo $day->format( 'd' ); ?></div>
											</strong>
										</div>
									</div>
								</th>
							<?php } ?>
						</tr>
						<tr>
							<?php foreach( $this->days as $day ) {
								$dark = ( $day->format( 'N' ) == 7 || $day->format( 'N' ) % 2 == 0 ) ? '#F5F5F5' : '';
							?>
								<td width="14%" style="background:<?php echo $dark; ?>" valign="top">
									<div class="row-fluid padded">
									<?php
									$hours = Crunchbutton_Admin_Hour::segmentsByDate( $day->format('Y-m-d'), '<br/>' );
									if( count( $hours > 0 ) ){
										foreach ( $hours as $hour ) {
											$color = Util::stringToColorCode( $hour[ 'login' ] );
											$text_color = ( Util::isDarkColor( $color ) ? 'FFF' : '000' );

											$admin = Admin::o( $hour[ 'id_admin' ] );
											$now = new DateTime( 'now', new DateTimeZone( $admin->timezone ) );

											$communities = '';
											if ( $admin->id_admin ) {
												$adminCommunities = [];
												$groups = $admin->groups();
												foreach ( $groups as $group ) {
													$pos = strrpos( $group->name, Crunchbutton_Group::DRIVER_GROUPS_PREFIX );
													if( $pos !== false ){
														$adminCommunities[ $group->name ] = Group::getRestaurantCommunityName( $group->name );
													}
												}
												if( count( $adminCommunities ) > 0 && $adminCommunities != '' ){
													$join = '';
													foreach( $adminCommunities as $communitie ) {
														$communities .= $join . $communitie;
														$join = ', ';
													}
												} else {
													$communities = 'None';
												}
											}
											?>
											<div style="width:80%;background:#<?php echo $color;?>;border-radius: 3px 3px 3px 3px;-moz-border-radius: 3px 3px 3px 3px;-webkit-border-radius: 3px 3px 3px 3px;border: 0px solid #000000;margin:2px 0;">
												<div style="padding:8px;font-weight:normal;">
													<?php if( $hasEditPermission ) { ?>
													<a class="modal-hours-open" title="Edit hours" href="/drivers/hours/edit/<?php echo $day->format( 'Y/m/d' ); ?>/<?php echo $hour[ 'id_admin' ]; ?>">
													<?php } ?>
														<span style="color:#<?php echo $text_color; ?>;">
															<strong style="font-size:14px;"><?php echo $hour[ 'name' ]; ?></strong>
															<div style="font-size:11px">(<?php echo $communities ?>)</div>
															<span class="admin-timezone">
																<?php echo $hour[ 'hours' ]; ?> (<?php echo $now->format( 'T' ); ?>)
															</span>
															<span class="pst-timezone">
																<?php echo $hour[ 'pst' ]; ?> (PST)
															</span>
															<br/>
														</span>
													<?php if( $hasEditPermission ) { ?>
													</a>
													<?php } ?>
												</div>
											</div>
											<?php
										}
									}
									?>
									<?php if( $hasEditPermission ) { ?>
									<div>
										<button class="btn btn-default modal-hours-open" href="/drivers/hours/add/<?php echo $day->format( 'Y/m/d' ); ?>/<?php echo $this->week; ?>" style="width:80%;" title="Add hours to <?php echo $day->format( 'M' ); ?> <?php echo $day->format( 'd' ); ?> (<?php echo $day->format( 'D' ); ?>)">
											<i class="icon-plus icon-2x"></i>
										</button>
									</div>
									<?php } ?>
									</div>
								</td>
							<?php } ?>
						</tr>
					</table>

					<br/>
					<div class="padded" style="font-size:16px;">
						<?php if( $hasEditPermission ) { ?>
							<button class="btn btn-blue" onclick="workingHours.copyAll();"><i class="icon-copy"></i> Copy the hours from the previous week</button>
						<?php } ?>
						<button style="margin-left:20px;" class="btn btn-default" onclick="workingHours.toggleTimezone();"><i class="icon-time"></i> Show hours in&nbsp;
						<span class="pst-timezone">driver's timezone</span>
						<span class="admin-timezone">PST timezone</span>
						</button>
					</div>
				</div>
		</div>
	</div>
</div>
<?php if( $hasEditPermission ) { ?>
<div class="modal hide fade" id="modal-hours" style="display: none;">
	<div class="modal-header">
		<button class="close" data-dismiss="modal" type="button">×</button>
		<h6 id="modal-hours-title"></h6>
	</div>
	<div class="modal-body" id="modal-hours-body"></div>
	<div class="modal-footer">
		<button class="btn btn-default" data-dismiss="modal">Close</button>
	</div>
</div>
<style type="text/css">
	.admin-timezone{
		display: none;
	}
</style>
<script type="text/javascript">

$( document ).ready( function(){
	$( '.modal-hours-open' ).click( function(e) {
		e.preventDefault();
		var url = $( this ).attr( 'href' );
		var title = $( this ).attr( 'title' );
		$.get( url, function( data ) {
			$( '#modal-hours' ).modal();
			$( '#modal-hours-title' ).html( title );
			$( '#modal-hours-body' ).html( data );
		} );
	} );
} );

var workingHours = {};
workingHours.admin_tzs = {};
workingHours.showingHoursAtTimezone = 'pst';
workingHours.toggleTimezone = function(){
	$( '.' + workingHours.showingHoursAtTimezone + '-timezone' ).hide();
	if( workingHours.showingHoursAtTimezone == 'pst' ){
		workingHours.showingHoursAtTimezone = 'admin';
	} else {
		workingHours.showingHoursAtTimezone = 'pst';
	}
	$( '.' + workingHours.showingHoursAtTimezone + '-timezone' ).show();
};


<?php foreach( $this->reps as $rep ) { ?>
	<?php $now = new DateTime( 'now', new DateTimeZone( $rep->timezone ) ); ?>
	workingHours.admin_tzs[ '<?php echo $rep->id_admin ?>' ] = '<?php echo $rep->timezone ?> (<?php echo $now->format( 'T' ); ?>)';
<?php } ?>
workingHours.admin_tz = '';
workingHours.setTimezone = function( id_admin ){
	workingHours.admin_tz = ( workingHours.admin_tzs[ id_admin ] ) ? workingHours.admin_tzs[ id_admin ] : 'America/Los_Angeles';
	$( '#form-timezone' ).html( '<strong>' + workingHours.admin_tz + '</strong>' );
};

workingHours.copyAll = function(){
	if( confirm( 'Confirm copy? This will remove all the hours from this week <?php echo $this->week . '/' . $this->year; ?> !' ) ){
		$.ajax( {
			url: '/api/drivers/hours/copy-all',
			method: 'POST',
			data: { 'year' : '<?php echo $this->year; ?>', 'week' : '<?php echo $this->week; ?>' },
			dataType: 'json',
		} ).done( function( data ) {
			if( data.success ){
				location.reload();
			} else {
				alert( 'Oops! ' + data.error );
			}
		} );
	}
};

workingHours.add = function(){

	var id_admin = $( '#form-id_admin' ).val();
	var hours = $.trim( $( '#form-hours' ).val() );

	if( id_admin == '' ){
		alert( 'Please choose the worker!' );
		$( '#form-id_admin' ).focus();
		return;
	}

	if( hours == '' ){
		alert( 'Please type the hours!' );
		$( '#form-hours' ).focus();
		return;
	}

	if( !workingHours.validateHours( hours ) ){
		alert( 'Unable to figure out what this time means!' );
		$( '#form-hours' ).focus();
		return;
	}

	var weekdays = [];
	$( '[name="form-weekdays"]' ).each( function(){
		var checkbox = $( this );
		if( checkbox.is( ':checked' ) ){
			weekdays.push( checkbox.val() );
		}
	} );

	$.ajax( {
		url: '/api/drivers/hours/add',
		method: 'POST',
		data: { 'id_admin' : id_admin, 'day' : $( '#form-day' ).val(), 'month' : $( '#form-month' ).val(), 'year' : $( '#form-year' ).val(), 'week' : $( '#form-week' ).val(), 'hours' : hours, 'weekdays' : weekdays },
		dataType: 'json',
	} ).done( function( data ) {
		if( data.success ){
			location.reload();
		} else {
			alert( 'Oops! ' + data.error );
		}
	} );
};

workingHours.edit = function(){

	var hours = $.trim( $( '#form-hours' ).val() );

	if( hours == '' ){
		alert( 'Please type the hours!' );
		$( '#form-hours' ).focus();
		return;
	}

	if( !workingHours.validateHours( hours ) ){
		alert( 'Unable to figure out what this time means!' );
		$( '#form-hours' ).focus();
		return;
	}

	$.ajax( {
		url: '/api/drivers/hours/edit',
		method: 'POST',
		data: { 'id_admin' : $( '#form-id_admin' ).val(), 'day' : $( '#form-day' ).val(), 'month' : $( '#form-month' ).val(), 'year' : $( '#form-year' ).val(), 'hours' : hours, 'weekdays' : [] },
		dataType: 'json',
	} ).done( function( data ) {
		if( data.success ){
			location.reload();
		} else {
			alert( 'Oops! ' + data.error );
		}
	} );
};

workingHours.copy = function(){
	if( confirm( 'Confirm copy? This will remove all the hours of this worker from this week!' ) ){
		$.ajax( {
			url: '/api/drivers/hours/copy',
			method: 'POST',
			data: { 'id_admin' : $( '#form-id_admin' ).val(), 'year' : $( '#form-year' ).val(), 'week' : $( '#form-week' ).val() },
			dataType: 'json',
		} ).done( function( data ) {
			if( data.success ){
				location.reload();
			} else {
				alert( 'Oops! ' + data.error );
			}
		} );
	}
};

workingHours.remove = function(){
	if( confirm( 'Confirm remove?' ) ){
		$.ajax( {
			url: '/api/drivers/hours/remove',
			method: 'POST',
			data: { 'id_admin' : $( '#form-id_admin' ).val(), 'day' : $( '#form-day' ).val(), 'month' : $( '#form-month' ).val(), 'year' : $( '#form-year' ).val() },
			dataType: 'json',
		} ).done( function( data ) {
			if( data.success ){
				location.reload();
			} else {
				alert( 'Oops! ' + data.error );
			}
		} );
	}
};

workingHours.validateHours = function( hours ){
	hours = hours.replace( /\(.*?\)/g, '' );
	segments = hours.split( /(?:and|,)/ );
	for( i in segments ) {
		if( !/^ *(\d+)(?:\:(\d+))? *(am|pm) *(?:to|-) *(\d+)(?:\:(\d+))? *(am|pm) *$/i.exec( segments[ i ] ) ) {
			return false;
		}
	}
	return true;
}

</script>
<?php } ?>