


$(document).ready(function () {
    deptList();

    $('#memberAddSaveBtn').on('click', function () {
        var userArray = [];
        $.each($('input[type=checkbox][name="memberChkList[]"]:checked'), function () {
            var item = {
                user_uid: $(this).val(),
                user_name: $(this).parent().find('span').eq(0).text().trim()
            }
            userArray.push(item);
        });

    });

    $('#groupNewModal').on('click', function () {
        $('#groupModal').modal('toggle');
    });

    
});

function newMemberClick(dept_uid) {
    var formData = {
        dept_uid: dept_uid
    }

    $.ajax({
        url: "/AuthApi/noGroupUserList",
        type: "post",
        data: formData,
        success: function (data, textStatus, jqXHR) {
            if (data.ret == "yes") {
                $('#nogroup_memberlist').html('<div class="list-group-header"> 勾選要加入群組的成員 </div>');

                var obj = data.userList;
                var items = "";
                $.each(obj, function (i, item) {
                    var isExist = 'N';

                    $.each($('#nestableMember .dd-list li'), function () {
                        var tmp_uid = $(this).attr('data-user-uid');
                        if (tmp_uid == item.user_uid) {
                            isExist = 'Y';
                        }
                    });

                    if (isExist == 'N') {
                        items += '<label class="list-group-item custom-control custom-checkbox mb-0"><input id="chk_' + item.user_uid + '" name="memberChkList[]" type="checkbox"  value="' + item.user_uid + '" class="custom-control-input"> <span class="custom-control-label">' + item.user_name + '</span></label>';
                    }

                    
                });

                $('#nogroup_memberlist').append(items);

                $('#memberModal').modal('toggle');
            } else {
                alert(data.message);

                if (data.err_code == "99999") {
                    location.href = "/Root/Login";
                }
            }
        },
        error: function (jqXHR, textStatus, errorThrown) {
            alert('網路或伺服器發生錯誤，請稍後重試!');
        }
    });
}

function groupList() {
    var dept_uid = $('#dept_select').val();
    var formData = {
        dept_uid: dept_uid
    }
    $.ajax({
        url: "/AuthApi/groupList",
        type: "post",
        data: formData,
        success: function (data, textStatus, jqXHR) {
            if (data.ret == "yes") {
                $('#group_div').html('');

                var obj = data.groups;
                var items = "";
                $.each(obj, function (i, item) {


                    


                });
            } else {
                alert(data.message);

                if (data.err_code == "99999") {
                    location.href = "/Root/Login";
                }
            }
        },
        error: function (jqXHR, textStatus, errorThrown) {
            alert('網路或伺服器發生錯誤，請稍後重試!');
        }
    });
}
function deptList() {
    $.ajax({
        url: "/AuthApi/authDeptList",
        type: "post",
        data: null,
        success: function (data, textStatus, jqXHR) {
            if (data.ret == "yes") {
                var obj = data.depts;
                var items = "";
                $.each(obj, function (i, item) {


                    $("#dept_select").append($("<option>", {
                        value: item.dept_uid,
                        text: item.dept_name
                    }));


                });
            } else {
                alert(data.message);

                if (data.err_code == "99999") {
                    location.href = "/Root/Login";
                }
            }
        },
        error: function (jqXHR, textStatus, errorThrown) {
            alert('網路或伺服器發生錯誤，請稍後重試!');
        }
    });
}