<?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('template_attribute'); ?>
            <?php 
            echo $template_name == '' ? '' : ': '.$template_name;
            ?>
            </div>
            <div class="row m-t-10">
                <div class="col-md-3  <?php echo $is_branch ? '' : 'hidden'; ?>  ">
                    <label><?php echo CommonLanguage::getMessage('choose_branch') ?></label>
                    <input id="search_branch_id" type="hidden" class="full-width" name="search_branch_id"  >
                </div>


                <?php if ($permissions->hasPermission('add_attribute')) { ?>
                    <div class="floatingContainer ">
                        <a href="" id="add_attribute" data-target="#add_attribute_modal" data-toggle="modal">
                            <div class="actionButton bg-info-light">
                                <p class="floatingText"><span class="floatingTextBG"><?php echo CommonLanguage::getMessage('add_attribute') ?></span></p>
                            </div>
                        </a>
                    </div>
                <?php } ?>
            </div>

        </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('attribute_id') ?></th>
                                    <th><?php print CommonLanguage::getMessage('attribute_name') ?></th>
                                    <th><?php print CommonLanguage::getMessage('attribute_key') ?></th>
                                    <th><?php print CommonLanguage::getMessage('attribute_value') ?></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_attribute_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>
            </div>
            <div class="modal-body">
                <form id="trusted_computer_form" method="POST" onsubmit="return false">
                    <input type="hidden" id="entry_id" name="entry_id" value="<?php echo $template_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  <?php echo $is_branch ? '' : 'hidden'; ?> ">
                            <label><?php echo CommonLanguage::getMessage('choose_branch') ?></label>
                            <input id="attr_branch_id" type="hidden" class="full-width" name="attr_branch_id" <?php echo $is_branch ? 'required' : ''; ?> >
                        </div>


                        <div class="col-md-12 form-group">
                            <label><?php print CommonLanguage::getMessage('attribute_type'); ?></label>
                            <select name="attribute_type" id="attribute_type" data-init-plugin="select2" class="full-width" required>
                                <option value="1"><?php echo CommonLanguage::getMessage('text'); ?></option>
                                <option value="2"><?php echo CommonLanguage::getMessage('image'); ?></option>
                            </select>
                        </div>

                        <div class="col-md-12 form-group">
                            <label><?php print CommonLanguage::getMessage('attribute_key'); ?></label>
                            <input class="form-control empty" id="attribute_key" name="attribute_key" type="text" required />
                        </div>
                        <div class="col-md-12 form-group attribute_value_div">
                            <label><?php print CommonLanguage::getMessage('attribute_value'); ?></label>
                            <input class="form-control empty" id="attribute_value" name="attribute_value" type="text" />
                        </div>

                        <div class="col-md-12 form-group image_div">
                            <label><?php print CommonLanguage::getMessage('image'); ?></label>
                            <input type="file" name="new_image_name" onchange="validateImage(this)" class="form-control input-rounded" data-max-size='12000' />
                        </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_attribute"><?php print CommonLanguage::getMessage('Save'); ?></button>
            </div>
            <?php echo Forms::ModalLoader('computerTrustedLoader'); ?>
        </div>
    </div>
</div>
<script>
    $(function() {
        build_select2($("#search_branch_id,#attr_branch_id"), "xhr/CommonSearch?col_name=branch_name&col_id=branch_id&table=tbl_organization_branches");

        ChangeTable();
        $('#search_branch_id').on('change', function() {
            ChangeTable();
        });

        $(".image_div").addClass('hidden');
        $('#attribute_type').on('change', function() {
            if ($(this).val() == 1) {
                $(".attribute_value_div").removeClass('hidden');
                $(".image_div").addClass('hidden');
            } else {
                $(".image_div").removeClass('hidden');
                $(".attribute_value_div").addClass('hidden');

            }
        });

        $('#tableWithSearch').on('click', '.delete_attribute', function() {
            console.log('clicked')
            var entry_value = $(this).attr('data-id')
            console.log('entry_value')
            console.log(entry_value)
            $.ajax({
                type: 'POST',
                url: 'SettingsAjax/deleteAttributeTemplate',
                data: {
                    attribute_id: entry_value
                },
                complete: function(data, vv) {
                    ChangeTable();

                }
            });

        });

        $("#add_attribute").on("click", function() {
            $("#add_attribute_modal .add-label").removeClass('hidden');
        });

        $("#submit_attribute").on("click", function() {
            if (!$("#trusted_computer_form").valid())
                return;
            var Data = new FormData(document.getElementById("trusted_computer_form"));
            $("#computerTrustedLoader").removeClass('hidden');
            $.ajax({
                url: "SettingsAjax/submitTemplateAttribute",
                method: "POST",
                data: Data,
                processData: false,
                contentType: false,
                dataType: "JSON",
                complete: function(jqXHR, status) {
                    console.log('jqXHR');
                    console.log(jqXHR);

                    if (status == 'success') {
                        if (jqXHR.responseJSON[1] == "success") {
                            ChangeTable();
                            hideModalAlert('add_attribute_modal', 'computer_trusted_alert');
                            $('#entry_id').val() == 0 ? $('.add_attribute_div').remove() : '';
                        }
                        AddAlert("#computer_trusted_alert", jqXHR.responseJSON[0], jqXHR.responseJSON[1]);
                    }
                    $("#computerTrustedLoader").addClass('hidden');
                }
            });
        });

        function ChangeTable() {
            var Date = {
                template_id: $("#entry_id").val(),
                search_branch_id: $("#search_branch_id").val(),
            };
            console.log('Date')
            console.log(Date)
            RebuildDataTable($('.panel-heading .panel-title').html(), [1, 2, 3, 4], 'SettingsAjax/getTemplateAttributeList', Date);

        }

    });
</script>