/*
 * The Hefei Ceshi RDC(Research and Development Centre) Group.
 * __________________
 *
 *    Copyright 2015-2019
 *    All Rights Reserved.
 *
 *    NOTICE:  All information contained herein is, and remains
 *    the property of Ceshi Company and its suppliers,
 *    if any.
 */
var page = {
    submitFlag: 0,
    dataGrid: {},
    toolbars: [
        {
            text: '查看',
            iconCls: 'icon-book',
            id: "tender_look_btn",
            handler: function () {
                var row = page.dataGrid.datagrid('getSelected');
                if (row == null) {
                    topWin.layer.msg("请选择需要查看的招标单!");
                    return;
                }
                page.editTender(row.id, 4);
            }
        }, "-",
        {
            text: '新增',
            iconCls: 'icon-add',
            hidden: true,
            id: 'tender_add_btn',
            handler: function () {
                page.editTender(0, 0);
            }
        }, "-",
        {
            text: '编辑',
            iconCls: 'icon-edit',
            id: "tender_edit_btn",
            handler: function () {
                var row = page.dataGrid.datagrid('getSelected');
                if (row == null) {
                    topWin.layer.msg("请选择需要修改的招标单!");
                    return;
                }
                if (row.status != 0 && row.status != 98) {
                    topWin.layer.msg("当前状态无法编辑!");
                    return;
                }
                var id = row.id;
                page.editTender(id, 1);
            }
        }, "-",
        {
            text: '发布',
            iconCls: 'icon-storage-move',
            id: "tender_release_btn",
            handler: function () {
                var row = page.dataGrid.datagrid('getSelected');
                if (row.status === 0 || row.status === 12 || row.status === 98) {
                    var id = row.id;
                    page.editTender(id, 3);
                } else {
                    topWin.layer.msg("请选择新建或招标方案审批驳回的招标单!");
                    return;
                }
            }
        }, "-",
        {
            text: '主管审批',
            iconCls: 'icon-storage-move',
            id: "tender_approval_btn",
            handler: function () {
                var row = page.dataGrid.datagrid('getSelected');
                if (row.status === 1) {
                    var id = row.id;
                    page.editTender(id, 5);
                } else {
                    topWin.layer.msg("请选择待审批的招标单!");
                    return;
                }
            }
        }, "-",
        {
            text: '截标',
            iconCls: 'icon-plan-delete',
            id: "tender_stop_btn",
            handler: function () {
                var row = page.dataGrid.datagrid('getSelected');
                if (row == null) {
                    topWin.layer.msg("请选择需要截标的招标单!");
                    return;
                }

                if (row.status == 97) {
                    topWin.layer.msg("该状态不支持截标!");
                    return;
                }
                topWin.layer.confirm('是否截标？', function (index) {
                    $.ajax({
                        url: g.ctx + "pc/tender/rest/stop",
                        type: "POST",
                        data: {id: row.id},
                        success: function () {
                            topWin.layer.close(index);
                            topWin.layer.alert("操作成功", {
                                icon: 1,
                                closeBtn: 0,
                                yes: function (i) {
                                    topWin.layer.close(i);
                                    page.tableReload();
                                }
                            });
                        }, error: function (data) {
                            topWin.layer.alert(data.responseJSON.message, {icon: 2});
                        }
                    });
                });

            }
        }, "-",
        {
            text: '删除',
            iconCls: 'icon-remove',
            id: "tender_delete_btn",
            handler: function () {
                var row = page.dataGrid.datagrid('getSelected');
                if (row == null) {
                    topWin.layer.msg("请选择需要删除的招标单!");
                    return;
                }
                if (row.status != 0) {
                    topWin.layer.msg("当前状态无法删除!");
                    return;
                }
                topWin.layer.confirm('是否删除？', function (index) {
                    var id = row.id;
                    page.delete(id, index);
                });

            }
        }, "-",
        {
            text: '复制',
            iconCls: 'icon-plan-copy',
            id: "tender_copy_btn",
            handler: function () {
                var row = page.dataGrid.datagrid('getSelected');
                if (row == null) {
                    topWin.layer.msg("请选择需要复制的招标单!");
                    return;
                }
                var id = row.id;
                page.editTender(id, 2);
            }
        }, "-",
        {
            text: '作废',
            iconCls: 'icon-cancel',
            id: "tender_trash_btn",
            handler: function () {
                var row = page.dataGrid.datagrid('getSelected');
                if (row == null) {
                    topWin.layer.msg("请选择需要作废的招标单!");
                    return;
                }
                //状态为已定标或已完成的标，不允许作废， 2025-04-14 邓京术
                if (row.status === 5 || row.status === 9) {
                    topWin.layer.msg("当前状态无法作废!");
                    return;
                }

                var id = row.id;
                page.toVoid(id);
            }
        }, "-",
        {
            text: '投标答疑',
            iconCls: 'icon-help',
            id: "tender_help_btn",
            handler: function () {
                var row = page.dataGrid.datagrid('getSelected');
                if (row == null) {
                    topWin.layer.msg("请选择需要投标答疑的招标单!");
                    return;
                }
                tenderComm.answerQuestions(row.id, 0)
            }
        }, '-',
        {
            text: '时间修改',
            iconCls: 'icon-anchor',
            id: "tender_wait_btn",
            handler: function () {
                var row = page.dataGrid.datagrid('getSelected');
                if (row == null) {
                    topWin.layer.msg("请选择需要修改时间的招标单!");
                    return;
                }
                var url = g.ctx + 'pc/tender/page/updateTime?id=' + row.id;
                page.againDateSetDialogElement = openDialog("againDateSetDialogElement", url, {
                    title: "招标时间修改",
                    width: '440px',
                    height: '600px',
                    cache: false,
                    modal: true,
                    border: false,
                    draggable: false,
                    shadow: false,
                    onClose: function () {
                        page.againDateSetDialogElement.dialog('destroy');
                    },
                    buttons: [{
                        text: '保存',
                        handler: function () {
                            topWin.frames['againDateSetDialogElement'].page.update(() => {
                                page.againDateSetDialogElement.dialog('close');
                            });
                        }
                    }, {
                        text: '关闭',
                        handler: function () {
                            page.againDateSetDialogElement.dialog('close');
                        }
                    }]
                });
            }
        }, '-',
        {
            text: '添加供应商',
            iconCls: 'icon-add',
            id: "tender_plus_supplier_btn",
            handler: function () {
                var row = page.dataGrid.datagrid('getSelected');
                if (row == null) {
                    topWin.layer.msg("请选择需要添加供应商的招标单!");
                    return;
                }
                page.openSupplierDialog(row.id);
            }
        },'-',
        {
            text: '生成合同',
            iconCls: 'icon-sum',
            id: 'tender_contract',
            handler: function () {

                var row = page.dataGrid.datagrid('getSelected');
                if (row == null) {
                    topWin.layer.msg("请选择一条招标单!");
                    return;
                }
                if (row.status != 5) {
                    topWin.layer.msg("请选择一条已定标的招标单!");
                    return;
                }
                CREATE_CONTRACT_COMMON.lookCreateContract(CONTRACT_TYPE.ASSET, '#tb', BUSINESS_TYPE.TENDER, row.id);
            }
        },'-',
        {
            text: '生成费用合同',
            iconCls: 'icon-sum',
            id: 'btn_cost_contract',
            handler: function () {

                const row = page.dataGrid.datagrid('getSelected');
                if (row == null) {
                    topWin.layer.msg("请选择一条招标单!");
                    return;
                }
                if (row.purchaseRequestType !== "01") {
                    topWin.layer.msg("请选择费用合同类招投标的招标单！");
                    return;
                }
                if (row.status !== 5 && row.status !== 9) {
                    topWin.layer.msg("请选择一条已定标或已完成的招标单!");
                    return;
                }
                CREATE_CONTRACT_COMMON.lookCreateContract(CONTRACT_TYPE.COST, '#tb', BUSINESS_TYPE.TENDER, row.id);
            }
        }
    ],
    openSupplierDialog: function (tenderId) {
        page.supplierDialog = openDialog("supplierDialog", "/tender/page/accelink/plus/supplier?id=" + tenderId, {
            title: '添加供应商',
            width: 800,
            height: 600,
            cache: false,
            modal: true,
            border: false,
            draggable: false,
            shadow: false,
            onClose: function () {
                page.supplierDialog.dialog('destroy');
            },
            buttons: [{
                text: '确定',
                handler: () => {
                    topWin.frames['supplierDialog'].page.save(() => {
                        page.supplierDialog.dialog('close');
                    });
                }
            }, {
                text: '关闭',
                handler: function () {
                    page.supplierDialog.dialog('close');
                }
            }]
        });
    },
    dialogElement: {},
    bindEvent: function () {
        $("#table_div").delegate('.lookup', 'click', function (event) {
            var id = $(this).data("id");
            page.lookup(id, 1);
        });
        $("#table_div").delegate('.lookup_confirm', 'click', function (event) {
            var id = $(this).data("id");
            page.lookup(id, 0);
        });
        $("#table_div").delegate('.lookup_question', 'click', function (event) {
            var id = $(this).data("id");
            tenderComm.answerQuestions(id, 0)
        });
        $("#table_div").delegate('.lookup_contract', 'click', function (event) {
            const id = $(this).data("id");
            page.lookup(id, 2);
        });
    },
    initTableDataGrid: function () {

        var columns = [[
            {
                field: 'ck',
                checkbox: true
            }, {
                field: 'id',
                hidden: true
            },
            {
                field: 'status',
                title: bidI18n['招标状态'],
                width: 110,
                align: 'left',
                formatter: function (value, row, index) {
                    return TENDER_STATUS_CONST[value].color
                }
            },
            {
                field: 'code',
                title: '招标编号',
                width: 180,
                align: 'left'

            },
            {
                field: 'title',
                title: '招标主题',
                width: 120,
                align: 'left'

            },
            {
                field: 'bidCount',
                title: '确认参加情况',
                width: 100,
                align: 'center',
                formatter: function (val, row) {
                    return '<a href="javascript:void(0)" class="easyui-linkbutton lookup_confirm" ' +
                        'data-id="' + row.id + '"' + '">查看(' + row.agreeCount + '/' + row.total + ')</a>';
                }

            },
            {
                field: 'questionCount',
                title: '答疑数量',
                width: 100,
                align: 'center',
                formatter: function (val, row) {
                    return '<a href="javascript:void(0)" class="easyui-linkbutton lookup_question" ' +
                        'data-id="' + row.id + '"' + '">查看(' + row.questionCount + ')</a>';
                }

            },
            {
                field: 'bidding',
                title: '投标情况',
                width: 80,
                align: 'center',
                formatter: function (val, row) {
                    return '<a href="javascript:void(0)" class="easyui-linkbutton lookup" ' +
                        'data-id="' + row.id + '"' + '">查看(' + row.bidedCount + '/' + row.agreeCount + ')</a>';
                }

            },
            {
                field: 'contractSituation',
                title: '转合同情况',
                width: 80,
                align: 'center',
                formatter: function (val, row) {
                    return '<a href="javascript:void(0)" class="easyui-linkbutton lookup_contract" ' +
                        'data-id="' + row.id + '"' + '">查看(' + row.convertedCount + '/' + row.contractCount + ')</a>';
                }
            },
            {
                field: 'purchaseGroupCode',
                title: '采购组',
                width: 100,
                align: 'left'
            },
            {
                field: 'createName',
                title: '创建人',
                width: 110,
                align: 'center'

            },
            {
                field: 'createTime',
                title: '创建时间',
                width: 120,
                align: 'left'

            },
            {
                field: 'remark',
                title: '备注',
                width: 150,
                align: 'left',
                formatter: function (val) {
                    return '<span title="' + val + '">' + val + '</span>'
                }

            },
            {
                field: 'approvelRemark',
                title: '确认意见',
                width: 150,
                align: 'left'

            }
        ]];

        page.dataGrid = $('#tb').datagrid({
            iconCls: 'icon-list',
            width: '100%',
            method: 'GET',
            height: $(window).height() - $('#searchForm').outerHeight() - 10,
            singleSelect: true,
            pagination: true,
            loadMsg: "数据加载中......",
            rownumbers: true,
            fitColumns: false,
            nowrap: true,
            remoteSort: true,
            striped: true,
            page: 1,
            pageSize: 20,
            queryParams: {
                page: 1,
                rows: 10
            },
            url: g.ctx + "pc/tender/rest/list",
            columns: columns,
            toolbar: page.toolbars,
            onLoadSuccess: function () {
                displayButtonByUserRole('招标管理');
            },
            onCheck: function (index, row) {
            }
        });
    },
    editTender: function (id, type) {
        var title = '';
        const btns = [{
            text: '关闭',
            handler: function () {
                page.dialogElement.dialog('close');
            }
        }]
        if (type === 2) {
            title = '复制';
            btns.unshift({
                text: '保存',
                handler: function () {
                    topWin.frames['editDialog'].tenderComm.saveOrUpdate(page.closeDialog);
                }
            })
        } else if (type === 1) {
            title = '编辑';
            btns.unshift({
                text: '保存',
                handler: function () {
                    topWin.frames['editDialog'].tenderComm.saveOrUpdate(page.closeDialog);
                }
            })
        } else if (type === 3) {
            title = '发布';
            btns.unshift({
                text: '发布',
                handler: function () {
                    topWin.frames['editDialog'].tenderComm.saveOrUpdate(page.closeDialog);
                }
            })
        } else if (type == 4) {
            title = '查看';
        } else if (type == 5) {
            title = '审核';
            btns.unshift({
                text: '通过',
                handler: function () {
                    topWin.frames['editDialog'].tenderComm.agree(page.closeDialog);
                }
            }, {
                text: '驳回',
                handler: function () {
                    topWin.frames['editDialog'].tenderComm.reject(page.closeDialog);
                }
            })
        } else {
            title = "新增";
            btns.unshift({
                text: '保存',
                handler: function () {
                    topWin.frames['editDialog'].tenderComm.saveOrUpdate(page.closeDialog);
                }
            })
        }
        page.dialogElement = openDialog("editDialog", g.ctx + "pc/tender/page/detail?id=" + id + "&type=" + type, {
            title: title + "招标单",
            width: 1200,
            height: '100%',
            cache: false,
            modal: true,
            border: false,
            draggable: false,
            shadow: false,
            onClose: function () {
                page.dialogElement.dialog('destroy');
            },
            buttons: btns
        });
    },
    release: function (id) {
        var url;
        url = g.ctx + "pc/tender/page/detail/1?id=" + id + "&type=0";
        page.dialogElement = openDialog("editDialog", url, {
            title: "提交招标单",
            width: 1200,
            height: '100%',
            cache: false,
            modal: true,
            border: false,
            draggable: false,
            shadow: false,
            onClose: function () {
                page.dialogElement.dialog('destroy');
            },
            buttons: [{
                text: '提交',
                handler: function () {
                    topWin.frames['editDialog'].tenderComm.saveOrUpdate(page.closeDialog);
                }
            }, {
                text: '关闭',
                handler: function () {
                    page.dialogElement.dialog('close');
                }
            }]
        });
    },
    delete: function (id, index) {
        $.ajax({
            url: g.ctx + "pc/tender/rest/remove", type: "POST", data: {id: id},
            success: function (d) {
                topWin.layer.alert("操作成功", {
                    icon: 1,
                    closeBtn: 0,
                    yes: function (iindex) {
                        topWin.layer.close(index);
                        page.tableReload();
                        topWin.layer.close(iindex);
                    }
                });
            }, error: function (data) {
                topWin.layer.alert(data.responseJSON.message, {icon: 2});
            }
        });
    },
    tableReload: function () {
        $('#tb').datagrid("reload")
    },
    closeDialog: function () {
        page.dialogElement.dialog('close');
        page.tableReload()
    },
    initStatusCombox: function () {
        $("#status").combobox({
            editable: false,
            valueField: 'value',
            textField: 'txt',
            multiple: true,
            data: getTenderStatusOption(),
            onLoadSuccess: function () {
            }
        });
    },
    answerQuestions: function (id) {
        page.answerQuestionsDialogElement = openDialog("answerQuestionsDialog", "accelink/tender/question/page/index?tenderId=" + id + "&type=" + 0, {
                title: "投标答疑",
                width: 1200,
                height: 753,
                cache: false,
                modal: true,
                border: false,
                draggable: false,
                shadow: false,
                onClose: function () {
                    page.answerQuestionsDialogElement.dialog('destroy');
                },
                buttons: [{
                    text: '关闭',
                    handler: function () {
                        page.answerQuestionsDialogElement.dialog('close');
                    }
                }]
            }
        );

    },
    info: function (id) {
        page.infoDialogElement = openDialog("infoDialog", "tender/page/accelink/lookup?id=" + id, {
            title: "招标单查看",
            width: 1200,
            height: 753,
            cache: false,
            modal: true,
            border: false,
            draggable: false,
            shadow: false,
            onClose: function () {
                page.infoDialogElement.dialog('destroy');
            },
            buttons: [{
                text: '关闭',
                handler: function () {
                    page.infoDialogElement.dialog('close');
                }
            }]
        });
    },
    toVoid: function (id) {


        topWin.layer.confirm('是否确认作废？', function (index) {
            $.ajax({
                url: g.ctx + "pc/tender/rest/toVoid",
                type: "POST",
                data: {id: id},
                success: function (d) {
                    page.tableReload();
                    topWin.layer.close(index);
                }
            });
        });
    },
    lookup: function (id, type) {
        var url = '';
        url = g.ctx + "pc/tender/page/process?id=" + id + "&type=" + type;
        page.lookupProcessDialog = openDialog("lookupProcessDialog", url,
            {
                title: "供应商投标情况",
                width: 800,
                height: 565,
                cache: false,
                modal: true,
                border: false,
                draggable: false,
                shadow: false,
                onClose: function () {
                    page.lookupProcessDialog.dialog('destroy');
                },
                buttons: [{
                    text: '关闭',
                    handler: function () {
                        page.lookupProcessDialog.dialog('close');
                    }
                }]
            });
    },
    evlUserDialogElement: {},
};


$(function () {
    page.initStatusCombox();
    page.initTableDataGrid();
    page.bindEvent();
});
