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

    <div class="col-md-4">
        <form action="/admin/ajax/addEditCategories" class="form" id="validation" method="post" enctype="multipart/form-data">
            <input type="hidden" value="<?= $id ?>" name="id" />
            
        <?php foreach ($lang 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" name="theTitle" placeholder="<?=$this->Get_Lang->gl('the_title',$adminLang)?> <?=$value->theName?>" />
            </div>
        <?php } ?>   
        <?php foreach ($lang as $key => $value) { ?>
            <div class="form-group">
                <h3><?=$value->theName?> URL</h3>
                <input id="url" name="url" class="form-control" placeholder="<?=$value->theName?> URL"/>
            </div>
        <?php } ?>   
        <?php foreach ($lang as $key => $value) { ?>
            
            <div class="form-group">
                <h3><?=$this->Get_Lang->gl('page_title',$adminLang)?> <?=$value->theName?></h3>
                <input id="PageTitle" name="PageTitle" class="form-control" placeholder=" <?=$this->Get_Lang->gl('page_title',$adminLang)?> <?=$value->theName?>"/>
            </div>     
        <?php } ?>   
        <?php foreach ($lang as $key => $value) { ?>
            
            <div class="form-group">
                <h3><?=$this->Get_Lang->gl('page_description',$adminLang)?> <?=$value->theName?></h3>
                <textarea id="pageDesc" name="pageDesc" class="form-control"> </textarea>
            </div>
        <?php } ?>   
            
<!--            <div class="form-group">
                <h3>Page Trace Code</h3>
                <textarea id="traceCode" name="traceCode" class="form-control"> </textarea>
            </div>-->
            
<!--             <div class="form-group">

                <h3 class="titleSelectPhoto">The photo<br><br><span style="color: #c70600; font-size: 15px;">! يرجى التقيد بقياس الصور للحصول على أفضل نتيجة</span></h3>
                <div class="col-md-12">
                    <div class="row">
                        <span class="btn btn-success fileinput-button">
                            <i class="glyphicon glyphicon-plus"></i>
                            <span>Select photo...(W:1600px - H:700px)</span>
                            <input id="fileupload" type="file" name="files[]" />
                        </span>

                        <div class="clearfix"><br></div>

                        <div id="progress" class="progress">
                            <div class="progress-bar progress-bar-success"></div>
                        </div>

                        <div id="files" class="files">

                        </div>
                        <div class="clearfix"><br></div>

                        <input id="file" type="hidden" name="file" value="" />

                        <?php
                        if ($thePhoto != NULL) {
                            ?>
                            <h2>Current Photo</h2>
                            <img width="300px" src="/public/uploads/php/files/categories/<?= $thePhoto ?>">
                            <?php
                        }
                        ?>
                    </div>
                </div>

                <script>
                    $(document).ready(function () {
                        $(function () {
                            'use strict';
                            $('#progress').hide();
                            var url = "/public/uploads/php/?pageType=categories";
                            $('#fileupload').fileupload({
                                url: url,
                                dataType: 'json',
                                done: function (e, data) {
                                    $.each(data.result.files, function (index, file) {
                                        //                $("#files").append("<div class='hover'><img src='/public//uploads/php/files/thumbnail/"+file.name+"' /><i data="+file.name+" class='glyphicon glyphicon-remove deleteImage'></i></div>");
                                        $("#files").append("<div class='photoPreview'><img width='300px' src='/public/uploads/php/files/categories/" + file.name + "' /></div>");

                                        if ($("#file").val() == "")
                                        {
                                            $("#file").val(file.name);
                                        } else {
                                            $("#file").val($("#file").val() + "|" + file.name);
                                        }
                                    });
                                },
                                progressall: function (e, data) {
                                    var progress = parseInt(data.loaded / data.total * 100, 10);
                                    $('#progress .progress-bar').css(
                                            'width',
                                            progress + '%'
                                            ).html(progress + '%');
                                }
                            }).bind('fileuploadstart', function (e) {
                                $('#progress').show();
                            }).bind('fileuploadstop', function (e) {
                                $('#saveBtn').show();
                                $('#progress .progress-bar').html("<?=$this->Get_Lang->gl('all_files_has_been_uploaded',$adminLang)?>");
                            });
                        });
                    });

                </script>
            </div>-->
            
            <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/admin/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.categories").addClass("active");
        $(".sidebar-dropdown.two").addClass("active");
        $(".sidebar-wrapper .sidebar-menu ul li a.categories").addClass("active");
        $(".sidebar-dropdown.two .sidebar-submenu").fadeIn();          
                
                    $(".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: {
                                'theTitle': {required: true},
                                'langId': {required: true},
                                'url': {required: true},
                            },
                            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 > 0) {

                                $('#theTitle').val('');
                                $('#pageDesc').val('');
                                $('#PageTitle').val('');
                                $('#url').val('');
                                $('#traceCode').val('');
                                $("#saveBtn").prop('disabled', true);
                                setTimeout(function () {
                                    $("#saveBtn").prop('disabled', false);
                                }, 3000);

                                common.showAlert("<?=$this->Get_Lang->gl('job_done_successfully',$adminLang)?>", "success", 3);
                                <?php if($id == 0){ ?>
                                window.open("/admin/categories/"+response, "_self");
                                <?php }else{ ?>
                                window.open("/admin/categories/<?=$id?>", "_self");
                                <?php } ?>
                            } else {
                                common.showAlert("<?=$this->Get_Lang->gl('error',$adminLang)?>", "success", 3);
                            }
                        }
                    });



                    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>
