/*
 * 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.
 */

/**
 *
 * @author BOGON
 * @version 1.0
 */
var page = {
    dataGrid: {},
    bindEvent: function () {
        $("#table_div").delegate('.lookup_question', 'click', function (event) {
            var id = $(this).data("id");
            tenderComm.answerQuestions(id, 0)
        });
    },    initTableDataGrid: function () {
        var toolbars = [

            {
                text: tenderBtns.bid,
                id: "bidding",
                iconCls: 'icon-storage-add',
                handler: function () {
                    var selectedRow = page.dataGrid.datagrid('getSelected');
                    if (!selectedRow) {
                        topWin.layer.msg(tenderTxt.txt8);
                        return;
                    }
                    if (selectedRow.agreeFlag != 1) {
                        topWin.layer.msg(tenderTxt.txt7);
                        return;
                    }
                    let currentTime = moment();
                    let startDay = moment(selectedRow.startDay);
                    let endDay = moment(selectedRow.endDay);
                    console.log(currentTime)
                    if (!currentTime.isBetween(startDay, endDay)) {
                        topWin.layer.msg("不在投标时间段内,无法投标!");
                        return;
                    }
                    if (selectedRow.status == BID_STATUS_CONST["3"].val || selectedRow.status == BID_STATUS_CONST["4"].val) {
                        bidComm.openDetailDialog(1, selectedRow.id, selectedRow.tenderId);
                    } else {
                        topWin.layer.msg(tenderTxt.txt17);
                    }
                }
            }, "-", {
                text: '议价',
                id: "bargaining_btn",
                iconCls: 'icon-user-setup',
                handler: function () {
                    var selectedRow = page.dataGrid.datagrid('getSelected');
                    if (!selectedRow) {
                        topWin.layer.msg("请先选择需要投标的投标单!");
                        return;
                    }
                    if (selectedRow.agreeFlag !== 1) {
                        topWin.layer.msg("请先点击确认参加!");
                        return;
                    }
                    if (selectedRow.status == BID_STATUS_CONST["5"].val) {
                        bidComm.openDetailDialog(2, selectedRow.id, selectedRow.tenderId);
                    } else {
                        topWin.layer.msg(tenderTxt.txt10);
                    }
                }
            }, "-",
            {
                text: tenderBtns.check,
                iconCls: 'icon-accept',
                id: "info",
                handler: function () {
                    var selectedRow = page.dataGrid.datagrid('getSelected');
                    if (!selectedRow) {
                        topWin.layer.msg(tenderTxt.txt1);
                        return;
                    }
                    bidComm.openDetailDialog(0, selectedRow.id, selectedRow.tenderId);
                }
            }, "-",
            {
                text: tenderTitles.tenderAQ,
                iconCls: 'icon-help',
                handler: function () {
                    var row = page.dataGrid.datagrid('getSelected');
                    if (row == null) {
                        topWin.layer.msg(tenderTxt.txt4);
                        return;
                    }
                    tenderComm.answerQuestions(row.tenderId, 1)
                }
            }
        ];

        var columns = [[
            {
                field: 'ck',
                checkbox: true
            }, {
                field: 'id',
                hidden: true
            },
            {
                field: 'status',
                title: tenderTitles.tenderStatus,
                width: 90,
                align: 'left',
                formatter: function (value, row, index) {
                    if (row)
                        return BID_STATUS_CONST[value].color
                }
            },
            {
                field: 'tenderCode',
                title: tenderTitles.tenderCodeNum,
                width: 180,
                align: 'left'

            },
            {
                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: 'tenderTitle',
                title: tenderTitles.tenderTit,
                width: 250,
                align: 'left'

            },
            {
                field: 'typeName',
                title: tenderTitles.tenderType,
                width: 100,
                hidden: true,
                align: 'left'
            },
            {
                field: 'startDay',
                title: "开始时间",
                width: 130,
                align: 'left'
            },
            {
                field: 'endDay',
                title: "截止时间",
                width: 130,
                align: 'left'
            },
            {
                field: 'tenderRemark',
                title: tenderTitles.remarks,
                width: 300,
                align: 'left'

            },
            {
                field: 'submitDepositFileName',
                title: '保证金附件',
                width: 160,
                align: 'left',
                formatter: function (val, row) {
                    var statusTitle = '无需保证金';
                    if (row.submitDepositFileId != '') {
                        return '<a href="#" onclick="previewFileForm(undefined,\'' + row.submitDepositFileName + '\',\'' + row.submitDepositFileId + '\')">预览</a>'
                    } else {
                        return statusTitle;
                    }
                }

            }
        ]];

        page.dataGrid = $('#tb').datagrid({
            iconCls: 'icon-list',
            width: '100%',
            method: "GET",
            height: $(window).height() - $('#searchForm').outerHeight() - 10,
            url: g.ctx + 'pc/tender/bid/rest/toBeQuotedList',
            singleSelect: true,
            pagination: true,
            loadMsg: tenderTxt.dataLoad,
            rownumbers: true,
            nowrap: true,
            remoteSort: true,
            striped: true,
            page: 1,
            pageSize: 20,
            queryParams: {
                page: 1,
                rows: 10
            },
            columns: columns,
            toolbar: toolbars
        });
    },
};
$(function () {
    page.initTableDataGrid();
    page.bindEvent();
});