<div class="row">
    <div class="col-lg-12">
        <h1 class="page-header"><?= $pageTitle ?></h1>
    </div>
</div>
<div class="row">

    <div class="col-md-6">
        <form action="/admin/ajax/addEditBlog/<?=$categoryId?>" class="form" id="validation" method="post" enctype="multipart/form-data">
            <input type="hidden" value="<?= $id ?>" name="id" />
            <input type="hidden" value="<?= $categoryId ?>" name="categoryId" />


            <?php foreach ($databaseLang as $key => $value) { ?>
            <div class="form-group">
                <h3><?=$this->Get_Lang->gl('the_title',$adminLang)?> - <?=$value->theName?></h3>
                <input class="form-control" type="text" id="theTitle<?=$value->laId?>" name="theTitle<?=$value->laId?>" placeholder="<?=$this->Get_Lang->gl('the_title',$adminLang)?> - <?=$value->theName?>" />
            </div>
            
            <div class="form-group">
                <h3><?=$this->Get_Lang->gl('the_text',$adminLang)?> - <?=$value->theName?></h3>
                <textarea id="theText<?=$value->laId?>" name="theText<?=$value->laId?>" class="form-control"></textarea>
            </div>
            <?php } ?>
            
            <div class="form-group">
                <h3>URL</h3>
                <input id="url" name="url" class="form-control" placeholder="URL"/>
            </div>                    
             
<div class="form-group">

    <h2 class="titleSelectPhoto"> <?=$this->Get_Lang->gl('the_photo',$adminLang)?> </h2>
    <div class="col-md-12">
            <div class="col-md-9" >
                <input type="text" type="hidden" name="file" id="fieldID" class="form-control"
                       <?php if($thePhoto){ echo "value='".$thePhoto."'"; }else{echo 'value=""';} ?> placeholder="URL" >
            </div>
            <div class="col-md-3">
                <a  href="javascript:open_popup('/public/lib/responsivefilemanager/filemanager/dialog.php?type=1&popup=1&multiple=0&field_id=fieldID')" class="btn btn-primary" type="button">
                    <?=$this->Get_Lang->gl('select_photo',$adminLang)?>...(W:800px - H:425px)
                </a>
            </div>
         <?php
        if ($thePhoto != NULL) { ?>
            <br><h3 style="margin-top:50px;"><?=$this->Get_Lang->gl('current_photo',$adminLang)?></h3>
            <img width='300px' src="<?= $thePhoto ?>">
        <?php  }  ?>
    </div>
    <br>
</div>
<script type="text/javascript">
function open_popup(url)
{
        var w = 880;
        var h = 570;
        var l = Math.floor((screen.width-w)/2);
        var t = Math.floor((screen.height-h)/2);
        var win = window.open(url, 'ResponsiveFilemanager', "scrollbars=1,width=" + w + ",height=" + h + ",top=" + t + ",left=" + l);
}

</script>
            
            <div class="form-group">
                <button id="saveBtn" class="btn btn-primary pull-left" type="submit"><?=$this->Get_Lang->gl("save",$adminLang)?></button>
            </div>
        </form>
    </div>

</div>
<script src="/public/lib/validation/jquery.validate.js"></script>
<script src="/public/js/jquery.form.js"></script>
<script src="/public/lib/validation/localization/messages_<?=$tlang?>.js" type="text/javascript"></script>
<script src="/public/lib/validation/localization/messages_<?=$tlang?>.min.js" type="text/javascript"></script>

<script>


       $(".nav>li>a.blog").addClass("active");    
       $(".sidebar-dropdown.one").addClass("active");
       $(".sidebar-wrapper .sidebar-menu ul li a.blog").addClass("active");
       $(".sidebar-dropdown.one .sidebar-submenu").fadeIn();  

<?php if($id != 1){ ?><?php foreach ($databaseLang as $key => $value) { ?> 
                    CKEDITOR.replace("theText<?=$value->laId?>", {
                        height: '280px',
                        skin: 'bootstrapck',                        
                        removePlugins: 'elementspath',
                        resize_enabled: false,
                        
                        extraPlugins : 'uploadimage',
                        filebrowserBrowseUrl : '/public/lib/kcfinder-3.12/browse.php',
                        filebrowserImageBrowseUrl : '/public/lib/kcfinder-3.12/browse.php?type=Images',
                        filebrowserUploadUrl : '/public/lib/kcfinder-3.12/upload.php',
                        filebrowserImageUploadUrl : '/public/lib/kcfinder-3.12/upload.php?type=Images',
                        filebrowserUploadMethod : 'form'
                    });
<?php } } ?>
                    $(".form").each(function () {
                        $(this).validate({
                            highlight: function (element) {
                                $(element).closest('.form-group').removeClass('has-success').addClass('has-error');
                            },
                            unhighlight: function (element) {
                                $(element).closest('.form-group').removeClass('has-error').addClass('has-success');
                            },
                            ignore: [],
                            rules: {
                                'url': {required: true},
                                <?php foreach ($databaseLang as $key => $value) { ?>
                                'theTitle<?=$value->laId?>': {required: true},
                                'theText<?=$value->laId?>': {
                                    required: function () {
                                        CKEDITOR.instances.theText<?=$value->laId?>.updateElement();
                                    }
                                },
                                <?php } ?>
                                
                                <?php if ($id == 0) { ?>
                                      'file': {required: true},  
                                <?php } ?>        
                            },
                            errorPlacement: function (error, element) {
                                error.insertAfter(element);
                            }
                        });
                    });


                    $(".form").ajaxForm({
                        beforeSubmit: function (arr, $form, options) {
                            $activeForm = $form;
                            if (!$form.valid())
                                return false;
                        },
                        complete: function (xhr) {
                            var response = xhr.responseText;
                            if (response == 1) {

                                $('#url').val('');
                                 <?php foreach ($databaseLang as $key => $value) { ?>
                                $('#theTitle<?=$value->laId?>').val('');
                                CKEDITOR.instances.theText<?=$value->laId?>.setData('');                                
                                <?php } ?>
                                $("#saveBtn").prop('disabled', true);
                                setTimeout(function () {
                                    $("#saveBtn").prop('disabled', false);
                                }, 3000);

                                common.showAlert("<?=$this->Get_Lang->gl('job_done_successfully',$adminLang)?>", "success", 3);
                                window.open("/admin/blog/<?=$categoryId?>", "_self");
                            } else {
                                common.showAlert("<?=$this->Get_Lang->gl('error',$adminLang)?>"+response, "success", 3);
                                $("html, body").animate({ scrollTop: 0 }, "slow");
                            }
                        }
                    });



                    function fillTheForm(frm, data) {
                        $.each(data, function (key, value) {
                            var $ctrl = $('[name=' + key + ']', frm);
                           if ($('[name=' + key + ']').length > 0){                            
                            switch ($ctrl.attr("type"))
                            {
                                case "text" :
                                case "hidden":
                                    $ctrl.val(value)
                                    break;
                                case "radio" :
                                case "checkbox":
                                    $ctrl.each(function () {
                                        if ($(this).attr('value') == value) {
                                            $(this).attr("checked", value);
                                        }
                                    });
                                    break;
                                default:
                                    if (!$ctrl.length)
                                    {
                                        var array = $.map(value, function (v, i) {
                                            return [v];
                                        });
                                        if (array.length > $("[name='" + key + "[]']", frm).length)
                                        {
                                            addCount = array.length - $("[name='" + key + "[]']", frm).length;
                                            for (var i = 0; i < addCount; i++) {
                                                $("[name='" + key + "[]']", frm).parents(".exercise-type-div").find(".addNew").trigger("click");
                                            }
                                        }
                                        $("[name='" + key + "[]']", frm).each(function (index) {
                                            $(this).val(array[index]);
                                        });
                                    } else {
                                        $ctrl.val(value);
                                    }
                            }
                            $ctrl.change();
                        }
                        });
                    }
<?php if ($id && $id != 0) { ?>
                        var Vdata =<?= $values ?>;
                        fillTheForm($('.form'), Vdata);
<?php } ?>

</script>
