var edit = {
    //保存数据
    saveOrUpdate: function (callback) {
        debugger;
        if(!$("#tender").form('validate')) return;
        if (tenderComm.submitFlag == 1) {
            return;
        }
        var postData = tenderComm.getFormData();
        if (postData['round'].startDay != undefined && postData['round'].endDay != undefined) {
            // 将字符串解析为moment对象
            let moment1 = moment(postData['round'].startDay, "YYYY-MM-DD HH:mm");
            let moment2 = moment(postData['round'].endDay, "YYYY-MM-DD HH:mm");

            if (moment1.isAfter(moment2)) {
                topWin.layer.msg("投标开始时间不能在投标截止时间之后!")
                return;
            }
        }

        //进行为空校验
        const requiredColumns = ['purchaseCategoryCode'];
        const missingColumns = requiredColumns.filter(col =>
            !postData.bidSupplierDtos.some(row => row.hasOwnProperty(col))
        );

        if (missingColumns.length > 0) {
            $('#tbb').tabs('select', '供应商清单');
            topWin.layer.msg("供应商信息：<span style='color: red;'>" + "采购品类编码必须选择" + "</span>", {icon: 2});
            return;
        }
        for (var i = 0; i < postData.bidSupplierDtos.length; i++) {
            var row = postData.bidSupplierDtos[i];
            for (var prop in row) {
                debugger;
                console.log(prop);
                if(prop == "purchaseCategoryCode"){
                    if (row[prop] == undefined || row[prop] === '') {
                        $('#tbb').tabs('select', '供应商清单');
                        topWin.layer.msg("供应商信息:" + (i + 1) + "行，<span style='color: red;'>" + "采购品类编码" + "</span>不能为空!", {icon: 2});
                        return;
                    }
                }

            }
        }

        let htAmount = 0;
        var enablePurchaseNumTotal = 0;
        //采购数量校验，不能大于可采购数量
        for (var i = 0; i < postData.tenderDetails.length; i++) {
            var row = postData.tenderDetails[i];
            debugger;
            if (row.purchaseBillCode && row.purchaseBillItemCode) {
                enablePurchaseNumTotal = row.enablePurchaseNum;
                htAmount = accAdd(htAmount, row.count);
                if (Number(row.count) > Number(row.enablePurchaseNum)) {
                    layer.msg('投标项目第' + (i + 1) + '行，采购数量:'+row.count+'不能大于可采购数量:'+row.enablePurchaseNum);
                    return;
                }
            }
        }

        if (htAmount > enablePurchaseNumTotal) {
            layer.msg('采购总数量:'+htAmount+'不能大于可采购数量:'+enablePurchaseNumTotal);
            return;
        }


        tenderComm.submitFlag = 1;
        var index = topWin.layer.load(0, {
            shade: [0.6, '#eee'] //0.1透明度的白色背景
        });
        $.ajax({
            url: g.ctx + 'pc/tender/rest/saveOrUpdate',
            type: "POST",
            contentType: 'application/json',
            data: JSON.stringify(postData),
            complete: function () {
                tenderComm.submitFlag = 0
                topWin.layer.close(index)
            },
            success: function (data) {
                topWin.layer.alert("保存成功", {icon: 1}, function (idx) {
                    topWin.layer.close(idx);
                    callback()
                });
            },
            error: function (data) {
                topWin.layer.alert(data.responseJSON.message, {icon: 2});
            }
        });
    },
    copy: function (field) {
        tenderComm.projectDataGrid.edatagrid("saveRow");
        var rows = tenderComm.projectDataGrid.edatagrid("getRows");
        var row = {}
        //取第一个
        var val = rows[0][field]
        row[field] = val
        $.each(rows, function (index) {
                tenderComm.projectDataGrid.datagrid("updateRow", {
                    index: index, row: row
                });
            }
        )
    }
}
$(function () {
    if (id != 0) {
        if (type !== 0) {
            tenderComm.editFristTime = true
        }
        tenderComm.eventBind();
        tenderComm.loadData(id);
    } else {
        tenderComm.eventBind();
        if (optType != 0) {
            tenderComm.editFristTime = true
            tenderComm.loadPurchaseDataData(purchaseIds);
        } else {
            tenderComm.initItemDataGrid();
            tenderComm.typeIdChange();
            tenderComm.initSupplierDataGrid([], 0);
            tenderComm.initOpenBidUserDatagird([]);
            tenderComm.initAttanchmentDatagrid([]);
            $("input[name='securityDeposit.securityDepositFlag'][value='0']").click();
        }
        setTimeout(function () {
            $("#tender").form("load", {'tender.thresholdFlag': '0'})
        }, 100)
    }
});