<?php $wo['blog_categories'] = blog_categories();?>
<div class="container-fluid">
    <div class="block-header">
        <h2>Blogs > Manage blog categories</h2>
    </div>
    <div class="row">
        <div class="col-lg-12 col-md-12">
            <div class="card">
                <div class="header">
                    <div class="row">
                        <div class="col-md-12" style="margin-bottom:0;">
                            <div class=" add_category_form_alert"></div>
                            <div class="row">
                                <div class="col-md-2">
                                    <button class="btn btn-info" id="add_new_category">Add new category</button>
                                </div>
                            </div>
                            <div class="clearfix"></div>
                        </div>
                    </div>
                </div>
                <div class="body">
                   <div class="table-responsive1">
                        <table class="table table-bordered table-striped table-hover">
                            <thead>
                                <tr>
                                    <th>ID</th>
                                    <th>Category Name</th>
                                    <th>Action</th>
                                </tr>
                            </thead>
                            <tbody>
                            <?php
                            $wo['category_id'] = 1;
                            foreach ($wo['blog_categories'] as $wo['category_key'] => $wo['category']) {
                                echo LoadAdminPage('manage-blog-categories/list',array('category_id' => $wo['category_id'], 'category_key' => $wo['category_key'] , 'category' => $wo['category'] ) );
                                $wo['category_id'] = $wo['category_id'] + 1;
                            }
                            ?>
                            </tbody>
                        </table>
                    </div>
                </div>
            </div>
        </div>
        <div class="clearfix"></div>
    </div>
    </div>
    <!-- #END# Vertical Layout -->
<script>
    $('.btn-lang').on('click', function(event) {
        $('#defaultModal .modal-body form .data').html('<div class="preloader pl-size-xl "><div class="spinner-layer pl-teal"><div class="circle-clipper left"><div class="circle"></div></div><div class="circle-clipper right"><div class="circle"></div></div></div></div>');
        var lang_id = $(this).attr('data-id');
        $.get('{{CONFIG ajax_url}}/ap/get_all_lang_key', {id: lang_id, lang_name: lang_id}, function(data, textStatus, xhr) {
            $('#defaultModal .modal-title').html('Edit key: ' + lang_id);
            $('#id_of_key').val(lang_id);
            $('#defaultModal .modal-body form .data').html(data.html);
        });
    });
    function Wo_SubmitLangForm() {
        $('.edit-key-settings').submit();
    }
    function Wo_SubmitAddCategoryForm() {
        $('.langsModalForm').submit();
    }
    $(function () {
        $('table').DataTable({
            "ordering": false
        });
        var form_lang_settings = $('form.edit-key-settings');
        form_lang_settings.ajaxForm({
            url: '{{CONFIG ajax_url}}/ap/update_lang_keys',
            beforeSend: function() {
                $('.btn-save').text('Please wait..');
            },
            success: function(data) {
                if (data.status == 200) {
                    $('.btn-save').text('SAVE CHANGES');
                    var value_to_use = $('[data-editable=1]').val();
                    var id_of_key = $('#id_of_key').val();
                    $('#edit_' + id_of_key).text(value_to_use);
                    $('#defaultModal').modal('hide');
                    location.reload();
                }
            }
        });

        var form_gender_settings = $('form.langsModalForm');
        form_gender_settings.ajaxForm({
            url: '{{CONFIG ajax_url}}/ap/add_new_blog_category',
            beforeSend: function() {
                $('.btn-save').text('Please wait..');
            },
            success: function(data) {

                if (data.status == 200) {
                    $('.btn-save').text('SAVE CHANGES');
                    $('#langsModal').modal('hide');
                    location.reload();
                }
                else{
                    $('.btn-save').text('SAVE CHANGES');
                    $('.langsModalAlert').html(data.message);
                }
                $("#langsModal").animate({ scrollTop: 0 }, 100);

            }
        });
    });

    $(document).on('click', '#add_new_category', function(event) {
        $('#langsModal').modal('show');
    });
    function Wo_DeleteCategory(key) {
        if (key == '') {
            return false;
        }
        if (!confirm('Are you sure you want to delete this category?')) {
            return false;
        }

        $.get('{{CONFIG ajax_url}}/ap/delete_blog_category', {key:key}, function (data) {
            if(data.status == 300){
                alert('You can not delete this record.');
            }else{
                $('#' + key).fadeOut(300, function () {
                    $(this).remove();
                });
            }
        });
    }
</script>
<div class="modal fade" id="defaultModal" tabindex="-1" role="dialog">
    <div class="modal-dialog" role="document">
        <div class="modal-content ">
            <div class="modal-header">
                <h4 class="modal-title" id="defaultModalLabel"></h4>
            </div>
            <div class="modal-body">
                <div class="alert alert-success edit-lang-settings-alert"></div>
                <form class="edit-key-settings" method="POST">
                    <div class="data"></div>
                    <input type="hidden" name="hash_id" value="<?php echo createMainSession();?>">
                    <input type="hidden" name="id_of_key" id="id_of_key" value="">
                </form>
            </div>
            <div class="modal-footer">
                <button type="button" onclick="Wo_SubmitLangForm();" class="btn-save btn btn-link waves-effect">SAVE CHANGES</button>
                <button type="button" class="btn btn-link waves-effect" data-dismiss="modal">CLOSE</button>
            </div>
        </div>
    </div>
</div>


<div class="modal fade" id="langsModal" tabindex="-1" role="dialog">
    <div class="modal-dialog" role="document">
        <div class="modal-content ">
            <div class="modal-header">
                <h4 class="modal-title" id="langsModalLabel"></h4>
            </div>
            <div class="modal-body">
                <div class="alert alert-danger langsModalAlert"></div>
                <form class="langsModalForm" method="POST">
                    <?php
                    $langs = db_langs();
                        unset($langs[0]);
                        unset($langs[1]);
                        unset($langs[2]);
                        unset($langs[3]);

                     foreach ($langs as $wo['key_']) { ?>
                        <div class="form-group">
                            <div class="form-lins">
                                <label class="form-lasbel"><?php echo ucfirst($wo['key_']); ?></label>
                                <input type="text"  name="<?php echo $wo['key_']; ?>" id="<?php echo $wo['key_']; ?>" class="form-control" style="border: 1px solid #ccc;"></input>
                            </div>
                        </div>
                    <?php } ?>
                    <div class="data"></div>
                    <input type="hidden" name="hash_id" value="<?php echo createMainSession();?>">
                </form>
            </div>
            <div class="modal-footer">
                <button type="button" onclick="Wo_SubmitAddCategoryForm();" class="btn-save btn btn-link waves-effect">SAVE CHANGES</button>
                <button type="button" class="btn btn-link waves-effect" data-dismiss="modal">CLOSE</button>
            </div>
        </div>
    </div>
</div>