function jsInportExcel(){

};
jsInportExcel.DataSourceGrid=null;

$(function(){
    if(App.query("GroupID")>0){
        $("#hhidParentTypeID").val(App.query("GroupID"));
    }
    if(App.query("GroupText")!=""){
       $("#htxtParentTypeName").val(App.query("GroupText"));   
    }
    jsInportExcel.LoadDataSourceGrid(); 
});

jsInportExcel.LoadDataSourceGrid=function(){
     try {
         var w = $(document.documentElement).width()-12;
         var gh = $(document.documentElement).height()*0.42;
    /*<-------------------------------------------------------------主列表---------------------------------------------------->*/
         var option = {
             height: gh,
             width: w,
             colModel:
             [
                { display: '字段序号', name: 'ColumnID', width: (w * 0.12), sortable: false, align: 'left',hide:true},
                { display: '字段来源', name: 'ColumnText', width: (w * 0.45), sortable: false, align: 'left'},
	            { display: '保存字段', name: 'SaveColumn', width: (w * 0.45), sortable: false, align: 'right',processType:'select',process:jsInportExcel.SaveColumnJSON}
		     ],
             customFunction: jsInportExcel.ShowPage,
             rp: 10000, 
             usepager:false,
             rowHeight:24,
             rowbinddata:true
         };
         jsInportExcel.DataSourceGrid = $("#tbDataSourceGrid").ikegrid(option);
    }
    catch (e) {
        alert("jsInportExcel.LoadDataSourceGrid:" + e.message);
    }
};

jsInportExcel.ShowPage=function(){
    var result=$.parseJSON('[{"result" :"0","returnval" :"暂时没有结果"}]');
    return result[0];
};

/*初始化保存字段*/
jsInportExcel.SaveColumnJSON=function(value,pid,rows){
    var tableNameList = [{ GroupName: "T_EMPLOYEE", GruopText: "个人资料",jsName: "jsPersonalInformation"}, { GroupName: "T_COMPANY", GruopText: "单位资料",jsName:"jsDepartmentInformation"}];
    var selectData = "<select  style='width:98%' id='selSaveColuSear_" + pid + "' >";
    selectData+="<OPTION value=''style='background-color:white;' >不导入</OPTION>"; 
    for (var j = 0; j < tableNameList.length; j++) {     
       if(App.FileIsExist(escape(App.ApplicationPath()+"Application\\TeleSecretary\\js\\"+tableNameList[j].jsName+".js"))){
            var inputJs = document.createElement("script");
            var _head = document.getElementsByTagName("head");
            inputJs.src = "../js/" + tableNameList[j].jsName + ".js";
            inputJs.type = "text/javascript";
            inputJs.id = tableNameList[j].jsName;
            _head[0].appendChild(inputJs);
            inputJs = null;
            _head = null;
            
            selectData += "<OPTGROUP LABEL='" + tableNameList[j].GruopText + "' style='background-color:#c8d9f6;'>";
            var arrayList = $.parseJSON(unescape(eval(tableNameList[j].jsName + ".dataJSON")));
            for (var i = 0; i < arrayList.length; i++) {
               for (var p = 0; p < arrayList[i].length; p++) {
                        selectData += "<OPTION value='" + tableNameList[j].GroupName + "." + arrayList[i][p].Field + "'style='background-color:white;' content='" + arrayList[i][p].content + "'>" + arrayList[i][p].title + "</OPTION>";
                }
            }     
            if(tableNameList[j].GroupName=="T_EMPLOYEE"){
                selectData += "<OPTION value='T_EMPLOYEE.COMPANY_ID' style='background-color:white;' >单位编号</OPTION>";
            }
            if(tableNameList[j].GroupName=="T_COMPANY"){
                selectData += "<OPTION value='T_COMPANY.SIMPLE_NAME' style='background-color:white;' >单位简称</OPTION>";
            }
            
        }  
        
//        var strSql="SELECT * FROM "+tableNameList[j].GroupName+" WHERE "+tableNameList[j].KeyField+"=0";
//        var strResult=DBReader.selectSqlForJson(strSql);
//        var result;
//        if (strResult != null) {
//            result = eval("[{" + strResult + "}]");
//            if(result[0].col!=undefined){
//                  for(var i=0;i<result[0].col.length;i++){
//                       selectData += "<OPTION value='" + tableNameList[j].GroupName + "." +result[0].col[i].FieldName + "'style='background-color:white;' >" + result[0].col[i].FieldName  + "</OPTION>";
//                  }
//            }
//        }    
    }
    selectData += "</select>"
    return selectData;
};

/*提交Excel文件*/
jsInportExcel.UploadExcelFile=function(){
    var filePath = App.LoadFile();
    if(filePath!="" && filePath!=undefined)
    {
        var fileExtension = (filePath.substring(filePath.lastIndexOf('.') + 1, filePath.length)).toLowerCase();
        if(fileExtension=="xls" || fileExtension=="xlsx"){
            $("#txtFilePath").val(filePath);
        }else{
            alert("请选择Excel文件！");
        }
    }
};

/*连接Excel */
jsInportExcel.ConnectionExcel=function(){   
    if($("#txtFilePath").val()!=""){ 
        $(document.body).mask("正在连接,请稍后...");
        var strPass=$.trim($("#txtPassWord").val()).split('\"').join('“').split('\'').join('‘');
        var strExcelTable = ExcelHelper.ReadSheet($("#txtFilePath").val(),strPass);
        if(strExcelTable!=""){
            var tbNameArr=strExcelTable.split("|")[0].split(",");
            var selInsertHtml="";
            for(var i=0;i<tbNameArr.length;i++){
                if(selInsertHtml==""){
                    selInsertHtml+='<select id="selDataSource" style="width:120px" onchange="jsInportExcel.BindDataSourceGrid()"><option value="'+tbNameArr[i]+'" selected="selected">'+tbNameArr[i]+'</option>';
                }else{
                    selInsertHtml+='<option value="'+tbNameArr[i]+'">'+tbNameArr[i]+'</option>';
                }       
            }
            selInsertHtml+="</select>";
            document.getElementById("tdSelDataSource").innerHTML=selInsertHtml;
             jsInportExcel.BindDataSourceGrid();  
             tbNameArr=null;
             selInsertHtml=null;         
        } 
        $(document.body).unmask();
        strPass=null;
        strExcelTable=null;
        CollectGarbage();
    }else{
        alert("请先选择要连接的文件！");
    }
};

/*根据Excel列添加Grid中的数据*/
jsInportExcel.BindDataSourceGrid=function(){
   jsInportExcel.DataSourceGrid.clearRow();
   var strPass=$.trim($("#txtPassWord").val()).split('\"').join('“').split('\'').join('‘');
   var strColumn = ExcelHelper.ReadColumn($("#txtFilePath").val(),strPass,$("#selDataSource").val(),1);
    if(strColumn!="" && $.trim(strColumn)!="|" && $.trim(strColumn)!="|1"){
        var columnText=strColumn.split("|")[0].split(",");
        var columnValue=strColumn.split("|")[1].split(",");
        for(var i=columnText.length-1;i>-1;i--){
            if(columnText[i]!=""){
              var newRowObj=jsInportExcel.DataSourceGrid.addRow(0);
              var initValue = [{ 'ColumnID': columnValue[i], 'ColumnText': columnText[i], 'SaveColumn': ''}];
              jsInportExcel.DataSourceGrid.setRowData(newRowObj,initValue);
              newRowObj=null;
              initValue=null;
           }
        }
        columnText=null;
        columnValue=null;
    }else{                 
        alert("没有可供导入的数据！");
    }
    strPass=null;
    strColumn=null;
    CollectGarbage();
};

/*导入Excel*/
jsInportExcel.InportExcelInfo=function(){
    $(document.body).mask("正在导入,请稍后...");
    var fileName=$("#txtFilePath").val();
    var strPass=$.trim($("#txtPassWord").val()).split('\"').join('“').split('\'').join('‘');
    var dataObj=new Object();
    var store=jsInportExcel.DataSourceGrid.getRowData();
    if(store.length>0){
        var strSourceIndex="";
        var strSourceColGroup="";
        var strSaveColumnGroup="";
        for(var i=0;i<store.length;i++){   
            if(unescape(store[i].SaveColumn)!=""){
                strSourceIndex+=store[i].ColumnID+",";
                strSourceColGroup+=unescape(store[i].ColumnText)+",";
                strSaveColumnGroup+=unescape(store[i].SaveColumn)+",";
            }        
        }

        dataObj.SoucreIndex=strSourceIndex.substring(0,strSourceIndex.length-1);        
        dataObj.SoucreColGroup=strSourceColGroup.substring(0,strSourceColGroup.length-1);
        dataObj.SaveColumnGroup=strSaveColumnGroup.substring(0,strSaveColumnGroup.length-1);
       
        strSourceIndex=null;
        strSourceColGroup=null;
        strSaveColumnGroup=null;
    }    
    var groupID=$("#hhidParentTypeID").val()==""?0:$("#hhidParentTypeID").val();
    ExcelHelper.ImportData(fileName,strPass,$("#selDataSource").val(),1,dataObj,groupID);
    ShowWindow.opener.execScript(App.query("FormIndex"),App.query("LoadAction"));
           
    fileName=null;
    strPass=null;
    dataObj=null;
    store=null;
    CollectGarbage();
    $(document.body).unmask();
};

/*选择分组*/
jsInportExcel.GetGroupType=function(){
     var FormIndex=ShowWindow.getFormIdx();
     ShowWindow.ShowMoledDialog("Application/TeleSecretary/frmCommunication/frmGroupTree.htm?hideID=&FormIndex="+FormIndex+"&LoadAction=jsInportExcel.SetValue(@str)","所属分组",260,500);                 
};

jsInportExcel.SetValue=function(id,text){
   if(id!="")
    {
        $("#htxtParentTypeName").val(text);
        $("#hhidParentTypeID").val(id);
    }
}

/*清空分组*/
jsInportExcel.resetDataItem=function(){
    $("#htxtParentTypeName").val("");
    $("#hhidParentTypeID").val("");
}