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

var page = {
    submitFlag: 0,
    scoreFlag: 0,
    mroFlag: 0,
    loadData: function (call) {
        tenderComm.loadDataAjax(id, function (d) {
            const fd = {}
            $.each(d.tender, function (idx, itm) {
                fd[tenderComm.tenderFormPrefix + idx] = itm + ""
            })
            page.mroFlag = d.tender.templateId === '412'//412:MRO招标模板
            tenderComm.initAttanchmentDatagrid(d.tenderAttachmentList, true);
            $("#tender").form("load", fd)
            //查看和审核所有控件只读
            // 设置所有 datetimebox 为只读模式
            $('.easyui-datetimebox').datetimebox('readonly', true);

            // 设置所有 NumberBox 为只读模式
            $('.easyui-numberbox').numberbox('readonly', true);

            // 设置所有 TextBox 为只读模式
            $('.easyui-textbox').textbox('readonly', true);

            // 设置所有 ComboBox 为只读模式
            $('.easyui-combobox').combobox('readonly', true);

            //如果是定标界面需要把定标备注改成可写
            if (!benchmarksComm.isReadOnly()) {
                $('input[textboxname="tender.bidRemark"]').textbox('readonly', false);
                $('input[textboxname="tender.spotFlag"]').textbox('readonly', false);
                $('input[textboxname="tender.businessAgreeFlag"]').textbox('readonly', false);
                $('input[textboxname="tender.customerBearFlag"]').textbox('readonly', false);
                $('input[textboxname="tender.bearRatio"]').textbox('readonly', false);
            }
            var typeId = $("#tender input[comboname='" + tenderComm.tenderFormPrefix + "typeId']").combobox('getValue');
            if (typeId) {
                var url2 = g.ctx + 'pc/tender/template/rest/listAll?typeId=' + typeId;
                $("#tender input[comboname='" + tenderComm.tenderFormPrefix + "templateId']").combobox("reload", url2);
            }
            page.scoreFlag = d.round.scoreFlag
            if (call !== undefined) {
                call(d)
            }
        })
    },
    saveOrUpdate: function (call, sumitFlag) {
        debugger;
        if (page.submitFlag == 1) {
            return
        }
        if (!$('#tender').form("validate")) {
            return;
        }
        var postData = {
            bidRemark: $('input[textboxname="tender.bidRemark"]').textbox('getValue'),
            id: id,
            purchaseCategoryCode: $('input[comboname="tender.purchaseCategoryCode"]').combobox("getValue"),
            spotFlag: $('input[comboname="tender.spotFlag"]').combobox("getValue"),
            businessAgreeFlag: $('input[comboname="tender.businessAgreeFlag"]').combobox("getValue"),
            customerBearFlag: $('input[comboname="tender.customerBearFlag"]').combobox("getValue"),
            bearRatio: $('input[numberboxname="tender.bearRatio"]').numberbox("getValue"),
            submitFlag: sumitFlag
        }
        postData['bidedFileIds'] = $("#fileIds").attr('data-fileIds')//定标附件
        benchmarksComm.tenderDetailDataGrid.edatagrid("saveRow")
        postData.bidDetails = benchmarksComm.tenderDetailDataGrid.datagrid("getRows");
        //需要判断是否全部议价截止
        if (sumitFlag === 1) {
            const ids = postData.bidDetails.map(row => row.id);
            var filteredRows = postData.bidDetails.filter(function (row) {
                return row.bargainingStatus === 1;
            });
            if (filteredRows.length > 0) {
                topWin.layer.alert("请先截止议价再定标!", {icon: 2});
                return;
            }
        }
        debugger;
        //需要判断是否选中了中标供应商
        let bidedRows = postData.bidDetails.filter(a => (a.status + "") === "1");
        if (bidedRows.length === 0 && sumitFlag === 1) {
            topWin.layer.alert("请选择中标供应商再定标!", {icon: 2});
            return;
        }
        var notOk = postData.bidDetails.filter(function (row) {
            return row.ok === 0;
        });
        //2025-03-20注释qzc
        // if (notOk.length > 0) {
        //     topWin.layer.alert("明细价格与总价格不一致，请议价调整!", {icon: 2});
        //     return;
        // }
        var index = topWin.layer.load(0, {
            shade: [0.6, '#eee'] //0.1透明度的白色背景
        });
        page.submitFlag = 1
        $.ajax({
            url: g.ctx + 'pc/tender/benchmarks/rest/submitBenchmarks',
            type: "POST",
            contentType: 'application/json',
            data: JSON.stringify(postData),
            complete: function () {
                page.submitFlag = 0
                topWin.layer.close(index)
            },
            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});
            }
        });

    },
    release: function (call) {
        var postData = {
            id: id
        }
        topWin.layer.confirm('确定要招标结果吗?', {icon: 3}, function (iindex) {
            var index = topWin.layer.load(0, {
                shade: [0.6, '#eee'] //0.1透明度的白色背景
            });
            page.submitFlag = 1
            $.ajax({
                url: g.ctx + 'pc/tender/benchmarks/rest/releaseBenchmarks',
                type: "POST",
                data: postData,
                complete: function () {
                    page.submitFlag = 0
                    topWin.layer.close(index)
                    topWin.layer.close(iindex)

                },
                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});
                }
            });
        })


    },
    spotFlagChange: function (data) {
        if (Number(data) === 1) {
            let val = $('input[textboxname="tender.businessAgreeFlag"]').combobox("getValue")
            $('input[textboxname="tender.businessAgreeFlag"]').combobox({disabled: false, required: true});
            $('input[textboxname="tender.businessAgreeFlag"]').combobox("setValue", val)
            let val1 = $('input[textboxname="tender.customerBearFlag"]').combobox("getValue")
            $('input[textboxname="tender.customerBearFlag"]').combobox({disabled: false, required: true});
            $('input[textboxname="tender.customerBearFlag"]').combobox("setValue", val1)
            $('input[textboxname="tender.bearRatio"]').numberbox({disabled: false, required: true});
        } else {
            $('input[textboxname="tender.businessAgreeFlag"]').combobox({disabled: true, required: false});
            $('input[textboxname="tender.customerBearFlag"]').combobox({disabled: true, required: false});
            $('input[textboxname="tender.bearRatio"]').numberbox({disabled: true, required: false});
            $('input[textboxname="tender.bearRatio"]').numberbox('setValue', '');
        }
    }
}
$(function () {
    page.loadData(function (d) {
        $("#fileIds").attr("data-fileIds", d.tender.bidedFileIds)
        $("#fileIds").addClass("custom-file-form custom-file-form-style")
        initFileForm('custom-file-form')
        benchmarksComm.loadField(d.tender.id, benchmarksComm.isReadOnly(),d.tender.templateId);
        benchmarksComm.initBidFileTable(id);
        benchmarksComm.loadApproverLog(id);
        setTimeout(() => {
            benchmarksComm.initRankingTable(id, d.tender.templateId);
        }, 500);
    })
})