<?php 
error_reporting(0);
include ('includes/header.php');

$table_name = 'intro';

$db->exec("CREATE TABLE IF NOT EXISTS {$table_name}(id INTEGER PRIMARY KEY,url TEXT)");

$rows = $db->query("SELECT COUNT(*) as count FROM {$table_name}");
$row = $rows->fetchArray();
$numRows = $row['count'];
if ($numRows == 0){
	$db->exec("INSERT INTO {$table_name}(url) VALUES('')");
}

$intro = $db->querySingle('SELECT url FROM intro WHERE id=1');

function Submit($sub_type, $db, $table_name){
	if ($sub_type == 'sub_new') {
		$target_dir = 'intro/';
		$target_file = basename($_FILES['fileToUpload']['name']);
		$gtg = 1;
		$ft = strtolower(pathinfo($target_file, PATHINFO_EXTENSION));
		

		$file_path = (isset($_SERVER['HTTPS']) && ($_SERVER['HTTPS'] !== 'off') ? 'https' : 'http') . '://' . $_SERVER['HTTP_HOST'] . dirname($_SERVER['PHP_SELF']) . '/intro/';
		if ($_POST['type'] == 'file') {
			if ($_FILES["fileToUpload"]["size"] > 10000000) {
				echo '<div class="alert alert-danger" id="success-alert1"><h4><i class="icon fa fa-times"></i>Sorry, your file is too large, max is 10mb.</h4></div>';
				$gtg = 0;
			}
			if ($ft != 'mp4') {
				echo '<div class="alert alert-danger" id="success-alert"><h4><i class="icon fa fa-times"></i>Sorry, only MP4 files are allowed.</h4></div>';
				$gtg = 0;
			}
			if ($gtg != 0) {
				$intro_f = $file_path . 'intro.mp4';
				echo $intro_f;
				if (file_exists($intro_f)) {
					unlink($intro_f);
				}
				move_uploaded_file($_FILES['fileToUpload']['tmp_name'], $target_dir . 'intro.mp4');
				$db->exec("UPDATE {$table_name} SET	url='$intro_f' WHERE id='1' ");
				echo "<script>window.location.href='intro.php?status=2'</script>";
			}
		}
		if ($_POST['type'] == 'url') {
			$db->exec("UPDATE {$table_name} SET	url='".$_POST['intro_url']."' WHERE id='1' ");
			echo "<script>window.location.href='intro.php?status=2'</script>";
		}
	}
	$db->close();
}

if (isset($_POST['submit'])){
	Submit('sub_new',$db, $table_name);
	}
if (!file_exists('intro')) {
	mkdir('intro', 511, true);
}
?>

        <div class="col-md-6 mx-auto">
            <div class="card-body">
                <div class="card bg-primary text-white">
                    <div class="card-header card-header-warning">
                        <center>
                            <h2><i class="icon icon-bullhorn"></i> Intro</h2>
                        </center>
                    </div>
                    
                    <div class="card-body">
                        <div class="col-12">
                            <h3>Upload Intro</h3>
							<h6>For no intro leave URL blank and submit</h6>
                        </div>
                            <form action="" method="post" enctype="multipart/form-data">
                               <br>
                               <div>
                                   <label for="selector">Select Intro Source</label>
                                   <select class="form-control"  style="width:auto;" name = "type" onchange="yesnoCheck(this);">
                                       <option value="url">URL</option>
                                       <option value="file">FILE</option>
                                   </select>
                               </div>
                               <br>
                               
                               
                               <div id="loc" class="form-group form-float form-group-lg">
                                   <div class="form-line">
                                       <label class="form-label"><strong>Intro URL</strong></label>
                               		<input type="text" class="form-control" name="intro_url" placeholder="http://host.com/folder/video.mp4">
                               	</div>
                               </div>
                               
                               <div id="ftu" class="form-group"  style="display: none;">
                                   <label class="control-label ">
                                       <strong>Intro File</strong>
                                   </label>
                                   <div class="input-group">
                                       <input type="file" name="fileToUpload" id="fileToUpload" >
                                   </div>
                               </div>

                                <div class="form-group">
                                    <center>
                                        <button class="btn btn-info " name="submit" type="submit">
                                            <i class="icon icon-check"></i> Submit
                                        </button>
                                    </center>
                                </div>
                            </form>
                  <div class="align-items-center">
                        <center>
                      <h3">Current Intro</h3>
                      <div><video width="500" autoplay="true" muted preload="auto" controls>  <source src="<?=$intro."?t=".time()?>" type="video/mp4"></video></div>
                  </div>
                    </div>
                </div>
            </div>
        </div>

<?php include ('includes/footer.php');?>
<script>

function yesnoCheck(that){
    if (that.value == "url"){
        document.getElementById("loc").style.display = "block";
    }else{
        document.getElementById("loc").style.display = "none";
    }
    if (that.value == "file"){
        document.getElementById("ftu").style.display = "block";
    }else{
        document.getElementById("ftu").style.display = "none";
    }
}
</script>