<?php
if (empty($_GET['id'])) {
   header("Location: " . $wo['config']['site_url']);
   exit();
}
$source = Wo_GetMovies(array('id' => $_GET['id']));
if (empty($source)) {
   header("Location: " . $wo['config']['site_url']);
   exit();
}
$source = $source[0];

?>
<div class="container-fluid">
    <div class="block-header">
        <h2>Manage Features > Movies > Edit Movie "<?php echo $source['name'];?>"</h2>
    </div>
    <!-- Vertical Layout -->
    <div class="row">
        <div class="col-lg-6 col-md-6">
            <div class="card">
                <div class="header">
                    <h2>Edit Movie</h2>
                </div>
                <div class="body">
                    <div class="" id="film-alert"></div>
                    <form class="newforum" method="POST" id="new-film">
                        <div class="form-group form-float">
                            <div class="form-line">
                                <input type="text" id="name" name="name" class="form-control" value="<?php echo $source['name'];?>">
                                <label class="form-label">Movie Title</label>
                            </div>
                        </div>
                        <div class="form-group form-float">
                            <div class="form-line">
                                <textarea name="description" id="description" class="form-control" cols="30" rows="3"><?php echo br2nl($source['description']);?></textarea>
                                <label class="form-label">Description</label>
                            </div>
                        </div>
                        <div class="form-group form-float">
                            <div class="form-line">
                                <textarea name="stars" id="stars" class="form-control" cols="30" rows="3"><?php echo $source['stars'];?></textarea>
                                <label class="form-label">Stars <small>Separated by comma,</small></label>
                            </div>
                        </div>
                        <div class="form-group form-float">
                            <div class="form-line">
                                <input type="text" id="producer" name="producer" class="form-control" value="<?php echo $source['producer'];?>">
                                <label class="form-label">Producer</label>
                            </div>
                        </div>
                        <div class="form-group form-float">
                            <div class="form-line">
                                <input type="number" id="release" name="release" class="form-control" min="1900" max="<?php echo date('Y') ?>" value="<?php echo $source['release'];?>">
                                <label class="form-label">Release</label>
                            </div>
                        </div>
                         <div class="form-group form-float">
                            <div class="form-line">
                                <input type="number" id="duration" name="duration" class="form-control" min="10" max="350" value="<?php echo $source['duration'];?>">
                                <label class="form-label">Duration</label>
                            </div>
                        </div>
                        <br>
                        <label for="country">Country</label>
                        <select class="form-control show-tick" id="country" name="country">
                        <?php foreach ($wo['countries'] as $c_key => $c_value): 
                        $selected_country = ($source['country'] == $c_key) ? 'selected' : '';
                        ?>
							<option value="<?php echo $c_key; ?>" <?php echo $selected_country;?>><?php echo $c_value; ?></option>
						<?php endforeach; ?>
                        </select>
                        <div class="clearfix"></div>
                        <br><br>
                        <label for="quanlity">Quality</label>
                        <select class="form-control show-tick" id="quanlity" name="quanlity">
			                <option value="cam" <?php echo ($source['quality'] == 'cam') ? 'selected' : '';?>>CAMRip</option>
							<option value="ts" <?php echo ($source['quality'] == 'ts') ? 'selected' : '';?>>TS</option>
							<option value="vsh" <?php echo ($source['quality'] == 'vsh') ? 'selected' : '';?>>VHSRip</option>
							<option value="wp" <?php echo ($source['quality'] == 'wp') ? 'selected' : '';?>>WP</option>
							<option value="scr" <?php echo ($source['quality'] == 'scr') ? 'selected' : '';?>>SCR (VHSScr)</option>
							<option value="dvds" <?php echo ($source['quality'] == 'dvds') ? 'selected' : '';?>>DVDScr</option>
							<option value="ts" <?php echo ($source['quality'] == 'ts') ? 'selected' : '';?>>TC</option>
							<option value="ldr" <?php echo ($source['quality'] == 'ldr') ? 'selected' : '';?>>LDRip</option>
							<option value="tv" <?php echo ($source['quality'] == 'tv') ? 'selected' : '';?>>TVRip</option>
							<option value="sat" <?php echo ($source['quality'] == 'sat') ? 'selected' : '';?>>SATRip</option>
							<option value="dvb" <?php echo ($source['quality'] == 'dvb') ? 'selected' : '';?>>DVBRip</option>
							<option value="dtv" <?php echo ($source['quality'] == 'dtv') ? 'selected' : '';?>>DTVRip</option>
							<option value="dvd" <?php echo ($source['quality'] == 'dvd') ? 'selected' : '';?>>DVD</option>
							<option value="hdr" <?php echo ($source['quality'] == 'hdr') ? 'selected' : '';?>>HDRip</option>
							<option value="web-dl" <?php echo ($source['quality'] == 'web-dl') ? 'selected' : '';?>>WEB-DL</option>
							<option value="hd-tv" <?php echo ($source['quality'] == 'hd-tv') ? 'selected' : '';?>>HD-TV</option>
							<option value="hd" <?php echo ($source['quality'] == 'hd') ? 'selected' : '';?>>HD DVD</option>
                        </select>
                        <div class="clearfix"></div>
                        <br><br>
                        <label for="genre">Genre</label>
                        <select class="form-control show-tick" id="genre" name="genre">
                        <?php foreach ($wo['film-genres'] as $g_key => $g_value): ?>
							<option value="<?php echo $g_key; ?>"  <?php echo ($source['genre'] == $g_key) ? ' selected' : '';?>><?php echo $g_value; ?></option>
						<?php endforeach;?>
                        </select>
                        <br><br>
                        <div class="form-group form-float">
                            <div class="form-line">
                                <input type="number" id="rating" name="rating" class="form-control" min="1" max="10" step="0.1" value="<?php echo $source['rating'];?>">
                                <label class="form-label">Rating (Between 1 -> 10)</label>
                            </div>
                        </div>
                        <div class="clearfix"></div>
                        <br><br>
                        <label for="new-blog-desc" title="Upload" class="pointer">Cover</label>  
				        <div class="main pointer" id="select-f-cover">
				           <label class="pointer"><i class="fa fa-camera fa-wh" aria-hidden="true"></i> <?php echo $wo['lang']['select'] ?></label>
				        </div>
				         <div id="progress" style="display: none">
				            <span id="percent">0%</span>
				            <div class="progress">
				             <div id="bar" class="progress-bar progress-bar-striped active"></div> 
				            </div>
				            <div class="clear"></div>
				         </div>
				        <input type="file" class="hidden" name="cover" id="film-cover" accept="image/*">
                        <input type="hidden" name="hash_id" value="<?php echo Wo_CreateSession();?>">
                        <input type="hidden" name="id" value="<?php echo $source['id'];?>">
                        <button type="submit" id="upload-film" class="btn btn-primary m-t-15 waves-effect">Add Movie</button>
                    </form>
                </div>
            </div>
        </div>
        <div class="clearfix"></div>
    </div>
    <!-- #END# Vertical Layout -->
<script>

	jQuery(document).ready(function($) {
		var bar = $('#bar');
	    var percent = $('#percent');
	    var status = $('#status');
	    var publisher_button = $('#publisher-button');
		$("#select-f-cover").click(function(event) {
	      $("#film-cover").click()
	    });

	    $("#film-cover").change(function(event) {
	      $("#select-f-cover").html("<img src='" + window.URL.createObjectURL(this.files[0]) + "' alt='Picture'  class='w100'>")
	    });


	    $('#new-film').ajaxForm({
	      url: Wo_Ajax_Requests_File() + '?f=edit-film',
	      beforeSend: function() {
	      	var percentVal = '0%';
	        bar.width(percentVal);
	        percent.html(percentVal);
	      },
	      uploadProgress: function (event, position, total, percentComplete) {
           var percentVal = percentComplete + '%';
           bar.width(percentVal);
           $('#progress').slideDown(200);
           if(percentComplete > 50) {
             percent.addClass('white');
           }
           percent.html(percentVal);
         },
	      type:"POST",
	      success: function(data) {
	      	$("html, body").animate({ scrollTop: 0 }, "slow");
	        bar.width(0);
	        percent.html('0%');
	        $('#progress').slideUp(200);
	        if (data['status'] == 200) {
	          $("#film-alert").html('<div class="alert alert-success">'+ data['message'] +'</div>');
	          $('#new-film').resetForm();
	        } 
	        else if (data['message']) {
	          $("#film-alert").html('<div class="alert alert-danger">' + data['message'] + '</div>');
	        } 
	      }});
	    $(".delete-film").click(function(event) {
	    	$("#delete-film").attr('data-film-ident', $(this).attr('id')).modal('show');
	    });
	});

	function Wo_IsURL(str) {
	  var pattern = new RegExp('^(https?:\\/\\/)?'+ 
	  '((([a-z\\d]([a-z\\d-]*[a-z\\d])*)\\.?)+[a-z]{2,}|'+ 
	  '((\\d{1,3}\\.){3}\\d{1,3}))'+ 
	  '(\\:\\d+)?(\\/[-a-z\\d%_.~+]*)*'+ 
	  '(\\?[;&a-z\\d%_.~+=-]*)?'+ 
	  '(\\#[-a-z\\d_]*)?$','i'); 
	  return pattern.test(str);
	}

	function Wo_FilmMediaUrl(pastedData) {
		var success = false;
		var media   = {};
		if (GetYoutubeId(pastedData)) {

		    media.type  = "youtube";
		    media.id    = GetYoutubeId(pastedData);
		    success     = true;
		    $("#embed_source").attr('name', 'iframe');
		}

		else if (Wo_GetVimeoID(pastedData)) {
		    media.type  = "vimeo";
		    media.id    = Wo_GetVimeoID(pastedData);
		    success     = true;
		    $("#embed_source").attr('name', 'iframe');
		}

		else if (Wo_IsURL(pastedData)) {
		    media.type  = "other";
		    media.id    = pastedData;
		    success     = true;
		    $("#embed_source").attr('name', 'other');
		}

		if (success) { 
			return media; 
		}

		else { 
			alert("Not valid source id detected"); 
			return false;
		}
	}
</script>