var SEGNO_COLUMN = 0;
var PRODUCTID_COLUMN = 1;
var WPROVIDERID_COLUMN = 2;
var PRODUCTTYPECODE4_COLUMN = 3;
var SHOPSIGN_COLUMN = 4;
var PUBLISHID_COLUMN = 0;
var SHOP_PRODUCTID_COLUMN = 1;
var SHOP_SHOPPINGID_COLUMN = 0;
var WIDE_RES_LENGTH = 55;
var WIDE_WPR_LENGTH = 12;
var SHORT_RES_LENGTH = 35;
var SHORT_WPR_LENGTH = 6;
var TEMP_LENGHT = 0;
var MAX_LENGHT = 1050;
var topTotalShop;
var topDetailShop;



efform_onload = function(){
	var eiInfo = _getEi();
	if(ef.get("i0-0-info").value == "" && ef.get("i0-0-producttypeid").value == ""){
		ef.get("isfirst").value = "1";
	} 
    button_query_onclick();
	ef.get("isfirst").value = "0";
	window.setInterval("objTimer()",1000);
}
var time = -1;
objTimer = function()
{
	if(time >= 0){
		if(time == 0){
			document.getElementById("taoziyuan").disabled = false;
			document.getElementById("taoziyuan").style.cursor = "pointer";
			document.getElementById("taoziyuan").alt = "淘资源";
		}else{
			time -= 1 ;
		}
	}
}
button_query_onclick = function(){

    var thickmin = ef.get("i0-0-thickmin").value;
    var thickmax = ef.get("i0-0-thickmax").value;
    var widemin = ef.get("i0-0-widemin").value;
    var widemax = ef.get("i0-0-widemax").value;
    var lengthmin = ef.get("i0-0-lengthmin").value;
    var lengthmax = ef.get("i0-0-lengthmax").value;
   
    if (thickmin != "") {
        if (isNaN(thickmin)) {
            alert("请输入正确的厚度最小值！");
            return;
        }
    }
    if (thickmax != "") {
        if (isNaN(thickmax)) {
            alert("请输入正确的厚度最大值！");
            return;
        }
    }
   
    if (widemin != "") {
        if (isNaN(widemin)) {
            alert("请输入正确的宽度最小值！");
            return;
        }
    }
    if (widemax != "") {
        if (isNaN(widemax)) {
            alert("请输入正确的宽度最大值！");
            return;
        }
    }
    if (lengthmin != "") {
        if (isNaN(lengthmin)) {
            alert("请输入正确的长度最小值！");
            return;
        }
    }
    if (lengthmax != "") {
        if (isNaN(lengthmax)) {
            alert("请输入正确的长度最大值！");
            return;
        }
    }
    
    var ajax_callback = {
        onSuccess: function(eiInfo){
            modalWin.hide();
			
            for (i = sel.rows.length - 1; i > 0; i--) {
                sel.deleteRow(i);
            }
            paint_resource(sel, eiInfo);
			
            var ei_info = _getEi();
            ei_info.set("currentPage", eiInfo.get("currentPage"));
            ei_info.set("totalPageCount", eiInfo.get("totalPageCount"));
            var currentPage = document.getElementById("currentPage");
            currentPage.innerText = '第 ' + eiInfo.get("currentPage") + ' 页';
            var totalPageCount = document.getElementById("totalPageCount");
            totalPageCount.innerText = '共 ' + eiInfo.get("totalPageCount") + ' 页';
			
        },
        onFail: function(eMsg){
            modalWin.hide();
            alert("查询失败，请重试，或联系客服。");
        }
    };
    
    var info = new EiInfo();
    info.setByNodeObject(document.forms[0]);
    info.set("limit", document.getElementById("perPageRecord").value);
    var modalWin = new EFModalWindow('progressWindow');
    modalWin.showProgressBar();
	time = 5;
	document.getElementById("taoziyuan").disabled = true;
	document.getElementById("taoziyuan").style.cursor = "wait";
	document.getElementById("taoziyuan").alt = "查询频率限制中";
	info.set("isFirst",ef.get("isfirst").value);
    EiCommunicator.send("BP07", "query", info, ajax_callback, false, true);
    
}


paint_resource = function(sel, eiInfo){
	var contextpath = ef.get("contextpath").value;
    var block = eiInfo.getBlock("r0");
    for (i = 0; i < block.getRows().length; i++) {
        var newTr = sel.insertRow(i + 1);
        if (i % 2 == 0) {
            newTr.className = 'none';
            newTr.onmouseover = function aa(){
                this.className = 'tr-over2'
            };
            newTr.onmouseout = function aa(){
                this.className = 'none'
            };
        }
        if (i % 2 == 1) {
            newTr.className = 'td-listrom';
            newTr.onmouseover = function aa(){
                this.className = 'tr-over2'
            };
            newTr.onmouseout = function aa(){
                this.className = 'td-listrom'
            };
        }
		
		
		//需要隐藏用来作为参数的隐藏单元格
		var newCell = newTr.insertCell();
        newCell.style.display = 'none';
        newCell.innerText = block.getCell(i, "segNo");
        
        var newCell = newTr.insertCell();
        newCell.style.display = 'none';
        newCell.innerText = block.getCell(i, "productId");
        
        var newCell = newTr.insertCell();
        newCell.style.display = 'none';
        newCell.innerText = block.getCell(i, "wproviderId");
        
        var newCell = newTr.insertCell();
        newCell.style.display = 'none';
        newCell.innerText = block.getCell(i, "productTypeId");
        
        var newCell = newTr.insertCell();
        newCell.style.display = 'none';
        newCell.innerText = block.getCell(i, "shopsign");
		
		
		var tempcode = "";
        
		tempcode = block.getCell(i, "sellerCustName") + "<br>" + block.getCell(i, "producingAreaShort");  
        var newCell = newTr.insertCell();
        newCell.innerHTML = "<td>" + tempcode + "</td>";
        newCell.className = 'td_center';
		

		
		var picName = "";
		var picDesc = "";
		if (block.getCell(i, "gradePic") == "D") {
			picDesc = "定";
		}
        if (block.getCell(i, "gradePic") == "P") {
			picDesc = "普";
		}
        if (block.getCell(i, "gradePic") == "J") {
			picDesc = "精";
		}
        if (block.getCell(i, "gradePic") == "T") {
			picDesc = "特";
		}
        if (block.getCell(i, "gradePic") == "N") {
			picDesc = "内";
		}
		tempcode = "<span style='font-weight: bold;	color: #0066FF;	font-size: 12px;'>"+picDesc+"</span>" 
				   +"&nbsp;"+(block.getCell(i, "weightmethoddesc").indexOf("理论")!=-1 ? "<span style='font-weight: bold;color: #0066FF;	font-size: 12px;'>理</span>":"<span style='font-weight: bold; color: #0066FF;	font-size: 12px;'>实</span>")+"<br>"
				   +(block.getCell(i, "discusstype") == "10" ?"<span style='font-weight: bold;	color: #0066FF;	font-size: 12px;'>议</span>&nbsp;":"")
				   +(block.getCell(i, "newFlag") == "0" ? "&nbsp;&nbsp;&nbsp;":"<span style='font-weight: bold;	color: #0066FF;	font-size: 12px;'>新</span>"+(block.getCell(i, "discusstype") == "10" ?"":"&nbsp;&nbsp;&nbsp;&nbsp;"));
		var newCell = newTr.insertCell();
        newCell.innerHTML = "<td>"+ tempcode +"</td>";
        newCell.className = 'td_center';
		
		
		
		
		tempcode = "<a class='con' style='cursor:hand;font-weight:bold' onclick='javascript:query_package(this,true);'>" + block.getCell(i, "prodTypeDesc") + "</a>"
					+"&nbsp;" + block.getCell(i, "shopsign")
					+"<br>"+block.getCell(i, "spec");
        var newCell = newTr.insertCell();
        newCell.innerHTML = "<td>"+tempcode+"</td>";
        newCell.className = 'td_center';
        
		tempcode = "<span style='font-weight:bold;font-size:10pt;'>"+block.getCell(i, "piece") +"</span>件"
				+"<br>"+"<span style='font-weight:bold;font-size:10pt;'>"+block.getCell(i, "weight")+"</span>吨";
        var newCell = newTr.insertCell();
        newCell.innerHTML = "<td>" + tempcode + "</td>";
        newCell.className = 'td_right';
        
		tempcode  = block.getCell(i, "price");
        var newCell = newTr.insertCell();
        newCell.innerHTML = "<span style='font-weight:bold;color: #FF3300;font-size:11pt;'>￥" + tempcode + "</span>";
        newCell.className = 'td_right';
        
		
		
		if(document.body.clientWidth >MAX_LENGHT){
			TEMP_LENGHT = WIDE_WPR_LENGTH;
		}else{
			TEMP_LENGHT = SHORT_WPR_LENGTH;
		}
      	
		var allWproviderName = block.getCell(i, "wprovidername");
        var shortWproviderName;
        if (allWproviderName.length > TEMP_LENGHT) {
			var xingche = ((block.getCell(i, "segNo")=="102"||block.getCell(i, "segNo")=="102101"||block.getCell(i, "segNo")=="102102")?allWproviderName.substr(allWproviderName.indexOf('行车')):"");
            shortWproviderName = allWproviderName.substring(0, TEMP_LENGHT) + "&nbsp;<img border='0' style='vertical-align: top' alt='"+allWproviderName+"' src='"+contextpath+"/ETS/P1/TD/NewBuyStyle/label.gif'/>";
			tempcode = block.getCell(i, "locArea")+ "&nbsp;"+xingche +"<br>"+ shortWproviderName;
        } else {
            shortWproviderName = allWproviderName;
			tempcode = block.getCell(i, "locArea")+"<br>"+ shortWproviderName;
        }
		
        var newCell = newTr.insertCell();
        newCell.innerHTML = "<td>" + tempcode + "</td>";
        newCell.className = 'td_center';
		
		
		
		if(document.body.clientWidth >MAX_LENGHT){
			TEMP_LENGHT = WIDE_RES_LENGTH;
		}else{
			TEMP_LENGHT = SHORT_RES_LENGTH;
		}
		
		var allResInfo = "";
		var shortResInfo = "";
		allResInfo = block.getCell(i, "specialDesc") +"&nbsp;" +block.getCell(i, "resourceDesc");
        if (allResInfo.length > TEMP_LENGHT) {
            shortResInfo = allResInfo.substring(0, TEMP_LENGHT) + "&nbsp;<img border='0' style='vertical-align: top' alt='"+allResInfo+"' src='"+contextpath+"/ETS/P1/TD/NewBuyStyle/label.gif'/>";
        } else {
            shortResInfo = allResInfo;
        }
		
        var piccode ="";
        if (block.getCell(i, "picturepath") !== "0") {
            piccode = "&nbsp;&nbsp;<a href=\"javascript:button_chakan_onclick('" + block.getCell(i, "segNo") + "_" + block.getCell(i, "productId") + "')\" style='text-decoration: underline'>资源图片</a>";
        } 
		
		tempcode = 
			block.getCell(i, "producingAreaCode")+"&nbsp;&nbsp;" 
			+ block.getCell(i, "tech_standard") +"&nbsp;&nbsp;" 
			+ block.getCell(i, "qualityGrade")+"&nbsp;&nbsp;"
			+ block.getCell(i, "spec10")
			+ piccode
			+"<br>"+shortResInfo;

		var newCell = newTr.insertCell();
        newCell.innerHTML = "<td>" + tempcode + "</td>";
        newCell.className = 'td_left';
    }
}
query_package = function(obj, changeDisplay){
    var row_index;
    var newTr;
    var ajax_callback = {
        onSuccess: function(eiInfo){
            var newTd = newTr.insertCell();
            newTd.colSpan = 7;
            newTd.style.backgroundColor = "F5FCFB";
            
            var newTable = document.createElement("TABLE");
            newTable.setAttribute("id", "sel_resource_" + segNo + "_" + productId + "_" + wproviderId);
            newTable.setAttribute("cellpadding", "1");
            newTable.setAttribute("cellspacing", "1");
            newTable.style.border = 0;
            
            if (!changeDisplay) newTable.style.display = 'none';
            var block = eiInfo.getBlock("r0");
            for (i = 0; i < block.getRows().length; i++) {
                var newTtr = newTable.insertRow();
				
				//需要隐藏用来作为参数的隐藏单元格
				var newCell = newTtr.insertCell();
                newCell.innerText = block.getCell(i, "publishId");
                newCell.style.display = 'none';
                
                var newCell = newTtr.insertCell();
                newCell.innerText = block.getCell(i, "putinWeight") + " 吨";
                newCell.style.backgroundColor = "F5FCFB";
                newCell.height = "22px";
                
                var newCell = newTtr.insertCell();
                newCell.innerText = "仓库：" + block.getCell(i, "wproviderName");
                newCell.style.backgroundColor = "F5FCFB";
                
                var newCell = newTtr.insertCell();
                newCell.innerText = "库位：" + block.getCell(i, "packLocation");
                newCell.style.backgroundColor = "F5FCFB";
                
                var newCell = newTtr.insertCell();
                newCell.style.width = "20px";
                newCell.style.backgroundColor = block.getCell(i, "ploveLevel");
                
                var actthick = efutils.trimString(block.getCell(i, "actthick"));
                if (actthick != "0") {
                    var newCell = newTtr.insertCell();
                    newCell.innerText = "实际厚度：" + actthick;
                    newCell.style.backgroundColor = "F5FCFB";
                }
                
                var newCell = newTtr.insertCell();
                newCell.innerText = "张数：" + block.getCell(i, "putinQty");
                newCell.style.backgroundColor = "F5FCFB";
                
                var productTypeId = block.getCell(i, "productTypeId");
                if (productTypeId == 'BI23') {
                    var newCell = newTtr.insertCell();
                    newCell.innerText = "实际长度：" + block.getCell(i, "actlength");
                    newCell.style.backgroundColor = "F5FCFB";
                }
                
                var outWproviderFee = efutils.trimString(block.getCell(i, "outWproviderFee"));
                if (outWproviderFee != "") {
                    var newCell = newTtr.insertCell();
                    newCell.innerText = "出库费：" + outWproviderFee + "元/吨";
                    newCell.style.backgroundColor = "F5FCFB";
                }
                
                var qualityDesc = efutils.trimString(block.getCell(i, "qualityDesc"));
                if (qualityDesc != "") {
                    var newCell = newTtr.insertCell();
                    newCell.innerText = "质量异议原因：" + qualityDesc;
                    newCell.style.backgroundColor = "F5FCFB";
                }
                
                var weightPremium = efutils.trimString(block.getCell(i, "weightPremium"));
                if (weightPremium * 1.0 != 0) {
                    var newCell = newTtr.insertCell();
                    newCell.innerText = "件重优惠：" + weightPremium;
                    newCell.style.backgroundColor = "F5FCFB";
                }
                
                
            }
            newTd.appendChild(newTable);
			
        },
        onFail: function(eMsg){
            alert("查询失败，请重试，或联系客服。");
        }
    };
    row_index = event.srcElement.parentNode.parentNode.rowIndex;
    newTr = event.srcElement.parentNode.parentNode.parentNode.parentNode.insertRow((row_index + 1));
    
    var segNo = event.srcElement.parentNode.parentNode.childNodes[SEGNO_COLUMN].innerText;
    var productId = event.srcElement.parentNode.parentNode.childNodes[PRODUCTID_COLUMN].innerText;
    var wproviderId = event.srcElement.parentNode.parentNode.childNodes[WPROVIDERID_COLUMN].innerText;
    
    var obj = document.getElementById("sel_resource_" + segNo + "_" + productId + "_" + wproviderId);
    if (obj == null) {
        var info = new EiInfo();
        info.set("productId", productId);
        info.set("wproviderId", wproviderId);
        info.set("segNo", segNo);
        EiCommunicator.send("P1TD05", "queryPacks", info, ajax_callback);
    } else {
        if (!changeDisplay) return;
        if (obj.style.display == '') {
            obj.style.display = 'none';
        } else {
            obj.style.display = '';
        }
    }
}


//上一页
prePage = function(){
    var eiInfo = _getEi();
    var perPageRecord = document.getElementById("perPageRecord").value * 1.0;
    var currentPage = eiInfo.get("currentPage");
    if (currentPage == 1) return;
    changePage(perPageRecord, perPageRecord * (currentPage - 2), --currentPage);
}

//下一页
nextPage = function(){
    var eiInfo = _getEi();
    var perPageRecord = document.getElementById("perPageRecord").value * 1.0;
    var currentPage = eiInfo.get("currentPage");
    var totalPageCount = eiInfo.get("totalPageCount");
    if (currentPage == totalPageCount) return;
    changePage(perPageRecord, perPageRecord * currentPage, ++currentPage);
}

//第一页
firstPage = function(){
    var eiInfo = _getEi();
    var perPageRecord = document.getElementById("perPageRecord").value * 1.0;
    //var totalPageCount = document.getElementById("totalPageCount").value*1.0;
    if (currentPage == 1) return;
    changePage(perPageRecord, 0, 1);
}

//最后一页
lastPage = function(){
    var eiInfo = _getEi();
    var perPageRecord = document.getElementById("perPageRecord").value * 1.0;
    var currentPage = eiInfo.get("currentPage");
    var totalPageCount = eiInfo.get("totalPageCount");
    if (currentPage == totalPageCount) return;
    changePage(perPageRecord, (totalPageCount - 1) * perPageRecord, totalPageCount);
}

//按页跳转
goPage = function(){
    var eiInfo = _getEi();
    var perPageRecord = document.getElementById("perPageRecord").value * 1.0;
    var currentPage = eiInfo.get("currentPage");
    var totalPageCount = eiInfo.get("totalPageCount");
    var pageNum = document.getElementById("pageNum").value * 1.0;
    if ((pageNum) > totalPageCount) {
        pageNum = totalPageCount;
    }
    if (pageNum < 1) {
        pageNum = 1;
    }
    changePage(perPageRecord, (pageNum - 1) * perPageRecord, pageNum);
    document.getElementById("pageNum").value = pageNum;
}

changePage = function(limit, offset, currentPage){
    
    var ajax_callback = {
        onSuccess: function(eiInfo){
            modalWin.hide();
            for (i = sel.rows.length - 1; i > 0; i--) {
                sel.deleteRow(i);
            }
            paint_resource(sel, eiInfo);
            var ei_info = _getEi();
            ei_info.set("currentPage", eiInfo.get("currentPage"));
            ei_info.set("totalPageCount", eiInfo.get("totalPageCount"));
            var currentPage = document.getElementById("currentPage");
            currentPage.innerText = '第 ' + eiInfo.get("currentPage") + ' 页';
            var totalPageCount = document.getElementById("totalPageCount");
            totalPageCount.innerText = '共 ' + eiInfo.get("totalPageCount") + ' 页';
			
        },
        onFail: function(eMsg){
            modalWin.hide();
            alert("查询失败，请重试，或联系客服。");
        }
    };
    var info = new EiInfo();
    info.setByNodeObject(document.forms[0]);
    info.set("limit", limit);
    info.set("offset", offset);
    info.set("currentPage", currentPage);
	var modalWin = new EFModalWindow('progressWindow');
    modalWin.showProgressBar();
    EiCommunicator.send("BP07", "query", info, ajax_callback, false, true);
}

efgrid_onRowClicked = function(grid_id, row_index){
    if (grid_id == "_efselect_divwindowsubNode") {
        var grid = efgrid.getGridObject("_efselect_divwindowsubNode");
        var eiColumn = grid.getColumn(0, TYPE_DATA);
        var colummCname = eiColumn.getCname();
        if (colummCname == "客户代码" || colummCname == "客户序列号" || colummCname == "客户简称") {
            ef.get("i0-0-customerid").value = grid.getCellValue(row_index, 2, TYPE_DATA, false);
            efwindow.hide();
        } else if (colummCname == "品种代码" || colummCname == "品种描述") {
            ef.get("i0-0-producttypeid").value = grid.getCellValue(row_index, 1, TYPE_DATA, false);
            efwindow.hide();
        } else if (colummCname == "产地简称" || colummCname == "产线全称") {
            ef.get("i0-0-producingarea").value = grid.getCellValue(row_index, 0, TYPE_DATA, false);
            efwindow.hide();
        } else if (colummCname == "存放地") {
            ef.get("i0-0-locArea").value = grid.getCellValue(row_index, 0, TYPE_DATA, false);
            efwindow.hide();
        } else if (colummCname == "仓库名称") {
            ef.get("i0-0-wprovider").value = grid.getCellValue(row_index, 0, TYPE_DATA, false);
            efwindow.hide();
        } else if (colummCname == "产地" ) {
            ef.get("i0-0-prducingAreaShort").value = grid.getCellValue(row_index, 0, TYPE_DATA, false);
            efwindow.hide();
        }
    }
}


expandDetail = function(obj){
	if(obj.style.display =="none"){
		obj.style.display = "block";
	}else{
		obj.style.display = "none";
	}
}


