function jsBLackListManage(){

};
jsBLackListManage.CurBlackListGrid=null;
jsBLackListManage.UserId=App.CurrentUser(App.Virsion.main).UserID;
jsBLackListManage.UserName=App.CurrentUser(App.Virsion.main).UserName;

$(function(){
    App.SetNullHref(0);
        $("#txtRemark").ikeEditor({ width: 180, height: 30, HiddentToolBar: true, ValueMode: 'Text' });
    App.SetNullHref(1);
    jsBLackListManage.initBLackListGrid(); 
});

/* 黑名单列表 */
jsBLackListManage.initBLackListGrid = function() {
    try 
    {
        var  w=$(document.documentElement).width()-1;
        var  gh= $(document.documentElement).height()-183;
        var tableGridOption =
        {
            height: gh,
            colModel: 
            [
                {display: '编号ID', name: 'ID', width: (w * 0.1), sortable: false, align: 'left' ,hide:true},
                {display: '电话号码', name: 'CallNum', width: (w * 0.25), sortable: true, align: 'left'},
                { display: '备注', name: 'Remark', width: (w * 0.4), sortable: true, align: 'left',process:jsBLackListManage.FormtRemark},             
                { display: '操作', name: 'ID', width: (w * 0.15), sortable: true, align: 'center' ,process:jsBLackListManage.FormtGridBtn}
            ],            
             customFunction: jsBLackListManage.ShowBLackListGrid,
             title:"黑名单列表",
             selectedonclick:true,
             singleselected: true,
             rp: 5,
             rowbinddata:true,
             rowhandler:jsBLackListManage.ClickBlackListGridRow,
             sortname: 'ID',
             sortorder: 'desc'
        };
        jsBLackListManage.CurBlackListGrid = $("#tbBlackListGrid").ikegrid(tableGridOption);
        tableGridOption = null;
    }
    catch (e) {
        alert("jsBLackListManage.initBLackListGrid:"+e.message);
    }
    CollectGarbage();
};
/*绑定显示数据*/
jsBLackListManage.ShowBLackListGrid = function(pageSize, pageIndex, searchStr,extpara,order) {
    var strCallNum="";
    
    if(searchStr!="" && searchStr!=undefined)
    {   
        strCallNum=searchStr[0]._CallNum;
    }

     var countTop=pageSize*(pageIndex-1);
     var strSql = "SELECT T_BlackList.ID, T_BlackList.CallNum, T_BlackList.Remark FROM T_BlackList ";
     
     var strWhere =" where 1=1 " ;
    if(strCallNum !="" && strCallNum != null)
    {
        strWhere = strWhere + " and T_BlackList.CallNum like '%"+ strCallNum +"%'";
    }

    if(order !="" && order != null)
   {
        strOrderBy="T_BlackList."+order;
   }
    strSql = strSql + strWhere+" Order By "+strOrderBy; 
    /*分页代码*/
    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;    
    }
 
    strCallNum=null;
    strResult=null;
    CollectGarbage();
    return strResult=null?null:result[0];
}

/*搜索*/
jsBLackListManage.BlackListSearchString=function(){
    var strWhere=[];
    var htxt_CallNum=$.trim($("#txt_CallNum").val()).split('\"').join('“').split('\'').join('‘');
    strWhere.push({_CallNum:htxt_CallNum});
    $("#tbBlackListGrid").DoSearch(strWhere);
    htxt_CallNum=null;
    CollectGarbage();
};

/*清空*/
jsBLackListManage.BlackListCancel=function(){
    $("#txt_CallNum").val('');
    jsBLackListManage.BlackListSearchString();
};

/*列表中格式化备注*/
jsBLackListManage.FormtRemark=function(value,pid,rows){
    if(value.toString()!="")
    {
        var strValue=unescape(value.toString()).length>10?unescape(value.toString()).substring(0,10)+"...":unescape(value.toString());
        return '<a href="#" title='+unescape(value.toString())+'>'+strValue+'</a>';
    }
};

/*列表中操作*/
jsBLackListManage.FormtGridBtn=function(value,pid,rows){
    return '<button class="button" onclick="jsBLackListManage.DeleteBlackInfo('+rows["ID"]+');">删除</button>';
};

/*删除号码*/
jsBLackListManage.DeleteBlackInfo=function(id){
    if(confirm("确定将该号码移出黑名单吗？"))
    {
        var txtSql="DELETE * FROM T_BlackList WHERE ID="+id;
        var result = DBReader.execSql(txtSql);
        if(result)
        {             
            PhoneHelper.ClearTelBook();/*清空话机黑名单*/
            jsBLackListManage.ReSetBlackListForCall();/*重新写入黑名单*/
            jsBLackListManage.BlackListSearchString();/*刷新黑名单列表*/
            ShowWindow.opener.execScript(App.query("formIndex"),"jsIndexPage.freshItemPage('tab-callRecord','jsTelBillCenterContent.CallRecordSearchString()')");/*刷新通话记录列表*/
        }
        txtSql=null;
        CollectGarbage();
    }
};

/*重新写入黑名单*/
jsBLackListManage.ReSetBlackListForCall=function(){
    var strSql="SELECT ID,CallNum,Remark FROM T_BlackList ORDER BY ID";
    var obj=DBReader.selectSqlForJson(strSql);
    if(obj!=null)
    {
        var result=eval("[{"+obj+"}]");
        if(result[0].result==1 && result[0].table!=undefined)
        {
            for(var i=0;i<result[0].table.length;i++)
            {
                /*将号码设置到话机黑名单中*/
                PhoneHelper.AddTelbookRec(result[0].table[i].CallNum,unescape(result[0].table[i].Remark));
            }
        }
    }
};

/*单击列表行时绑定数据*/
jsBLackListManage.ClickBlackListGridRow=function(target){
     $(target).click(function(){
            $("#txtTelNum").val($(target).attr("CallNum"));
            $("#txtRemark").value(unescape($(target).attr("Remark")))
     });
};
/*保存*/
jsBLackListManage.SaveBlackListInfo=function(){
    var result=0;
    var alertArr=[];
    var strCallNum=$.trim($("#txtTelNum").val()).split('\"').join('“').split('\'').join('‘');
    var strRemark=$("#txtRemark").value()==""?"":escape($("#txtRemark").value());
    if(strCallNum!="")
    {
        var entity=jsEntity.CreateEntityByTableName_access("T_BlackList");   
        entity.Fields.CallNum=strCallNum;
        entity.Fields.Remark=strRemark;
        var CallID=jsCommonLogic.selColumnData("ID","T_BlackList"," WHERE CallNum='"+strCallNum+"'");
        if(CallID>0)
        {
            entity.Fields.ID=CallID;
            entity.Fields.LastModifier=jsBLackListManage.UserName;
            entity.Fields.LastModifierID=jsBLackListManage.UserId;
            entity.Fields.LastModifyTime="@Time";
            result = entity.Update(entity);
        }
        else
        {           
            entity.Fields.Creator=jsBLackListManage.UserName;
            entity.Fields.CreatorID=jsBLackListManage.UserId;
            entity.Fields.CreateTime="@Time";
            if(jsCommonLogic.selColumnData("count(*)","T_BlackList","")>48)
            {
                alertArr.push("超出设置范围，黑名单只能设置48个号码！");
            }
            else
            { 
               PhoneHelper.AddTelbookRec(strCallNum,strRemark);/*将号码设置到话机黑名单中*/
               result = entity.Insert(entity);
            }
        }
        if(result)
        {
            jsBLackListManage.BlackListSearchString();
        }
        else 
        {
            if(alertArr.length>0)
            {
                alert(alertArr[0]);
            }
            else
            {
                 alert("保存没有成功!");
            }
        }
        alertArr.length=0;
        alertArr=null;
        DBReader.DisposeEntity(entity);
        CallID=null;
    }
    else
    {
        alert("请输入电话号码！");
    }
    strCallNum=null;
    strRemark=null;
    result=null;
    CollectGarbage();
};