var page = {
    submitFlag: 0,
    fileDataGrid: {},
    fileUpload: function (v1, v2) {
        var filebox = $("#fileInput").filebox("files");
        if (filebox.length == 1) {
            var formData = new FormData();
            formData.append("file", filebox[0]);
            fileUploadAjaxWithCall(formData, (d) => {
                let tmp = {
                    fileId: d.id,
                    fileName: d.fileName
                }
                page.fileDataGrid.datagrid("appendRow", tmp);
                $("#fileInput").filebox("clear")
            })
        }

    },
    initData: function () {
        var columns = [[
            {
                field: 'ck',
                checkbox: true
            },
            {
                field: 'fileName',
                title: '文件名称',
                width: 450,
                align: 'left'
            },
            {
                field: 'id',
                title: '操作',
                width: 70,
                align: 'left',
                formatter: function (value, row, index) {
                    return '<a href="#" onclick="previewFileForm(undefined,\'' + row.fileName + '\',\'' + row.fileId + '\')">预览</a>'
                }
            }]];
        var toolbars = [{
            text: '上传',
            iconCls: 'icon-add',
            hidden: true,
            handler: () => {
                $("#fileUpload .filebox-label").click();
            }
        }, {
            text: '删除',
            iconCls: 'icon-remove',
            hidden: true,
            handler: () => {
                var rows = page.fileDataGrid.datagrid("getChecked");
                if (rows.length <= 0) {
                    topWin.layer.alert("请选择需要删除的附件!");
                    return;
                }
                for (var i = rows.length - 1; i >= 0; i--) {
                    var rowIndex = page.fileDataGrid.datagrid('getRowIndex', rows[i]);
                    page.fileDataGrid.datagrid('deleteRow', rowIndex);
                }
            }
        }];

        page.fileDataGrid = $("#file_table").datagrid({
            iconCls: 'icon-list',
            width: '100%',
            height: $(window).height() - $('.ui-form-row').outerHeight() - 30,
            singleSelect: false,
            pagination: false,
            loadMsg: "数据加载中......",
            rownumbers: true,
            fitColumns: false,
            nowrap: true,
            remoteSort: true,
            striped: true,
            columns: columns
        });
        if (readonly == '1') {
            $('.easyui-textbox').textbox('readonly', true);
            $('.easyui-radiobutton').radiobutton('disable');
            if (isSupplier === '1') {
                $(".all_flag_html").hide()
            }
        } else {
            $("#file_table").datagrid({
                toolbar: toolbars
            });
        }
        if (id != 0) {
            $.ajax({
                url: g.ctx + "pc/tender/question/rest/detail",
                data: {id: id}, success: function (d) {
                    $("#questionForm").form("load", d)
                    if (d.sendType === 1) {
                        $(".all_flag_html").hide()
                    }
                    var files = d.files;
                    $.each(files, function (index, item) {
                        page.fileDataGrid.datagrid("appendRow", item);
                    })
                }
            })
        } else {
            $(".all_flag_html").hide()
        }
        if (type == '1') {
            $("input[textboxname='answer']").textbox({required: false})
            $("#answer_html").hide();
        }
    },
    save: function (call) {
        if (page.submitFlag === 1) {
            return
        }
        if (!$("#questionForm").form('validate')) {
            topWin.layer.msg("必填项不得为空!");
            return false;
        }
        let data = $("#questionForm").serializeJson();
        var files = page.fileDataGrid.datagrid("getRows");
        var ids = files.map(a => a.fileId);
        data['tenderId'] = tenderId
        data['id'] = id
        data['questionFileId'] = ids.join(',')
        page.submitFlag = 1;
        $.ajax({
            url: g.ctx + "pc/tender/question/rest/save",
            type: "POST",
            contentType: "application/json",
            data: JSON.stringify(data),
            complete: function () {
                page.submitFlag = 0
            },
            success: function (d) {
                call();
            },
            error: function (data) {
                topWin.layer.alert(data.responseJSON.message, {icon: 2});
            }
        });
    }
}
$(function () {
    page.initData();
})