"use strict";
var popupN;  // used for id of popup

var ewzG1;

/* Set the selections to reflect the  permissions stored for the selected user */
/* Show a summary of these permissions */

var ewzG;

jQuery(function() {
    popupN = 0;
});

function ewz_set_user(){
    var userid = jQuery("#ewz_user_perm").val();
    jQuery("#ewz_have_perm").val(userid);
    ewz_show_perms(userid);
}
function ewz_set_ewz_user(){
    var userid = jQuery("#ewz_have_perm").val();
    jQuery("#ewz_user_perm").val(userid);
    ewz_show_perms(userid);
}

function ewz_show_perms( userid ){

    jQuery("#ewz_can_edit_layout").val([]);
    jQuery("#ewz_can_assign_layout").val([]);
    jQuery("#ewz_can_edit_webform").val([]);
    jQuery("#ewz_can_manage_webform").val([]);
    jQuery("#ewz_can_manage_webform_L").val([]);
    jQuery("#ewz_can_download_webform").val([]);
    jQuery("#ewz_can_download_webform_L").val([]);
    jQuery("#ewz_can_edit_rating_form").val([]);
    jQuery("#ewz_can_see_user_data").prop("checked", false);

    var i, j, val, str, opt,
        permcount = 0,
        pp = '<table>';
    ewzG = ewzG1.gvar;
    if( ewzG.ewz_perms ){
        for(i = 0; i < ewzG.ewz_perms.length; ++i){
            if(ewzG.ewz_perms[i].user_id == userid){
                if( ewzG.ewz_perms[i].meta_key == 'ewz_can_see_user_data' &&
                    ewzG.ewz_perms[i].meta_value == 1
                  ){
                    jQuery("#ewz_can_see_user_data").prop("checked", true);
                    pp += "<tr><td>Can See User Data</td><td>True</td></tr>";
                }
                for(j = 0; j < ewzG.ewz_perms[i].meta_value.length; ++j){
                    val = ewzG.ewz_perms[i].meta_value[j];
                    str = '#' + ewzG.ewz_perms[i].meta_key +' option[value=' + val + ']';
                    opt = jQuery(str);
                    if(opt !== null){
                        ++permcount;
                        opt.prop("selected","selected");
                        pp += "<tr><td>" +jQuery('#' + ewzG.ewz_perms[i].meta_key +'T').text() + "</td><td> " + opt.text() + "</td></tr>";
                    }
                }
            }
        }
    }
    pp += "</table>";

    if( permcount > 0 ){
        jQuery('#uperms').html('<br><i><b>All Currently Assigned Permissions for this user</b></i>: ' + pp);
    } else {
        jQuery('#uperms').html('<br><i>No EntryWizard permissions currently assigned for this user</i>');
    }
}

/* validation and removal of "none" options */
function ewz_check_perm_input(the_form){
    var except;
    jQuery('#submit').prop("disabled", true);
    try{
        var user = jQuery("#ewz_user_perm option:selected").val();
        if( !user || '0' === user ){
            jQuery('#submit').prop("disabled", false);
            ewz_alert('P001',"Please select a user for whom the permissions are to apply", ++popupN);
            return false;
        }
        jQuery('select[name$="[]"] option[value="0"]').remove();
        return true;
    } catch(except) {
        jQuery('#submit').prop("disabled", false);
        ewz_alert('P002',"Sorry, there was an unexpected error: " + except.message, ++popupN);
        return false;
    }
}
