<?php
require_once(dirname(__FILE__) . "/../../../dbcon.php");

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

$location = trim($_POST['location']);
$movie_code = isset($_POST['movie_code']) ? trim($_POST['movie_code']) : '';

$step = "select_movie";
if($movie_code != '') {
	$step = "select_cinemas";
}

if($step == "select_movie") {
	?>
	<div class="inline_container">
		<form method="post" name="im_cinemas_frm" id="im_cinemas_frm">
		<table cellpadding="0" cellspacing="0" border="0" width="50%">
			<tr>
				<th align="left" colspan="2">Movie Selection</th>
			</tr>
			<tr>
				<td class="itdh" align="left" nowrap>Existing Movie</td>
				<td class="itd" align="left" width="80%">
					<select name="movie_code" id="movie_code">
						<?php
						$q = "select m.*, YEAR(release_date) as m_y from im_movies m left join im_showtimes s on m.movie_code=s.movie_code where s.id is null order by m_y desc, m.title asc;";
						$r = mysql_query($q) or die("<strong>$q</strong>".mysql_error());
						if($r && mysql_num_rows($r) > 0) {
							$opt_release_date = 0;
							$optgroup = "";
							echo '<option value="" selected="selected">Select Existing</option>';
							while ($row = mysql_fetch_assoc($r)) {
								$ex_title = stripslashes($row['title']);
								$ex_movie_code = $row['movie_code'];
								$ex_release_date = intval(substr($row['release_date'], 0, 4));
								$selected = '';
								if($ex_movie_code == $movie_code) $selected = 'selected="selected"';
								if($opt_release_date != $ex_release_date) {
									$opt_release_date = $ex_release_date;
									if($optgroup == 1) {
										echo '</optgroup>';
										$optgroup = 0;
									}
									if($optgroup == "" || $optgroup == 0) {
										echo '<optgroup label="',$opt_release_date,'">';
										$optgroup = 1;
									}
								}
								echo '<option value="',$ex_movie_code,'" ',$selected,'>',$ex_title,'</option>';
							}
						}
						else {
							echo '<option value="" selected="selected">No Existing Movies</option>';
						}
						?>
					</select>	
				</td>
			</tr>
			
		</table>
		<input type="hidden" name="location" value="<?php echo $location;?>">
		</form>
	</div>
	<?php
}

if($step == "select_cinemas") {
	$movie_title = "Unknown Movie - Please Reselect";
	$q = "select * from im_movies where movie_code='$movie_code';";
	$r = @mysql_query($q);
	if($r && mysql_num_rows($r) == 1) {
		 $row = mysql_fetch_assoc($r);
		 
		 $movie_title = stripslashes($row['title']) . ' (' . $row['year'] . ')';
	}
	
	
	$cinemas = array();
	$q = "select * from im_cinemas where cinema_status='1' and cinema_location='$location' order by cinema_location asc, cinema_title asc;";
	$r = mysql_query($q) or die("<strong>$q</strong>".mysql_error());
	if($r && mysql_num_rows($r)>0) {
		while ($row = mysql_fetch_assoc($r)) {
			$tmp = $row;
			foreach ($tmp as $fld=>$val) {
				$tmp[$fld] = stripslashes($val);
			}
			$cinemas[$row['cinema_code']] = $tmp;
		}
	}
	
	
	if(isset($_POST['add_cinema'])) {
		$cineplay = $_POST['cineplay'];
		$cinetag = $_POST['cinetag'];
		$qin = array();
		if(is_array($cineplay) && count($cineplay) > 0) {
			$q = "delete from im_showtimes where movie_code='$movie_code' and location='$location';";
			$r = mysql_query($q) or die("<strong>$q</strong>".mysql_error());
			foreach ($cineplay as $ind=>$times) {
				$times_d = trim($times['d']);
				if($times_d != "") {
					$times_d = preg_replace('/\s\s+/', ' ', $times_d);
				}
				
				$times_w = trim($times['w']);
				if($times_w != "") {
					$times_w = preg_replace('/\s\s+/', ' ', $times_w);
				}
				
				$show_times = mysql_real_escape_string($times_d . ',w ' . $times_w);
				if($times_d != "" || $times_w != '') {
					$cinema_code = $cinetag[$ind];
					$location = $cinemas[$cinema_code]['cinema_location'];
					$q = "insert into im_showtimes (cinema_code, movie_code, showtimes, location)
						values ('$cinema_code', '$movie_code', '$show_times', '$location');";
					$qin [] = $q;
				}
			}
			
			if(count($qin) > 0) {
				
				foreach ($qin as $i=>$q) {
					$r = mysql_query($q) or die("<strong>$q</strong>".mysql_error());
					$msg .= "<br/>The Cinema was successfully added";
				}
			}
			
			
		}
	}
	
	
	
	
	// Get The Cinemas Playing this movie
	$cine_show = array();
	$q = "select * from im_showtimes where movie_code='$movie_code' and location='$location' order by location asc;";
	$r = mysql_query($q) or die("<strong>$q</strong>".mysql_error());
	if($r && mysql_num_rows($r)>0) {
		while ($row = mysql_fetch_assoc($r)) {
			$tmp = $row;
			foreach ($tmp as $fld=>$val) {
				$tmp[$fld] = stripslashes($val);
			}
			$cine_show[$row['cinema_code']] = $tmp;
		}
	}
	
	function cine_select($cinemas, $count=1, $code="") {
		//print_r($cinemas);
		$ret = '<select name="cinetag['.$count.']" class="cine_times">';
		$ret .= '<option value="" selected="selected">Select Cinema</option>';
		$opt_loc = "";
		$opt_on = 0;
		foreach ($cinemas as $cinema_code=>$cdata) {
			$loc = $cdata['cinema_location'];
			if($opt_loc != $loc) {
				if($opt_on == 1) {
					$ret .= '</optgroup>';
					$opt_on = 0;
				}
				$opt_loc = $loc;
				$opt_on = 1;
				$ret .= '<optgroup label="'.$loc.'">';
			}
			$selected = '';
			if($code != "" && $code == $cinema_code) $selected = 'selected="selected"';
			$ret .= '<option value="'.$cinema_code.'" ' . $selected . '>' . $cdata['cinema_title'] . ' ('.$cdata['cinema_location'].')' . '</option>';
		}
		$ret .= '</select>';
		
		return $ret;
	}
	
	function cine_select_unique($cinemas, $ex_cinemas, $count=1, $code="") {
		//print_r($cinemas);
		$ret = '<select name="cinetag['.$count.']" class="cine_times">';
		$ret .= '<option value="" selected="selected">Select Cinema</option>';
		$opt_loc = "";
		$opt_on = 0;
		foreach ($cinemas as $cinema_code=>$cdata) {
			if(!isset($ex_cinemas[$cinema_code])) {
				$loc = $cdata['cinema_location'];
				if($opt_loc != $loc) {
					if($opt_on == 1) {
						$ret .= '</optgroup>';
						$opt_on = 0;
					}
					$opt_loc = $loc;
					$opt_on = 1;
					$ret .= '<optgroup label="'.$loc.'">';
				}
				$selected = '';
				if($code != "" && $code == $cinema_code) $selected = 'selected="selected"';
				$ret .= '<option value="'.$cinema_code.'" ' . $selected . '>' . $cdata['cinema_title'] . ' ('.$cdata['cinema_location'].')' . '</option>';
			}
		}
		$ret .= '</select>';
		
		return $ret;
	}
	
	if($err != '') {
		echo '<div class="error"><strong>Error</strong><br/>' . $err . '</div>';
	}
	
	if($msg != '') {
		echo '<div class="note"><strong>Success</strong><br/>' . $msg . '</div>';
	}
	?>
	<div class="inline_container">
		<form method="post" name="im_cinemas_frm" id="im_cinemas_frm">
		<table cellpadding="0" cellspacing="0" border="0" width="100%">
			<tr>
				<td class="itd" align="left" colspan="3">Showtimes for <u><strong><?php echo $movie_title; ?></strong></u> in <strong><?php echo ucwords($location);?></strong> cinemas</td>
			</tr>
			<tr>
				<th align="center" width="20%">Cinema</th>
				<th align="center" width="40%">Daily Hours</th>
				<th align="center" width="40%">Weekend Only Hours</th>
			</tr>
			<?php
			$k = 0;
			if(count($cinemas) > 0) {
				if(count($cine_show) > 0) {
					if(count($cine_show) >= 10) $k = count($cine_show) + 4;
					foreach ($cine_show as $cinema_code => $data) {
						$cinema_title = $cinemas[$cinema_code]['cinema_title'];
						$cinema_location = $cinemas[$cinema_code]['cinema_location'];
						$times = $cine_show[$cinema_code]['showtimes'];
						
						$ss = explode(",", $times);
						$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);
								}
							}
						}
						?>
						<tr>
							<td class="itdh" align="left" valign="middle" nowrap><?php echo cine_select($cinemas, $k, $cinema_code);?></td>
							<td class="itd_lb" align="left">
								<input type="text" name="cineplay[<?php echo $k;?>][d]" style="width:100%;" class="times" value="<?php echo $d;?>">
							</td>
							<td class="itd_lb" align="left">
								<input type="text" name="cineplay[<?php echo $k;?>][w]" style="width:100%;" class="times" value="<?php echo $w;?>">
							</td>
						</tr>
						<?php
						$k++;
					}
				}
				
				if($k < 10) {
					for($i=$k; $i<10; $i++) {
						?>
						<tr>
							<td class="itdh" align="left" valign="middle"><?php echo cine_select($cinemas, $i);?></td>
							<td class="itd_lb" align="left">
								<input type="text" name="cineplay[<?php echo $i;?>][d]" style="width:100%;" class="times" value="">
							</td>
							<td class="itd_lb" align="left">
								<input type="text" name="cineplay[<?php echo $i;?>][w]" style="width:100%;" class="times" value="">
							</td>
						</tr>
						<?php
					}
				}
			}
			?>
		</table>
		<input type="hidden" name="movie_code" value="<?php echo $movie_code;?>">
		<input type="hidden" name="location" value="<?php echo $location;?>">
		<input type="hidden" name="add_cinema" value="1">
		</form>
	</div>
	<?php
}
?>