<?php 

$langs = pt_db_langs();

if (empty($_GET['id']) || !in_array($_GET['id'], $langs)) {
    header("Location: " . PT_Link('admin-cp'));
    exit();
}

?>

<div class="container-fluid">
    <div class="block-header">
        <h2>Languages > Manage Languages</h2>
    </div>
    <!-- Vertical Layout -->
    <div class="row">
        <div class="col-lg-12 col-md-12">
            <div class="card">
                <div class="header">
                    <h2>Manage & Edit Languages</h2>
                </div>
                <div class="body">
                   <div class="table-responsive1">
                        <table class="table table-bordered table-striped table-hover">
                            <thead>
                                <tr>
                                	<th>ID</th>
                                    <th>Key Name</th>
                                    <th>Value</th>
                                    <th>Action</th>
                                </tr>
                            </thead>
                            <tbody>
                                <?php
					            $langs = pt_get_langs($_GET['id']);
					            if (count($langs) > 0) {
					            	$count = 1;
					                foreach ($langs as $lang_key => $lang_value) {

					                    echo PT_LoadAdminPage('edit-lang/list',array(
                                            "LANG_ID" => ($count++),
                                            "LANG_KEY" => $lang_key,
                                            "LANG_VALUE" => $lang_value
                                        ));
					                }
					            }  
					            ?>
                            </tbody>
                        </table>
                    </div>
                </div>
            </div>
        </div>
        <div class="clearfix"></div>
    </div>
    <!-- #END# Vertical Layout -->
<script>
$('.btn-lang').on('click', function(event) {
	$('.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("{{LINK aj/ap/get_lang_key}}",{id: lang_id, lang_name: '<?php echo $_GET['id']; ?>'}, function(data, textStatus, xhr) {
		$('.modal-title').html('Edit key: ' + lang_id);
		$('#id_of_key').val(lang_id);
		$('.modal-body form .data').html(data.html);
	});
});
$(function () {
	$('table').DataTable();
	var form_lang_settings = $('form.edit-key-settings');
    form_lang_settings.ajaxForm({
        url: '{{LINK aj/ap/update_lang_key}}',
        beforeSend: function() {
            $('.btn-save').text('PLEASE WAIT..');
        },
        success: function(data) {
            if (data.status == 200) {
            	$('.btn-save').text('SAVE CHANGES');
            	var value_to_use = form_lang_settings.find('textarea').first().val();
            	var id_of_key = $('#id_of_key').val();
            	$('#edit_' + id_of_key).text(value_to_use);
            	$('#edit-lang-modal').modal('hide');
            }
        }
    });
});

function Wo_SubmitLangForm() {
	$('.edit-key-settings').submit();
}
</script>
<div class="modal fade" id="edit-lang-modal" 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  text-center"></div>
                	<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>