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

var page = {
    lookupFlag: 0,
    answerTypeChange: function (val, oldVal) {
        if (val === 1 || val === 2) {
            $(".answer-div").show();
            page.loadGrid();
        } else {
            $(".answer-div").hide();
        }
    },
    loadGrid: function () {
        let url;
        if ($("#id").val()) {
            url = g.ctx + 'pc/supplier/questionnaire/rest/item/list';
        }
        let toolbar = []
        if (page.lookupFlag !== '1') {
            toolbar = [
                {
                    text: '新增',
                    iconCls: 'icon-add',
                    handler: function () {
                        $("#tb").edatagrid("appendRow", {})
                    }
                },
                {
                    text: '删除',
                    iconCls: 'icon-remove',
                    handler: function () {
                        page.tableDelete()
                    }
                }, {
                    text: '取消删除',
                    iconCls: 'icon-ok',
                    handler: function () {
                        page.tableCancel();
                    }
                }]
        }
        $("#tb").edatagrid({
            url: url,
            method: "GET",
            iconCls: 'icon-list',
            height: document.documentElement.clientHeight - 365,
            autoSave: true,
            singleSelect: true,
            autoHeight: true,
            loadMsg: "数据加载中......",
            rownumbers: true,
            fitColumns: true,
            nowrap: true,
            remoteSort: true,
            striped: true,
            queryParams: {
                questionnaireId: $("#id").val()
            },
            columns: [[
                {
                    field: 'state',
                    title: '类型',
                    width: '20%',
                    align: 'left',
                    formatter: function (value) {
                        switch (value) {
                            case 1:
                                return '<span style="color:blue;">新增</span>';
                            case 2:
                                return '<span style="color:green;">编辑</span>';
                            case 3:
                                return '<span style="color:red;">删除</span>';
                            default:
                                return '初始';
                        }
                    }
                },
                {
                    field: 'answerCode',
                    title: '选项编码',
                    width: '30%',
                    align: 'left',
                    editor: {
                        type: 'textbox',
                        options: {
                            required: true
                        }
                    }
                },
                {
                    field: 'answerContent',
                    title: '选项内容',
                    width: '45%',
                    align: 'left',
                    editor: {
                        type: 'textbox',
                        options: {
                            required: true
                        }
                    }
                }
            ]],
            toolbar: toolbar
        });
    },
    /**
     * 表格删除
     * @param tb 表格el
     */
    tableDelete: function () {
        let row = $("#tb").datagrid('getSelected');
        if (row) {
            let index = $("#tb").datagrid('getRowIndex', row);
            $("#tb").datagrid('updateRow', {index: index, row: {state: 3}});
        }
    },
    /**
     * 表格删除取消
     * @param tb 表格el
     */
    tableCancel: function () {
        let row = $("#tb").datagrid('getSelected');
        if (row) {
            let index = $("#tb").datagrid('getRowIndex', row);
            if (row.state && row.state === 3) {
                $("#tb").datagrid('updateRow', {index: index, row: {state: 2}});
            }
        }
    },
    saveOrUpdate: function (call) {
        let val = $("#answerType").combobox("getValue")
        var rows = []
        if (val === '1' || val === '2') {
            $("#tb").edatagrid('saveRow');
            var rows = $("#tb").datagrid('getRows').filter(item => item.state !== 3);
            let tipMsg = []
            $.each(rows, function (idx, itm) {
                if (!itm.answerCode) {
                    tipMsg.push('第' + (idx + 1) + '行选项编码不能为空')
                }
                if (!itm.answerContent) {
                    tipMsg.push('第' + (idx + 1) + '行选项内容不能为空')
                }
            })
            if (rows.length <= 0) {
                topWin.layer.msg('请添加选项!')
                return
            }
            if (tipMsg.length > 0) {
                topWin.layer.msg(tipMsg.join("<br/>"))
                return
            }
        }

        let validate = $('#form').form('validate');
        if (!validate) {
            topWin.layer.msg("必填项不得为空!");
            return false;
        }
        var formData = $('#form').serializeJson();
        formData['item'] = rows
        $.ajax({
            url: g.ctx + 'pc/supplier/questionnaire/rest/saveOrUpdate',
            method: "post",
            contentType: 'application/json',
            data: JSON.stringify(formData),
            success: function (data) {
                topWin.layer.alert("保存成功", {icon: 1}, function (idx) {
                    topWin.layer.close(idx);
                    call()
                });
            },
            error: function (data) {
                topWin.layer.alert(data.responseJSON.message, {icon: 2});
            }
        });
    }
}
$(function () {
    let val = $("#answerType").combobox("getValue")
    page.lookupFlag = $("#lookupFlag").val()
    if (val === '1' || val === '2') {
        $(".answer-div").show();
        page.loadGrid();
    } else {
        $(".answer-div").hide();
    }
})