<?php
@session_start();
require_once(dirname(__FILE__) . "/../../../adbcon.php");

$err = '';
$msg = '';

if(isset($_POST['sched_id'])) {
	$sched_id = intval($_POST['sched_id']);
	
	if(isset($_POST['sched_save'])) {
		$showtimes = mysql_real_escape_string(trim($_POST['showtimes']['d']) . ',w ' . trim($_POST['showtimes']['w']));
		
		if(isset($_POST['remove'])) {
			$q = "delete from im_showtimes where id='$sched_id';";
			$r = @mysql_query($q);
			
			if($r && mysql_affected_rows() == 1) {
				$msg = 'Cinema entry was removed';
			}
			else {
				$err .= 'Cinema entry was not removed';
			}
		}
		else {
			$q = "update im_showtimes set showtimes='$showtimes' where id='$sched_id';";
			$r = @mysql_query($q);
			if($r && mysql_affected_rows() != -1) {
				$msg = 'Show Times saved.';
			}
			else {
				$err = 'Show Times NOT saved.';
			}
		}
	}
	
	if($err != '') {
		echo '<div class="error"><strong>Error</strong><br/>' . $err . '</div>';
	}
	
	if($msg != '') {
		echo '<div class="note"><strong>Success</strong><br/>' . $msg . '</div>';
	}
	
	$q = "select c.*, s.*, m.title from im_cinemas c, im_showtimes s, im_movies m where c.cinema_code=s.cinema_code and s.movie_code=m.movie_code and s.id='$sched_id';";
	$r = mysql_query($q) or die("<strong>$q</strong>".mysql_error());
	if($r && mysql_num_rows($r)>0) {
		$row = mysql_fetch_assoc($r);
		//echo '<pre>', var_export($row, true), '</pre>';
		$d = '';
		$w = '';
		$showtimes = trim(stripslashes($row['showtimes']));
		$ss = explode(",", $showtimes);
		$p = count($ss);
		if($p > 0) {
			if($p == 2) {
				$d = trim($ss[0]);
				$w = trim($ss[1]);
				$w = str_replace( array("weekends", "also", "at", "weekend", "w"), "", strtolower($w) );
				$w = trim($w);
			}
			elseif($p == 1) {
				$tmp = strtolower(trim($ss[0]));
				if(strpos($tmp, "w") === false) {
					$d = trim($tmp);
				}
				else {
					$w = str_replace( array("weekends also at", "weekend", "w"), "", $tmp );
					$w = trim($w);
				}
			}
		}
		
		?>
		<div class="container-fluid">
			<form method="post" id="im_cinemas_frm" role="form" class="form-horizontal">
			<table class="table table-bordered">
				<tr>
					<td>Cinema</td>
					<td colspan="2">
						<?php
						echo stripslashes($row['cinema_title']);
						?>
					</td>
				</tr>
				<tr>
					<td>Movie</td>
					<td colspan="2">
						<?php
						echo stripslashes($row['title']);
						?>
					</td>
				</tr>
				<tr>
					<td>Location</td>
					<td colspan="2">
						<?php
						echo stripslashes($row['location']);
						?>
					</td>
				</tr>
				<tr>
					<td valign="top">Show times</td>
					<td>
						<strong>Daily Hours</strong><br/>
						<input type="text" name="showtimes[d]" value="<?php echo $d;?>" class="form-control input-sm">
					</td>
					<td>
						<strong>Weekend Only Hours</strong><br/>
						<input type="text" name="showtimes[w]" value="<?php echo $w;?>" class="form-control input-sm">
					</td>
				</tr>
				<tr>
					<td>Remove?</td>
					<td colspan="2">
                        <div class="checkbox">
                            <label class="">
                            <input type="checkbox" name="remove" value="1"> Check ONLY if you want to remove it
                            </label>
                        </div>
					</td>
				</tr>
			</table>
			<input type="hidden" name="sched_id" value="<?php echo $sched_id;?>">
			<input type="hidden" name="sched_save" value="1">
			</form>
		</div>
		<?php
		
	}
}

?>