$include('/storage/data/department-data-json.js');
$include('/storage/data/role-data-json.js');


// 页面初始化事件调用函数
function init() {
    dataListPageInit();
    dataListFilter();
    dataListLocation();

    fastui.list.scrollDataLoad('/web/admin/user/user-list-json.ashx');
}


// 页面调整事件调用函数
function resize() {
    dataListPageInit();
}


// 数据列表页面初始化
function dataListPageInit() {
    var keyword = $query('keyword');

    // 列表页面布局调整
    fastui.list.layoutResize();

    if (typeof(jsonDataDepartment) == 'undefined') {
        $id('filter-department').style.display = 'none';
        $id('button-classify-department').style.display = 'none';
        $id('location').style.display = 'none';
    }

    if (typeof(jsonDataRole) == 'undefined') {
        $id('filter-role').style.display = 'none';
        $id('button-classify-role').style.display = 'none';
    }

    if (typeof(jsonDataDepartment) == 'undefined' && typeof(jsonDataRole) == 'undefined') {
        $id('button-classify').style.display = 'none';
    }

    if (keyword.length == 0) {
        fastui.valueTips('keyword', lang.user.tips.value['keyword']);
    } else {
        $id('keyword').value = keyword;
    }
}


// 数据列表筛选
function dataListFilter() {
    var departmentId = $query('departmentid') || 0;
    var roleId = $query('roleid');
    var status = $query('status');
    var items;
    var i;

    if (typeof(jsonDataDepartment) != 'undefined') {
        if (departmentId == 0) {
            items = '<li class=\"item-current\">' + lang.user['unlimited'] + '</li>';
        } else {
            items = '<li onClick=\"$location(\'departmentid\', \'\');\">' + lang.user['unlimited'] + '</li>';
        }

        for (i = 0; i < jsonDataDepartment.length; i++) {
            if (jsonDataDepartment[i].dbs_departmentid == departmentId) {
                if (jsonDataDepartment[i].dbs_id == departmentId) {
                    items += '<li class=\"item-current\">' + jsonDataDepartment[i].dbs_name + '</li>';
                } else {
                    items += '<li onClick=\"$location(\'departmentid\', ' + jsonDataDepartment[i].dbs_id + ');\">' + jsonDataDepartment[i].dbs_name + '</li>';
                }
            }
        }

        $id('filter-department').innerHTML += '<ul>' + items + '</ul>';
    }

    if (typeof(jsonDataRole) != 'undefined') {
        if (roleId == 0) {
            items = '<li class=\"item-current\">' + lang.user['unlimited'] + '</li>';
        } else {
            items = '<li onClick=\"$location(\'roleid\', \'\');\">' + lang.user['unlimited'] + '</li>';
        }

        for (i = 0; i < jsonDataRole.length; i++) {
            if (jsonDataRole[i].dbs_id == roleId) {
                items += '<li class=\"item-current\">' + jsonDataRole[i].dbs_name + '</li>';
            } else {
                items += '<li onClick=\"$location(\'roleid\', ' + jsonDataRole[i].dbs_id + ');\">' + jsonDataRole[i].dbs_name + '</li>';
            }
        }

        $id('filter-role').innerHTML += '<ul>' + items + '</ul>';
    }

    if (status.length == 0) {
        items = '<li class=\"item-current\">' + lang.user['unlimited'] + '</li>';
    } else {
        items = '<li onClick=\"$location(\'status\', \'\');\">' + lang.user['unlimited'] + '</li>';
    }

    for (i in lang.user.data.status) {
        if (i == status) {
            items += '<li class=\"item-current\">' + lang.user.data.status[i] + '</li>';
        } else {
            items += '<li onClick=\"$location(\'status\', \'' + i + '\');\">' + lang.user.data.status[i] + '</li>';
        }
    }

    $id('filter-status').innerHTML += '<ul>' + items + '</ul>';
}


// 数据列表路径
function dataListLocation() {
    try {
        var id = $query('departmentid') || 0;
        var path = $id('location').$class('path')[0];
        var departmentId;
        var name;
        var location;
        var i;

        if (id == 0) {
            path.innerHTML += lang.user['all'];
        } else {
            path.innerHTML += '<a href=\"/web/admin/user/user-list.html\">' + lang.user['all'] + '</a>';
        }

        if (id == 0) {
            return;
        }

        for (i = 0; i < jsonDataDepartment.length; i++) {
            if (jsonDataDepartment[i].dbs_id == id) {
                departmentId = jsonDataDepartment[i].dbs_departmentid;
                name = jsonDataDepartment[i].dbs_name;
                break;
            }
        }

        location = ' / ' + name;

        while (departmentId > 0) {
            for (i = 0; i < jsonDataDepartment.length; i++) {
                if (jsonDataDepartment[i].dbs_id == departmentId) {
                    id = jsonDataDepartment[i].dbs_id;
                    departmentId = jsonDataDepartment[i].dbs_departmentid;
                    name = jsonDataDepartment[i].dbs_name;
                    break;
                }
            }

            location = ' / <a href=\"javascript: $location(\'departmentid\', ' + id + ');\">' + name + '</a>' + location;
        }

        path.innerHTML += location;
    } catch(e) {}
}


// 数据列表项目移除(询问)
function dataListItemRemove(index) {
    var id = jsonData.items[index].dbs_id;
    var username = jsonData.items[index].dbs_username;

    fastui.dialogConfirm(lang.user.tips.confirm['remove'] + '<br />' + username, 'dataListItemRemoveOk(' + id + ')');
}


// 数据列表项目移除(提交)
function dataListItemRemoveOk(id) {
    var data = 'id=' + id;

    fastui.coverShow('waiting-cover');

    $ajax({
        type: 'POST', 
        url: '/web/admin/user/user-action.ashx?action=remove', 
        async: true, 
        data: data, 
        callback: 'dataListActionCallback'
        });
}


// 数据列表操作(询问)
function dataListAction(action) {
    var checkboxes = $name('id');
    var selected = false;
    var tips;
    var i;

    // 检查是否已选择项目
    for (i = 0; i < checkboxes.length; i++) {
        if (checkboxes[i].checked == true) {
            selected = true;
            break;
        }
    }

    if (selected == false) {
        fastui.textTips(lang.user.tips['please-select-item']);
        return;
    }

    switch(action) {
        case 'removeall':
        tips = lang.user.tips.confirm['remove-all'];
        break;
    }

    fastui.dialogConfirm(tips, 'dataListActionOk(\'' + action + '\')');
}


// 数据列表操作(提交)
function dataListActionOk(action) {
    var checkboxes = $name('id');
    var data = '';
    var i;

    for (i = 0; i < checkboxes.length; i++) {
        if (checkboxes[i].checked == true) {
            data += 'id=' + checkboxes[i].value + '&';
        }
    }

    if (data.length > 0) {
        data = data.substring(0, data.length - 1);

        fastui.coverShow('waiting-cover');

        $ajax({
            type: 'POST', 
            url: '/web/admin/user/user-action.ashx?action=' + action, 
            async: true, 
            data: data, 
            callback: 'dataListActionCallback'
            });
    }
}


// 数据列表操作(回调函数)
function dataListActionCallback(data) {
    fastui.coverHide('waiting-cover');

    if (data == 'complete') {
        fastui.list.scrollDataLoad(fastui.list.path, true);

        fastui.iconTips('tick');
    } else {
        fastui.iconTips('cross');
    }
}


// 数据列表视图
function dataListView(field, reverse, primer) {
    var list = fastui.list.dataTable('data-list');
    var row;
    var i;
    var j;

    if (typeof(jsonData) == 'undefined') {
        return;
    }

    if (field.length == 0) {
        fastui.list.sortChange('username', false);
    } else {
        jsonData.items.sort($jsonSort(field, reverse, primer));

        fastui.list.sortChange(field.substring(field.indexOf('_') + 1), reverse);
    }

    j = list.rows.length;

    for (i = j; i < jsonData.items.length; i++) {
        row = list.addRow(i);

        // 复选框
        row.addCell(
            {width: '20', align: 'center'}, 
            '<input name=\"id\" type=\"checkbox\" class=\"checkbox\" value=\"' + jsonData.items[i].dbs_id + '\" />'
            );

        // 图标
        row.addCell(
            {width: '32'}, 
            '<img src=\"/ui/images/datalist-user-icon.png\" width=\"32\" />'
            );

        // 用户账号
        row.addCell(
            {width: '200'}, 
            function() {
                var html = '';

                html += '' + jsonData.items[i].dbs_username + '';
                html += '&nbsp;&nbsp;';
                html += '<font color=\"#757575\">' + jsonData.items[i].dbs_position + '</font>';

                return html;
            });

        // 部门
        row.addCell(
            null, 
            '' + userDepartment(jsonData.items[i].dbs_departmentid) + ''
            );

        // 角色
        row.addCell(
            {width: '100'}, 
            '' + userRole(jsonData.items[i].dbs_roleid) + ''
            );

        // 状态
        row.addCell(
            {width: '50'}, 
            '' + userStatus(jsonData.items[i].dbs_status) + ''
            );

        // 操作
        row.addCell(
            {width: '250'}, 
            function() {
                var html = '';

                html += '<div class=\"datalist-action\">';
                html += '<span class=\"button\" onClick=\"fastui.windowPopup(\'user-modify\', \'' + lang.user.button['modify'] + ' - ' + jsonData.items[i].dbs_username + '\', \'/web/admin/user/user-modify.html?id=' + jsonData.items[i].dbs_id + '\', 800, 500);\">' + lang.user.button['modify'] + '</span>';
                html += '<span class=\"button\" onClick=\"fastui.windowPopup(\'user-transfer\', \'' + lang.user.button['transfer'] + ' - ' + jsonData.items[i].dbs_username + '\', \'/web/admin/user/user-transfer.html?id=' + jsonData.items[i].dbs_id + '\', 800, 500);\">' + lang.user.button['transfer'] + '</span>';
                html += '<span class=\"button\" onClick=\"dataListItemRemove(' + i + ');\">' + lang.user.button['remove'] + '</span>';
                html += '</div>';

                return html;
            });

        if (i - j == fastui.list.size) {
            break;
        }
    }

    // 数据列表事件绑定(点击选择项目)
    fastui.list.bindEvent(j);

    // 数据列表分块加载(应用于数据重载)
    fastui.list.scrollLoadBlock(field, reverse, primer, i);
}


// 获取用户部门
function userDepartment(id) {
    if (typeof(jsonDataDepartment) == 'undefined') {
        return '';
    }

    try {
        var items = new Array();
        var department = '';
        var i;
        var j;

        items = id.split('/');

        for (i = 1; i < items.length - 1; i++) {
            for (j = 0; j < jsonDataDepartment.length; j++) {
                if (jsonDataDepartment[j].dbs_id == items[i]) {
                    department += jsonDataDepartment[j].dbs_name + ' / ';
                    break;
                }
            }
        }

        return department.substring(0, department.length - 3);
        } catch(e) {}
}


// 获取用户角色
function userRole(id) {
    if (typeof(jsonDataRole) == 'undefined') {
        return '';
    }

    try {
        var i;

        for (i = 0; i < jsonDataRole.length; i++) {
            if (jsonDataRole[i].dbs_id == id) {
                return jsonDataRole[i].dbs_name;
            }
        }

        return '';
        } catch(e) {}
}


// 获取用户状态
function userStatus(status) {
    try {

        if (status == 1) {
            return lang.user.data.status['job-on'];
        } else if (status == 0) {
            return lang.user.data.status['job-off'];
        }
        } catch(e) {}
}