//异步提交
function ajaxSubmit($formID) 
{
	var $form = $($formID);
	var data = $form.serialize();
	var url = $form.attr('action');
	var flag=false;
	$.ajax({
		type: "post",
		url: url,
		async: false,
		dataType: "json",
		data: data,
		success: function(json_data) {
			if (json_data.status == 'ok') 
			{
				$('.modal').modal('hide');
				show_tips('ok', json_data.msg);
				if (json_data.action=='add_card' || json_data.action=='add_nav' || json_data.action=='edit') {
					setTimeout(function(){
						location.href=location.href;
					},1500);
				};
				flag=true;
			}
			else if (json_data.status == 'ok_go') 
			{
				show_tips('ok', json_data.msg);
				setTimeout(function(){
						location.href=json_data.url;
				},1200);
			}
			else if (json_data.status == 'no') 
			{
				show_tips('no', json_data.msg);
			}
			else if (json_data.status == 'ok_notips') {
				flag=true;
			}
		},
		error: function(XMLHttpRequest, textStatus, errorThrown) {
			show_tips('no', errorThrown);
		}
	});
	return flag;
}

//添加链接初始化
function init_add_nav(){
	$("#n_name").val('');
    $("#n_link").val('');
    $("#n_sn").val('');
    $("#n_description").val('');
}

//编辑链接
function edit_nav(n_name,n_link,n_sn,n_description) {
    $("#n_name").val(n_name);
    $("#n_link").val(n_link);
    $("#n_sn").val(n_sn);
    $("#n_description").val(n_description);
    $('.form-add-nav').attr('action','/webht.php/index/edit');
    $(".bs-nav-modal-sm").modal('show');
}

//删除链接
function delete_nav(n_sn) {
	if (confirm("确定要删除该链接？")) {
        $("#n_sn").val(n_sn);
        $('.form-add-nav').attr('action','/webht.php/index/delete');
        if(ajaxSubmit('.form-add-nav')){
        	$('.nav-item-'+n_sn).remove();
        }
	}
}

//提示模态框
function show_tips(type, text) 
{
	var Timeout = '1200';
	var m_type = 'sm';
	if (type == 'ok') {
		var style = 'glyphicon-ok text-success';
	} else if(type == 'no2'){
		m_type='lg';
		var style = 'glyphicon-remove text-danger';
	} else if(type == 'loading'){
		var style = '';
		text='<img src="/css/images/loading.gif">';
		Timeout = '30000';
	} else {
		var style = 'glyphicon-remove text-danger';
		Timeout = '3000';
	}
	var html = '<div class="modal" id="tips-modal" tabindex="-1" role="dialog" aria-hidden="true"><div class="modal-dialog modal-'+m_type+'" style="margin-top:100px;"><div class="modal-content col-xs-24" style="padding:15px 15px;border-radius: 0px;"><div class="media"><div class="media-left"><span id="tips-modal-icon" class="media-object glyphicon ' + style + '" style="font-size:56px;"></span></div><div class="media-body"><span id="tips-modal-text" style="font-size:20px;padding-left:15px;line-height:55px;">' + text + '</span></div></div></div></div></div>';
	$("body").append(html);

	$('#tips-modal').modal('show');
	if (m_type!='lg') {
		setTimeout(function() {
			$('#tips-modal').fadeIn('2000').modal('hide');
		}, Timeout);
	};
}


$(document).ready(function() 
{ 
	$('.delete_type_link').mouseover(function(){
　　　　$(this).html('删除').css({'background':'#f2dede'});
　　}).mouseout(function(){
　　　　$(this).html($(this).attr('data-title')).css({'background':'#afd9ee'});
　　}).click(function(){
		if (confirm("确定要删除该卡片？")) {
			var data = '';
			var url = $(this).attr('data-href');
			var that=$(this);
			$.ajax({
				type: "get",
				url: url,
				dataType: "json",
				data: data,
				success: function(json_data) {
					if (json_data.status == 'ok') {
						show_tips('ok', json_data.msg);
						that.parent('div').remove();
						setTimeout(function(){
							location.href=location.href;
						},2000);
					} else if (json_data.status == 'no') {
						show_tips('no', json_data.msg);
					} 
				},
				error: function(XMLHttpRequest, textStatus, errorThrown) {
					show_tips('no', errorThrown);
				}
			});
		}
	});

	$("body").on('click',".load-modal",function(){
		var url=$(this).attr('data-href');
		var modal=$(this).attr('data-modal');
		if ($('.'+modal).length>0) {
			$('.'+modal).remove();
		}
		$.post(url,'',function(html){
			$(html).appendTo('body');
			$('.'+modal).modal('show');
		});
		$('.'+modal).modal('show');
	});

	$(".link_unverify").click(function() {
		var url=$(this).attr('data-href');
		$.post(url,'',function(html){
			$(".left-col").html(html);
		});
		$(".app_nav>ul>li>a").removeClass('active');
		$(this).addClass('active');
		$(".bs-verify-modal-lg").modal('hide');
	});

	$("body").on('click',".ajax-link",function(){
		var title=$(this).attr('data-title');
		if (confirm("确定要"+title+"该类型？")==false) {
			return false;
		}
		var url=$(this).attr('data-href');
		var that=$(this);
		$.ajax({
			type: "get",
			url: url,
			dataType: "json",
			data: '',
			success: function(json_data) {
				if (json_data.status == 'ok') {
					show_tips('ok', json_data.msg);
					that.parent('td').parent('tr').remove();
				} else if (json_data.status == 'no') {
					show_tips('no', json_data.msg);
				} 
			},
			error: function(XMLHttpRequest, textStatus, errorThrown) {
				show_tips('no', errorThrown);
			}
		});
	});

	//表单填写初始化
	$('body').on('change',".td-select",function(){
		if($(this).children('option:selected').val()=='不正常'){
			$(this).parent('.media-left').next('.media-body').children('textarea').removeClass('hidden');
		}else{
			$(this).parent('.media-left').next('.media-body').children('textarea').addClass('hidden').val('');
		}
	}).on('change',".datepicker",function(){
		$(".wf_time").val($(this).val());
	});

}); 