
let el = {
    tb: '#tb',
    useState: '#useState',
}

$(function () {
    initPageSelect();
    loadGrid();
});
/**
 * 初始化页面下拉框数据
 */
function initPageSelect() {
    // 初始化使用状态下拉框
    $(el.useState).combobox({
        valueField: 'value',
        textField: 'label',
        value: -1,
        data: [
            {label: '全部', value: -1},
            {label: '使用', value: 0},
            {label: '未使用', value: 1}
        ],
        editable: false,
        prompt: '-- 请选择 --',
        panelHeight: 'auto'
    });
}

function loadGrid() {
    $('#tb').datagrid({
        url: g.ctx + "pc/tender/expertManage/rest/list",
        iconCls: 'icon-list',
        title: '',
        height: gridheight,
        width: gridwidth,
        singleSelect: true,
        pagination: true,
        idField: 'id',
        autoHeight: true,
        loadMsg: "数据加载中......",
        rownumbers: true,
        fitColumns: false,
        remoteSort: true,
        columns: [[
            {
                field: 'ck',
                checkbox: true
            },
            {
                field: 'orgCode',
                hidden: true
            },
            {
                field: 'orgName',
                title: '部门',
                width: 80,
                align: 'left'
            },
            {
                field: 'workNo',
                title: '登录名',
                width: 80,
                align: 'left'
            }, {
                field: 'name',
                title: '姓名',
                width: 120,
                align: 'left'
            },
            {
                field: 'email',
                title: '邮箱地址',
                width: 120,
                align: 'left'
            },
            {
                field: 'mobile',
                title: '手机号码',
                width: 120,
                align: 'left'
            },
            {
                field: 'typeNames',
                title: '打分维度',
                width: 150,
                align: 'left'
            }
        ]],
        toolbar: ["-", {
            text: '新增',
            iconCls: 'icon-add',
            hidden: true,
            handler: function () {
                addOrEdit(0);
            }
        }, "-", {
            text: '编辑',
            iconCls: 'icon-edit',
            handler: function () {
                addOrEdit(1);
            }
        }, "-", {
            text: '删除',
            hide: true,
            iconCls: 'icon-remove',
            handler: function () {
                delFactory();
            }
        }]
    });
}


function addOrEdit(flag) {
    var id= '';
    if(flag===1){
        var cbox = $('#tb').datagrid('getSelected');
        if (cbox === null || cbox === undefined) {
            layer.msg("请选中一条记录");
            return;
        }
        id = cbox.id;
    }

    var moldFormDialog = openDialog('moldForm', g.ctx + 'pc/tender/expertManage/detail?id=' + id, {
        title: flag===0?'新增':'编辑',
        width: '900px',
        height: '520px',
        cache: false,
        modal: true,
        border: false,
        draggable: false,
        shadow: false,
        onClose: function () {
            moldFormDialog.dialog('destroy');
        },
        buttons: [
            {
                text: itablei8n.save,
                iconCls: 'icon-save',
                handler: function () {
                    topWin.frames['moldForm'].saveOrUpdate(function () {
                        topWin.layer.msg('数据保存成功', {icon: 1});
                        moldFormDialog.dialog('close');
                        $(el.tb).datagrid('reload');
                    });
                }
            },
            {
                text: itablei8n.close,
                iconCls: 'icon-cancel',
                handler: function () {
                    moldFormDialog.dialog('close');
                }
            }
        ]
    });
}


function delFactory() {
    var cbox = $('#tb').datagrid('getSelected');
    if (cbox === null || cbox === undefined) {
        layer.msg("请选中一条记录");
        return;
    }

    var moldForm = cbox.moldCode;

    parent.layer.confirm('是否删除？', function (index) {
        var loading = top.layer.msg('处理中', {icon: 16, shade: [0.1, '#000'], time: false});
        var url = g.ctx + 'pc/sys/mold/moldForm/rest/remove';
        $.ajax({
            url: url,
            method: "post",
            data: {moldCode: moldForm},
            success: function (data) {
                parent.layer.close(index);
                $(el.tb).datagrid('reload');
            },
            complete: function(data) {
                top.layer.close(loading);
            },
            error: function (data) {
                layer.close(index);
                layer.alert(JSON.parse(data.responseText).message);
            }
        });
    });
}