/**
 *
 * @author BOGON
 * @version 1.0
 */

var page = {
    initData: function () {
        $("#tb").datagrid({
            height: document.documentElement.clientHeight - $("#searchForm").outerHeight()-10,
            url: g.ctx + 'pc/supplier/questionnaire/rest/list',
            method: "GET",
            singleSelect: select == 1 ? false : true,
            pagination: true,
            autoHeight: true,
            loadMsg: "数据加载中......",
            rownumbers: true,
            fitColumns: false,
            nowrap: true,
            remoteSort: true,
            striped: true,
            page: 1,
            pageSize: 10,
            queryParams: {},
            columns: [[
                {
                    field: 'ck',
                    checkbox: 'true'
                },
                {
                    field: 'questionCode',
                    title: '问卷编码',
                    width: '120',
                    align: 'left'
                },
                {
                    field: 'questionTitle',
                    title: '问卷标题',
                    width: '160',
                    align: 'left'
                },
                {
                    field: 'questionDesc',
                    title: '问卷描述',
                    width: '240',
                    align: 'left'
                },
                {
                    field: 'answerType',
                    title: '问卷类型',
                    width: '120',
                    align: 'left',
                    formatter: function (value) {
                        switch (value) {
                            case 1:
                                return '单选';
                            case 2:
                                return '多选';
                            case 3:
                                return '文本';
                        }
                    }
                },
                {
                    field: 'status',
                    title: '状态',
                    width: '120',
                    align: 'left',
                    formatter: function (value) {
                        switch (value) {
                            case 1:
                                return '<span style="color: green">正常</span>';
                            case 0:
                                return '<span style="color: red">冻结</span>';

                        }
                    }
                }
            ]],
            toolbar: [
                {
                    text: '新增',
                    id: 'btn-add',
                    iconCls: 'icon-add',
                    handler: function () {
                        page.detailDialog();
                    }
                }, {
                    text: '编辑',
                    id: 'btn-edit',
                    iconCls: 'icon-edit',
                    handler: function () {
                        var row = $('#tb').datagrid('getChecked');
                        if (row.length <= 0) {
                            topWin.layer.msg("请选中一条记录");
                            return;
                        }
                        page.detailDialog(row[0].id);
                    }
                }, {
                    text: '查看',
                    id: 'btn-look',
                    iconCls: 'icon-large-smartart',
                    handler: function () {
                        var row = $('#tb').datagrid('getChecked');
                        if (row.length <= 0) {
                            topWin.layer.msg("请选中一条记录");
                            return;
                        }
                        page.detailDialog(row[0].id, true);
                    }
                }, {
                    text: '删除',
                    id: 'btn-remove',
                    iconCls: 'icon-remove',
                    handler: function () {
                        var row = $('#tb').datagrid('getChecked');
                        if (row.length <= 0) {
                            topWin.layer.msg("请选中一条记录");
                            return;
                        }
                        topWin.layer.confirm(tipsi18n.sfsc,{icon: 3}, function (index) {
                            $.ajax({
                                url: g.ctx + "pc/supplier/questionnaire/rest/remove",
                                data: {id: row[0].id},
                                type: "POST",
                                success: function (data) {
                                    topWin.layer.alert("保存成功", {icon: 1}, function (idx) {
                                        topWin.layer.close(idx);
                                        $('#tb').datagrid('reload');
                                    });
                                },
                                error: function (data) {
                                    topWin.layer.alert(data.responseJSON.message, {icon: 2});
                                }
                            })
                        })
                    }
                }, {
                    text: '冻结|解冻',
                    id: 'btn-lock',
                    iconCls: 'icon-lock',
                    handler: function () {
                        page.updateStatus();
                    }
                }
            ]
        });
    },
    detailDialog: function (id, lookupFlag) {
        let title = ''
        let btns = [{
            text: itablei8n.close, handler: function () {
                questionnaireEditDialog.dialog('close');
            }
        }]
        if (id != undefined && lookupFlag != undefined) {
            title = '查看'
        } else if (id != undefined) {
            title = '编辑'
            btns.unshift({
                text: itablei8n.save, handler: function () {
                    topWin.frames['questionnaireEditDialog'].page.saveOrUpdate(function () {
                        questionnaireEditDialog.dialog('close');
                        $('#tb').datagrid('reload');
                    });
                }
            })
        } else {
            title = '新增'
            btns.unshift({
                text: itablei8n.save, handler: function () {
                    topWin.frames['questionnaireEditDialog'].page.saveOrUpdate(function () {
                        questionnaireEditDialog.dialog('close');
                        $('#tb').datagrid('reload');
                    });
                }
            })
        }
        let url = g.ctx + 'pc/supplier/questionnaire/page/detail?lookupFlag=' + (lookupFlag == undefined ? '0' : '1') + '&id=' + (id == undefined ? '' : id);
        var questionnaireEditDialog = openDialog('questionnaireEditDialog', url, {
            title: title,
            width: '750px',
            height: '100%',
            cache: false,
            modal: true,
            border: false,
            draggable: true,
            shadow: false,
            onClose: function () {
                questionnaireEditDialog.dialog('destroy');
            },
            buttons: btns
        });
    },
    updateStatus: function () {
        let row = $('#tb').datagrid('getChecked');
        if (row.length <= 0) {
            topWin.layer.msg("请选中一条记录");
            return;
        }
        let title = ''
        if (row[0].status == 1) {
            title = '冻结'
        } else {
            title = '解冻'
        }
        topWin.layer.confirm('是否' + title,{icon: 3}, function (index) {
            $.ajax({
                url: g.ctx + "pc/supplier/questionnaire/rest/updateStatus",
                data: {id: row[0].id},
                type: "POST",
                success: function (data) {
                    topWin.layer.alert(title + "成功", {icon: 1}, function (idx) {
                        topWin.layer.close(idx);
                        $('#tb').datagrid('reload');
                    });
                },
                error: function (data) {
                    topWin.layer.alert(data.responseJSON.message, {icon: 2});
                }
            })
        })
    },
    getChecked: function (call) {
        var rows = $('#tb').datagrid('getChecked');
        if (rows.length <= 0) {
            topWin.layer.msg("请选中至少一条记录");
            return;
        }
        call(rows)
    }

}
$(function () {
    page.initData()
})