<div class="container-fluid container-fixed-lg bg-white">
    <div class="panel panel-transparent">
        <div class="panel-heading">
            <div class="panel-title"><?php print $title; ?></div>
        </div>
        <div class="panel-body">
            <div class="row form-group">
                <div class="col-md-3 ">
                    <label><?php echo CommonLanguage::getMessage('options') ?></label>
                    <select name="user_options" id="user_options" multiple class="full-width" data-init-plugin="select2">
                        <option value="1"><?php print CommonLanguage::getMessage('subscription_block'); ?></option>
                        <option value="2"><?php print CommonLanguage::getMessage('customer_group'); ?></option>
                        <option value="3"><?php print CommonLanguage::getMessage('notification_type'); ?></option>
                    </select>
                </div>
            </div>
            <div class="row m-t-10">
                <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('user') ?></th>
                                    <th style="width:25%;" class="table_options_col"><?php print CommonLanguage::getMessage('options') ?></th>
                                </tr>
                            </thead>
                            <tbody>
                            </tbody>
                        </table>
                    </div>
                </div>
            </div>
            <div class="row">
                <div class="container-sm-height data-options hidden">
                    <button class=" btn bg-info-light text-white pull-right" id="submit_user_setting" style="color:#fff;background-color:#4c0870  ;padding-top: 10px;top: 20px;min-width:253px;" type="button"><?php print CommonLanguage::getMessage('Save'); ?></button>
                </div>
            </div>
        </div>
        <?php echo Forms::ModalLoader('user_setting_loader'); ?>
    </div>
</div>
<script>
    $(function(){
        $('#user_options').on('change',function(){
            ActualAjax();
        });
        ActualAjax();
        $('#tableWithSearch').on('draw.dt',function(){
            var row = $("#tableWithSearch").dataTable().fnGetNodes();
            build_select2($('input[name=block_ids]'), "xhr/searchSubscriptionBlocks?is_parent=1",1);
            build_select2($("input[name=customer_group_ids]"), "xhr/general_group_Search?group_type=cust",1);
            build_select2($("input[name=notification_type_ids]"), "xhr/notification_type_search",1);

            for (var i = 0; i < row.length; i++) {
                var current_block_select = $('input[name=block_ids]',$(row[i]));
                if(current_block_select.attr('data-content-multiple') && current_block_select){
                    var multiple_select_content =  JSON.parse(current_block_select.attr('data-content-multiple'));
                    current_block_select.select2('val', [])
                    current_block_select.data().select2.updateSelection(multiple_select_content);
                }
                var current_cust_group_select = $('input[name=customer_group_ids]',$(row[i]));
                if(current_cust_group_select.attr('data-content-multiple') && current_cust_group_select){
                    var multiple_select_content =  JSON.parse(current_cust_group_select.attr('data-content-multiple'));
                    current_cust_group_select.select2('val', [])
                    current_cust_group_select.data().select2.updateSelection(multiple_select_content);
                }
                var current_not_type_select = $('input[name=notification_type_ids]',$(row[i]));
                if(current_not_type_select.attr('data-content-multiple') && current_cust_group_select){
                    var multiple_select_content =  JSON.parse(current_not_type_select.attr('data-content-multiple'));
                    current_not_type_select.select2('val', [])
                    current_not_type_select.data().select2.updateSelection(multiple_select_content);
                }
            }
        });
        $('#submit_user_setting').on('click',function(){
            var user_setting = [],
                row = $("#tableWithSearch").dataTable().fnGetNodes();
            for (var i = 0; i < row.length; i++) {
                user_setting.push({user_id:$('span.user_full_name',$(row[i])).attr('data-user-id'),
                                   block_ids:$('input[name=block_ids]',$(row[i])).val(),
                                   customer_group_ids:$('input[name=customer_group_ids]',$(row[i])).val(),
                                   notification_type_ids:$('input[name=notification_type_ids]',$(row[i])).val(),
                                  });
            }
            var Data = {user_setting: user_setting,user_options:$('#user_options').val()};

            $('#user_setting_loader').addClass('hidden');
            $.ajax({
                url: "SettingsAjax/submitUserSetting",
                method: "POST",
                data: Data,
                dataType:"json",
                complete: function (jqXHR, status) {
                    if (status == 'success') {
                        if (jqXHR.responseJSON[1] == "success") {
                            toastr.success(jqXHR.responseJSON[0]);
                            ActualAjax();
                        }else{
                            toastr.error(jqXHR.responseJSON[0]);
                        }
                        $('#user_setting_loader').addClass('hidden');
                    }
                }
            });
        });
    });
    function ActualAjax() {
        var options_count = 0, options_data = [];
        if ($("#user_options").val()) {
            options_data = $('#user_options').select2('data');
            options_count = options_data.length;
            $('.data-options').removeClass('hidden');
        }else{
            $('.data-options').addClass('hidden');
        }


        var table_options_cols = $('#tableWithSearch thead th.table_options_col');
        var first_table_options_col = table_options_cols.first();
        table_options_cols.not(first_table_options_col).remove();
        var last_options_col;
        var prt_cols = [0], options_val = [];
        for (var i = 0; i < options_count; i++) {
            if (i == 0)    //first one already exist so don't clone
                last_options_col = first_table_options_col;
            else
                last_options_col = first_table_options_col.clone().insertAfter(last_options_col);
            last_options_col.html(options_data[i]['text']);
            options_val.push(options_data[i]['id']);
            prt_cols.push(4 + i);
        }
        var user_list_dt = RebuildDataTable($('.panel-heading .panel-title').html(), prt_cols, 'SettingsAjax/getUserSetting', {user_options: options_val}, '#tableWithSearch');
        user_list_dt.columns().visible(true);
    }
</script>