var commonGridObj_recvamount;
var comboCustomer;//委托单位

function $(id){
    return document.getElementById(id);
}

function createGrid(){
         //header
    var headerArgs_recvamount = new Array("<input type=\"checkbox\" id=\"ck_all\" onclick=\"checkAll('ck_all')\" />","实收名称","录入编号","实收金额","付款单位","收款人","收款时间","币别","结算金额","未结算金额","结算时间","结算人","备注");
    //width
    var widthArgs_recvamount = new Array("40","0","120","100","100","130","130","60","100","100","100","150","150");
    //column align                       
    var colAlignArgs_recvamount = new Array("center","center","center","center","center","center","center","center","center","center","center","center","center");
    //column sort               
    var colSortArgs_recvamount = new Array("na","str","str","str","str","str","str","str","str","str","str","str","str");
    //column type
    var colTypeArgs_recvamount = new Array("ch","ro","ro","ro","ro","ro","ro","ro","ro","ro","ro","ro","ro");
 
    
    commonGridObj_recvamount = new TGridCommon("mygrid_container_recvamount","form1",headerArgs_recvamount,widthArgs_recvamount,colAlignArgs_recvamount,colSortArgs_recvamount,colTypeArgs_recvamount);
    
    commonGridObj_recvamount.setResponseUrl("SettlementGridSourceApp.aspx");
    commonGridObj_recvamount.setRequest("SettlementGridSourceApp.aspx?handle=getrecvamount&show_page=15&cur_page=1&uid="+commonGridObj_recvamount.newGuid());
    commonGridObj_recvamount.setSourceType("json");
    commonGridObj_recvamount.setSkin("xp");
    commonGridObj_recvamount.setImageUrl("../images/");
    commonGridObj_recvamount.initGrid();
    commonGridObj_recvamount.bind();

    commonGridObj_recvamount.initPageByHandle("getrecvamount");
//    commonGridObj_settlement.getGridObj().attachEvent("onRowSelect", function(id,ind){
//        var openSet = "height=520, width=1000, toolbar=no, menubar=no,scrollbars=no, resizable=no,location=no, status=no,Top= " +(screen.height-520)/2 + ",Left= "+(screen.width-1000)/2
//        var openType = "_blank";
//            
//        var openUrl = "SettlementEdit.aspx?handle=edit&id="+id;
//        window.open (openUrl,openType,openSet); 
//        
//    });
//    
//    commonGridObj_settlement.clickEvent = function(){
//        commonGridObj_settlement.getGridObj().attachEvent("onRowSelect", function(id,ind){
//            var openSet = "height=520, width=1000, toolbar=no, menubar=no,scrollbars=no, resizable=no,location=no, status=no,Top= " +(screen.height-520)/2 + ",Left= "+(screen.width-1000)/2
//            var openType = "_blank";
//                
//            var openUrl = "SettlementEdit.aspx?handle=edit&id="+id;
//            window.open (openUrl,openType,openSet); 
//            
//        });
//    }
    commonGridObj_recvamount.isDBClickEvent = true;
    
    commonGridObj_recvamount.getGridObj().attachEvent("onRowDblClicked", function(rId,cInd){ 
            var openSet = "height=450, width=1000, toolbar=no, menubar=no,scrollbars=no, resizable=no,location=no, status=no,Top= " +(screen.height-450)/2 + ",Left= "+(screen.width-1000)/2
            var openType = "_blank";
                
            var openUrl = "ReceiveAmountEdit.aspx?handle=edit&id="+rId;
            var tempWin = window.open ("about:blank",openType,openSet);
            tempWin.location = openUrl;
    });
    
    commonGridObj_recvamount.dbclickEvent = function(){
        commonGridObj_recvamount.getGridObj().attachEvent("onRowDblClicked", function(rId,cInd){  
            var openSet = "height=450, width=1000, toolbar=no, menubar=no,scrollbars=no, resizable=no,location=no, status=no,Top= " +(screen.height-450)/2 + ",Left= "+(screen.width-1000)/2
            var openType = "_blank";
                
            var openUrl = "ReceiveAmountEdit.aspx?handle=edit&id="+rId;
            var tempWin = window.open ("about:blank",openType,openSet);
            tempWin.location = openUrl;
        });
    }
}


function initCombo(){
    createComboCustomer();//结算单位
}

function refreshList(){
    createGrid();
}

//根据条件查询付费申请信息
function search(){
    var _begintime = $("txt_beginDate").value.trim();
    
    if(_begintime.trim() != ""){
        if(!validateDateTime(_begintime)){
            alert("起始时间格式错误");
            return;
        }
    }
    var _endtime = $("txt_endDate").value.trim();
    if(_endtime.trim() != ""){
        if(!validateDateTime(_endtime)){
            alert("结束时间格式错误");
            return;
        }
    }
    
    
    var _customer = $("h_customer").value.trim();
    var _checkno = $("txt_billno").value.trim();
    
    var searchJson = "";
    //begin
    searchJson +="{";
    searchJson += "[";
    
    //如果查询条件设置了起始时间和结束时间则判断结束时间是否大于起始时间
    if(_begintime != "" && _endtime != ""){
        if(_begintime.length > 9 && _endtime.length > 9){
            var status = DateTimeEqual(_begintime,_endtime);
            if(status == 0){
                searchJson += "\"btime\":\""+_begintime+"\","
                searchJson += "\"etime\":\""+_endtime+"\","
            }else if(status == -1){
                alert("时间设置错误,起始时间不能大于结束时间,请修改!");
                return;
            }
//            else if(status == -2){
//                alert("时间设置错误,起始时间不能等于结束时间,请修改!");
//                return;
//            }
        }
        else{
            alert("时间格式有问题,长度应大于9个字符,请修改!");
            return;
        }
    }else{
        searchJson += "\"btime\":\""+_begintime+"\","
        searchJson += "\"etime\":\""+_endtime+"\","
    }
    
    searchJson += "\"cus\":\""+_customer+"\",";
    searchJson += "\"no\":\""+_checkno+"\"";
    searchJson += "]";
    searchJson +="}";//alert(searchJson);
    
    commonGridObj_recvamount.initGrid();
    commonGridObj_recvamount.setRequest("SettlementGridSourceApp.aspx?handle=getrecvamount&show_page=15&cur_page=1&search="+escape(searchJson)+"&uid="+commonGridObj_recvamount.newGuid());
    commonGridObj_recvamount.bind();
    
    commonGridObj_recvamount.setSearch(searchJson);
    commonGridObj_recvamount.initPageByHandle("getrecvamount");
    
    commonGridObj_recvamount.isDBClickEvent = true;
    commonGridObj_recvamount.getGridObj().attachEvent("onRowDblClicked", function(rId,cInd){ 
            var openSet = "height=450, width=1000, toolbar=no, menubar=no,scrollbars=no, resizable=no,location=no, status=no,Top= " +(screen.height-450)/2 + ",Left= "+(screen.width-1000)/2
            var openType = "_blank";
                
            var openUrl = "ReceiveAmountEdit.aspx?handle=edit&id="+rId;
            window.open (openUrl,openType,openSet); 
    });
    
    commonGridObj_recvamount.dbclickEvent = function(){
        commonGridObj_recvamount.getGridObj().attachEvent("onRowDblClicked", function(rId,cInd){  
            var openSet = "height=450, width=1000, toolbar=no, menubar=no,scrollbars=no, resizable=no,location=no, status=no,Top= " +(screen.height-450)/2 + ",Left= "+(screen.width-1000)/2
            var openType = "_blank";
                
            var openUrl = "ReceiveAmountEdit.aspx?handle=edit&id="+rId;
            window.open (openUrl,openType,openSet); 
        });
    }
}
//校验时间格式
function validateDateTime(dateTimeObj){
    var isValid = false;
    
    var parseToObj = new Date(dateTimeObj.replace(/-/g, "/"));
    if(!isNaN(parseToObj)){
        isValid = true;
    }
    
    return isValid;
}

function DateTimeEqual(beginObj,endObj){
    var isEqual = 0;
    var begin = new Date(beginObj.replace(/-/g, "/"));
    var end = new Date(endObj.replace(/-/g, "/"));
    
    if(Date.parse(begin) - Date.parse(end) > 0){
        isEqual = -1;//起始时间大于结束时间
    }else if(Date.parse(begin) - Date.parse(end) == 0){
        isEqual = -2;//起始时间与结束时间相等
    }
    return isEqual;
}

String.prototype.trim = function() { return this.replace(/(^\s*)|(\s*$)/g,""); }

function createComboCustomer(){//委托单位
    comboCustomer = dhtmlXComboFromSelect("sel_customer");            
    comboCustomer.loadXML("../FeeCodes/WebCrmClientAdapter.aspx?mask=0&pos=0");
    
    comboCustomer.attachEvent("onChange", function(){
        comboCustomer.setComboText($("h_customer").value);
    }); 
    
    comboCustomer.attachEvent("onBlur", function(){
        var s1 = comboCustomer.getComboText().trim();
        var s2 = s1.indexOf("|");
        if(s2>0){
            s2=s2+2;
        }
        var s3 = s1.substring(s2);
        if(s2>0){                    
            comboCustomer.setComboText(s3);
            $("h_customer").value=s3;
        }
        else
        {
            comboCustomer.setComboText($("h_customer").value);
        }
    }); 
    
    comboCustomer.setComboText($("h_customer").value);  
    comboCustomer.attachEvent("onKeyPressed", function(keyCode){
        if(keyCode!="8"){          
            var arrayArg = new Array();
            setTimeout(function(){
               var filterCount = 0;
               var is = -1;
               var ishd = $("h_customer").value.trim();
               if("" != comboCustomer.getComboText().trim()){
                   while("undefined" != typeof comboCustomer.getOptionByIndex(filterCount)){
                        var comboValue = comboCustomer.getOptionByIndex(filterCount).text.toUpperCase();//.value;
                        var enterValue = comboCustomer.getComboText().trim().toUpperCase();
                        $("h_customer").value = comboCustomer.getComboText().trim().toUpperCase();
                        if(comboValue.indexOf(enterValue) == 0){
                            comboCustomer.selectOption(filterCount,true,true); 
                            is=filterCount;
                            break;
                        }
                        ++filterCount;
                   }
                   if(is==-1){
                        $("h_customer").value = ishd;
                        alert("无此内容,请重新选择!");
                   }
               }
             },10);
         }
    });     
                       
    comboCustomer.attachEvent("onSelectionChange", function(){
        $("h_customer").value = comboCustomer.getComboText().trim();
    }); 
}

//初始化界面
function initGrid(){
    $("h_customer").value = "";
    comboCustomer.setComboText($("h_customer").value);
    $("txt_beginDate").value = "";
    $("txt_endDate").value = "";
    $("txt_billno").value = "";
    createGrid();
}

function InvoiceVerification(){
    var openSet = "height=560, width=1000, toolbar=no, menubar=no,scrollbars=no, resizable=no,location=no, status=no,Top= " +(screen.height-490)/2 + ",Left= "+(screen.width-1000)/2
    var openType = "_blank";
        
    var openUrl = "../Invoice/RecvSettleInvoiceVerification.aspx";
    window.open (openUrl,openType,openSet); 
}

function checkAll(id){
    if($(id).checked){
        for(var i=0;i<commonGridObj_recvamount.getGridObj().getRowsNum();i++){
            commonGridObj_recvamount.getGridObj().cellByIndex(i,0).setValue(1);
        }
    }else{
        for(var i=0;i<commonGridObj_recvamount.getGridObj().getRowsNum();i++){
            commonGridObj_recvamount.getGridObj().cellByIndex(i,0).setValue(0);
        }
    }
}

function cancelSettle(){
    var success = 0;
    var failure = 0;
    var redo = 0;
    var noinfo = 0;
    
    var iCount = 0;
    var recvArg = new Array();
    
    for(var i=0;i<commonGridObj_recvamount.getGridObj().getRowsNum();i++){
        var checkState = commonGridObj_recvamount.getGridObj().cellByIndex(i,0).getValue();
        
        var billNO = commonGridObj_recvamount.getGridObj().cellByIndex(i,1).getValue().trim();
        if(checkState == 1){
            recvArg.push(billNO);
            iCount++;
        }
    }
    if(iCount > 0){
        var delCount = 0;
        if(recvArg.length > 0){
            if(window.confirm("您确定要将所选应收费用删除")){
                while(delCount < recvArg.length){
                    var url = "SettlementGridSourceApp.aspx?handle=cancelrecvsettle&billno="+recvArg[delCount]+"&uid="+commonGridObj_recvamount.newGuid();
                    var loader = dhtmlxAjax.getSync(url);
                    var stateInfo = loader.xmlDoc.responseText;
                    var state = parseInt(stateInfo);
                    
                    if(state == 1){
                        success++;
                    }else if(state == -3){
                        failure++;
                    }else if(state == -4){
                        failure++;
                    }else{
                        failure++;
                    }
                    delCount++;
                }
            }
        }
        createGrid();
        alert("执行撤销收费结算操作 共 "+delCount+" 条 成功 "+success+" 条 失败 "+failure+" 条");
    }else{
        alert("未选中要操作的费用项");
    }
}

function addRecvAmount(){
    var openSet = "height=450, width=1000, toolbar=no, menubar=no,scrollbars=no, resizable=no,location=no, status=no,Top= " +(screen.height-450)/2 + ",Left= "+(screen.width-1000)/2
    var openType = "_blank";
        
    var openUrl = "ReceiveAmountEdit.aspx?handle=add";
    var tempWin = window.open ("about:blank",openType,openSet);
    //tempWin.blur();
    //tempWin.opener.focus();
    tempWin.location = openUrl;
}

function deleteRecvAmount(){
    var delArg = new Array();
    var iDelCount = 0;
    for(var i=0;i<commonGridObj_recvamount.getGridObj().getRowsNum();i++){
        var checkState = commonGridObj_recvamount.getGridObj().cellByIndex(i,0).getValue();
        var rId = commonGridObj_recvamount.getGridObj().getRowId(i);
        if(checkState == 1){
            iDelCount++;
            delArg.push(rId);
        }
    }
    
    if(iDelCount > 0){
        for(var j=0;j<delArg.length;j++){
            var url = "SettlementGridSourceApp.aspx?handle=deleterecvamount&recvamountid="+delArg[j]+"&uid="+commonGridObj_recvamount.newGuid();
            var loader = dhtmlxAjax.getSync(url);
            var stateInfo = loader.xmlDoc.responseText;
            var state = parseInt(stateInfo);
           
        }
        createGrid();
    }
    
}