function jsBookListModle(){

};
jsBookListModle.AddressBookGrid=null;  /*通讯簿列表模式中的列表*/
//jsBookListModle.CurSqlStr=null;

jsBookListModle.LoadListModleDom=function(){
        var arrString=new  Array();
        arrString.push("<!--通讯簿列表操作按钮-->");
        arrString.push('<div id="custom-toolbar">');
        arrString.push("</div>");
        arrString.push("<!--通讯簿列表-->");
        arrString.push('<table id="tbAdrressBook">');
        arrString.push("</table>");
        document.getElementById("callListModle").innerHTML=arrString.join("");
        
        jsBookListModle.LoadToolBarBtn();/*通讯簿操作按钮*/
        jsBookListModle.LoadCallBookGrid();/*通讯簿主列表*/
}

/*通讯簿操作按钮*/
jsBookListModle.LoadToolBarBtn=function(){
    customOption = { items:[
        { type: 'button', text: '新建档案', useable: 'T', className: 'tableAdd',handler: function(){ 
            jsBookCardModle.ChangeToAddInfo();
            jsCallBookList.IsListOrCardModle('List');
        } },
        { type: 'button', text: '修改', useable: 'T', className: 'tableEdit',handler: function(){   
            jsBookListModle.ChangeListOrCardModle();
         }},
        { type: 'button', text: '删除选中', useable: 'T', className: 'tableClose',handler: function() {
            if( jsBookListModle.AddressBookGrid.getSelectedRows().length>0)
            {
                if(confirm("确定要删除吗？"))   
                {
                        $(jsBookListModle.AddressBookGrid.getSelectedRows()).each(function(){
                            jsBookListModle.DeleteAllSelData($(this).attr("EMPLOYEE_ID"),$(this).attr("COMPANY_ID"));/*删除选择的*/
                        });    
            //        jsBookListModle.AddressBookGrid.flexReload();
                       jsBookListModle.CallRecordSearchString("default");
                }
            }
            else
            {
                alert("请选择要删除的行！");
            }
        }},
        { type: 'separate' },
        { type: 'button', text: '列设置', useable: 'T', className: 'tableSet',handler: function(){ jsBookListModle.OpenColSetWindow();}},
        { type: 'separate' },
        { type: 'button', text: '导入', useable: 'T', className: 'tableSave',handler: function() { } },                          
        { type: 'button', text: '档案卡', useable: 'T', className: 'tableView',handler:  function(){
            jsBookListModle.ChangeListOrCardModle();
        }}
     ]};
     $("#custom-toolbar").toolbar(customOption);
}

/*打开列设置窗口*/
jsBookListModle.OpenColSetWindow=function(){
        var FormIndex=ShowWindow.getFormIdx();
        ShowWindow.ShowMoledDialog("Application/TeleSecretary/AddressBookColSet/frmAddressBookColSet.htm?FormIndex="+FormIndex+"&LoadAction=jsIndexPage.freshItemPage('tab-addressBook','jsBookListModle.CallRecordSearchString()","列设置",500,500);
};

/*绑定档案卡模式数据*/
jsBookListModle.ChangeListOrCardModle=function(){
        var strEmpIDList="";
        if( jsBookListModle.AddressBookGrid.getSelectedRows().length>0)
        {
            $( jsBookListModle.AddressBookGrid.getSelectedRows()).each(function(){
                if(strEmpIDList!=""){
                    strEmpIDList+=",";
                }
                strEmpIDList+=$(this).attr("EMPLOYEE_ID");
            });
        }
        else
        {
            var store =  jsBookListModle.AddressBookGrid.getRowData();
            for(var i=0;i<store.length;i++)
            {
                if(strEmpIDList!=""){
                     strEmpIDList+=",";
                }
                strEmpIDList+=store[i].EMPLOYEE_ID;
            }
        }     
        jsBookCardModle.curSelectedEmpIDList=escape(strEmpIDList);
        jsBookCardModle.curInfoID=0;/*重置当前数据集下标*/
        jsBookCardModle.BindData();
        jsCallBookList.IsListOrCardModle('List');
};

/*通讯簿主列表*/
jsBookListModle.LoadCallBookGrid=function(){       
    var w = $("#middleContanier").width() ;
    var gh = 200;
    var tableGridOption =
    {
        height: gh,
      //  width: w,
        colModel:[
            {display: '联系人ID', name: 'EMPLOYEE_ID', width: (w * 0.1), sortable: false, align: 'left',hide:true },
            {display: '姓名', name: 'NAME', width: (w * 0.1), sortable: false, align: 'left' },
            { display: '性别', name: 'MANORWOMAN', width: (w * 0.03), sortable: false, align: 'left' },
            { display: '单位ID', name: 'COMPANY_ID', width: (w * 0.15), sortable: false, align: 'left' ,hide:true},
            { display: '单位', name: 'CmpName', width: (w * 0.15), sortable: false, align: 'left' },
            { display: '地址', name: 'ADDRESS', width: (w * 0.2), sortable: false, align: 'left' },
            { display: '手机号码', name: 'MOBILE', width: (w * 0.08), sortable: false, align: 'left' },
            { display: '固话号码', name: 'TEL1', width: (w * 0.08), sortable: false, align: 'left' }
        ],        
        customFunction: jsBookListModle.ShowBookListPage,
        showcheckbox:true,
        selectedonclick:true,
        rowbinddata:true,
        sortname: 'EMPLOYEE_ID',
        sortorder: 'asc',
        rowhandler:jsBookListModle.doubleClickToCardModle,
        rp:10
    };
    jsBookListModle.AddressBookGrid = $("#tbAdrressBook").ikegrid(tableGridOption);
    tableGridOption = null;
};

jsBookListModle.doubleClickToCardModle=function(target)
{
    $(target).dblclick(function(){
         jsBookListModle.ChangeListOrCardModle();
    })
}

/*通讯簿列表获取数据*/
jsBookListModle.CurSearchStringObj=[{
_Type:'-1',  /*-1全部，0熟悉度查询，1陌生度查询*/
_BeginTime:'',  /*开始时间*/
_EndTime:'',    /*结束时间*/
_EmpName:'',    /*姓名*/
_Sex:'',      /*性别(男or女)*/
_Customer:'',     /*单位*/
_Address:'',     /*地址*/
_PageSize:'',     /*一页多少条记录*/
_PageIndex:'',       /*第几页*/
_Order:''    /*排序,默认值按ID倒序*/
}];
jsBookListModle.ShowBookListPage = function(pageSize, pageIndex, searchStr, extpara, order) {
    var strPara = "";
    if (searchStr != "" && searchStr != undefined) {
        jsBookListModle.CurSearchStringObj._Type=searchStr[0]._Type;
        jsBookListModle.CurSearchStringObj._BeginTime=searchStr[0]._BeginTime ;
        jsBookListModle.CurSearchStringObj._EndTime=searchStr[0]._EndTime ;
        jsBookListModle.CurSearchStringObj._EmpName=searchStr[0]._EmpName ;
        jsBookListModle.CurSearchStringObj._Sex=searchStr[0]._Sex ;
        jsBookListModle.CurSearchStringObj._Customer=searchStr[0]._Customer ;
        jsBookListModle.CurSearchStringObj._Address=searchStr[0]._Address ;
        jsBookListModle.CurSearchStringObj._PageSize=pageSize;
        jsBookListModle.CurSearchStringObj._PageIndex=pageIndex ;
        jsBookListModle.CurSearchStringObj._Order=order;
    }
    else {
        jsBookListModle.CurSearchStringObj._PageSize=pageSize;
        jsBookListModle.CurSearchStringObj._PageIndex=pageIndex ;
        jsBookListModle.CurSearchStringObj._Order=order;
    }
    var strResult = jsBookListModle.GetAddressBookJson();
    var result;
    if (strResult != null) {
        result = eval("[{" + strResult + "}]");
        result[0].pageIndex = pageIndex;
    }
    strPara = null;
    CollectGarbage();
    return strResult == null ? null : result[0];
}

jsBookListModle.GetAddressBookJson=function(){
    var type =  jsBookListModle.CurSearchStringObj._Type;       
    var startTime =  jsBookListModle.CurSearchStringObj._BeginTime;
    var endTime = jsBookListModle.CurSearchStringObj._EndTime;
    var empName = jsBookListModle.CurSearchStringObj._EmpName;       
    var sex =  jsBookListModle.CurSearchStringObj._Sex;  
    var customer =  jsBookListModle.CurSearchStringObj._Customer;
    var address =jsBookListModle.CurSearchStringObj._Address;  
    var pageSize  = jsBookListModle.CurSearchStringObj._PageSize;
    var pageCount =jsBookListModle.CurSearchStringObj._PageIndex;
    var orderBy =  jsBookListModle.CurSearchStringObj._Order; 

    var strOrderBy = " T_EMPLOYEE.EMPLOYEE_ID ";
    var countTop=pageSize*(pageCount-1);

    var strSql = "SELECT EMPLOYEE_ID, NAME, MANORWOMAN,T_EMPLOYEE.COMPANY_ID, iif(IsNull( SIMPLE_NAME ), FULL_NAME, SIMPLE_NAME ) AS CmpName, ADDRESS, MOBILE, TEL1 FROM T_EMPLOYEE LEFT JOIN T_COMPANY ON T_EMPLOYEE.COMPANY_ID=T_COMPANY.COMPANY_ID ";

    var strWhere =" where 1=1 " ;
    if(type ==0)
    {
        var shortWhere = "select EMPID from T_DIARY where 1=1 " ;
        if(startTime !="" && startTime != null)
        {
              shortWhere = shortWhere + " and T_DIARY.CONTRACTTIME >= "+ startTime ;
        }
        if(endTime !="" && endTime != null)
        {
               shortWhere = shortWhere + " and T_DIARY.CONTRACTTIME <= "+ endTime ;
        }
        strWhere = strWhere + " and T_EMPLOYEE.EMPLOYEE_ID in (" + shortWhere +")";
    }
    else if(type ==1)
    {
        var shortWhere = "select EMPID from T_DIARY where 1=1 " ;
        if(startTime !="" && startTime != null)
        {
              shortWhere = shortWhere + " and CONTRACTTIME >= "+ startTime ;
        }
        if(endTime !="" && endTime != null)
        {
             shortWhere = shortWhere + " and CONTRACTTIME <= "+ endTime ;
        }
        strWhere = strWhere + " and T_EMPLOYEE.EMPLOYEE_ID not in (" + shortWhere +")";
    }
    if(empName !="" && empName != null)
    {
        strWhere = strWhere + " and T_EMPLOYEE.NAME like '%"+ empName +"%'";
    }
    if(sex !="" && sex != null)
    {
        strWhere = strWhere + " and T_EMPLOYEE.MANORWOMAN ='"+ sex+"'";
    }
    if(customer !="" && customer != null)
    {
         strWhere = strWhere + " and (T_COMPANY.FULL_NAME like '%"+ customer +"%' OR T_COMPANY.SIMPLE_NAME like '%"+ customer +"%')";
    }
    if(address !="" && address != null)
    {
         strWhere = strWhere + " and T_COMPANY.ADDRESS like '%"+ address +"%'";
    }      
    /*if( jsCommunicationQueryCondition.allTypeChk!=1)
    {*/
        if(jsCommunicationQueryCondition.curSelNode!=null)
        {
            strWhere+=" and T_COMPANY.GROUP_ID="+jsCommunicationQueryCondition.curSelNode.id;
        }
   /* }*/
    if(orderBy !="" && orderBy != null)
    {
         strOrderBy=orderBy;
    }
    strSql = strSql + strWhere+" Order By "+strOrderBy;     

    /*分页代码*/
    if(countTop >0)
    {
           strSql="select TOP "+ pageSize +" (select count(EMPLOYEE_ID) from ( " + strSql +")) as AllCount, * from (" + strSql +") where EMPLOYEE_ID NOT IN (SELECT TOP "+ countTop +" EMPLOYEE_ID from (" + strSql + "))";
    }
    else
    {
         strSql="select TOP "+ pageSize +" (select count(EMPLOYEE_ID) from ( " + strSql +")) as AllCount, * from (" + strSql +") ";
    }
    //jsBookListModle.CurSqlStr=strSql;
    //window.clipboardData.setData("text",strSql);
    var obj=DBReader.selectSqlForJson(strSql);
    
    /*清空变量释放内存*/
    type =null;  startTime=null; endTime=null;
    empName=null; sex=null;  customer=null; 
    address=null;  pageSize=null;  
    pageCount=null; orderBy=null;
    CollectGarbage();  
    return obj;
};

/*通话记录列表搜索*/
jsBookListModle.CallRecordSearchString=function(strType)
{
    try
    {
        /*获取查询区域返回的查询条件*/
        jsCommunicationQueryCondition.GetQueryPara(strType);
        if(strType=="familiar")
        {
            jsCommunicationQueryCondition.queryObj[0]._Type=0;
        }
        else if(strType=="strange")
        {
           jsCommunicationQueryCondition.queryObj[0]._Type=1;
        }
        else 
        {
           jsCommunicationQueryCondition.queryObj[0]._Type=-1;
        }
        $("#tbAdrressBook").DoSearch(jsCommunicationQueryCondition.queryObj);
    }
    catch(e)
    {
        alert("jsTelBillCenterContent.CallRecordSearchString:"+e.message);
    }
    CollectGarbage();
}

 jsBookListModle.DeleteAllSelData=function(empID,comID){
    var strSql="DELETE * FROM T_EMPLOYEE WHERE EMPLOYEE_ID ="+empID;
    var result = DBReader.execSql(strSql);
    var count=jsCommonLogic.selColumnData("count(*)","T_EMPLOYEE","WHERE COMPANY_ID="+comID);
    if(count==1)
    {
        var txtSql="DELETE * FROM T_COMPANY WHERE COMPANY_ID="+comID;
        DBReader.execSql(txtSql);
        txtSql=null;
    }

    strSql=null;
    count=null;
    CollectGarbage();
    return result;
 };