var weburl = 'Subscriptions.aspx';
var urldata = 'type=ajax&Action=BindSubscriptions&ran=' + Math.random();

$(document).ready(function(){
	BindSubscriptions(weburl,urldata);
});

function BindSubscriptions(weburl,urldata){
	$('#template').hide();//将第一行模板隐藏
	$.ajax({
        type:"get",
        dataType:"json",
        url:weburl+'?NoCopyRight=1',
        data:urldata,
        cache:false,
        success:function(msg)
        {
			$('tr:gt(0)','#datas').remove();
            var data = msg.Subscriptions;
			$.each(data,function(i,n)
			{
				var row = $('#template').clone();
                row.find('#SubscriptionsID').text(n.SubscriptionsID);
				row.find('#SubscriptionsName').text(n.SubscriptionsName);
				row.find('#Description').html(n.Description);
				row.find('#Add').html('<a href="Options.aspx?Action=Add&SubscriptionsID=' + n.SubscriptionsID + '">添加订阅选项</a>');
				row.find('#Modify').html('<a href="Subscriptions.aspx?Action=Modify&SubscriptionsID=' + n.SubscriptionsID + '">修改</a>');
                row.find('#Delete').html('<a href="#">删除</a>').find('a').click(function()
                   {
                        if(confirm('确定要删除订阅类型吗?删除将不可恢复'))
                        {
                            $.get('Subscriptions.aspx?NoCopyRight=1&type=ajax&Action=DeleteSubscriptions&SubscriptionsID=' + n.SubscriptionsID);
							$(this).parent().parent().remove();
                        }
                        return false;
                   });
				row.show().mouseover(function(){$(this).css("cursor","pointer");}); //将复制的行显示出来并添加样式;
				row.attr("id","subscriptionsID_" + n.SubscriptionsID);//改变绑定好数据的行的id
				row.find('#Modify').siblings(':not("#Delete"):not("#Add")').toggle(
					function(){
						if($('[@id^=option]').length > 0)
						{
							$('[@id^=option]').remove();
							$('[@id^=content]').remove();
						}
						//$('.lgq').click().removeClass('lgq');
//						$(this).addClass('lgq');
						$.ajax({
							type:"get",
							dataType:"json",
							url:weburl+'?NoCopyRight=1',
							data:'type=ajax&Action=BindSubscriptionsOptions&SubscriptionsID=' + n.SubscriptionsID,
							cache:false,
							async:false,
							success:function(msg1)
							{
								var data1 = msg1.SubscriptionsOptions;
								var option = '';
								option += '<tr id="option">';
								option += '<th class="c1 ccenter OptionsColor">ID</th>';
								option += '<th class="c2 ccenter OptionsColor">选项名称</th>';
								option += '<th class="c3 ccenter OptionsColor">类型名称</th>';
								option += '<th class="c4 ccenter OptionsColor">添加邮件</th>';
								option += '<th class="c5 ccenter OptionsColor">修改</th>';
								option += '<th class="c6 ccenter OptionsColor">删除</th>';
								option += '</tr>';
								var p = 0;
								$.each(data1,function(j,m)
								{
									option += '<tr id=option_' + m.SubscriptionsOptionsID + '>';
									option += '<td class="c1 ccenter OptionsColor">' + m.SubscriptionsOptionsID + '</td>';
									option += '<td class="c2 OptionsColor">' + m.SubscriptionsOptionsName + '</td>';
									option += '<td class="c3 OptionsColor">' + n.SubscriptionsName + '</td>';
									option += '<td id=AddOption_' + m.SubscriptionsOptionsID + ' class="c4 ccenter OptionsColor"><a href="Content.aspx?Action=Add&SubscriptionsID=' + n.SubscriptionsID +'&OptionsID=' + m.SubscriptionsOptionsID + '">添加邮件</a></td>';
									option += '<td id=ModifyOption_' + m.SubscriptionsOptionsID + ' class="c5 ccenter OptionsColor"><a href="Options.aspx?Action=Modify&OptionsID=' + m.SubscriptionsOptionsID + '">修改</a></td>';
									option += '<td id=DeleteOption_' + m.SubscriptionsOptionsID + ' class="c6 ccenter OptionsColor"><a href="#">删除</a></td>';
									option += '</tr>';
									p++;
								});
								if(p == 0)
								{
									option += '<tr id="option0"><td colspan="6" class="ccenter" style="color:red;">此订阅类型下还没有添加订阅选项</td></tr>';
								}
								$("#subscriptionsID_" + n.SubscriptionsID).after(option);
								$('[@id^=DeleteOption_]').find('a').click(function(){
									if(confirm('确定要删除订阅选项吗?删除将不可恢复'))
									{
										$.get('Subscriptions.aspx?NoCopyRight=1&type=ajax&Action=DeleteSubscriptionsOptions&OptionID=' + $(this).parent().attr('id').split('_')[1]);
										$(this).parent().parent().remove();
									}
									return false;
								});
							},
							error:function(XMLHttpRequest,textStatus){if(textStatus=='parsererror'){alert('身份已过期,请重新登陆!')}else{alert('error')};}
						});
						
						$('[@id^=option_]').each(function(){
							$(this).mouseover(function(){$(this).css("cursor","pointer");});
							var thisID = $(this).attr('id');
							$(this).find('[@id^=ModifyOption_]').siblings(':not("[@id^=DeleteOption_]"):not("[@id^=AddOption_]")').toggle(
								function(){	
									if($('[@id^=content]').length > 0)
									{
										$('[@id^=content]').remove();
									}
									$.ajax({
										type:"get",
										dataType:"json",
										url:weburl+'?NoCopyRight=1',
										data:'type=ajax&Action=BindSubscriptionsContent&OptionID=' + thisID.split('_')[1],
										cache:false,
										async:false,
										success:function(msg2)
										{
											var data2 = msg2.SubscriptionsContent;
											var content = '';
											content += '<tr id="content">';
											content += '<th class="c1 ccenter EmailsColor">ID</th>';
											content += '<th class="c2 ccenter EmailsColor">邮件名称</th>';
											content += '<th class="c3 ccenter EmailsColor">操作人（操作时间）</th>';
											content += '<th class="c4 ccenter EmailsColor">发送邮件</th>';
											content += '<th class="c5 ccenter EmailsColor">修改</th>';
											content += '<th class="c6 ccenter EmailsColor">删除</th>';
											content += '</tr>';
											var f = 0;
											$.each(data2,function(k,o)
											{
												content += '<tr id=content_' + o.SubscriptionsContentID + '>';
												content += '<td class="c1 ccenter EmailsColor">' + o.SubscriptionsContentID + '</td>';
												content += '<td class="c2 EmailsColor">' + o.SubscriptionsContentName + '</td>';
												content += '<td class="c3 ccenter EmailsColor">' + o.MemberName + '（' + o.AddTime + '）</td>';
												content += '<td id=SendEmail_' + o.SubscriptionsContentID + ' class="c4 ccenter EmailsColor"><input type="button" name="send" value="立即发送" style="height:20px;" /></td>';
												content += '<td id=ModifyEmail_' + o.SubscriptionsContentID + ' class="c5 ccenter EmailsColor"><a href="Content.aspx?Action=Modify&ContentID=' + o.SubscriptionsContentID + '">修改</a></td>';
												content += '<td id=DeleteEmail_' + o.SubscriptionsContentID + ' class="c6 ccenter EmailsColor"><a href="#">删除</a></td>';
												content += '</tr>';
												f++;
											});	
											if(f == 0)
											{
												content += '<tr id="content0"><td colspan="6" class="ccenter" style="color:red;">此订阅选项下还没有添加订阅邮件</td></tr>';
											}
											$('#option_' + thisID.split('_')[1]).after(content);
											$('[@id^=DeleteEmail_]').find('a').click(function(){
												if(confirm('确定要删除订阅邮件吗?删除将不可恢复'))
												{
													$.get('Subscriptions.aspx?NoCopyRight=1&type=ajax&Action=DeleteSubscriptionsContent&ContentID=' + $(this).parent().attr('id').split('_')[1]);
													$(this).parent().parent().remove();
												}
												return false;
											});
											$('[@id^=SendEmail_]').find('[@name=send]').click(function(){
												if(confirm('确定要发送邮件吗？'))
												{
													$.get('Subscriptions.aspx?NoCopyRight=1&type=ajax&Action=SendEmail&ContentID=' + $(this).parent().attr('id').split('_')[1]);
													alert('发送成功！');
												}
												return false;
											});
										},
										error:function(XMLHttpRequest,textStatus){if(textStatus=='parsererror'){alert('身份已过期,请重新登陆!')}else{alert('error')};}
									});
								},
								function(){
									$('[@id^=content]').remove();
								}
							);
						});
					},
					function(){
						$('[@id^=option]').remove();
						$('[@id^=content]').remove();
					});
				
				row.appendTo("#datas");
            });
		},
		error:function(XMLHttpRequest,textStatus){if(textStatus=='parsererror'){alert('身份已过期,请重新登陆!')}else{alert('error')};}
	});
}