var el = {
    searchForm: '#searchForm',
    type: '#type',
    toolbar: '.datagrid-toolbar',
    status: '#status',
    quote_btn: '#quote_btn',
    propose_btn: '#propose_btn',
    look_btn: '#look_btn',
    inquirySupplierStatus: '#inquirySupplierStatus',
    tb: '#tb',
    quoteOrBargainDialog: null,
    bodyHeight: document.documentElement.clientHeight
};
$(function () {
    initDict('inquiry_type');
    initNecessaryDatas('pc/sys/purchaseOrg/rest/listAll,pc/sys/purchaseGroup/rest/listAll');
    initCombo();
    loadGrid();
});
function initCombo() {
    //报价状态 0-新建；1-报价中；2-议价中；3-已报价；4-询价截止；5-询价结束；
    $(el.inquirySupplierStatus).combobox({
        valueField: 'value',
        textField: 'label',
        data: [
            {label: '全部', value: ''},
            {label: '待报价', value: 1},
            {label: '议价中', value: 2},
            {label: '已报价', value: 3},
            {label: '已议价', value: 6},
            {label: '询价截止', value: 4},
            {label: '询价结束', value: 5}
        ],
        editable: false,
        panelHeight: 'auto'
    });
}

function loadGrid() {
    var searchHeight = $(el.searchForm).outerHeight();
    var pager = initGridPagerNew(el.bodyHeight - searchHeight, 37, 40);
    var status = Number($(el.type).val())===0?'2,3,7':''
    var toolbar = [];
    if (Number($(el.type).val())===0){
        toolbar.push(
            {
                text: BJGL.GYSBJ_BJ,
                iconCls: 'icon-edit',
                id:'quote_btn',
                handler: function () {
                    quote(0);
                }
            }, '-',
            {
                text: BJGL.BJGL__YJ,
                iconCls: 'icon-edit',
                id:'propose_btn',
                handler: function () {
                    quote(1);
                }
            }, '-',
            {
                text: BJGL.BJCX_CK,
                iconCls: 'icon-tip',
                id:'look_btn',
                handler: function () {
                    quote(2);
                }
            }
        )
    }else {
        toolbar.push(
            {
                text: BJGL.BJCX_CK,
                iconCls: 'icon-tip',
                id:'look_btn',
                handler: function () {
                    quote(2);
                }
            }
        )
    }
    $(el.tb).datagrid({
        height: el.bodyHeight - searchHeight,
        url: g.ctx + 'pc/inquiry/supplier/quote/rest/list?status='+status,
        singleSelect: true,
        pagination: true,
        pageSize: pager.pageSize,
        pageList: pager.pageList,
        loadMsg: '数据加载中......',
        rownumbers: true,
        queryParams: {
        },
        columns: [[
            {
                field: 'ck',
                checkbox: true
            },
            {
                field: 'inquirySupplierStatus',
                title: BJGL.BJCX_BJZT,
                width: 110,
                formatter: function (value) {
                    if(value === 1){
                        return '<span style="color: orange;">待报价</span>';
                    }
                    if(value === 2){
                        return '<span style="color: red;">议价中</span>';
                    }
                    if(value === 3){
                        return '<span style="color: green;">已报价</span>';
                    }
                    if(value === 4){
                        return '<span style="color: grey;">询价截止</span>';
                    }
                    if(value === 5){
                        return '<span style="color: rebeccapurple;">询价结束</span>';
                    }
                    if(value === 6){
                        return '<span style="color: green;">已议价</span>';
                    }
                    return '';
                }
            },
            {
                field: 'inquiryCode',
                title: BJGL.BJCX_XJDBH,
                width: 150
            },
            {
                field: 'inquiryTitle',
                title: BJGL.BJCX_ZT,
                width: 300
            },
            {
                field: 'beginTime',
                title: BJGL.BJCX_XJKSSJ,
                width: 140,
                formatter: function (value) {
                    if(value){
                        return moment(value).format('YYYY-MM-DD HH:mm');
                    }
                    return '';
                }
            },
            {
                field: 'endTime',
                title: BJGL.BJCX_XJJSSJ,
                width: 140,
                formatter: function (value) {
                    if(value){
                        return moment(value).format('YYYY-MM-DD HH:mm');
                    }
                    return '';
                }
            },
            {
                field: 'createUserName',
                title: BJGL.BJCX_XJR,
                width: 100
            },
            {
                field: 'remark',
                title: BJGL.BJCX_BZ,
                width: 270
            }
        ]],
        toolbar: toolbar,
        onLoadSuccess: function () {
            $(el.tb).datagrid('clearSelections');
        },
        onCheck: function (rowIndex, field, value) {
            var rows = $(el.tb).datagrid('getRows');
            var inquirySupplierStatus = rows[rowIndex].inquirySupplierStatus;
            if(inquirySupplierStatus === 1){
                $(el.quote_btn).linkbutton('enable');
                $(el.propose_btn).linkbutton('disable');
                $(el.look_btn).linkbutton('disable');
                return false;
            }
            if(inquirySupplierStatus === 2){
                $(el.quote_btn).linkbutton('disable');
                $(el.propose_btn).linkbutton('enable');
                $(el.look_btn).linkbutton('disable');
                return false;
            }
            if(inquirySupplierStatus === 3){
                $(el.quote_btn).linkbutton('enable');
                $(el.propose_btn).linkbutton('disable');
                $(el.look_btn).linkbutton('enable');
                return false;
            }
            if(inquirySupplierStatus === 4){
                $(el.quote_btn).linkbutton('disable');
                $(el.propose_btn).linkbutton('disable');
                $(el.look_btn).linkbutton('enable');
                return false;
            }
            if(inquirySupplierStatus === 5){
                $(el.quote_btn).linkbutton('disable');
                $(el.propose_btn).linkbutton('disable');
                $(el.look_btn).linkbutton('enable');
                return false;
            }
            if(inquirySupplierStatus === 6){
                $(el.quote_btn).linkbutton('disable');
                $(el.propose_btn).linkbutton('enable');
                $(el.look_btn).linkbutton('disable');
                return false;
            }
        }
    });
}

function quote(quoteType){
    var row = $(el.tb).datagrid('getSelected'), userId = $(el.userId).val();
    if (!row) {
        topWin.layer.alert('请选中一条记录', {icon: 0});
        return false;
    }
    var buttons = [];
    if (quoteType === 2){
        buttons = [{
            text: '关闭',
            iconCls: 'icon-cancel',
            handler: function () {
                quoteDialogElement.dialog('close');
            }
        }]
    }else {
        buttons = [{
            text: '提交',
            iconCls: 'icon-save',
            handler: function () {
                topWin.frames['quoteDialog'].saveQuote(function () {
                    topWin.layer.msg('数据保存成功', {icon: 1});
                    quoteDialogElement.dialog('close');
                    $(el.tb).datagrid('reload');
                });
            }
        }, {
            text: '关闭',
            iconCls: 'icon-cancel',
            handler: function () {
                quoteDialogElement.dialog('close');
            }
        }]
    }
    var id = row.id;
    var quoteDialogElement = openDialog("quoteDialog", g.ctx + "pc/inquiry/supplier/quote/quote?id="+id+"&quoteType="+quoteType, {
        title: (quoteType === 0? "报价" :quoteType === 1?"议价":"查看"),
        width: '100%',
        height: '100%',
        cache: false,
        modal: true,
        border: false,
        draggable: false,
        shadow: false,
        onClose: function () {
            quoteDialogElement.dialog('destroy');
        },
        buttons: buttons
    });

}