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

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

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

$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) {
		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="form-control input-sm">';
	$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="form-control input-sm"">';
	$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="container-fluid">
	<form method="post" name="im_cinemas_frm" id="im_cinemas_frm">
	<div class="box box-primary">
		<div class="box-header with-border">
			<h3>Add Cinema Playing This Movie</h3>
		</div>
        <div class="box-body">
            <table class="table table-bordered">
                <thead>
                <tr>
                    <th>Cinema</th>
                    <th>Daily Hours</th>
                    <th>Weekend Only Hours</th>
                </tr>
                </thead>
                <tbody>
                <?php
                $k = 0;
                if(count($cinemas) > 0 && count($cinemas) > count($cine_show)) {
                    ?>
                    <tr>
                        <td>
                            <div class="form-group">
                            <?php echo cine_select_unique($cinemas, $cine_show, count($cine_show));?>
                            </div>
                        </td>
                        <td>
                            <div class="form-group">
                            <input type="text" name="cineplay[<?php echo count($cine_show);?>][d]" class="form-control input-sm" value="">
                            </div>
                        </td>
                        <td>
                            <div class="form-group">
                            <input type="text" name="cineplay[<?php echo count($cine_show);?>][w]" class="form-control input-sm" value="">
                            </div>
                        </td>
                    </tr>
                    <?php
                }
                else {
                    ?>
                    <tr>
                        <td colspan="3" class="text-danger">There are no cinemas.</td>
                    </tr>
                    <?php
                }

                ?>
                </tbody>
            </table>
        </div>


	</div>
	<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>