/*
 * 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 = {
    dataGrid: {},
    dialogElement: {},
    tableReload: function () {
        page.dataGrid.datagrid("reload");

    },
    pass: function (ids) {
        topWin.layer.confirm('您确定要审批通过选中的记录吗?', {icon: 3}, function (index) {
            page.postDepositStatus(ids, 1, "", function () {
                topWin.layer.close(index);
                page.tableReload();
            })
        })
    },
    reject: function (ids) {
        topWin.layer.prompt({title: '请输入驳回原因', formType: 2}, function (value, index, elem) {
            if (value === '') return elem.focus()
            console.log("驳回原因：" + value);
            page.postDepositStatus(ids, 2, value, function () {
                topWin.layer.close(index);
                page.tableReload();
            })
        })
    },
    refundDeposit: function (ids) {
        topWin.layer.confirm('您确定要修改保证金退回状态吗?', {icon: 3}, function (index) {
            page.postMarginStatus(ids, 1,  function () {
                topWin.layer.close(index);
                page.tableReload();
            })
        })
    },
    postDepositStatus: function (ids, status, reason, call) {

        $.ajax({
            url: g.ctx + "pc/tender/securityDeposit/rest/updateDepositStatus",
            type: "POST",
            data: {ids: ids.join(","), status: status, reason: reason},
            success: function () {
                topWin.layer.alert("操作成功", {icon: 1}, function (idx) {
                    call()
                    topWin.layer.close(idx);
                });
            }, error: function (data) {
                topWin.layer.alert(data.responseJSON.message, {icon: 2});
            }
        });
    }, postMarginStatus: function (ids,status, call) {

        $.ajax({
            url: g.ctx + "pc/tender/securityDeposit/rest/updateMarginStatus",
            type: "POST",
            data: {ids: ids.join(","), status: status},
            success: function () {
                topWin.layer.alert("操作成功", {icon: 1}, function (idx) {
                    call()
                    topWin.layer.close(idx);
                });
            }, error: function (data) {
                topWin.layer.alert(data.responseJSON.message, {icon: 2});
            }
        });
    },
    initTableDataGrid: function () {
        var toolbars = [
            {
                text: '通过',
                iconCls: 'icon-ok',
                hidden: true,
                handler: function () {
                    var rows = page.dataGrid.datagrid('getChecked');
                    if (rows.length <= 0) {
                        topWin.layer.msg("请选择至少一个需要通过的投标单!");
                        return;
                    }
                    var ids = [];
                    $.each(rows, function (index, item) {
                        ids.push(item.id);
                    });
                    page.pass(ids);
                }
            }, "-",
            {
                text: '驳回',
                iconCls: 'icon-clear',
                hidden: true,
                handler: function () {
                    var rows = page.dataGrid.datagrid('getChecked');
                    if (rows.length <= 0) {
                        topWin.layer.msg("请选择至少一个需要驳回的投标单!");
                        return;
                    }
                    //确认状态已驳回的数据不允许再驳回操作
                    var zt = rows[0].securityDepositStatus;
                    if (zt == 2) {
                        topWin.layer.msg("选择的数据确认状态为已驳回，请勿重复驳回!");
                        return;
                    }
                    var ids = [];
                    $.each(rows, function (index, item) {
                        ids.push(item.id);
                    });
                    page.reject(ids);
                }
            }, "-",
            {
                text: '查看',
                iconCls: 'icon-book',
                handler: function () {
                    var rows = page.dataGrid.datagrid('getChecked');
                    if (rows.length != 1) {
                        topWin.layer.msg("请选择一个需要查看的招标单!");
                        return;
                    }
                    page.info(rows[0].tenderId);
                }
            }, "-",
            {
                text: '保证金退回',
                iconCls: 'icon-clear',
                hidden: true,
                handler: function () {
                    var rows = page.dataGrid.datagrid('getChecked');
                    if (rows.length <= 0) {
                        topWin.layer.msg("请选择至少一个需要驳回的投标单!");
                        return;
                    }
                    //确认状态已驳回的数据不允许再驳回操作
                    var bzj = rows[0].marginStatus;
                    if (bzj == 1) {
                        topWin.layer.msg("选择的数据保证金已退回，请勿重复退回!");
                        return;
                    }
                    var ids = [];
                    $.each(rows, function (index, item) {
                        ids.push(item.id);
                    });
                    page.refundDeposit(ids);
                }
            }
        ];

        var columns = [[
            {
                field: 'ck',
                checkbox: true
            },
            {
                field: 'securityDepositStatus',
                title: '确认状态',
                width: 90,
                align: 'left',
                formatter: function (value, row, index) {
                    if (value == 1) {
                        return '<span style="color: darkgreen;">通过</span>'
                    } else if (value == 2) {
                        return '<span style="color: grey;">拒绝</span>'
                    } else {
                        return '<span style="color: red;">确认中</span>'
                    }
                }
            },
            {
                field: 'tenderTitle',
                title: '招标标题',
                width: 120,
                align: 'left'

            },
            {
                field: 'typeName',
                title: '招标类型',
                width: 80,
                align: 'left'

            },
            {
                field: "supplierName",
                title: "供应商名称",
                width: 120
            },
            {
                field: 'supplierCode',
                title: '供应商编码',
                width: 120,
                align: 'left'
            },
            {
                field: 'createName',
                title: '创建人',
                width: 110,
                align: 'center'

            }, {
                field: 'submitDepositFileId',
                title: '付款凭证查看',
                width: 300,
                align: 'left',
                formatter: function (value, row) {
                    var html = '&nbsp;&nbsp;&nbsp;<a href="#" onclick="previewFileForm(undefined,\'' + row.submitDepositFileName + '\',\'' + row.submitDepositFileId + '\')">预览</a>'
                    return '<a href="' + g.ctx + "pc/sys/file/rest/download/" + row.submitDepositFileId + '">' + row.submitDepositFileName + '</a>' + html;
                }

            },
            {
                field: 'marginStatus',
                title: '保证金是否退回',
                width: 150,
                align: 'left',
                formatter: function (value, row, index) {
                    if (value == 1) {
                        return '<span style="color: darkgreen;">已退回</span>'
                    } else {
                        return '<span style="color: red;">未退回</span>'
                    }
                }

            },
            {
                field: 'tenderCode',
                title: '招标编号',
                align: 'left',
                width: 180,
            },
            {
                field: 'createTime',
                title: '创建时间',
                width: 120,
                align: 'left'

            },
            {
                field: 'remark',
                title: '备注',
                width: 150,
                align: 'left'

            },
            {
                field: 'reason',
                title: '原因',
                width: 150,
                align: 'left'

            }
        ]];

        page.dataGrid = $('#tb').datagrid({
            iconCls: 'icon-list',
            width: '100%',
            method: "GET",
            height: $(window).height() - $('#searchForm').outerHeight() - 10,
            url: g.ctx + 'pc/tender/securityDeposit/rest/approvelList',
            singleSelect: false,
            pagination: true,
            loadMsg: "数据加载中......",
            rownumbers: true,
            remoteSort: true,
            striped: true,
            page: 1,
            pageSize: 20,
            queryParams: {
                page: 1,
                rows: 10
            },
            columns: columns,
            toolbar: toolbars
        });
    },

    closeDialog: function () {
        page.dialogElement.dialog('close');
        page.tableReload();
    },
    info: function (id) {
        page.infoDialogElement = openDialog("infoDialog", g.ctx + "pc/tender/page/detail?id=" + id + "&type=4", {
            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');
                }
            }]
        });
    }
};
$(function () {
    page.initTableDataGrid();
});
