function jsColSetForComModel(){

};
jsColSetForComModel.ColumnSetGrid=null;/*列表*/
jsColSetForComModel.ColType=App.query("ColType");
jsColSetForComModel.IsHaveColSetFile=null;/*是否已经存在列设置JS文件*/

$(function(){
    jsColSetForComModel.LoadJsColumnSetInfo();
    jsColSetForComModel.initColumnSetGrid();
    jsColSetForComModel.LoadPersionalInfoDom(); /*始终以模板内容为主*/
});

/*注入用户设置结果JS*/
jsColSetForComModel.LoadJsColumnSetInfo=function(){
    var fileName="";
    if(jsColSetForComModel.ColType=="com"){
        fileName="jsColSetForComFile.js";
    }
    else{
       fileName="jsColSetForPerFile.js";
    }
   if(App.FileIsExist(escape(App.ApplicationPath()+"Application\\TeleSecretary\\js\\"+fileName)))   
   {
        App.SetNullHref(0);
        var obj=document.getElementsByTagName("HEAD");
        var scriptObj=document.createElement("script");
        scriptObj.src="../js/"+fileName;
        scriptObj.id="js_ComColumnSetFile";
        obj[0].appendChild(scriptObj);
        jsColSetForComModel.IsHaveColSetFile=1;
        scriptObj=null;
        obj=null;
        App.SetNullHref(1);
   }
   fileName=null;
   CollectGarbage();
};

/*列设置列表*/
jsColSetForComModel.initColumnSetGrid=function(){
    try {
        var w = $( document.documentElement).width()-2;
         var gh = $( document.documentElement).height()-80;
         var option = {
             height: gh,
             width: w,
             colModel:[
                { display: '表名', name: 'TableName', width: (w * 0.1), sortable: true, align: 'left',hide:true},
                { display: '字段名', name: 'DataField', width: (w * 0.1), sortable: true, align: 'left',hide:true},
                { display: '标题', name: 'Title', width: (w * 0.25), sortable: true, align: 'left'},
                { display: '显示', name: 'IsDisplay', width: (w * 0.1), sortable: true, align: 'center',process:jsColSetForComModel.AllowDisplayOrNot,processType:'check'},
                { display: '排序', name: 'IsOrder', width: (w * 0.1), sortable: true, align: 'center',process:jsColSetForComModel.AllowOrderOrNot,processType:'check'},
                { display: '导出', name: 'IsExport', width: (w * 0.1), sortable: true, align: 'center',process:jsColSetForComModel.AllowExportOrNOt,processType:'check',hide:true},
	            { display: '操作', name: 'ID', width: (w * 0.3), sortable: true,align:'center',process: jsColSetForComModel.FormatGridOperate}
		     ],
             customFunction: jsColSetForComModel.ShowColumnSet,
             rp: 10000,
             usepager:false,
             rowHeight:24,
             rowbinddata:true
         };
        jsColSetForComModel.ColumnSetGrid = $("#tbComColSetGrid").ikegrid(option);
    }
    catch (e) {
        alert("jsColSetForComModel.initColumnSetGrid:" + e.message);
    }
};

/*列表获得数据源*/
jsColSetForComModel.ShowColumnSet=function(){
   var result=$.parseJSON('[{"result" :"0","returnval" :"暂时没有结果"}]');
   return result[0];
};

/*是否显示*/
jsColSetForComModel.AllowDisplayOrNot=function(value,pid,rows){
    return ' <input id="chkAllowDisplay_'+pid+'" type="checkbox" />';
}
/*是否排序*/
jsColSetForComModel.AllowOrderOrNot=function(value,pid,rows){
    return ' <input id="chkAllowOrder_'+pid+'" type="checkbox" />';
}
/*是否导出*/
jsColSetForComModel.AllowExportOrNOt=function(value,pid,rows){
    return ' <input id="chkAllowExport_'+pid+'" type="checkbox" />';
}
/*列表操作：上移|下移*/
jsColSetForComModel.FormatGridOperate=function(value,pid,rows,rowIndex){
      return '<input id="btnUp_'+pid+'" type="button" value="上移" class="buttonList" onclick="jsColSetForComModel.UpCurrRow(\''+pid+'\')"/>&nbsp;<input id="btnNext_'+pid+'" type="button" value="下移" class="buttonList" onclick="jsColSetForComModel.DownCurrRow(\''+pid+'\')"/>';
};

/*上移*/
jsColSetForComModel.UpCurrRow=function(rID){
    try {
        var upRow = jsColSetForComModel.ColumnSetGrid.getRowByID(rID);
        var index = upRow.rowIndex;
        if (index > 0) {
            var RowObj =  jsColSetForComModel.ColumnSetGrid.addRow(index - 1);
            RowObj.id = rID;
            for (var i = 0; i < upRow.cells.length; i++) {
                RowObj.cells(i).innerHTML = upRow.cells(i).innerHTML
            }
             jsColSetForComModel.ColumnSetGrid.deleteRow(index + 1);
            RowObj = null;
        }
    }
    catch (e) {
        alert("Error for UpCurrRow:" + e.message);
    }
    rID = null;
    index = null;
    upRow = null;
    CollectGarbage();
};

/*下移*/
jsColSetForComModel.DownCurrRow=function(rID){
     try {
        var DownRow =jsColSetForComModel.ColumnSetGrid.getRowByID(rID);
        var index = DownRow.rowIndex;
        var upRow = jsColSetForComModel.ColumnSetGrid.getRowByIndex(index + 1);
        if (upRow != undefined) {
            var RowObj = jsColSetForComModel.ColumnSetGrid.addRow(index + 2);
            RowObj.id = rID;
            for (var i = 0; i < DownRow.cells.length; i++) {
                RowObj.cells(i).innerHTML = DownRow.cells(i).innerHTML
            }
            jsColSetForComModel.ColumnSetGrid.deleteRow(index);
            RowObj = null;
        }
    }
    catch (e) {
        alert("Error for DownCurrRow:" + e.message);
    }
    rID = null;
    DownRow = null;
    index = null;
    upRow = null;
    CollectGarbage();
};

/*注入系统设置JS结果*/
jsColSetForComModel.LoadPersionalInfoDom=function(){
    if(jsColSetForComModel.ColType=="com"){
           jsColSetForComModel.IsHaveComInfoSetJs=null;
           if(App.FileIsExist(escape(App.ApplicationPath()+"Application\\TeleSecretary\\js\\jsDepartmentInformation.js")))   
           {
                App.SetNullHref(0);
                var comObj=document.getElementsByTagName("HEAD");
                var comScriptObj=document.createElement("script");
                comScriptObj.src="../js/jsDepartmentInformation.js";
                comScriptObj.id="js_DepartmentInformation";
                comObj[0].appendChild(comScriptObj);
               /*加载设置内容*/
                setTimeout("jsColSetForComModel.AddPersionalOrCompanyInfo()",100);

                comScriptObj=null;
                comObj=null;
                App.SetNullHref(1);
           }  
    }
    else if(jsColSetForComModel.ColType=="per"){
         jsColSetForComModel.IsHavePerInfoSetJs=null;
         if(App.FileIsExist(escape(App.ApplicationPath()+"Application\\TeleSecretary\\js\\jsPersonalInformation.js")))
           {
                App.SetNullHref(0);
                var divObj=document.getElementsByTagName("HEAD");
                var scriptObj=document.createElement("script");
                scriptObj.src="../js/jsPersonalInformation.js";
                scriptObj.id="js_PersonalInformation";
                divObj[0].appendChild(scriptObj);
                /*加载设置内容*/
                setTimeout("jsColSetForComModel.AddPersionalOrCompanyInfo()",100);
                   
                scriptObj=null;
                divObj=null;
                App.SetNullHref(1);
           }
    }

   CollectGarbage();
};

/*加载列表数据*/
jsColSetForComModel.AddPersionalOrCompanyInfo=function(){
    var obj;
    var strTableName="";
    if(jsColSetForComModel.ColType=="per")
    {
        obj=eval(unescape(jsPersonalInformation.dataJSON));
        strTableName="T_EMPLOYEE";
    }
    else if(jsColSetForComModel.ColType=="com")
    {
        obj=eval(unescape(jsDepartmentInformation.dataJSON));
        strTableName="T_COMPANY";
    }       
    var strTbArr="";
    if(obj!=null)
    {
       // var i=obj.length-1;  
        for(var i=0;i<obj.length;i++)
        {  
            //var j=obj[i].length-1;
            for(var j=0 ; j<obj[i].length ;j++)
            {
                var strTitle = unescape(obj[i][j].title); //alert(unescape(obj[i][j].content));
                var ItemObj=document.createElement(unescape(obj[i][j].content));//alert(ItemObj.outerHTML);
//                if(ItemObj.contorlType!=undefined && ItemObj.contorlType=="phone"){ alert(ItemObj.imgType); alert(ItemObj.Field);};
                var strField=ItemObj.Field; 
                if(strTitle!="" && strField!=undefined && strField!="")
                 {                
                    strTbArr+=',{"TbName":"'+strTableName+'",';
                    strTbArr+='"DataField":"'+strField+'",';
                    strTbArr+='"Title":"'+strTitle+'",';
                    strTbArr+='"IsDisplay":"0",';
                    strTbArr+='"IsOrder":"1",';
                    strTbArr+='"IsExport":"1"}';      
                 }
                 strTitle=null;
                 strField=null;
                 ItemObj=null;           
            }      
        }
//    alert(strTbArr);  
        obj.length=0;
        obj=null;
    } 
        
    jsColSetForComModel.BindDataInfoBySetInfo(strTbArr.substring(1));  
    strTbArr="";
    CollectGarbage();
};

/*增加行到列表*/
jsColSetForComModel.BindDataInfoBySetInfo=function(strTableArr){
if(strTableArr!="")
{
    /*模板*/
    var obj=eval("["+ strTableArr +"]");
    if( jsColSetForComModel.IsHaveColSetFile==1)
    {         
        /*用户设置结果*/ 
       var fileSetString="";
        if(jsColSetForComModel.ColType=="com"){
           fileSetString=jsColSetForComFile.ColSetString;
        }
        else{
           fileSetString=jsColSetForPerFile.ColSetString;
        }
        if(fileSetString!="")
        {
            var colSetObj=eval("["+fileSetString+"]"); 
            var k=colSetObj.length-1;
            var isAddColumn="";
            if(fileSetString.indexOf("单位名称")==-1 && fileSetString.indexOf("SIMPLE_NAME")==-1){
                var defRowObj=jsColSetForComModel.ColumnSetGrid.addRow(0);
                var defValue=[{'TableName':'T_COMPANY','DataField': 'SIMPLE_NAME','Title': '单位名称','IsDisplay':0,'IsOrder':1,'IsExport':1}];
                jsColSetForComModel.ColumnSetGrid.setRowData(defRowObj,defValue);
                defRowObj=null;
                defValue=null;
           }
            for(;k>-1;k--)
            {
                /*根据用户设置的结果集顺序判断当前字段仍在模板中则直接加载用户设置*/
                if(strTableArr.indexOf( unescape(colSetObj[k].Title))!=-1 && strTableArr.indexOf(colSetObj[k].DataField)!=-1)
                {
                    var newRowObj=  jsColSetForComModel.ColumnSetGrid.addRow(0);
                    var initValue = [{'TableName':colSetObj[k].TbName,'DataField': colSetObj[k].DataField , 'Title': unescape(colSetObj[k].Title),'IsDisplay':colSetObj[k].IsDisplay,'IsOrder':colSetObj[k].IsOrder,'IsExport':colSetObj[k].IsExport}];
                    jsColSetForComModel.ColumnSetGrid.setRowData(newRowObj,initValue);
                    newRowObj=null;
                    initValue=null;
                    if(isAddColumn!="")
                    {
                        isAddColumn+=",";
                    }
                    isAddColumn+=colSetObj[k].DataField+":"+unescape(colSetObj[k].Title);
                } 
                if(unescape(colSetObj[k].Title)=="单位名称" && colSetObj[k].DataField=="SIMPLE_NAME"){
                    var defRowObj=jsColSetForComModel.ColumnSetGrid.addRow(0);
                    var defValue=[{'TableName':colSetObj[k].TbName,'DataField': colSetObj[k].DataField , 'Title': unescape(colSetObj[k].Title),'IsDisplay':colSetObj[k].IsDisplay,'IsOrder':colSetObj[k].IsOrder,'IsExport':colSetObj[k].IsExport}];
                    jsColSetForComModel.ColumnSetGrid.setRowData(defRowObj,defValue);
                    defRowObj=null;
                    defValue=null;
                }
            }

            for(var j=0;j<obj.length;j++)
            {   
                /*根据模板顺序判断模板中当前字段在用户设置集中没有则添加到第一行*/                      
                if(isAddColumn.indexOf(obj[j].Title)==-1 && isAddColumn.indexOf(obj[j].DataField)==-1)
                {
                    var newRowObj=  jsColSetForComModel.ColumnSetGrid.addRow(0);
                        var initValue = [{'TableName':obj[j].TbName,'DataField': obj[j].DataField,'Title': obj[j].Title,'IsDisplay':0,'IsOrder':1,'IsExport':1}];
                        jsColSetForComModel.ColumnSetGrid.setRowData(newRowObj,initValue);
                        newRowObj=null;
                        initValue=null;
                }
            }    
            colSetObj=null;
            k=null;
            isAddColumn=null;    
        }
        fileSetString=null;
    }
    else
    {    
        /*用户还没有进行列设置，直接加载模板数据*/
        var i=obj.length-1;
        for(;i>-1;i--)
        {
            var newRowObj=  jsColSetForComModel.ColumnSetGrid.addRow(0);
            var initValue = [{'TableName':obj[i].TbName,'DataField': obj[i].DataField , 'Title': obj[i].Title,'IsDisplay':0,'IsOrder':1,'IsExport':1}];
            jsColSetForComModel.ColumnSetGrid.setRowData(newRowObj,initValue);
            newRowObj=null;
            initValue=null;
        }
        if(jsColSetForComModel.ColType=="com"){
            var defRowObj=jsColSetForComModel.ColumnSetGrid.addRow(0);
            var defValue=[{'TableName':'T_COMPANY','DataField': 'SIMPLE_NAME', 'Title': '单位名称','IsDisplay':0,'IsOrder':1,'IsExport':1}];
            jsColSetForComModel.ColumnSetGrid.setRowData(defRowObj,defValue);
            defRowObj=null;
            defValue=null;
        }
    }
}
obj=null;
strTableArr="";
CollectGarbage();  
};

/*保存*/
jsColSetForComModel.SaveAddressBookColSet=function(){
    var store= jsColSetForComModel.ColumnSetGrid.getRowData();
    var strArr="";
    var strShowCol="";
    var strHideCol="";
    for(var i=0;i<store.length;i++)
    {
        strArr+=',{"TbName":"'+store[i].TableName+'",';
        strArr+='"DataField":"'+store[i].DataField+'",';
        strArr+='"Title":"'+store[i].Title+'",';
        strArr+='"IsDisplay":"'+store[i].IsDisplay+'",';
        strArr+='"IsOrder":"'+store[i].IsOrder+'",';
        strArr+='"IsExport":"'+store[i].IsExport+'",'
        strArr+='"RowIndex":"'+i+'"}';      
        if(store[i].IsDisplay==0){
           strHideCol+=","+store[i].DataField;
        }
        else if(store[i].IsDisplay==1){
            strShowCol+=","+store[i].DataField;
        }
    }
    strArr=strArr.substring(1);
    
    var jsFile=[];
    var fileName="";
    if(jsColSetForComModel.ColType=="com"){
        fileName="jsColSetForComFile";
    }
    else{
       fileName="jsColSetForPerFile";
    }
    jsFile.push("function "+fileName+"()");
    jsFile.push("{};");
    /*设置的结果字符串*/
    jsFile.push(fileName+".ColSetString='"+strArr+"';");
    
    var jsSource=jsFile.join("");
    if(App.FileIsExist(escape(App.ApplicationPath()+"Application\\TeleSecretary\\js\\"+fileName+".js")))
    { 
         /*删除原有的本地文件*/
         App.FileDelete(App.ApplicationPath() + "Application\\TeleSecretary\\js\\" + fileName+".js");  
    }
    /*创建JS文件*/
    var result = App.CreateFile(App.ApplicationPath() + "Application\\TeleSecretary\\js\\", fileName+".js", jsSource); 
    
    if(result)
    {      
        var formIndex=App.query("FormIndex");
        var loadAction=App.query("LoadAction");   
        ShowWindow.opener.execScript(formIndex,loadAction.replace("@hideStr",strHideCol.substring(1)).replace("@showStr",strShowCol.substring(1)).replace("@colType",jsColSetForComModel.ColType));      
         alert("保存成功！");     
        ShowWindow.close();        
        formIndex=null;
        loadAction=null;
    }
    else
    {
        alert("保存没有成功！");
    }
    store.length=0;
    store=null;
    strArr="";
    jsFile.length=0;
    jsFile=null;
    jsSource.length=0;
    jsSource=null;
    result=null;
    fileName=null;
    CollectGarbage();
};