var imagePath = "";
var divHeight = ""; // 查询面板高度
var dataGridWidth = ""; // 查询面板宽度
var dataGridHeight = "";
var bodyWidth = 0;
var bodyHeight = 0;
var editor1 = null;
var operation = "";
var serverIp = "";
var addWinWidth = 0;  //新增窗口宽度
var addWinHeight = 0;  //新增窗口高度
var languageType = "chinese";
//var status = ['报名成功','缴费成功','书籍发放','DC号','准考证打印','发票寄送','证书寄送'];
var subjectArray = [];
var statusArray = [];
var activePhase;
//var registerTypeArray = ['首次报名','补考报名','延考及恢复'];
var rows=[];

//扩展Date的format方法
Date.prototype.format = function (format) {
    var o = {
        "M+": this.getMonth() + 1,
        "d+": this.getDate(),
        "h+": this.getHours(),
        "m+": this.getMinutes(),
        "s+": this.getSeconds(),
        "q+": Math.floor((this.getMonth() + 3) / 3),
        "S": this.getMilliseconds()
    }
    if (/(y+)/.test(format)) {
        format = format.replace(RegExp.$1, (this.getFullYear() + "").substr(4 - RegExp.$1.length));
    }
    for (var k in o) {
        if (new RegExp("(" + k + ")").test(format)) {
            format = format.replace(RegExp.$1, RegExp.$1.length == 1 ? o[k] : ("00" + o[k]).substr(("" + o[k]).length));
        }
    }
    return format;
}

$(function() {
	divHeight = $("#queryDiv").height();
	dataGridWidth = $("#queryDiv").width();
	dataGridHeight = divHeight + 45;
	
	bodyWidth = document.body.clientWidth;
	bodyHeight = document.documentElement.clientHeight;
	addWinWidth = (bodyWidth/100)*80;
	addWinHeight = (addWinWidth/16)*9;
	//初始状态为关闭
	$("#addNewsWinId").window({closed:true});
	$("#exportWindow").window({closed:true});
	$("#editStatusWinId").window('close');
	$("#addEmailInfoWinId").window('close');
	$("#emailSendWinId").window('close');
	//初始状态是隐藏
	$("#newsTypeId01").hide();//无用
	$("#newsTypeId02").hide();
    //获得相应数据长度
    var length1 = 0;
    var length2 = 0;
    statusArray = new Array(length1);
    subjectArray = new Array(length2);
    
    initDataTable();
	serverIp = document.location.hostname;
	initInputWidth();

    initExportWindow();

    $.each(subjectArray,function(n,v){
        var href = "/admin/business/registerManage/exporgExcel2?phase="+activePhase+"&flagExt=0&subject2=" +(n+6)+
            "&fields=submitTime,dcNumber,title,dateOfBirth,gender_en,name_en,company_en,address_en,country,phone,email,city_en,subject2,name,address,invoiceType,invoiceTitle";
        var html="<a href='"+href+"' class='exp-btn'>"+v+"</a>";
        $(".quick-export").append(html);
    })
});
function initInputWidth(){
	$("#title00Id").attr("style", "width:" + fixShortWidth() + "px"); 
	$("#content00Id").attr("style", "width:" + fixShortWidth() + "px");
	$("#recipients00Id").attr("style", "width:" + fixShortWidth() + "px");
}
function initExportWindow(){
    $(".dropable .ele .up").on("click",function(){
        $from = $(this).parent().parent();
        $to = $from.prev(".block");
        if($to.length>0){
            $to.insertAfter($from);
        }
    });
    $(".dropable .ele .down").on("click",function(){
        $from = $(this).parent().parent();
        $to = $from.next(".block");
        if($to.length>0){
            $to.insertBefore($from);
        }
    });
}

function initDataTable(){
	var h = document.documentElement.clientHeight;
	$("#registersTable").datagrid({
		height : h - dataGridHeight,
		nowrap : true,
		striped : true,
		pagination : true,
		rownumbers : true,
		pageList : [15,20,30,40,50],
		autoRowHeight : false, 
		fitColumns : true,  
		title : "邮件列表",
        queryParams : {
            phase:activePhase
        },
		url : '/business/EmailManage/queryEmail',
		columns : [ [ {
			field : 'ck',
			title : 'id',
			width : 15, 
			checkbox : true
		},{
			field : 'title',
			title : '主题',
			align : 'left',
			width : 100
		}, {
			field : 'status',
			title : '是否成功',
			align : 'center',
			width : 50
		},  {
			field : 'recipients',
			title : '收件人',
			align : 'center',
			width : 300
		}, {
			field : 'addresser',
			title : '发件人',
			align : 'center',
			width : 100
		}, {
            field : 'content',
            title : '邮件内容',
            align : 'center',
            width : 400
        }, {
            field : 'creator',
            title : '创建人',
            align : 'center',
            width : 50
        }, {
            field : 'createTime',
            title : '创建时间',
            align : 'center',
            width : 100
        }] ],
		onDblClickRow : function(index, row) { // 双击"任一行"执行
		},
		onLoadSuccess : function(data) { // 加载成功之后执行
		}
	});
}

function view(val,row){
    return "<a href=\"javascript:viewData("+row.id+")\">"+row.name_en+"</button>";
}

//导出为excel exportExl()
function exportExl(){
    var name = $("#name00Id").val();
    var status2 = $("#registerStatus200Id").combobox('getValue');
    var phase = $("#phaseId").combobox('getValue');
    var subject2 = $("#subject200Id").combobox('getValue');
    var queryParams = null;

    var out ='';
    $(".dropable .block").each(function(){
        if ($(this).find("input").get(0).checked) {
            if (out.length>0) {
                out+=",";
            };
            out+=$(this).find(".ele-name").attr("val");
        };
    });
    name = encodeURI(encodeURI(name));
    location.href="/admin/business/registerManage/exporgExcel2?name="+name+"&subject2="+subject2+"&status2="+status2
    +"&fields="+out+"&flagExt=0&phase="+phase;
    $("#exportWindow").window('close');
}

//查询
function queryData() {
    var title = $("#title00Id").val();
    var content = $("#content00Id").val();
    var recipients = $("#recipients00Id").val();
    $("#registersTable").datagrid({
        queryParams : {
            title:title,
            content:content,
            recipients:recipients
        }
    });
}

//清空查询条件
function clearQueryForm(){
	$("#queryFormId").form('clear');
}
 
function formatDateField(selector,val){
    console.log(val);
    if(val!=''&&val!=undefined&&val!='undefined'){
        var d = new Date(parseInt(val));
        d.setTime(d.getTime() + 60*60*1000);
        $(selector).val(d.format("yyyy-MM-dd"));
        $(selector).datebox("setValue",d.format("yyyy-MM-dd"));
    }
}

/**
 * id数组转换为json字符串
 */
function arrayTojson(arr) { 
	var jsonIds = "[";
	for ( var i = 0; i < arr.length; i++) {
		if (i == arr.length - 1) {
			jsonIds += arr[i].id;
		} else {
			jsonIds += arr[i].id + ",";
		}
	}
	jsonIds += "]";

	return jsonIds; 
}

//设置 long 宽度自适应   相对窗口
function fixLongWidth(){ 
	return addWinWidth*0.66;        
} 
           
//设置 short 宽度自适应
function fixShortWidth(){    
	return addWinWidth*0.284;            
} 
  
//关闭窗口
function closeWindow(){
	$("#addNewsWinId").window('close');
}
function closeWindow2(){
	$("#editStatusWinId").window('close');
}
