<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_branches_list'); ?></div>
            <div class="row m-t-10">
                <div class="col-md-3 <?php echo $permissions->hasPermission('Branches') ? '' : 'hidden'; ?>">
                    <label><?php echo CommonLanguage::getMessage('choose_branch') ?></label>
                    <input id="search_branch_id" type="hidden" class="full-width" name="search_branch_id" >
                </div>
                <div class="col-md-3 form-group">
                    <label><?php print CommonLanguage::getMessage('template_key'); ?></label>
                    <input type="hidden" id="template_key" name="template_key" class="full-width">
                </div>
                <div class="col-md-3 form-group">
                    <label><?php print CommonLanguage::getMessage('branch_attributes_list'); ?></label>
                    <a href="Settings/attribute_list/branch"   class=" btn btn-primary  btn-cons full-width  "><?php print CommonLanguage::getMessage('branch_attributes_list'); ?></a>
                </div>


                <?php if ($permissions->hasPermission('template_branches')) { ?>
                    <div class="floatingContainer ">
                        <a href="Settings/template_branches">
                            <div class="actionButton bg-info-light">
                                <p class="floatingText"><span class="floatingTextBG"><?php echo CommonLanguage::getMessage('template_branches') ?></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('ID') ?></th>
                                    <th><?php print CommonLanguage::getMessage('branch') ?></th>
                                    <th><?php print CommonLanguage::getMessage('template_name') ?></th>
                                    <th><?php print CommonLanguage::getMessage('template_key') ?></th>
                                    <th><?php print CommonLanguage::getMessage('Action') ?></th>
                                </tr>
                            </thead>
                            <tbody>
                            </tbody>
                        </table>
                    </div>
                </div>
            </div>
        </div>
    </div>
</div>
<script>
    $(function() {
        build_select2($("#search_branch_id"), "xhr/CommonSearch?col_name=branch_name&col_id=branch_id&table=tbl_organization_branches");
        build_select2($("#template_key"), "xhr/CommonSearch?col_id=template_key&col_name=template_key&table=tbl_print_template");

        RebuildDataTable($('.panel-heading .panel-title').html(), [1, 2, 3, 4], 'SettingsAjax/getTemplateBranchesList');
        $('#search_branch_id,#template_key').on('change', function() {
            ChangeTable();
        });

        $('#tableWithSearch').on('click', '.delete_template', function() {
            var entry_value = $(this).parents('div.Action').attr('data-id')
            $.ajax({
                type: 'POST',
                url: 'SettingsAjax/deleteTemplate',
                data: {
                    template_id: entry_value
                },
                complete: function(data, vv) {
                    console.log(data)
                    if(data.responseText == 1){
                        alert("delete attributes first")
                    }else
                        RebuildDataTable($('.panel-heading .panel-title').html(), [1, 2, 3, 4], 'SettingsAjax/getTemplateBranchesList');

                }
            });
        });

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

        }


    });
</script>