<?php 

if (empty($_GET['id']) || !is_numeric($_GET['id'])) {
    header('Location: ' . PT_Link('404'));
    exit();
}

$id       = PT_Secure($_GET['id']);
$article  = $db->where('id',$id)->getOne(T_POSTS);

if (empty($article)) {
    header('Location: ' . PT_Link('404'));
    exit();
}

$article->image = PT_GetMedia($article->image);

?>


<div class="container-fluid">
    <div class="block-header">
        <h2>Articles > Edit article</h2>
    </div>
    <!-- Vertical Layout -->
    <div class="row">
        <div class="col-lg-12 col-md-12">
            <div class="card">
                <div class="header">
                    <h2>Edit article</h2>
                </div>
                <div class="body">
                    <div class="create-article-alert">
                       
                    </div>
                    
                    <form class="form submit-new-article-settings" method="POST">
                        <div class="row">
                            <div class="col-md-8">
                                <div class="form-line">
                                <input type="text" class="form-control" name="title" value="<?php echo $article->title; ?>">
                                </div>
                                <div class="form-line">
                                    <textarea name="description" class="form-control"><?php echo $article->description; ?></textarea>
                                </div>
                                <div class="form-line">
                                    <textarea name="text" id="new-article"><?php echo PT_Decode($article->text); ?></textarea>
                                </div>
                                
                            </div>
                            <div class="col-md-4">
                                <div class="form-line">
                                    <div class="preview-article-image">
                                        <img src="<?php echo $article->image; ?>" alt="Post Image">
                                    </div>
                                </div>
                                <div class="form-line">
                                    <label for="article-tags" class="width-100">Category</label>
                                    <select name="category" class="form-control">
                                        <?php foreach ($pt->categories as $cat_id => $category): ?>
                                            <?php if ($cat_id == $article->category): ?>
                                                <option value="<?php echo $cat_id;?>" selected='selected'>
                                                    <?php echo $category;?>
                                                </option>
                                            <?php else: ?>
                                                <option value="<?php echo $cat_id;?>">
                                                    <?php echo $category;?>
                                                </option>
                                            <?php endif; ?>
                                        <?php endforeach; ?>
                                    </select>
                                    <div class="clearfix"></div>
                                </div>
                                <div class="form-line">
                                    <label for="article-tags" class="width-100">Tags</label>
                                    <div class="bootstrap-tagit-wrapper">
                                       <input id="article-tags" type="text" class="form-control" name="tags" value="<?php echo $article->tags; ?>"> 
                                    </div>
                                    <div class="clear"></div>
                                </div>
                                <div>
                                    <button type="submit" class="btn btn-primary m-t-15 waves-effect">Save &amp; Publish</button>
                                    <button type="submit" name="draft" class="btn btn-default m-t-15 waves-effect">Save Draft</button>
                                </div>
                            </div>
                        </div>
                        <input type="file" class="hidden" id="article-image" name="image">
                        <input type="hidden" name="id" value="<?php echo $id; ?>">
                    </form>
                </div>
            </div>
        </div>
        <div class="clearfix"></div>
    </div>
</div>


<script>
    jQuery(document).ready(function($) {
        tinymce.init({
          selector: '#new-article',  // change this value according to your HTML
          auto_focus: 'element1',
          height: 500,
          relative_urls: false,
          remove_script_host: false,
          toolbar: 'insertfile undo redo | styleselect | bold italic | alignleft aligncenter alignright alignjustify | bullist numlist outdent indent | link image  uploadImages |  preview media fullpage | forecolor backcolor emoticons',
          plugins: [
              'advlist autolink link image  lists charmap  preview hr anchor pagebreak spellchecker',
              'searchreplace wordcount visualblocks visualchars code fullscreen insertdatetime media nonbreaking',
              'save table contextmenu directionality emoticons template paste textcolor'
          ]
        });

        $(".preview-article-image").click(function(event) {
            $("#article-image").trigger('click');
        });

        $("#article-image").change(function(event) {
            var self       = $(this);
            var image_blob = false;

            if (window.URL !== undefined) {
                image_blob = window.URL.createObjectURL(self.prop('files')[0]);
            } 

            else if (window.webkitURL !== undefined) {
                image_blob = window.webkitURL.createObjectURL(self.prop('files')[0]);
            } 

            if (image_blob) {
              $(".preview-article-image").html("<img src='" + image_blob + "' alt='Picture'>");  
            }
            
        });

        $('#article-tags').tagsinput();
        var form = $(".submit-new-article-settings");
        form.submit(function(event) {
            var text    = tinymce.activeEditor.getContent({format: 'raw'});
            var message = $(".create-article-alert");
            if (!text){
                scrollToTop();
                message.html('<div class="alert alert-warning"><i class="fa fa-exclamation-circle"></i> Something went wrong Please try again!</div>');
                PT_Delay(function(){
                    window.location.reload();
                },1000);
                return false;
            }
            else{
                $("#new-article").val(text);
            }
        });

        form.ajaxForm({
            url: "{{LINK aj/ap/update-article}}",
            type:"POST",
            dataType:"json",
            data:{},
            beforeSend: function() {
                
            },
            success: function(data) {
                var message = $(".create-article-alert");
                if (data.status == 200) {
                    scrollToTop();
                    message.html('<div class="alert alert-success"><i class="fa fa-exclamation-circle"></i> New Article  has been successful saved!</div>');
                    PT_Delay(function(){
                        window.location.href = data.url;
                    },1000);
                }

                else if (data.status == 400) {
                    scrollToTop();
                    message.html('<div class="alert alert-warning"><i class="fa fa-exclamation-circle"></i> Please fill the required info!</div>');
                }

                else if (data.status == 401) {
                    scrollToTop();
                    message.html('<div class="alert alert-warning"><i class="fa fa-exclamation-circle"></i> Post title is too short!</div>');
                }

                else if (data.status == 402) {
                    scrollToTop();
                    message.html('<div class="alert alert-warning"><i class="fa fa-exclamation-circle"></i> Post description is too short!</div>');
                }

                else if (data.status == 403) {
                    scrollToTop();
                    message.html('<div class="alert alert-warning"><i class="fa fa-exclamation-circle"></i> Post text is required!</div>');
                } 

                else if (data.status == 404) {
                    scrollToTop();
                    message.html('<div class="alert alert-warning"><i class="fa fa-exclamation-circle"></i> The file is too big, please increase your server upload limit in php.ini!</div>');
                }

                else if (data.status == 405) {
                    scrollToTop();
                    message.html('<div class="alert alert-warning"><i class="fa fa-exclamation-circle"></i> The Image file is Not valid.please select a valid image!</div>');
                }

                else if (data.status == 406) {
                    scrollToTop();
                    message.html('<div class="alert alert-warning"><i class="fa fa-exclamation-circle"></i> Invalid Category id.Please check your details!</div>');
                }

                else if (data.status == 500) {
                    scrollToTop();
                    message.html('<div class="alert alert-danger"><i class="fa fa-exclamation-circle"></i> Error found, please try again later!</div>');
                }
            }
        });
    }); 
</script>