<?php 
//echo '<pre>';
//print_r($_COOKIE);
//die('okk');
?>
<div class="container-fluid container-fixed-lg bg-white">
    <div class="panel panel-transparent">
        <div class="panel-heading">
            <div class="panel-title"><?php print CommonLanguage::getMessage('trusted_computers'); ?></div>
            <?php if ($permissions->hasPermission('add_trusted_computer') && empty($_COOKIE[DB_NAME.'_computer']) || ( $trusted_exist == 0 &&  !empty($_COOKIE[DB_NAME.'_computer']))) { ?>
                <div class="col-xs-12 p-0 m-b-10 trusted_computer_button_div">  
                    <div class="floatingContainer ">
                        <a href="" id="new_trusted_computer" data-target="#add-edit-computer-trusted-modal" data-toggle="modal" >
                            <div class="actionButton bg-info-light" >
                                <p class="floatingText"><span class="floatingTextBG"><?php echo CommonLanguage::getMessage('add_computer_trusted') ?></span></p>
                            </div>
                        </a>
                    </div>
                </div>
            <?php } ?>
        </div>
        <div class="panel-body">
            <div class="row">
                <div class="col-md-12">
                    <div class="table-responsive">
                        <table class="table table-bordered setting-table" id="tableWithSearch">
                            <thead>
                                <tr>
                                    <th><?php print CommonLanguage::getMessage('computer_name') ?></th>
                                    <th><?php print CommonLanguage::getMessage('Action') ?></th>
                                </tr>
                            </thead>
                            <tbody>
                            </tbody>
                        </table>
                    </div>
                </div>
            </div>
        </div>
    </div>
</div>
<div class="modal fade" id="add-edit-computer-trusted-modal" data-backdrop="static" tabindex="-1" role="dialog" aria-hidden="true">
    <div class="modal-dialog modal-sm">
        <div class="modal-content">
            <div class="modal-header">
                <button type="button" class="close" data-dismiss="modal" aria-hidden="true"><i class="pg-close fs-20"></i>
                </button>
                <h5 class="add-label h-labels text-left bold hidden"><?php echo CommonLanguage::getMessage('add_computer_to_trusted'); ?></h5>
                <h5 class="edit-label h-labels text-left bold hidden"><?php echo CommonLanguage::getMessage('edit_computer'); ?></h5>
            </div>
            <div class="modal-body">
                <form id="trusted_computer_form" method="POST" onsubmit="return false">
                    <input type="hidden" id="entry_id" name="entry_id"/>
                    <div class="row">
                        <?php echo Forms::AlertDiv('computer_trusted_alert'); ?>
                    </div>
                    <div class="row  m-t-10">
                        <div class="col-md-12 form-group">
                            <label><?php print CommonLanguage::getMessage('computer_name'); ?></label>
                            <input class="form-control empty" id="computer_name" name="computer_name" type="text" required autocomplete="off" />
                        </div>
                    </div> 
                </form>
            </div>
            <div class="modal-footer">
                <button class="btn btn-default" data-dismiss="modal"><?php print CommonLanguage::getMessage('Close'); ?></button>
                <button class="btn bg-info-light text-white" id="submit_computer_trusted"><?php print CommonLanguage::getMessage('Save'); ?></button>
            </div>
            <?php echo Forms::ModalLoader('computerTrustedLoader'); ?>
        </div>
    </div>
</div>
<script>
    $(function(){
        RebuildDataTable($('.panel-heading .panel-title').html(), [0], 'SettingsAjax/getComputersTrusted');
         $('#tableWithSearch').on('click', '.delete_trusted_computer', function () {
            var entry_value = $(this).parents('div.Action').attr('data-id')
            IncludeDeleteModal(common_delete_modal, {entry_id: entry_value}, function () {
                RebuildDataTable($('.panel-heading .panel-title').html(), [0], 'SettingsAjax/getComputersTrusted');
            }, "SettingsAjax/deleteTrustedComputer");
        });
        $("#new_trusted_computer").on("click", function () {
            $('#entry_id').val(0)
            $("#add-edit-computer-trusted-modal .add-label").removeClass('hidden');
        });
        
        $("#tableWithSearch").on("click",'.edit_trusted_computer', function () {
            var entry_value = $(this).parents('div.Action').attr('data-id')
            $('#entry_id').val(entry_value)
            $('#computer_name').val($('td:eq(0)',$(this).parents('tr')).html())
            $("#add-edit-computer-trusted-modal .edit-label").removeClass('hidden');
        });
        $("#submit_computer_trusted").on("click", function () {
            if (!$("#trusted_computer_form").valid())
                return;
            var Data = new FormData(document.getElementById("trusted_computer_form"));
            $("#computerTrustedLoader").removeClass('hidden');
            $.ajax({
                url: "SettingsAjax/submitTrustedComputer",
                method: "POST",
                data: Data,
                processData: false,
                contentType: false,
                dataType: "JSON",
                complete: function (jqXHR, status) {
                    if (status == 'success') {
                        if (jqXHR.responseJSON[1] == "success") {
                            RebuildDataTable($('.panel-heading .panel-title').html(), [0], 'SettingsAjax/getComputersTrusted');
                            hideModalAlert('add-edit-computer-trusted-modal', 'computer_trusted_alert');
                            $('#entry_id').val() == 0 ? $('.trusted_computer_button_div').remove() : '';
                        }
                        AddAlert("#computer_trusted_alert", jqXHR.responseJSON[0], jqXHR.responseJSON[1]);
                    }
                    $("#computerTrustedLoader").addClass('hidden');
                }
            });
        });
    });
</script>