var user_group_idx = 0;
var user_group_code_list = [];

var C_LIST_USER_GROUP = "511";
var C_NEW_USER_GROUP = "512";

var user_group_id = 0;

function addNewUserGroup() {
    LoadingScreen('show');
    screen_index++;
    var data = { "type": C_NEW_USER_GROUP, "index": screen_index };
    var url = "index.pzx?c=" + user_group_entry_url + "&d=" + new Date().getTime();
    callServer(url, "json", data, onResponseUserGroup, null);
}
function reviewUserGroup() {
    LoadingScreen('show');
    if (user_group_id != 0) {
        screen_index++;
        var data = {
            "type": C_GET,
            "index": screen_index,
            "id": user_group_id,
        };
        var url = "index.pzx?c=" + user_group_entry_url + "&d=" + new Date().getTime();
        callServer(url, "json", data, onResponseUserGroup, null);
    } else {
        LoadingScreen("hide");
        swal.fire("Warning", "Please select a user group", "warning");
    }
}

function deleteUserGroup() {
    //get who checked into the list
    var lines = [];

    $("input[name='user_group_chk[]']:checked").each(function (i) {
        lines[i] = $(this).val();
    });
    //confirmation and delete column
    if (lines.length > 0) {
        swal.fire("System Confirmation", "Are you sure you wan to delete the selected record line?", "warning", {
            buttons: {
                ok: true,
                cancel: "Cancel",
            },
        }).then(function (result) {
            switch (result) {
                case "ok":
                    LoadingScreen("show");
                    for (var i = 0; i < lines.length; i++) {
                        var id = $('#user_group_id' + lines[i]).html();
                        var data = { "type": C_DELETE, "id": id, "index": screen_index };
                        var url = "index.pzx?c=" + user_group_url + "&d=" + new Date().getTime();

                        callServer(url, "json", data, onResponseUserGroup, null);
                    }
                    break;
            }
        });
    } else {
        LoadingScreen("hide");
        swal.fire("Warning", "Please select at least one user group to delete.", "warning");
    }
    
}

function userGroupFilter() {
    LoadingScreen("show");
    var data = {
        "type": C_LIST_USER_GROUP,
        "index": screen_index,
        "sorts": sorts,
    };
    var url = "request.pzx?c=" + user_group_url + "&d=" + new Date().getTime();
    callServer(url, "json", data, onResponseUserGroup, null);
}

function onResponseUserGroup(obj, resp) {
    LoadingScreen("hide");
    if (resp.status != C_OK) {
        swal.fire('Warning', resp.mesg, 'warning');
        return;
    }
    if (resp.type == C_LIST_USER_GROUP) {
        user_group_code_list = [];
        var myObj = JSON.parse(resp.data);
        showUserGroupList(myObj);
    } else if (resp.type == C_GET) {
        showContentIndex(resp.data.htmlContent);
        
        $("#btnSaveUserGroup").attr("onclick", "updateUserGroup();");
        //change button to Update
        $('#btnSaveUserGroup').html('Update');
        $('#txtUserGroupID').val(resp.data.headerData.id);
        $('#txtUserGroupCode').val(resp.data.headerData.code);
        $('#txtUserGroupName').val(resp.data.headerData.name);
        var admin_rights = resp.data.headerData.admin;

        $('#usergroup-marketing-entity').prop('checked', admin_rights.marketing_entity);
        $('#usergroup-marketing-view-others').prop('checked', admin_rights.marketing_view_others);
        $('#usergroup-marketing-edit-others').prop('checked', admin_rights.marketing_edit_others);
        $('#usergroup-sales-account').prop('checked', admin_rights.sales_account);
        $('#usergroup-sales-suspect').prop('checked', admin_rights.sales_suspect);
        $('#usergroup-sales-prospect').prop('checked', admin_rights.sales_prospect);
        $('#usergroup-sales-opportunity').prop('checked', admin_rights.sales_opportunity);
        $('#usergroup-sales-view-others').prop('checked', admin_rights.sales_view_others);
        $('#usergroup-sales-edit-others').prop('checked', admin_rights.sales_edit_others);
        $('#usergroup-integration-customer').prop('checked', admin_rights.integration_customer);
        $('#usergroup-integration-prospect').prop('checked', admin_rights.integration_prospect);
        $('#usergroup-integration-export-data').prop('checked', admin_rights.integration_export_data);
        $('#usergroup-integration-destination-master').prop('checked', admin_rights.integration_destination_master);
        $('#usergroup-admin-user').prop('checked', admin_rights.admin_user);
        $('#usergroup-admin-user-group').prop('checked', admin_rights.admin_user_group);
        $('#usergroup-admin-industry').prop('checked', admin_rights.admin_industry);
        $('#usergroup-admin-business-sector').prop('checked', admin_rights.admin_business_sector);
        $('#usergroup-admin-business-nature').prop('checked', admin_rights.admin_business_nature);
        $('#usergroup-admin-software').prop('checked', admin_rights.admin_software);
        $('#usergroup-admin-department').prop('checked', admin_rights.admin_department);
        $('#usergroup-report-contact').prop('checked', admin_rights.report_contact);
        $('#usergroup-report-summary').prop('checked', admin_rights.report_summary);
        $('#usergroup-report-inquiry').prop('checked', admin_rights.report_inquiry);
        $('#usergroup-report-history').prop('checked', admin_rights.report_history);
        $('#usergroup-report-conversation').prop('checked', admin_rights.report_conversation);
        $('#usergroup-report-all-account').prop('checked', admin_rights.report_all_account);
        $('#corporate-gift-company').prop('checked', admin_rights.gift_company);
        $('#corporate-gift-listing').prop('checked', admin_rights.gift_listing);
        $('#corporate-gift-master').prop('checked', admin_rights.gift_master);
        $('#campaign-company').prop('checked', admin_rights.campaign_company);
        $('#campaign-listing').prop('checked', admin_rights.campaign_listing);
        $('#campaign-master').prop('checked', admin_rights.campaign_master);
    }
    else if (resp.type == C_DELETE) {
        swal.fire('Success', resp.mesg, 'success');
        userGroupFilter();
    } else
        if (resp.type == C_NEW_USER_GROUP) {
            showContentIndex(resp.data.htmlContent);
            $("#contentTitle").html(resp.data.headTitle);

            $("#btnSaveUserGroup").attr("onclick", "addUserGroup();");
        } else if (resp.type == "C_USER_GROUP_READ_EXCEL") {
            $('#importUserGroupModal').model('hide');
            swal.fire('Success', resp.mesg, 'success');
            userGroupFilter();
        }
}

function showUserGroupList(myObj) {
    user_group_idx = 0;
    user_group_id = 0;
    $('#user-group-table-body').html('');
    for (i in myObj) {
        id = myObj[i].id;
        code = myObj[i].code;
        user_group_name = myObj[i].name;

        user_group_code_list.push(code);

        $('#user-group-table-body').append(
            '<tr id="tr_user_group' + user_group_id + '">' +
            '<td><input type="checkbox" id="user_group_checkbox' + user_group_idx + '" class="user_group_checkbox" value="' + user_group_idx + '" name="user_group_chk[]" ></td>' +
            '<td><label id="user_group_code' + user_group_idx + '" class="user_group_code" value="' + code + '" >' + code + '</label></td>' +
            '<td><label id="user_group_name' + user_group_idx + '" class="user_group_name" value="' + user_group_name + '" >' + user_group_name + '</label></td>' +
            '<td style="display:none" class="user_group_id" id="user_group_id' + user_group_idx + '">' + id + '</td>' +
            '<td style="display:none" class="user_group_idx" id="user_group_idx' + user_group_idx + '">' + user_group_idx + '</td>' +
            '</tr>'
        );

        $("#user-group-table-body tr").click(function () {
            user_group_id = $(this).find('td').eq(3).html();
            $(this).addClass('highlight').siblings().removeClass('highlight');
        });

        user_group_idx++;
    }
}
function checkUserGroupCode(obj) {
    if ($('#txtUserGroupCode').val() == "") {
        var code = $(obj).val();
        if (jQuery.inArray(code, user_group_code_list) > -1) {
            swal.fire('Warning', "User Groupc Code is exist.", 'warning');
            return;
        }
    }
}
function getUserGroupAutoSearch(type, idx) {
    var data = { "type": type, "idx": idx };
    var url = "request.pzx?c=" + user_group_url + "&d=" + new Date().getTime();
    callServer(url, "json", data, onUserGroupAutoSearch, null);
}