<?php 
session_start();
$title = "Article Setup";
$group = "cms";
$menu = "article";
include('header.php');

$status="";
$title="";
$photo="";
$category = "";
$body="";
$author = "";
$date_created = date('d-m-Y');
if(isset($_POST['articleid']))
{
    $_SESSION['articleid']=$_POST['articleid'];

    $sql="select * from news where id=:id";
    $value=array(':id'=>$_SESSION['articleid']);
    $r=$con->select_query($sql,$value);
    foreach($r as $value)
    {
        $status=$value['is_published'];
        $body=$value['body'];
        $title=$value['title'];
        $category=$value['category_id'];
        $photo = $value['featured_image'];
        $date_created = $value['date_created'];
        $author = $r['author'];
        $_SESSION['isEdit']=true;
    }
}
?>
<script type="text/javascript" src="../tinymce/tinymce.min.js"></script>
<script type="text/javascript">
                     
                     
                        tinymce.init({
                            selector: "textarea",
                            theme: "modern",
                            plugins: [
                                "advlist autolink lists link image charmap print preview hr anchor pagebreak",
                                "searchreplace wordcount visualblocks visualchars code fullscreen",
                                "insertdatetime media nonbreaking save table contextmenu directionality",
                                "emoticons template paste textcolor colorpicker textpattern imagetools"
                            ],
                            toolbar1: "insertfile undo redo | styleselect | bold italic | alignleft aligncenter alignright alignjustify | bullist numlist outdent indent | link image",
                            toolbar2: "print preview media | forecolor backcolor emoticons",
                            image_advtab: true,
                            templates: [
                                {title: 'Test template 1', content: 'Test 1'},
                                {title: 'Test template 2', content: 'Test 2'}
                            ]
                        });
                        
                        </script>
<script>

function SaveInsert()
{
	var ed = tinyMCE.get('alt_body');
	//alert(ed.getContent());
	$('#bodytxt').val(ed.getContent());
	
	var fileUpload = $("#photo").get(0);
    var files = fileUpload.files;
    var data = new FormData();
    for (var i = 0; i < files.length ; i++) {
        data.append('photo',files[i],files[i].name);
    }
    
	var datastring = $("#newsform").serializeArray();
	$.each(datastring,function(key,input){
        data.append(input.name,input.value);
    });
	
	var l = $( '.ladda-button-demo1' ).ladda();
	l.ladda( 'start');	
	$.ajax({
	            type: "POST",
	            url: "../include/insert_ajax.php",
	            contentType: false,
                processData: false,
	            data: data,
	            dataType: 'json',
	            cache: false,
	            success: function(data) {
	            	l.ladda( 'stop' );
	            	if(data.success == 1)
	            	{
	            		setTimeout(function() {
	                        toastr.options = {
	                            closeButton: true,
	                            progressBar: true,
	                            positionClass: "toast-top-full-width",
	                            showMethod: 'slideDown',
	                            timeOut: 4000
	                        };
	                        toastr.success('Article added successfully');
	                        $("input[type='text'], textarea, input[type='password'], input[type='number']").val("");
	                    }, 1300);
	            	}
	            	else if(data.success == 3)
	            	{
	            		setTimeout(function() {
	                        toastr.options = {
	                            closeButton: true,
	                            progressBar: true,
	                            positionClass: "toast-top-full-width",
	                            showMethod: 'slideDown',
	                            timeOut: 4000
	                        };
	                        toastr.error('Error, please enter required fields');

	                    }, 1300);
	            	}
	            	if(data.file_error == 1)
	            	{
	            		setTimeout(function() {
	                        toastr.options = {
	                            closeButton: true,
	                            progressBar: true,
	                            positionClass: "toast-top-full-width",
	                            showMethod: 'slideDown',
	                            timeOut: 4000
	                        };
	                        toastr.error('Photo not uploaded. Photo must be jpg, jpeg, png or gif format. ');

	                    }, 1300);
	            	}
	            	else if(data.file_error == 2)
	            	{
	            		setTimeout(function() {
	                        toastr.options = {
	                            closeButton: true,
	                            progressBar: true,
	                            positionClass: "toast-top-full-width",
	                            showMethod: 'slideDown',
	                            timeOut: 4000
	                        };
	                        toastr.error('No Featured Photo Uploaded!');

	                    }, 1300);
	            	}
	            	else if(data.file_error == 3)
	            	{
	            		setTimeout(function() {
	                        toastr.options = {
	                            closeButton: true,
	                            progressBar: true,
	                            positionClass: "toast-top-full-width",
	                            showMethod: 'slideDown',
	                            timeOut: 10000
	                        };
	                        toastr.error('File size too large! Photo size limit is 500kb');

	                    }, 1300);
	            	}
	            },
	            error: function(){
	                  alert('error handling here');
	                  l.ladda( 'stop' );
	            }
	        });
}

function SaveUpdate()
{
	var ed = tinyMCE.get('alt_body');
	//alert(ed.getContent());
	$('#bodytxt').val(ed.getContent());
	
	var fileUpload = $("#photo").get(0);
    var files = fileUpload.files;
    var data = new FormData();
    for (var i = 0; i < files.length ; i++) {
        data.append('photo',files[i],files[i].name);
    }
    
	var datastring = $("#newsform").serializeArray();
	$.each(datastring,function(key,input){
        data.append(input.name,input.value);
    });	
	var l = $( '.ladda-button-demo2' ).ladda();
	l.ladda( 'start');
	$.ajax({
	            type: "POST",
	            url: "../include/update_ajax.php",
	            contentType: false,
                processData: false,
	            data: data,
	            dataType: 'json',
	            cache: false,
	            success: function(data) {
	            	l.ladda( 'stop' );
	            	if(data.success == 1)
	            	{
	            		setTimeout(function() {
	                        toastr.options = {
	                            closeButton: true,
	                            progressBar: true,
	                            positionClass: "toast-top-full-width",
	                            showMethod: 'slideDown',
	                            timeOut: 4000
	                        };
	                        toastr.success('Article saved successfully');
	                    }, 1300);
	            	}
	            	else if(data.success == 3)
	            	{
	            		setTimeout(function() {
	                        toastr.options = {
	                            closeButton: true,
	                            progressBar: true,
	                            positionClass: "toast-top-full-width",
	                            showMethod: 'slideDown',
	                            timeOut: 4000
	                        };
	                        toastr.error('Error, please enter required fields');

	                    }, 1300);
	            	}
	            	if(data.file_error == 1)
	            	{
	            		setTimeout(function() {
	                        toastr.options = {
	                            closeButton: true,
	                            progressBar: true,
	                            positionClass: "toast-top-full-width",
	                            showMethod: 'slideDown',
	                            timeOut: 4000
	                        };
	                        toastr.error('Photo not uploaded. Photo must be jpg, jpeg, png or gif format. ');

	                    }, 1300);
	            	}
	            	else if(data.file_error == 2)
	            	{
	            		setTimeout(function() {
	                        toastr.options = {
	                            closeButton: true,
	                            progressBar: true,
	                            positionClass: "toast-top-full-width",
	                            showMethod: 'slideDown',
	                            timeOut: 4000
	                        };
	                        toastr.error('No Featured Photo Uploaded!');

	                    }, 1300);
	            	}
	            },
	            error: function(){
	                  alert('error handling here');
	                  l.ladda( 'stop' );
	            }
	        });
}
</script>
<script>
$(document).ready(function(){
	$('#data_5 .input-daterange').datepicker({
	    keyboardNavigation: false,
	    forceParse: false,
	    autoclose: true
	});
})
</script>
   <style>
.input-daterange input {
    text-align: left;
}
</style> 

<div class="content-body">
        	<div class="row">
                    <div id="recent-transactions" class="col-12">
                        <div class="card">
                            <div class="card-header">
                                <h4 class="card-title">Article Setup</h4>
                            </div>
                            <div class="card-content">
                            	<form method="post" action="article_setup" id="newsform" enctype="multipart/form-data">
                                <div class="row row-pad">
                                    <div class="col-md-2">
                                        <label for="name">Date Created</label>
                                    </div>
                                    <div class="col-md-10" id="data_5">
                                        <div class="input-daterange input-group" id="datepicker" style="width: 100%">
                                            <input type="text" class="form-control" name="date_created" value="<?php echo $date_created?>"/>
                                        </div>
                                    </div>
                                </div>
                                <div class="row row-pad">
                                    <div class="col-md-2">
                                        <label for="name">Title</label>
                                    </div>
                                    <div class="col-md-10">
                                        <input type="text" name="title" class="form-control" value="<?php echo $title;?>" required/>
                                    </div>
                                </div>
                                
                                <div class="row row-pad">
                                    <div class="col-md-2">
                                        <label for="name">Author</label>
                                    </div>
                                    <div class="col-md-10">
                                        <input type="text" name="author" class="form-control" value="<?php echo $author;?>" required/>
                                    </div>
                                </div>
                                
                                <div class="row row-pad">
                                    <div class="col-md-2">
                                        <label for="name">Body</label>
                                    </div>
                                    <div class="col-md-10">
                                        <input type="hidden" name="body" id="bodytxt"/>
                                        <textarea name="alt_body" id="alt_body" style="height: 300px"><?php echo $body?></textarea>
                                    </div>
                                </div>
                                
                                <div class="row row-pad">
                                    <div class="col-md-2">Featured Image</div>
                                    <div class="col-md-6">
                                        <div class="fileupload fileupload-new" data-provides="fileupload">
                                                  <div class="fileupload-new thumbnail" style="width: 200px; height: 150px;">
                                                        <?php 
                                                            if($photo != "")
                                                            {
                                                                echo '<img src="'.UPLOADS_FOLDER.$photo.'" alt="" />';
                                                            }
                                                            else 
                                                            {
                                                                echo '<img src="http://www.placehold.it/200x150/EFEFEF/AAAAAA&amp;text=no+image" alt="" />';
                                                            }
                                                        ?>
                                                      
                                                  </div>
                                                  <div class="fileupload-preview fileupload-exists thumbnail" style="max-width: 200px; max-height: 150px; line-height: 20px;"></div>
                                                  <div>
                                                   <span class="btn btn-white btn-file">
                                                   <span class="fileupload-new"><i class="icon-paper-clip"></i> Select image</span>
                                                   <span class="fileupload-exists"><i class="icon-undo"></i> Change</span>
                                                   <input type="hidden" name="oldphoto" value="<?php echo $photo?>"/>
                                                   <input type="file" id="photo" name="photo" class="default" />
                                                   </span>
                                                      <a href="#" class="btn btn-danger fileupload-exists" data-dismiss="fileupload"><i class="icon-trash"></i> Remove</a>
                                                  </div>
                                              </div>
                                    </div>
                                </div>
                                
                                <div class="row row-pad">
                                    <div class="col-md-2">
                                        <label for="name">Category</label>
                                    </div>
                                    <div class="col-md-6">
                                        <select name="category" id="categoryid" class="form-control">
                                            <option value="">--Select category--</option>
                                            <?php 
                                                $sql = "select id,name from news_category where status=1";
                                                $q=$con->select_query($sql);
                                                foreach($q as $r)
                                                {
                                                    echo '<option value="'.$r['id'].'">'.$r['name'].'</option>';
                                                }
                                            ?>
                                        </select>
                                        <?php 
                                            if(!empty($body))
                                            {
                                                echo '<script>document.getElementById("categoryid").value="'.$category.'"</script>';
                                            }
                                        ?>
                                    </div>
                                </div>
                                          
                                <div class="row row-pad">
                                    <div class="col-md-2">
                                        
                                    </div>
                                    <div class="col-md-6">
                                        <label> <input type="checkbox" id="status" name="status" <?php echo ($status==1 ? "checked" : "")?>/> Publish </label>
                                    </div>
                                </div>
                               
                                <div class="row row-pad">
                                    <div class="col-md-2">
                                        
                                    </div>
                                    <div class="col-md-6">
                                    <?php 
                                        if($_SESSION['isEdit']==true){
                                            echo '
                                            <input type="hidden" name="update" value="article_setup"/>
                                            <button type="button" onclick="SaveUpdate()" class="ladda-button ladda-button-demo2 btn btn-primary"  data-style="zoom-in">Update Article</button>';
                                        } else {
                                            echo '
                                            <input type="hidden" name="insert" value="article_setup"/>
                                            <button type="button" onclick="SaveInsert()" class="ladda-button ladda-button-demo1 btn btn-primary"  data-style="zoom-in">Add Article</button>';
                                        }
                                    ?>
                                        <a href="article_list" class="btn btn-warning">Cancel</a>
                                    </div>
                                </div>
                                </form>
                            </div>
                        </div>
                    </div>
                </div>
                <!--/ Recent Transactions -->

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

   
