<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('system_setting'); ?></div>
            <div class="row">
                <div class="col-md-3">
                    <input type="text" id="system_setting_search" name="system_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="systemDefaultTable">
                            <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('defaultSettingLoader'); ?>
    </div>
</div>
<script>
    $(document).on('keydown', function (e) {
        if (e.which == 13) 
            RebuildDataTable($('.panel-heading .panel-title').html(), [], 'SettingsAjax/getSystemSetting', {setting_search:$('#system_setting_search').val()}, '#systemDefaultTable');
    });
    $(function(){
        RebuildDataTable($('.panel-heading .panel-title').html(), [], 'SettingsAjax/getSystemSetting', {setting_search:$('#system_setting_search').val()}, '#systemDefaultTable');
        $('#systemDefaultTable').on('draw.dt',function(){
            var row = $("#systemDefaultTable").dataTable().fnGetNodes();
            for (var i = 0; i < row.length; i++) {
                var setting_type = $('.submit-default-setting',$(row[i])).attr('data-type');
                if(setting_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(current_select.attr('data-content') == 'depot'){
                        build_select2(current_select, "xhr/CommonSearch?col_name=Depot_Name&col_id=Depot_ID&table=tbl_stock_depot_warehouse",is_multiple);
                    }
                    if(current_select.attr('data-content') == 'cash_pay_acc'){
                        build_select2(current_select, "xhr/accIDSearch?type=cash",is_multiple);
                    }
                    if(current_select.attr('data-content') == 'check_pay_acc'){
                        build_select2(current_select, "xhr/accIDSearch?type=check",is_multiple);
                    }
                    if(current_select.attr('data-content') == 'price'){
                        build_select2(current_select, "xhr/CommonSearch?col_name=Class_Name&col_id=Class_ID&table=tbl_pricing_class",is_multiple); 
                    }
                    if(current_select.attr('data-content') == 'org_branch'){
                        build_select2(current_select, "xhr/CommonSearch?col_name=branch_name&col_id=branch_id&table=tbl_organization_branches",is_multiple); 
                    }
                    if(current_select.attr('data-content') == 'currency'){
                        build_select2(current_select, "xhr/CommonSearch?col_name=Currency_Name&col_id=Currency_ID&table=tbl_common_currency",is_multiple);
                    }
                    if(current_select.attr('data-content') == 'prod_cat'){
                        build_select2(current_select, "xhr/CommonSearch?col_name=Cat_Name&col_id=Cat_ID&table=tbl_product_cat",is_multiple);
                    }
                    if(current_select.attr('data-content') == 'country'){
                        build_select2(current_select, "xhr/CommonSearch?col_name=Country_Name&col_id=Country_ID&table=tbl_all_countries",is_multiple);
                    }
                    if(current_select.attr('data-content') == 'city'){
                        build_select2(current_select, "xhr/CommonSearch?col_name=Name&col_id=ID&table=tbl_all_cities",is_multiple);
                    }
                    if(current_select.attr('data-content') == 'customers'){
                        build_select2(current_select, "xhr/CommonSearch?col_name=Customer_Name&col_id=Customer_ID&table=tbl_customers",is_multiple);
                    }
                    if(current_select.attr('data-content') == 'deals_stage'){
                        build_select2(current_select, "xhr/CommonSearch?col_name=stage_title&col_id=stage_id&table=tbl_deals_stage",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);
                    }
                }else if(setting_type == 'option')
                    $('select[data-type=option]',$(row[i])).select2();
                else if(setting_type == 'time')
                    $('input[data-content=timepicker]',$(row[i])).datetimepicker({format:'HH:mm:ss'});
            }
        })
        $('#systemDefaultTable').on('click','.submit-default-setting',function(){
            var setting_value_type = $(this).attr('data-type'),setting_value;
            if(setting_value_type == 'bool'){
                setting_value = $('input[name='+$(this).attr('data-content')+']:checked').length > 0 ? 1 : 0 ;
            }else if(setting_value_type == 'option'){
                var select_type = $('select[name='+$(this).attr('data-content')+']').attr('data-select-type'); 
                if(select_type == 'm'){
                    setting_value = $('select[name='+$(this).attr('data-content')+']').val() ?  $('select[name='+$(this).attr('data-content')+']').val().toString()  : '';
                }else{
                    setting_value = $('select[name='+$(this).attr('data-content')+']').val();
                }
            }else{
                setting_value = $('input[name='+$(this).attr('data-content')+']').val();
                if(setting_value_type == 'percent' && (parseFloat(setting_value) < 0  || parseFloat(setting_value) > 100)){
                    toastr.error('<?php echo CommonLanguage::getMessage('percent_between_0_100');?>');
                    return;
                }
                if(setting_value_type == 'pos_number' &&  parseFloat(setting_value) < 0){
                    toastr.error('<?php echo CommonLanguage::getMessage('enter_number_greater_equal_0');?>');
                    return;
                }
				 if(setting_value_type == 'number' &&  (isNaN(parseFloat(setting_value)) && setting_value !== '' )){
                    toastr.error('<?php echo CommonLanguage::getMessage('enter_number_only');?>');
                    return;
                }
            }
            var Data = {setting_value:setting_value,setting_id:$(this).attr('data-id'),
                        setting_name:$(this).attr('data-content')}; 
            submitDefaultSetting(Data);
        });
    });
    function submitDefaultSetting(Data){
        $('#defaultSettingLoader').removeClass('hidden');
        $.ajax({
            url: "SettingsAjax/submitDefaultSetting",
            method: "POST",
            data: Data,
            dataType:"json",
            complete: function (jqXHR, status) {
                if (status == 'success') {
                    $('#defaultSettingLoader').addClass('hidden');
                }
            }
        });
    }
</script>