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

var page = {
    submitFlag: 0,
    /**
     * 初始化整改措施表格
     */
    initDataGrid: function (noticeId) {

        let height = document.documentElement.clientHeight - $("#dataForm").outerHeight() - 20;
        var defaults = {
            url: g.ctx + 'pc/spe/rectifiedNotice/rest/measuresList',
            queryParams: {
                noticeId: noticeId
            },
            loadMsg: '数据远程载入中,请等待...',
            singleSelect: true,
            pagination: false,
            checkbox: true,
            method: "GET",
            rownumbers: true,
            height: height <= 350 ? 350 : height,
            autoSave: true
        };

        var columns = [
            {
                field: 'ck',
                checkbox: true
            },
            {
                field: 'id',
                hidden: true
            },
            {
                field: 'noticeOrderId',
                hidden: true
            },
            {
                field: 'measuresContent',
                title: '处理措施',
                width: 360
            },
            {
                field: 'personLiable',
                title: '责任人',
                width: 90
            },
            {
                field: 'attachments',
                title: '附件操作',
                width: 90,
                align: 'center',
                formatter: function (value, row, index) {
                    return '<a href="javascript:lookAttachment(' + row.id + ')" >查看</a>';
                }
            },
            {
                field: 'planCompleteTime',
                title: '计划完成时间',
                width: 130,
                formatter: function (value) {
                    if (value) {
                        return moment(value).format('YYYY-MM-DD');
                    }
                    return '';
                }
            },
            {
                field: 'actualCompleteTime',
                title: '完成时间',
                width: 140,
                formatter: function (value) {
                    if (value) {
                        return moment(value).format('YYYY-MM-DD HH:mm:ss');
                    }
                    return '';
                }
            },
            {
                field: 'status',
                title: '状态',
                width: 80,
                formatter: function (value) {
                    if (value === 0) {
                        return 'open';
                    }
                    if (value === 1) {
                        return 'close';
                    }
                }
            }
        ];

        defaults.toolbar = [
            {
                text: '添加',
                iconCls: 'icon-add',
                handler: function () {
                    $("#tbMeasures").edatagrid('addRow', {
                        id: 0,
                        noticeOrderId: noticeId,
                        indexRemark: '',
                        measuresContent: '',
                        personLiable: '',
                        measuresAttachment: '',
                        actualCompleteTime: '',
                        status: 0
                    });
                }
            }, "-",
            {
                text: '删除',
                iconCls: 'icon-remove',
                handler: function () {
                    var rows = $("#tbMeasures").edatagrid('getSelections');
                    if (rows.length === 0) {
                        topWin.layer.msg('请选中记录进行操作');
                        return false;
                    }
                    for (var i = 0; i < rows.length; i++) {
                        var rowIndex = $("#tbMeasures").datagrid('getRowIndex', rows[i]);
                        $("#tbMeasures").datagrid('updateRow', {
                            index: rowIndex,
                            row: {noticeOrderId: -1}
                        });
                        $("#tbMeasures").datagrid('deleteRow', rowIndex);
                    }
                }
            }
        ];
        columns = [
            {
                field: 'ck',
                checkbox: true
            },
            {
                field: 'id',
                hidden: true
            },
            {
                field: 'noticeOrderId',
                hidden: true
            },

            {
                field: 'indexRemark',
                title: '不合格项',
                width: 360,
                editor: {
                    type: 'textbox',
                    options: {
                        prompt: '请输入不合格项'
                    }
                }
            },
            {
                field: 'measuresContent',
                title: '处理措施',
                width: 360,
                editor: {
                    type: 'textbox',
                    options: {
                        prompt: '请输入处理措施'
                    }
                }
            },
            {
                field: 'personLiable',
                title: '责任人',
                width: 90,
                editor: {
                    type: 'textbox',
                    options: {
                        prompt: '请输入责任人'
                    }
                }
            },
            {
                field: 'attachments',
                title: '附件操作',
                width: 90,
                align: 'center',
                formatter: function (value, row, index) {
                    return '<a href="javascript:" onclick="page.uploadRequireFileUI(' + index + ')">操作</a>';
                }
            },
            {
                field: 'planCompleteTime',
                title: '计划完成时间',
                width: 130,
                // formatter: function (value) {
                //     if (value) {
                //         return moment(value).format('YYYY-MM-DD');
                //     }
                //     return '';
                // },
                editor: {
                    type: 'datetimebox',
                    options: {
                        prompt: '请选择计划完成时间',
                        panelWidth: '160',
                        panelHeight: '250',
                        editable: false
                    }
                }
            },
            {
                field: 'actualCompleteTime',
                title: '完成时间',
                width: 140,
                formatter: function (value) {
                    if (value) {
                        return moment(value).format('YYYY-MM-DD HH:mm:ss');
                    }
                    return '';
                }
            },
            {
                field: 'status',
                title: '状态',
                width: 80,
                formatter: function (value) {
                    if ((value * 1) === 0) {
                        return 'open';
                    }
                    if ((value * 1) === 1) {
                        return 'close';
                    }
                },
                editor: {
                    type: 'combobox',
                    options: {
                        valueField: 'value',
                        textField: 'label',
                        data: [{label: 'open', value: 0}, {label: 'close', value: 1}],
                        editable: false,
                        panelHeight: 'auto',
                        onSelect: function (data) {
                            if (data.value === 1) {
                                var row = $("#tbMeasures").edatagrid('getSelected');
                                if (null != row) {
                                    row.actualCompleteTime = Date.parse(new Date());
                                }
                            }
                        }
                    }
                }
            }
        ];
        defaults.columns = [columns];
        var settings = jQuery.extend(true, {}, defaults);
        $("#tbMeasures").edatagrid(settings);
    },
    //附件上传
    uploadRequireFileUI: function (rec) {
        var rows = $("#tbMeasures").datagrid('getRows');
        var row = rows[rec];
        openFileUploadDialog("noticeFileUploadDialog", {
            title: "附件上传",
            fileIds: row.attachments,
            callBack: function (fileIds) {
                page.returnAddRequirefileUpload(fileIds.join(","))
            }
        })
    },
    // 选择附件回调
    returnAddRequirefileUpload: function (ids) {
        var rows = $("#tbMeasures").datagrid('getSelected');
        var inx = $("#tbMeasures").datagrid('getRowIndex', rows);
        $("#tbMeasures").datagrid('endEdit', inx);
        $("#tbMeasures").datagrid('updateRow', {
            index: inx,
            row: {
                attachments: ids
            }
        });
        $("#tbMeasures").datagrid('beginEdit', inx);
    },
    updateData: function (callback) {
        if (page.submitFlag === 1) {
            return
        }
        page.submitFlag = 1
        var load = topWin.layer.load(2);
        $("#tbMeasures").edatagrid('saveRow');
        let rows = $("#tbMeasures").datagrid('getRows');
        var data = {}; // 创建一个空对象来存储数据
        $('.ap_re').each(function () {
            var name = $(this).attr('name');
            var value = $(this).val();
            data[name] = value;
        });
        var formData = $('#dataForm').serializeJson();
        formData['reportJson'] = JSON.stringify(data)
        formData['measures'] = rows
        $.ajax({
            type: 'POST',
            url: g.ctx + 'pc/spe/rectifiedNotice/rest/updateNotice',
            contentType: 'application/json',
            data: JSON.stringify(formData),
            complete: function () {
                topWin.layer.close(load)
                page.submitFlag = 0
            },
            success: function () {
                topWin.layer.msg('操作成功', {icon: 1, time: 500}, function () {
                    callback();
                });
            },
            error: function (data) {
                topWin.layer.alert(data.responseJSON.message, {icon: 2});
            }
        });
    }
}
$(function () {
    page.initDataGrid($("#id").val())
})