<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/addEditAtranslate" class="form" id="validation" method="post" enctype="multipart/form-data">
            <input type="hidden" value="<?= $id ?>" name="id" />
            <input type="hidden" value="<?=$isAdmin ?>" name="isAdmin" />


            <div class="form-group">
                <h3><?=$this->Get_Lang->gl('define_word',$adminLang)?></h3>
                <input class="form-control" type="text" id="define" name="define" placeholder="<?=$this->Get_Lang->gl('define_word',$adminLang)?>" />
            </div>          

            <div class="form-group">
                <h3>Arabic</h3>
                <input class="form-control" type="text" id="ar" name="ar" placeholder="Arabic" />
            </div>          

            <div class="form-group">
                <h3>English</h3>
                <input class="form-control" type="text" id="en" name="en" placeholder="English" />
            </div>          

            <div class="form-group">
                <h3>Turkish</h3>
                <input class="form-control" type="text" id="tr" name="tr" placeholder="Turkish" />
            </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/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>
                     $(".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: {
                                'define': {required: true},
                                'ar': {required: true},
                                'en': {required: true},
                                'tr': {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 == 1) {

                                $('#define').val('');
                                $('#ar').val('');
                                $('#en').val('');
                                $('#tr').val('');
                                $("#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/atranslate/<?=$isAdmin?>", "_self");
                            } else {
                                common.showAlert("<?=$this->Get_Lang->gl('error',$adminLang)?> <br>" + response, "danger", 0);
                                $("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>
