function jsModelList(){

};
jsModelList.PageStatus=App.query("pageStatus");
jsModelList.modelGrid=null;

jsModelList.initModelGrid=function(){
    var w =  $(document.documentElement).width();
    var gh = $(document.documentElement).height()-$("#divInfoSearch").height()-80;
    if(jsModelList.PageStatus=="sel"){
        gh = $(document.documentElement).height()-$("#divInfoSearch").height()-105;
    }
    var option = {
         height: gh,
         width: w,
         colModel:[
            { display: '编号', name: 'ID', width: (w * 0.15), sortable: true, align: 'left',hide:true},
            { display: '内容', name: 'SMSTXT', width: (w * 0.4), sortable: true, align: 'left',process:jsModelList.FormatDetailContent,processType:'text'},
            { display: '操作', name: 'ID', width: (w * 0.1), sortable: true,hide:jsModelList.PageStatus=="man"?false:true,process: jsModelList.operateDetailButton}
         ],
         customFunction: jsModelList.ShowModelGrid,
         rowhandler: jsModelList.doubleClick,
         rp: 20,
         rowbinddata:true,
         sortname: 'ID',
         sortorder: 'desc'
    };
    jsModelList.modelGrid= $("#tbModelGrid").ikegrid(option);
};
/*获取数据*/
jsModelList.ShowModelGrid=function(pageSize, pageIndex, searchStr,extpara,order){
    var countTop=pageSize*(pageIndex-1);
    var strSql = "SELECT ID,SMSTXT FROM  T_SMSPHRASE";
    var strWhere =" where 1=1 " ;
    if (searchStr != "" && searchStr != undefined) {     
           if(searchStr[0]._Content!="" && searchStr[0]._Content!=null)
          {
            strWhere+=" AND SMSTXT like '%"+searchStr[0]._Content+"%'"
          }     
    }
   var strOrderBy="";
   if(order !="" && order != null)
   {
        var str=order.toUpperCase().replace(/ASC/i,"").replace(/DESC/i,"").replace(/\s/g, "");
        if(str !="ID"){
            strOrderBy=order+",ID DESC";
         }
        else{
            strOrderBy=order;
         }   
   }
  strSql += strWhere+" Order By "+strOrderBy;   //  window.clipboardData.setData("text",strSql);
    /*分页代码*/
    if(countTop >0){
         strSql="select TOP "+ pageSize +" (select count(ID) from ( " + strSql +")) as AllCount, * from (" + strSql +") where ID NOT IN (SELECT TOP "+ countTop +" ID from (" + strSql + "))";
    }
    else{
        strSql="select TOP "+ pageSize +" (select count(ID) from ( " + strSql +")) as AllCount,  * from (" + 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];
};

/*双击行选择*/
jsModelList .doubleClick=function(target){
    $(target).dblclick(function(){     
        if(jsModelList.PageStatus=="sel"){
            ShowWindow.opener.execScript(App.query("formIdx"),App.query("action").replace('@Mid',$(target).attr("ID")));
           ShowWindow.close();
        }
    });
};

/*搜索*/
jsModelList.ModelInfoSearchString=function(){
    var  strWhere=[];
    var strContent=$("#txtInfoContent").val().split('\"').join('“').split('\'').join('‘');
    strWhere.push({_Content:strContent});
    $("#tbModelGrid").DoSearch(strWhere);
}

/*清空*/
jsModelList.Cancel=function(){
    $("#txtInfoContent").val("");
    jsModelList.ModelInfoSearchString();
}

/*信息内容*/
jsModelList.FormatDetailContent=function(value,pid,rows){
//    if(value.toString()!=""){
//         var strValue=unescape(value.toString()).length>20?unescape(value.toString()).substring(0,20)+"...":unescape(value.toString());
//         return '<input id="txtInfoContent_'+pid+'"  type="text" style="width:90%" class="inputList" value="'+strValue+'" title="'+unescape(value.toString())+'"/>';
//    }
//    else{
//         return '<input id="txtInfoContent_'+pid+'" class="inputList" type="text" style="width:90%"/>';
//    }
    
      if(value.toString()!="")
    {
        var strValue=unescape(value.toString()).length>20?unescape(value.toString()).substring(0,20)+"...":unescape(value.toString());
        return '<a href="#" title='+unescape(value.toString())+'>'+strValue+'</a>';
    }
};

/*列表操作*/
jsModelList.operateDetailButton=function(value,pid,rows){
      return '<a onclick="jsModelList.OpenContentWin(\'edit\',\''+value+'\');" style="cursor:pointer;"><img src="../../AppFrame/APICss/default/ico/user_add.png" />修改</a>&nbsp;<a onclick="jsModelList.delInfoMethod(\''+pid+'\');" style="cursor:pointer;"><img src="../../AppFrame/APICss/default/ico/user_delete.gif" />删除</a>';
};

/*删除数据*/
jsModelList.delInfoMethod=function(rowID){
    var rowObj= jsModelList.modelGrid.getRowByID(rowID);
    if (window.confirm("确定删除吗？")) {
       var rowData = jsModelList.modelGrid.getRowData(rowObj);
       if(rowData[0].ID>0){
            var txtSql="DELETE * FROM T_SMSPHRASE WHERE ID="+rowData[0].ID;
            DBReader.execSql(txtSql);
            txtSql=null;
       }
       rowData=null;
       jsModelList.modelGrid.deleteRow(rowObj.rowIndex);
    }
    rowObj=null;
    CollectGarbage();
};

/*保存数据*/
jsModelList.SaveModelInfo=function(rowID){
    var result=-1;    
    var rowObj = jsModelList.modelGrid.getRowByID(rowID);
    var rowData =jsModelList.modelGrid.getRowData(rowObj);
    if(unescape(rowData[0].SMSTXT)!=""){
        var entity = jsEntity.CreateEntityByTableName_access("T_SMSPHRASE");
        entity.Fields.USERID=1;
        entity.Fields.SMSTXT=rowData[0].SMSTXT;    
        if(rowData[0].ID>0){
            entity.Fields.ID=rowData[0].ID;
            result=entity.Update(entity);
        }
        else{       
            result=entity.Insert(entity);
        }            
        if(result){
            jsModelList.ModelInfoSearchString();
            alert("保存成功！");
        }
        else{
            alert("保存没有成功！");
        }
        DBReader.DisposeEntity(entity);
    }else{
        alert("内容不能为空！");
    }
    rowObj=null;
    rowData=null;        
    CollectGarbage();
}

/*新增行*/
jsModelList.AddInfoRow=function(){
    var newRowObj=jsModelList.modelGrid.addRow(0);
    var initValue = [{'ID':'',  'Content': ''}];
    jsModelList.modelGrid.setRowData(newRowObj,initValue);
    newRowObj=null;
    initValue=null;
    CollectGarbage();
};

/*打开更改短信模板窗体*/
jsModelList.OpenContentWin=function(isType,id){
    var curID=0;
    if(isType=="edit"){
        curID=id;
    }
    ShowWindow.ShowMoledDialog("Application/TeleSecretary/SMSMessage/frmPreSMSContent.htm?formIdx=" + ShowWindow.getFormIdx() + "&action=jsIndexPage.freshItemItemPage('tab-messageSend','jsModelList.ModelInfoSearchString()')&pageStatus="+isType+"&curID="+curID, "短信模板", 560, 280, 0);
}

$(function(){
    if(jsModelList.PageStatus=="sel"){
        $("#imgAddInfoRow").hide();
        $("#divHelp").show();
    }
    jsModelList.initModelGrid();
});