<?php 
    include('public/fcm.php');
    require_once("public/thumbnail_images.class.php");
    include_once('functions.php');

    if(isset($_POST['submit'])) {
        $news_text = $_POST['news_title'];
        if ($_FILES['post_image']['name'] != '') {
            $news_image = time().'_'.$_FILES['post_image']['name'];
            $pic2            = $_FILES['post_image']['tmp_name'];
            $tpath2          = 'upload/'.$news_image;
            copy($pic2, $tpath2);
        } else {
            $news_image = '';
        }
        if ($_FILES['post_video']['name'] != '') {
            $news_video = time().'_'.$_FILES['post_video']['name'];
            $pic2            = $_FILES['post_video']['tmp_name'];
            $tpath2          = 'upload/'.$news_video;
            copy($pic2, $tpath2);
        } else {
            $news_video = '';
            
        }
        $data = array(
            'cat_id'            => $_POST['cat_id'],
            'news_title'        => $news_text,
            'news_image'        => $news_image,
            'news_video'        => $news_video,
            'content_type'      => $_POST['upload_type']
            );

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

        $_SESSION['msg'] = "";
        header( "Location:add-news.php?q=".$_POST['cat_id']);
        exit;

    }

    $sql_category = "SELECT * FROM tbl_category ORDER BY cid DESC";
    $category_result = mysqli_query($connect, $sql_category);
?>

<script type="text/javascript">

    $(document).ready(function(e) {

        $("#upload_type").change(function() {
            var type = $("#upload_type").val();

                if (type == "Text") {
                    $("#video_post").hide();
                    $("#image_post").hide();
                    $("#text_post").show();
                }

                if (type == "Photo") {
                    $("#text_post").hide();
                    $("#video_post").hide();
                    $("#image_post").show();
                }
                if (type == "Video") {
                    $("#text_post").hide();
                    $("#image_post").show();
                    $("#video_post").show();
                }
        });

        $( window ).load(function() {
        var type=$("#upload_type").val();
            if (type == "Text") {
                    $("#video_post").hide();
                    $("#image_post").hide();
                    $("#text_post").show();
                }
                if (type == "Photo") {
                    $("#text_post").hide();
                    $("#video_post").hide();
                    $("#image_post").show();
                }
                if (type == "Video") {
                    $("#text_post").hide();
                    $("#image_post").show();
                    $("#video_post").show();
                }

        });

    });

</script>

   <section class="content">
   
        <ol class="breadcrumb">
            <li><a href="dashboard.php">Dashboard</a></li>
            <li><a href="manage-news.php">Manage Shayari</a></li>
            <li class="active">Add Shayari</a></li>
        </ol>

       <div class="container-fluid">

            <div class="row clearfix">
                <div class="col-lg-12 col-md-12 col-sm-12 col-xs-12">

                	<form id="form_validation" method="post" enctype="multipart/form-data">
                    <div class="card">
                        <div class="header">
                            <h2>ADD SHAYARI</h2>
                                <?php if(isset($_SESSION['msg'])) { ?>
                                    <br><div class='alert alert-info'>Added Successfully...</div>
                                    <?php unset($_SESSION['msg']); } ?>
                        </div>
                        <div class="body">
                        	<div class="row clearfix">
                                <div class="col-sm-6">
                                    <div class="form-group">
                                        <div class="font-12">Category *</div>
                                        <select class="form-control show-tick" name="cat_id" id="cat_id">
                                            <?php while ($data = mysqli_fetch_array ($category_result)) { ?>
                                            <option value="<?php echo $data['cid'];?>" <?php if($_GET['q']==$data['cid']){echo 'selected';}?>><?php echo $data['category_name'];?></option>
                                                <?php } ?>
                                        </select>
                                    </div>
                                </div>
                                <div class="col-sm-6">
                                    <div class="form-group">
                                        <div class="font-12">Content Type *</div>
                                        <select class="form-control show-tick" name="upload_type" id="upload_type">
                                                <option value="Text">Text</option>
                                                <option value="Photo">Photo</option>
                                                <option value="Video">Video</option>
                                        </select>
                                    </div>
                                    </div>
                                    <div class="col-sm-6">
                                        <div id="text_post">
                                    <div class="form-group">
                                        <div class="font-12">Shayari</div>
                                        <div class="form-line">
                                            <textarea class="form-control" name="news_title" id="news_title" placeholder="Shayari Text"></textarea>
                                        </div>
                                    </div>
                                    </div><div class="col-sm-12">
                                    <div class="col-sm-6">
                                    <div id="image_post">
                                        <div class="font-12 ex1">Image Primary</div>
                                        <div class="form-group">
                                            <input type="file" name="post_image" id="post_image" class="dropify-image" data-max-file-size="2M" data-allowed-file-extensions="jpg jpeg png gif"/>
                                        </div>
                                    </div></div>
                                    <div class="col-sm-6">
                                    <div id="video_post">
                                        <div class="font-12 ex1">Video Primary</div>
                                        <div class="form-group">
                                            <input type="file" name="post_video" id="post_video" class="dropify-image" data-max-file-size="10M" data-allowed-file-extensions="mp4 mkv 3gp 3gpp avi"/>
                                        </div>
                                    </div></div></div>
                                    <button type="submit" name="submit" class="btn bg-blue waves-effect pull-right">PUBLISH</button>
                                    
                                </div>

                            </div>
                        </div>
                    </div>
                    </form>

                </div>
            </div>
            
        </div>

    </section>