function jsSelectCompany(){

};



jsSelectCompany.SelectCompanyGrid=null;
$(function(){
     var w = $(document.documentElement).width()-1;
     var gh = $(document.documentElement).height() - $("#div-top-container").height()-80;
/*<-------------------------------------------------------------主列表---------------------------------------------------->*/
     var option = {
         height: gh,
         width: w,
         colModel:
         [
            { display: '编号', name: 'COMPANY_ID', width: (w * 0.1), sortable: false, align: 'left',hide:true},
            { display: '所属分组', name: 'GROUP_ID', width: (w * 0.1), sortable: false, align: 'left',hide:true},
            { display: '公司简称', name: 'SIMPLE_NAME', width: (w * 0.2), sortable: true, align: 'left' },
            { display: '公司全称', name: 'FULL_NAME', width: (w * 0.2), sortable: true, align: 'left' },
            { display: '地址', name: 'ADDRESS', width: (w * 0.35), sortable: true, align: 'left' }      
		 ],
         customFunction: jsSelectCompany.ShowPage,
         rowhandler:jsSelectCompany.doubleDealClick,
         rp: 10,
         rowbinddata:true,
         sortname: 'COMPANY_ID',
         sortorder: 'desc'
     };
     jsSelectCompany.SelectCompanyGrid = $("#tbCompanyList").ikegrid(option);
});

/*获取数据源*/
jsSelectCompany.ShowPage = function(pageSize, pageIndex, searchStr, extpara, order) {
    var countTop = pageSize * pageIndex;
    var strSql = "SELECT top " + countTop + " COMPANY_ID,SIMPLE_NAME,ADDRESS,FULL_NAME,GROUP_ID FROM T_COMPANY ";
    var strWhere = " WHERE 1=1";
    var sort = " desc";
    if (order != undefined && order.toUpperCase().indexOf("ASC") != -1) {
        sort = " asc";
    }
    var comName = unescape(App.query("comName"));
    if (comName != "") {
        strWhere += " and ( SIMPLE_NAME <> '" + comName + "' or FULL_NAME <> '" + comName + "')"
    }

    if (searchStr != "" && searchStr != undefined) {
        if (searchStr[0]._strComName != "" && searchStr[0]._strComName != null) {
            strWhere += " and ( SIMPLE_NAME like '%" + searchStr[0]._strComName + "%' or FULL_NAME like '%" + searchStr[0]._strComName + "%')";
        }
        if (searchStr[0]._strComAddress != "" && searchStr[0]._strComAddress != null) {
            strWhere += "and ADDRESS like '%" + searchStr[0]._strComAddress + "%'";
        }
    }

    strSql = strSql + strWhere + " Order By " + order;
    /*分页代码*/
    if (countTop > 0) {
        //           strSql="select TOP "+ pageSize +" (select count(COMPANY_ID) from ( " + strSql +")) as AllCount, * from (" + strSql +") where COMPANY_ID NOT IN (SELECT TOP "+ countTop +" COMPANY_ID from (" + strSql + "))";
        var totalCount = DBReader.selectSqlForJson("select count(*) as AllCount  from T_COMPANY " + strWhere);
        var count = null;
        if (totalCount != null) {
            count = eval("[{" + totalCount + "}]");
        }
	    var iAllCount=count[0].table[0].AllCount;
	    if(iAllCount==0)
	    {
		    iAllCount=1;
	    }
        strSql = "    select( select count(*)  from T_COMPANY " + strWhere + " ) as AllCount,* from (select top " + (countTop>iAllCount? (iAllCount-countTop+ pageSize): pageSize) + " * from ( " + strSql + "  ,COMPANY_ID asc)   order by " + order.toUpperCase().replace("DESC", "").replace("ASC", "") + (sort == " asc" ? " desc" : " asc") + "   ,COMPANY_ID asc)order by " + order.toUpperCase().replace("DESC", "").replace("ASC", "") + sort;
    }
    else {
        strSql = "select TOP " + pageSize + " (select count(COMPANY_ID) 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;
    }
    countTop = null;
    strSql = null;
    strWhere = null;

    CollectGarbage();
    return strResult == null ? null : result[0];
}
/*搜索*/
jsSelectCompany.searchString=function(strComName){
    var strWhere=[];
    if(strComName==undefined)
    {
        strComName="";
    }
    var htxt_ComName=$.trim($("#hTxtComName").val()).split('\"').join('“');htxt_ComName=htxt_ComName.split('\'').join('‘');
    var htxt_ComAddress=$.trim($("#hTxtComAddress").val()).split('\"').join('“');htxt_ComAddress=htxt_ComAddress.split('\'').join('‘');   
    strWhere.push({_strComName:strComName,_strComName:htxt_ComName,_strComAddress:htxt_ComAddress});
    $("#tbCompanyList").DoSearch(strWhere);
};

/*搜索*/
jsSelectCompany.Cancel=function(){
     $("#frSearchForm").resetForm();
     jsSelectCompany.searchString();
};

/*双击行确定选择*/
jsSelectCompany.doubleDealClick=function(target){
      $(target).dblclick(function(){
           ShowWindow.opener.execScript(App.query("formIndex"),App.query("action").replace('@ComID',$(target).attr("COMPANY_ID")).replace('@ComName',$(target).attr("SIMPLE_NAME")).replace('@GroupID',$(target).attr("GROUP_ID")));
           ShowWindow.close();
      });   
};
