let el = {
    searchForm: '#searchForm',
    tb: '#tb',
    state: '#state',
    title: '#title',
    code: '#code',
    btn_file_eSign: '#btn-file-eSign',
    bodyHeight: document.documentElement.clientHeight
};

$(function () {
    initDict('')
    initCombo();
    loadGrid();
});




function loadGrid() {
    var columns = [
        { field: 'contractTitle', title: '合同名称', align: 'left' , width: 200 },
        { field: 'contractCode', title: '合同编号', align: 'left' , width: 150 },
        { field: 'cardNumber', title: 'SAP订单号', align: 'left' , width: 100 },
        { field: 'contractTypeName', title: '合同类型', align: 'left' , width: 100},
        { field: 'createUserName', title: '采购员', align: 'left' , width: 100 },
        { field: 'contractCompanyName', title: '甲方', align: 'left' , width: 180 },
        { field: 'contractSupplierName', title: '乙方', align: 'left' , width: 180 },
        { field: 'contractApplyContent', title: '合同说明', align: 'left' , width: 100 },
        { field: 'contractAmount', title: '合同总金额', align: 'left' , width: 100 },
        { field: 'subjectCode', title: '科目', align: 'left' , width: 100 },
        { field: 'materialModel', title: '详细规格参数', align: 'left' , width: 100 },
        { field: 'warrantyPeriod', title: '质保期', align: 'left' , width: 80 },
        { field: 'materialBrand', title: '品牌', align: 'left' , width: 80 },
        { field: 'basisUnit', title: '单位', align: 'left' , width: 80 },
        { field: 'price', title: '单价', align: 'left' , width: 80 },
        { field: 'num', title: '数量', align: 'left' , width: 80 },
        { field: 'taxRateName', title: '税率', align: 'left' , width: 80 },
        { field: 'contractDetailAmount', title: '总金额', align: 'left' , width: 80 },
        { field: 'requireName', title: '请购人', align: 'left' , width: 100 },
        { field: 'deliveryDate', title: '交货日期', align: 'left' , width: 150, exportDateFormatter:'date',
            formatter: function (value) {
                if (!value) {
                    return;
                }
                return moment(new Date(value)).format('YYYY-MM-DD');
            } },
        { field: 'invoiceTime', title: '开票时间', align: 'left' , width: 150, exportDateFormatter:'dateTime',
            formatter: function (value) {
                if (!value) {
                    return;
                }
                return moment(new Date(value)).format('YYYY-MM-DD HH:mm:ss');
            } },
        // { field: 'purchaseCode', title: '请购单编码', align: 'left' , width: 100 },
        // { field: 'totalAmount', title: '请购金额', align: 'left' , width: 100 },
        // { field: 'increasePrice', title: '降价率', align: 'left' , width: 100,
        //     formatter: function (value) {
        //         if (!value) {
        //             return;
        //         }
        //         return value+'%';
        //     } }
    ];
    var searchHeight = $(el.searchForm).outerHeight();
    var pager = initGridPagerNew(el.bodyHeight - searchHeight, 37, 40);
    $(el.tb).datagrid({
        url: '/pc/contract/manage/rest/report/page?supplierFlag='+$("#supplierFlag").val(),
        iconCls: 'icon-list',
        height: el.bodyHeight - searchHeight,
        singleSelect: false,
        fitColumns: false,
        pagination: true,
        loadMsg: "数据加载中......",
        rownumbers: true,
        nowrap: true,
        striped: true,
        pageSize: pager.pageSize,
        pageList: pager.pageList,
        columns: [columns],
        toolbar: [
            {
                text: '导出',
                iconCls: 'icon-print',
                id: 'btn_export',
                handler: function () {
                    setOperationLog("导出按钮", "MRO合同报表");
                    openExportDialog('pc/contract/manage/rest/report/export','tb',searchParams());
                }
            },
            {
                text: '日期时间修改',
                iconCls: 'icon-import',
                id: 'icon_mark_batch_update',
                handler: function () {
                    var rows = $(el.tb).datagrid('getChecked');
                    if (rows.length <= 0) {
                        layer.msg("请选择需要修改的数据");
                        return;
                    }
                    var ids = [];
                    $.each(rows, function (idx, itm) {
                        ids.push(itm.id);
                    })
                    var updateTimeDialog = openDialog("updateTimeDialog", g.ctx+"pc/contract/manage/page/updateTimePage?ids=" + ids.join(","), {
                        title: '日期时间修改',
                        width: 500,
                        height: '500',
                        cache: false,
                        modal: true,
                        border: false,
                        draggable: false,
                        shadow: false,
                        onClose: function () {
                            updateTimeDialog.dialog('destroy');
                        },
                        buttons: [{
                            text: '确定',
                            iconCls: 'icon-save',
                            handler: function () {
                                topWin.frames['updateTimeDialog'].page.save(() => {
                                    updateTimeDialog.dialog('close');
                                    $(el.tb).datagrid('reload');
                                });

                            }
                        }, {
                            text: '关闭',
                            iconCls: 'icon-cancel',
                            handler: function () {
                                updateTimeDialog.dialog('close');
                            }
                        }]
                    });
                }
            },
        ],
        onLoadSuccess: function (data) {
            // displayButtonByUserRole('contract_report');
            // $(el.tb).datagrid('clearSelections');
        }
    });
}


/**
 * 初始化下拉框
 */
function initCombo() {
    $(el.state).combobox({
        valueField:'code',
        textField:'text',
        panelHeight:'auto',
        editable:false,
        prompt: '--- 请选择 ---',
        data:[
            {'code':'0','text':'暂存'},
            {'code':'1','text':'待提交'},
            {'code':'2','text':'审批中'},
            {'code':'3','text':'通过'},
            {'code':'4','text':'驳回'},
            {'code':'5','text':'签约'},
            {'code':'6','text':'弃用'}
        ]
    })
}


/**
 * 查询
 */
function searchGrid() {
    $(el.tb).datagrid('options').queryParams = $("#searchForm").serializeJson();
    $(el.tb).datagrid('load');
}

/**
 * 重置筛选条件
 */
function resetGrid() {
    $(el.title).textbox('setValue', '');
    $(el.code).textbox('setValue', '');
    searchGrid();
}