<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('account_setting'); ?></div>
            <div class="row">
                <div class="col-md-3">
                    <input type="text" id="account_setting_search" name="account_setting_search" class="form-control"/>
                </div>
            </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="accountSettingTable">
                            <thead>
                                <tr>
                                    <!--                                    <th class="< ?php echo (Session::is_smart_admin() ? '' : 'hidden');?>">< ?php print CommonLanguage::getMessage('variable') ?></th>-->
                                    <th><?php print CommonLanguage::getMessage('variable') ?></th>
                                    <th><?php print CommonLanguage::getMessage('definer') ?></th>
                                    <th style="width:25%;"><?php print CommonLanguage::getMessage('value') ?></th>
                                    <th style="width:10%;"><?php print CommonLanguage::getMessage('Action') ?></th>
                                </tr>
                            </thead>
                            <tbody>
                            </tbody>
                        </table>
                    </div>
                </div>
            </div>
        </div>
        <?php echo Forms::ModalLoader('accountDefaultLoader'); ?>
    </div>
</div>

<script>
    $(document).on('keydown', function (e) {
        if (e.which == 13) 
            RebuildDataTable($('.panel-heading .panel-title').html(), [], 'SettingsAjax/getAccountSetting', {setting_search:$('#account_setting_search').val()}, '#accountSettingTable');
    });
    $(function(){
        RebuildDataTable($('.panel-heading .panel-title').html(), [], 'SettingsAjax/getAccountSetting', {setting_search:$('#account_setting_search').val()}, '#accountSettingTable');
        $('#accountSettingTable').on('draw.dt',function(){
            var row = $("#accountSettingTable").dataTable().fnGetNodes();
            for (var i = 0; i < row.length; i++) {
                if($('.submit-account-setting',$(row[i])).attr('data-type')== 'table'){
                    var current_select = $('input.setting-select',$(row[i])),
                        is_multiple = current_select.attr('data-select-type') == 's' ? '' : '1';
                    if(current_select.attr('data-content') == 'chart_of_accounts'){
                        build_select2(current_select, "xhr/accIDSearch",is_multiple); 
                    }
                    if(is_multiple == '1' && current_select.attr('data-content-multiple')){
                        var multiple_select_content =  JSON.parse(current_select.attr('data-content-multiple'));
                        current_select.select2('val', [])
                        current_select.data().select2.updateSelection(multiple_select_content);
                    }
                }
            }
        });
        $('#accountSettingTable').on('click','.submit-account-setting',function(){
            var setting_value_type = $(this).attr('data-type'),setting_value;
            setting_value = $('input[name='+$(this).attr('data-content')+']').val();
            if(setting_value_type == 'number' &&  parseFloat(setting_value) < 0){
                toastr.error('<?php echo CommonLanguage::getMessage('enter_number_greater_equal_0');?>')
                return;
            }
            var Data = {setting_value:setting_value,setting_id:$(this).attr('data-id'),
                        setting_name:$(this).attr('data-content')}; 
            submitAccountSetting(Data);
        });
    });
    function submitAccountSetting(Data){
        $('#accountDefaultLoader').removeClass('hidden');
        $.ajax({
            url: "SettingsAjax/submitAccountSetting",
            method: "POST",
            data: Data,
            dataType:"json",
            complete: function (jqXHR, status) {
                if (status == 'success') {
                    $('#accountDefaultLoader').addClass('hidden');
                }
            }
        });
    }
</script>