<?php 

    $page_title="Add Recipe";

    include("includes/header.php");
    require("includes/function.php");
    require("language/language.php");

    $cat_qry="SELECT * FROM tbl_category ORDER BY category_name";
    $cat_result=mysqli_query($mysqli,$cat_qry); 

    if(isset($_POST['submit']))
    {

     if ($_POST['recipe_type']=='video')
     {
                  //Video
      $video_url=$_POST['video_url'];

      $youtube_video_url = addslashes($_POST['video_url']);
      parse_str( parse_url( $youtube_video_url, PHP_URL_QUERY ), $array_of_vars );
      $video_id=  $array_of_vars['v'];

       //Image
      $recipe_image=rand(0,99999)."_".$_FILES['recipe_image']['name'];

       //Main Image
      $tpath1='images/'.$recipe_image;        
      $pic1=compress_image($_FILES["recipe_image"]["tmp_name"], $tpath1, 80);

      //Thumb Image 
      $thumbpath='images/thumbs/'.$recipe_image;   
      $thumb_pic1=create_thumb_image($tpath1,$thumbpath,'200','200');

    }


    if ($_POST['recipe_type']=='image')
    {


      $recipe_image=rand(0,99999)."_".$_FILES['recipe_image']['name'];

                  //Main Image
      $tpath1='images/'.$recipe_image;        
      $pic1=compress_image($_FILES["recipe_image"]["tmp_name"], $tpath1, 80);

                  //Thumb Image 
      $thumbpath='images/thumbs/'.$recipe_image;   
      $thumb_pic1=create_thumb_image($tpath1,$thumbpath,'200','200');   

      $video_url='';
      $video_id='';
    } 

    $data = array(
     'cat_id'  =>  cleanInput($_POST['cat_id']),
     'sub_cat_id'  => ($_POST['sub_cat_id']) ? $_POST['sub_cat_id'] : 0,
     'recipe_type'  =>  cleanInput($_POST['recipe_type']),
     'recipe_name'  =>  cleanInput($_POST['recipe_name']), 
     'recipe_time'  =>  cleanInput($_POST['recipe_time']),
     'recipe_ingredients'  =>  trim($_POST['recipe_ingredients']),
     'recipe_direction'  =>  trim($_POST['recipe_direction']),
     'video_url'  =>  $video_url,
     'video_id'  =>  $video_id,
     'recipe_image'  =>  $recipe_image
    );		

    $qry = Insert('tbl_recipe',$data);	


    $_SESSION['msg']="10";
    header( "Location:add_recipe.php");
    exit;
  }


?>
<div class="row">
  <div class="col-md-12">
    <?php
      if(isset($_GET['redirect'])){
            echo '<a href="'.$_GET['redirect'].'" class="btn_back"><h4 class="pull-left" style="font-size: 20px;color: #e91e63"><i class="fa fa-arrow-left"></i> Back</h4></a>';
          }
          else{
            echo '<a href="manage_recipe.php" class="btn_back"><h4 class="pull-left" style="font-size: 20px;color: #e91e63"><i class="fa fa-arrow-left"></i> Back</h4></a>';
          }
    ?>
    <div class="card">
      <div class="page_title_block">
        <div class="col-md-5 col-xs-12">
          <div class="page_title"><?=$page_title?></div>
        </div>
      </div>
      <div class="clearfix"></div>
      <div class="card-body mrg_bottom"> 
        <form action="" name="add_form" method="post" class="form form-horizontal" enctype="multipart/form-data">
          <div class="section">
            <div class="section-body">
             <div class="form-group">
              <label class="col-md-3 control-label">Recipe Title :-</label>
              <div class="col-md-6">
                <input type="text" name="recipe_name" id="recipe_name" value="" class="form-control"  required="required">
              </div>
            </div>
            <div class="form-group">
              <label class="col-md-3 control-label">Category :-</label>
              <div class="col-md-6">
                <select name="cat_id" id="cat_id" class="select2" required="required">
                  <option value="">--Select Category--</option>
                  <?php
                  while($cat_row=mysqli_fetch_array($cat_result))
                  {
                   ?>          						 
                   <option value="<?php echo $cat_row['cid'];?>"><?php echo $cat_row['category_name'];?></option>	          							 
                   <?php
                 }
                 ?>
               </select>
             </div>
           </div>
           <div class="form-group">
            <label class="col-md-3 control-label">Select Sub Category :-</label>
            <div class="col-md-6">
              <select name="sub_cat_id" id="sub_cat_id" class="select2">
                <option value="">--Select Sub Category--</option>
              </select>
            </div>
          </div>
          <div class="form-group">
            <label class="col-md-3 control-label">Recipe Type :-</label>
            <div class="col-md-6">                       
              <select name="recipe_type" id="recipe_type" style="width:280px; height:25px;" class="select2" required>
               <option value="image">Image</option>
               <option value="video">Video</option>
             </select>
           </div>
          </div>
         <div class="form-group">
          <label class="col-md-3 control-label">Recipe Time :-</label>
          <div class="col-md-6">
            <input type="text" name="recipe_time" id="recipe_time" value="" class="form-control" required>
          </div>
        </div>
        <div class="form-group">
          <label class="col-md-3 control-label">Recipe Ingredients :-</label>
          <div class="col-md-6">
            <textarea name="recipe_ingredients" id="recipe_ingredients" class="form-control"></textarea>
            <script>CKEDITOR.replace( 'recipe_ingredients' );</script>
          </div>
        </div>
        <br>
        <div class="form-group">
          <label class="col-md-3 control-label">Recipe Direction :-</label>
          <div class="col-md-6">                    
            <textarea name="recipe_direction" id="recipe_direction" class="form-control"></textarea>
            <script>CKEDITOR.replace('recipe_direction');</script>
          </div>
        </div>
        <br/>
        <div id="video_url_display" class="form-group" style="display:none;">
          <label class="col-md-3 control-label">Video URL :-
            <p class="control-label-help">(Only YouTube Video URL Supported)</p>
          </label>
          <div class="col-md-6">
            <input type="text" name="video_url" id="video_url" value="" class="form-control">
          </div>
        </div>

        <div id="thumbnail" class="form-group">
          <label class="col-md-3 control-label">Featured Image:-
            <p class="control-label-help">(Recommended resolution: 300x400, 400x500 OR Rectangle Image)</p>
          </label>
          <div class="col-md-6">
            <div class="fileupload_block">
              <input type="file" name="recipe_image" value="fileupload" required="required" accept=".png, .jpg, .JPG .PNG" onchange="fileValidation()" id="fileupload">
              <div class="fileupload_img" id="uploadPreview"><img type="image" src="assets/images/portrait.jpg" alt="image" style="width: 90px;height: 120px" /></div>
            </div>
          </div>
        </div>
        <div class="form-group">
          <div class="col-md-9 col-md-offset-3">
            <button type="submit" name="submit" class="btn btn-primary">Save</button>
          </div>
        </div>
      </div>
    </div>
  </form>
</div>
</div>
</div>
</div>

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

<script type="text/javascript">
  $(document).ready(function() {

      $("select[name='cat_id']").change(function(){
        $.get('ajax_sub_cat.php?id=' + $(this).val(), function(data) {
          $("#sub_cat_id").html(data);
        }); 
      });

      $("#recipe_type").change(function(){

        var type=$("#recipe_type").val();

        if(type=="video")
        {
          $("#video_url_display").show();
        } 
        else
        {
          $("#video_url_display").hide();
        }
      });
  });

  function fileValidation(){
    var fileInput = document.getElementById('fileupload');
    var filePath = fileInput.value;
    var allowedExtensions = /(\.png|.PNG|.jpg|.JPG)$/i;
    if(!allowedExtensions.exec(filePath)){
        alert('Please upload file having extension .png, .jpg, .PNG, .JPG only.');
        fileInput.value = '';
        return false;
    }else{
        //image preview
        if (fileInput.files && fileInput.files[0]) {
            var reader = new FileReader();
            reader.onload = function(e) {
                document.getElementById('uploadPreview').innerHTML = '<img src="'+e.target.result+'" style="width:90px;height:120px"/>';
            };
            reader.readAsDataURL(fileInput.files[0]);
        }
    }
  }
</script>        
