function jsMessageList(){

};

jsMessageList.SMSGrid=null;
jsMessageList.PageStatus=null;

$(function(){
    jsMessageList.PageStatus=App.query("pageType");
    if(jsMessageList.PageStatus== "1"){
        $("#fontTitle").html("草稿列表");
    }else if(jsMessageList.PageStatus== "2"){
        $("#fontTitle").html("待发送列表");
    }else if(jsMessageList.PageStatus == "3"){
        $("#fontTitle").html("已发送列表");
    }
    jsMessageList.LoadMessageList();
});

jsMessageList.LoadMessageList=function(){
    try {
         var w = $(document.documentElement).width();
         var gh = $(document.documentElement).height() - 80;
    /*<-------------------------------------------------------------主列表---------------------------------------------------->*/
        var defOrder="T_SMSTO.MAKEDATE";
        var operID="TOID";
        if(jsMessageList.PageStatus=="1"){
            defOrder="T_SMSSEND.SID";
            operID="SID";
        }
         var option = {
             height: gh,
             width: w,
             colModel:
             [
                { display: '接收号码', name: 'MOBILE', width: (w * 0.15), sortable: true, align: 'left'},
                { display: '收信人', name: 'NAME', width: (w * 0.08), sortable: true, align: 'left'},
	            { display: '公司简称', name: 'COMPANYNAME', width: (w * 0.15), sortable: true, align: 'left' },
	            { display: '日期', name: 'MAKEDATE', width: (w * 0.12), sortable: true, align: 'left'},
	            { display: '分段数', name: 'SUBSECTION', width: (w * 0.05), sortable: false, align: 'left'},
	            { display: '短信内容', name: 'CONTENT', width: (w * 0.2), sortable: true, align: 'left',process:jsMessageList.FormatContent},
	            { display: '当前状态', name: 'STATUS', width: (w * 0.05), sortable: true, align: 'left'},
	            { display: '操作', name: "TOID", width: (w * 0.08), sortable: true, align: 'center',hide:(jsMessageList.PageStatus=="1"?false:true),process:jsMessageList.btnOperate}
		     ],
             customFunction: jsMessageList.ShowPage,
             rp: 20,
             sortname:defOrder,
             sortorder: 'desc',
             rowbinddata:true
         };
         jsMessageList.SMSGrid = $("#tbGrid").ikegrid(option);
         w=null;
         gh=null;
         defOrder=null;
         operID=null;
         option=null;
         CollectGarbage();
    }
    catch (e) {
        alert("jsMessageList.LoadMessageList:" + e.message);
    }
}

jsMessageList.ShowPage=function(pageSize, pageIndex, searchStr, extpara, order){
    var countTop=pageSize*(pageIndex-1);
    var strSql = "";
    if(jsMessageList.PageStatus=="1"){
        strSql="SELECT  T_SMSSEND.SID as TOID,'' as MOBILE, '' as NAME,'' as COMPANYNAME, '' as MAKEDATE, '' as SUBSECTION, T_SMSSEND.CONTENT, '草稿' as STATUS FROM T_SMSSEND"
    }else if(jsMessageList.PageStatus=="2"){
        strSql="SELECT T_SMSTO.TOID,T_SMSTO.MOBILE, T_SMSTO.NAME, iif(IsNull( T_COMPANY.SIMPLE_NAME ), T_COMPANY.FULL_NAME, T_COMPANY.SIMPLE_NAME ) as COMPANYNAME, T_SMSTO.MAKEDATE, T_SMSTO.SUBSECTION, T_SMSSEND.CONTENT, '待发送' as STATUS FROM (T_SMSTO INNER JOIN T_SMSSEND ON T_SMSTO.SMSID = T_SMSSEND.SID) LEFT JOIN T_COMPANY ON T_SMSTO.CUSTOMERID = T_COMPANY.COMPANY_ID";
    }else if(jsMessageList.PageStatus=="3"){
        strSql="SELECT  T_SMSTO.TOID,T_SMSTO.MOBILE, T_SMSTO.NAME,iif(IsNull( T_COMPANY.SIMPLE_NAME ), T_COMPANY.FULL_NAME, T_COMPANY.SIMPLE_NAME ) as COMPANYNAME, T_SMSTO.MAKEDATE, T_SMSTO.SUBSECTION, T_SMSSEND.CONTENT,T_SMSTO.STATUS FROM (T_SMSTO INNER JOIN T_SMSSEND ON T_SMSTO.SMSID = T_SMSSEND.SID) LEFT JOIN T_COMPANY ON T_SMSTO.CUSTOMERID = T_COMPANY.COMPANY_ID";
    }
    var strWhere =" where 1=1 " ;
    if(jsMessageList.PageStatus=="1"){/*草稿*/
        strWhere+="AND ISDRAFT=true";
    }if(jsMessageList.PageStatus=="2"){/*待发送*/
        strWhere+="AND ISDRAFT=false  AND T_SMSTO.STATUS is null ";
    }else if(jsMessageList.PageStatus=="3"){/*已发送*/
        strWhere+=" AND T_SMSTO.STATUS is not null";        
    }
    var sort = " desc";
    if (order != undefined && order.toUpperCase().indexOf("ASC") != -1) {
        sort = " asc";
    }    
  strSql += strWhere+" Order By "+order;   //  
    /*分页代码*/
    if(countTop >0){   
//        if(jsMessageList.PageStatus=="1"){
//            strSql="select( select count(*)  from ( SELECT T_SMSSEND.SID FROM T_SMSSEND " + strWhere + " )) as AllCount,* from (select top " + pageSize + " * from ( " + strSql + "  ,T_SMSSEND.SID asc)   order by " + order.toUpperCase().replace("DESC", "").replace("ASC", "") + (sort == " asc" ? " desc" : " asc") + "   ,T_SMSSEND.SID asc)order by " + order.toUpperCase().replace("DESC", "").replace("ASC", "") + sort;
//        }else{
//            strSql="select( select count(*)  from ( SELECT T_SMSTO.TOID FROM T_SMSTO INNER JOIN  T_SMSSEND  ON T_SMSTO.SMSID = T_SMSSEND.SID " + strWhere + " )) as AllCount,* from (select top " + pageSize + " * from ( " + strSql + "  ,T_SMSTO.MAKEDATE asc)   order by " + order.toUpperCase().replace("DESC", "").replace("ASC", "") + (sort == " asc" ? " desc" : " asc") + "   ,T_SMSTO.MAKEDATE asc)order by " + order.toUpperCase().replace("DESC", "").replace("ASC", "") + sort;
//        }  
         strSql="select TOP "+ pageSize +" (select count(TOID) from ( " + strSql +")) as AllCount, * from (" + strSql +") where TOID NOT IN (SELECT TOP "+ countTop +" TOID from (" + strSql + "))";
    }
    else{
        strSql="select TOP "+ pageSize +" (select count(TOID) from ( " + strSql +")) as AllCount,  * from (" + strSql +") ";
    }
    window.clipboardData.setData("text",strSql);
    var strResult=DBReader.selectSqlForJson(strSql);
    var result;
    if(strResult!=null)
    {
        result=eval("[{"+strResult+"}]");
        result[0].pageIndex=pageIndex;    
    }
    strResult=null;strOrderBy=null;

    CollectGarbage();
    return strResult=null?null:result[0];
}

/*草稿列表时，操作*/
jsMessageList.btnOperate=function(value,pid,rows){
    return '<input type="button" id="btnSend_'+pid+'" class="button" value="发送" onclick="jsMessageList.SendSMS(\''+value+'\')"/>';
}

jsMessageList.SendSMS=function(id){
    App.SetNullHref(0);
    parent.document.getElementById("ifrMainContent").src="frmSendMessage.htm?PreID="+id;
    App.SetNullHref(1);
}

jsMessageList.FormatContent=function(value,pid,rows){
    return unescape(value.toString());
}