<%@ Control Language="C#" %>
<%@ Import Namespace="Kesion.Publics" %>
<%@ Import Namespace="Kesion.BLL" %>
<%@ Import Namespace="Kesion.Cache" %>
<%@ Import Namespace="Kesion.HtmlTags" %>
<%@ Import Namespace="Kesion.APPCode" %>
<%@ Import Namespace="Kesion.Weixin" %>
<%@ Import Namespace="System.Data" %>
<%@ Import Namespace="System.Xml" %>
<script runat="server" type="text/C#">
	dynamic __user = HttpContext.Current.Items["user"];                //初始化用户信息，
	private static dynamic __info = new System.Dynamic.ExpandoObject();   //初始化当前页信息
	bool isLoadPage = true;  //定义页面是否加载
	DataRow dr2 = null;

	public int ParentID = 0;
	public int MenuID = 0;
	private string TypeName = string.Empty;
	public string title = string.Empty;
	public DateTime addDate = DateTime.Now;
	public string DefaultPic = string.Empty;
	public string Intro = string.Empty;
	public string action = string.Empty;
	public int KeyID = 0;
	private string tuwenid = string.Empty;
	private string attention = string.Empty;
	public int ChannelID = 0;
	public int InfoID = 0;
	public int sendId = 0;
	public int SuCaiId = 0;
	private string sutuwenid = string.Empty;
	private string automaticreply = string.Empty;
	bool PannelShow = true;
	string ShowBtn = "";
	protected void Page_Load(object sender, EventArgs e)
	{
		
		action = KSCMS.S("action");
		
		ParentID = Utils.StrToInt(KSCMS.S("parentID"));
		MenuID = Utils.StrToInt(KSCMS.S("menuID"));
		KeyID = Utils.StrToInt(KSCMS.S("keyId"));
		sendId = Utils.StrToInt(KSCMS.S("sendid"));
		SuCaiId = Utils.StrToInt(KSCMS.S("sucaiId"));
		if (action == "attention")
		{
			attention = DataFactory.GetOnlyValue("select tuwenid from KS_WeixinKeyWord where IsKeyWord=1 and id=" + KeyID + " and TypeFlag=" + new TemporaryVar().GroupID + " and userid=" + new TemporaryVar().UserID + "");
		}
		else if (action == "automaticreply")
		{
			automaticreply = DataFactory.GetOnlyValue("select tuwenid from KS_WeixinKeyWord where IsKeyWord=2 and id=" + KeyID + " and TypeFlag=" + new TemporaryVar().GroupID + " and userid=" + new TemporaryVar().UserID + "");
		}
		else if (action == "keyword")
		{
			tuwenid = DataFactory.GetOnlyValue("select tuwenid from KS_WeixinKeyWord where IsKeyWord=0 and id=" + KeyID + " and TypeFlag=" + new TemporaryVar().GroupID + " and userid=" + new TemporaryVar().UserID + "");

		}
		else if (action == "menu")
		{
			TypeName = DataFactory.GetOnlyValue("select TypeName from KS_WeixinMenu where id=" + MenuID + " and TypeFlag=" + new TemporaryVar().GroupID + " and userid=" + new TemporaryVar().UserID + "");

		}
		else if (action == "sucai")
		{
			sutuwenid = DataFactory.GetOnlyValue("select tuwenid from KS_WeixinPigmsg where TypeFlag=" + new TemporaryVar().GroupID + " and userid=" + new TemporaryVar().UserID + " and id=" + SuCaiId);
		}
		if (!IsPostBack)
		{
			if (action == "attention")
			{
				#region 关注回复
				ShowBtn = "none";
				if (KeyID == 0)
				{
					PannelShow = true;
				}
				else
				{
					if (!string.IsNullOrEmpty(attention) && attention != "" && attention != "0")
					{
						modify("attention");
						PannelShow = false;
					}
					else
					{
						PannelShow = true;
					}
					if (KSCMS.S("caozuo") == "delete")
					{
						delete(MenuID, "attention");
						PannelShow = true;
					}

				}
				#endregion
			}
			else if (KSCMS.S("do") == "save")
			{
				Button3_Click();
			}
			else if (action == "menu")
			{
				#region 菜单管理

				PannelShow = true;
				if (TypeName == "TuWen")
				{
					PannelShow = false;
					modify("menu");
					if (KSCMS.S("caozuo") == "delete")
					{
						delete(MenuID, "menu");
						PannelShow = true;
					}
				}
				#endregion
			}
			else if (action == "keyword")
			{
				#region 关键字
				ShowBtn = "none";

				if (KeyID == 0)
				{
					PannelShow = true;
				}
				else
				{
					if (!string.IsNullOrEmpty(tuwenid) && tuwenid != "" && tuwenid != "0")
					{
						modify("keyword");
						PannelShow = false;
					}
					else
					{
						PannelShow = true;
					}
					if (KSCMS.S("caozuo") == "delete")
					{
						delete(MenuID, "keyword");
						PannelShow = true;
					}
				}
				#endregion
			}
			else if (action == "sendmsg")
			{
				#region 人工客服
				PannelShow = true;
				ShowBtn = "none";
				#endregion

			}
			else if (action == "sucai")
			{
				#region 素材管理
				ShowBtn = "none";

				if (SuCaiId == 0)
				{
					PannelShow = true;
				}
				else
				{
					if (!string.IsNullOrEmpty(sutuwenid) && sutuwenid != "0")
					{
						modify("sucai");
						PannelShow = false;
					}
					else
					{
						PannelShow = true;
					}
					if (KSCMS.S("caozuo") == "delete")
					{
						delete(MenuID, "sucai");
						PannelShow = true;
					}

				}
				#endregion
			}

			else if (action == "qunfa")
			{
				#region 群发
				PannelShow = true;
				ShowBtn = "none";
				#endregion
			}
			else if (action == "automaticreply")
			{
				#region 消息自动回复
				ShowBtn = "none";
				if (KeyID == 0)
				{
					PannelShow = true;
				}
				else
				{
					if (!string.IsNullOrEmpty(automaticreply) && automaticreply != "" && automaticreply != "0")
					{
						modify("automaticreply");
						PannelShow = false;
					}
					else
					{
						PannelShow = true;
					}
					if (KSCMS.S("caozuo") == "delete")
					{
						delete(MenuID, "automaticreply");
						PannelShow = true;
					}
				}
				#endregion
			}
		}
	}
	public string GetDeteleButton()
	{
		#region 删除按钮
		string str = string.Empty;
		if (action == "attention")
		{
			str = "<a href=\"" + BasicField.GetUserUrl("wx/wx_TuWenList", "frame-no,action-attention,keyId-" + KeyID + ",caozuo-delete") + "\" class=\"huifu_deletebutton\"  ><img src=\"/admin/weixin/images/delete.png\"></a>";
		}
		else if (action == "menu")
		{
			str = "<a href=\"" + BasicField.GetUserUrl("wx/wx_TuWenList", "frame-no,action-menu,menuID-" + MenuID + ",caozuo-delete,parentID-" + ParentID + "") + "\" class=\"huifu_deletebutton\"  ><img src=\"/admin/weixin/images/delete.png\"></a>";
		}
		else if (action == "keyword")
		{
			str = "<a href=\"" + BasicField.GetUserUrl("wx/wx_TuWenList", "frame-no,action-keyword,keyId-" + KeyID + ",caozuo-delete") + "\" class=\"huifu_deletebutton\"  ><img src=\"/admin/weixin/images/delete.png\"></a>";
		}
		else if (action == "sucai")
		{
			str = "<a href=\"" + BasicField.GetUserUrl("wx/wx_TuWenList", "frame-no,action-sucai,sendid-" + Utils.StrToInt(KSCMS.S("Sendid")) + ",caozuo-delete,sucaiId-" + SuCaiId + "") + " \" class=\"huifu_deletebutton\"  ><img src=\"/admin/weixin/images/delete.png\"></a>";
		}
		else if (action == "automaticreply")
		{
			str = "<a href=\"" + BasicField.GetUserUrl("wx/wx_TuWenList", "frame-no,action-automaticreply,keyId-" + KeyID + ",caozuo-delete") + "\" class=\"huifu_deletebutton\"  ><img src=\"/admin/weixin/images/delete.png\"></a>";
		}
		return str;
		#endregion
	}
	private void modify(string action)
	{
		if (action == "attention")
		{
			#region 关注回复
			using (DataTable dt = DataFactory.GetDataTable("select Top 1 * from KS_WeixinKeyWord where id=" + KeyID + " and TypeFlag=" + new TemporaryVar().GroupID + " and IsKeyWord=1 and userid=" + new TemporaryVar().UserID + ""))
			{
				if (dt.Rows.Count > 0)
				{
					DataRow dr = dt.Rows[0];
					if (!string.IsNullOrEmpty(dr["TuWenID"].ToString()))
					{
						string[] tuwenid = string.Concat(dr["TuWenID"].ToString(), "§").Split('§');

						using (DataTable dt2 = DataFactory.GetDataTable("select Top 1 * from KS_Enterprisenews where id=" + tuwenid[2] + " "))
						{
							if (dt2.Rows.Count > 0)
							{
								 dr2 = dt2.Rows[0];
								title = dr2["title"].ToString();
								addDate = Utils.StrToDateTime(dr2["adddate"].ToString());
								Intro = Utils.RemoveHtml(dr2["intro"].ToString());
								DefaultPic = dr2["photo"].ToString() == "" ? "/SysImg/nopic.gif" : dr2["photo"].ToString();
								ChannelID =0;
								InfoID = Utils.StrToInt(dr2["id"].ToString());
							}
						}
					}
				}
			}
			#endregion
		}
		else if (action == "keyword")
		{
			#region 关键字
			using (DataTable dt = DataFactory.GetDataTable("select Top 1 * from KS_WeixinKeyWord where id=" + KeyID + " and TypeFlag=" + new TemporaryVar().GroupID + " and userid=" + new TemporaryVar().UserID + ""))
			{
				if (dt.Rows.Count > 0)
				{
					DataRow dr = dt.Rows[0];
					if (!string.IsNullOrEmpty(dr["TuWenID"].ToString()))
					{
						string[] tuwenid = string.Concat(dr["TuWenID"].ToString(), "§").Split('§');

						using (DataTable dt2 = DataFactory.GetDataTable("select Top 1 * from KS_Enterprisenews where id=" + tuwenid[2] + " and userid=" + new TemporaryVar().UserID + ""))
						{
							if (dt2.Rows.Count > 0)
							{
								 dr2 = dt2.Rows[0];
								title = dr2["title"].ToString();
								addDate = Utils.StrToDateTime(dr2["adddate"].ToString());
								Intro = Utils.RemoveHtml(dr2["intro"].ToString());
								DefaultPic = dr2["photo"].ToString() == "" ? "/SysImg/nopic.gif" : dr2["photo"].ToString();
								ChannelID = 0;
								InfoID = Utils.StrToInt(dr2["id"].ToString());
							}
						}
					}
				}
			}
			#endregion

		}
		else if (action == "menu")
		{
			#region 菜单
			using (DataTable dt = DataFactory.GetDataTable("select Top 1 * from KS_WeixinMenu where id=" + MenuID + " and TypeFlag=" + new TemporaryVar().GroupID + " and userid=" + new TemporaryVar().UserID + ""))
			{
				if (dt.Rows.Count > 0)
				{
					DataRow dr = dt.Rows[0];
					int infoid = Utils.StrToInt(dr["infoid"].ToString());
					int m_id = Utils.StrToInt(dr["ChannelID"].ToString());
					using (DataTable dt2 = DataFactory.GetDataTable("select Top 1 * from KS_Enterprisenews where id=" + infoid + " "))
					{
						if (dt2.Rows.Count > 0)
						{
							 dr2 = dt2.Rows[0];
							title = dr2["title"].ToString();
							addDate = Utils.StrToDateTime(dr2["adddate"].ToString());
							Intro = Utils.RemoveHtml(dr2["intro"].ToString());
							DefaultPic = dr2["photo"].ToString() == "" ? "/SysImg/nopic.gif" : dr2["photo"].ToString();
							ChannelID = 0;
							InfoID = Utils.StrToInt(dr2["id"].ToString());
						}
					}
				}
			}
			#endregion
		}
		else if (action == "sucai")
		{
			#region 素材管理
			using (DataTable dt = DataFactory.GetDataTable("select Top 1 * from KS_WeixinPigmsg where id=" + SuCaiId + " and TypeFlag=" + new TemporaryVar().GroupID + " and userid=" + new TemporaryVar().UserID + ""))
			{
				if (dt.Rows.Count > 0)
				{
					DataRow dr = dt.Rows[0];
					if (!string.IsNullOrEmpty(dr["TuWenID"].ToString()))
					{
						string[] tuwenid = string.Concat(dr["TuWenID"].ToString(), "§").Split('§');

						using (DataTable dt2 = DataFactory.GetDataTable("select Top 1 * from KS_Enterprisenews where id=" + tuwenid[2] + " "))
						{
							if (dt2.Rows.Count > 0)
							{
								 dr2 = dt2.Rows[0];
								title = dr2["title"].ToString();
								addDate = Utils.StrToDateTime(dr2["adddate"].ToString());
								Intro = Utils.RemoveHtml(dr2["intro"].ToString());
								DefaultPic = dr2["photo"].ToString() == "" ? "/SysImg/nopic.gif" : dr2["photo"].ToString();
								ChannelID = 0;
								InfoID = Utils.StrToInt(dr2["id"].ToString());
							}
						}
					}
				}
			}
			#endregion
		}
		else if (action == "automaticreply")
		{
			#region 消息回复
			using (DataTable dt = DataFactory.GetDataTable("select Top 1 * from KS_WeixinKeyWord where id=" + KeyID + " and TypeFlag=" + new TemporaryVar().GroupID + " and IsKeyWord=2 and userid=" + new TemporaryVar().UserID + ""))
			{
				if (dt.Rows.Count > 0)
				{
					DataRow dr = dt.Rows[0];
					if (!string.IsNullOrEmpty(dr["TuWenID"].ToString()))
					{
						string[] tuwenid = string.Concat(dr["TuWenID"].ToString(), "§").Split('§');
						using (DataTable dt2 = DataFactory.GetDataTable("select Top 1 * from KS_Enterprisenews where id=" + tuwenid[2] + " "))
						{
							if (dt2.Rows.Count > 0)
							{
								 dr2 = dt2.Rows[0];
								title = dr2["title"].ToString();
								addDate = Utils.StrToDateTime(dr2["adddate"].ToString());
								Intro = dr2["intro"].ToString();
								DefaultPic = dr2["photo"].ToString() == "" ? "/SysImg/nopic.gif" : dr2["photo"].ToString();
								ChannelID = 0;
								InfoID = Utils.StrToInt(dr2["id"].ToString());
							}
						}
					}
				}
			}
			#endregion
		}
	}
	protected void Button3_Click()
	{
		if (action == "attention")
		{
			Hashtable ht = new Hashtable();
			ht.Add("Title", "自动回复");
			ht.Add("KeyWord", "关注自动回复");
			ht.Add("Type", 0);
			string site = KSCMS.S("txtSite");
			if (!string.IsNullOrEmpty(site))
			{
				ht.Add("tuwenid", site);
			}
			if (KeyID != 0)
			{
				CommonData.Update("ks_weixinkeyword", ht, "where id=" + KeyID);
			}
			else
			{
				ht.Add("userid", new TemporaryVar().UserID);
				ht.Add("AddPeople", new TemporaryVar().UserName);
				ht.Add("AddTime", DateTime.Now);
				ht.Add("IsKeyWord", 1);
				ht.Add("TypeFlag", new TemporaryVar().GroupID);
				CommonData.Insert("ks_weixinkeyword", ht);
			}
			DataFactory.ExecuteNonQuery("update KS_WeixinEpConfig set AttentionStatus=1 where userid=" + new TemporaryVar().UserID + "");
			Kesion.Weixin.ks_weixin.ClearKeyWordCache();
			Msg.Alert("恭喜，保存成功！", KSCMS.GetComeUrl());
		}
		else if (action == "menu")
		{
			Hashtable ht = new Hashtable();
			ht.Add("ModifyNum", 1);
			ht.Add("type", "view");
			string site = KSCMS.S("txtSite");
			if (!string.IsNullOrEmpty(site))
			{
				string[] names = string.Concat(site, "§").Split('§');
				ht.Add("ClassID", names[0]);
				ht.Add("ChannelID", names[1]);
				ht.Add("infoid", names[2]);

			}
			ht.Add("keyword", "图文");
			ht.Add("TypeName", "TuWen");
			CommonData.Update("KS_WeixinMenu", ht, "where id=" + MenuID);
			Msg.AlertDoFun("恭喜，保存成功!","top.location.reload()");
			isLoadPage = false;
			return;
		}
	}
	private void delete(int MenuID, string action)
	{
		if (action == "attention")
		{
			Hashtable ht = new Hashtable();
			ht.Add("TuWenID", string.Empty);
			CommonData.Update("ks_weixinkeyword", ht, "where id=" + KeyID + " and IsKeyWord=1");
			Kesion.Weixin.ks_weixin.ClearKeyWordCache();
		}
		else if (action == "automaticreply")
		{
			Hashtable ht = new Hashtable();
			ht.Add("TuWenID", string.Empty);
			CommonData.Update("ks_weixinkeyword", ht, "where id=" + KeyID + " and IsKeyWord=2");
			Kesion.Weixin.ks_weixin.ClearKeyWordCache();
		}
		else if (action == "keyword")
		{
			Hashtable ht = new Hashtable();
			ht.Add("TuWenID", string.Empty);
			CommonData.Update("ks_weixinkeyword", ht, "where id=" + KeyID);
		}
		else if (action == "menu")
		{
			Hashtable ht = new Hashtable();
			ht.Add("type", "view");
			ht.Add("keyword", string.Empty);
			ht.Add("ModifyNum", 0);
			ht.Add("TypeName", string.Empty);
			ht.Add("ClassID", 0);
			ht.Add("ChannelID", 0);
			ht.Add("infoid", 0);
			CommonData.Update("KS_WeixinMenu", ht, "where id=" + MenuID);
		}
		else if (action == "sucai")
		{
			#region 素材
			Hashtable ht = new Hashtable();
			ht.Add("SystemType", string.Empty);
			ht.Add("TuWenID", string.Empty);
			CommonData.Update("KS_WeixinPigmsg", ht, "where id=" + SuCaiId);
			Msg.AlertDoFun("恭喜，删除成功!", " parent.clear(" + Utils.StrToInt(KSCMS.S("sendid")) + ");location.href='" + KSCMS.GetComeUrl()+"'");
			#endregion
		}
		Msg.Alert("恭喜，删除成功!");
	}
	public string GetVal(string field)
	{
		if (dr2 != null)
		{
			return dr2[field].ToString();
		}
		else
		{
			return "";
		}
	}
</script>
<%
	if (!isLoadPage) return;
%>

<script>
	function SelectClassPage(FieldID) {
		
		var url = "<%=BasicField.GetUserUrl("wx/wx_GetTuWen","action-"+action+",id-"+sendId+"") %>";
		url += ",frame-no,nowIndex-" + parent.nowIndex + ",fieldID-" + FieldID;
	
		top.openWin("选择图文", url, false, 1000, 500);
   	}
</script>
<script type="text/javascript">
	$(function () {
		var tempstr = $("#ShowTxtTuWen<%=sendId%>", parent.document).val();
    		if (tempstr.length > 10) {
    			$("#mycontentpic").html(tempstr);
    		}
    	});
    	//iframe 自适应高度
    	$(window.parent.document).find("#iframemain<%=sendId%>").load(function () {
		autoIframeHeight();
	});

	function autoIframeHeight() {
		var main = $(window.parent.document).find("#iframemain<%=sendId%>");
    		//alert($(document).height());
    		var thisheight = $(document).height() + 30;
    		if (thisheight < 300) thisheight = 300;
    		main.height(thisheight);
    	//	$(window.parent.document).find("#iframemain<%=sendId%>").parent().height($(window.parent.document).find("#main").contents().find("body").height() + 30); //设置Iframe外层高度
		}
</script>
<link href="/SysImg/teacher/css/newswx.css" rel="stylesheet" />
<%if (PannelShow)
  { %>
 
<form name="myForm" method="post" action="<%=BasicField.GetUserUrl("wx/wx_TuWenList","do-save") %>" id="myForm">
<input name="action" value="<%=KSCMS.S("action") %>" type="hidden" />
<input name="menuID" value="<%=KSCMS.S("menuID") %>" type="hidden" />
<div class="tab_cont_cover jsMsgSendTab" data-index="0" style="display: block;">
	<div class="media_cover">
		<div style="display: none">
			<input name="txtsite" type="text" id="txtsite" class="inp245" style="width:50%;" /></div>
		<span class="create_access" id="mycontentpic" onclick="SelectClassPage('txtsite');">
			<i class="icon36_common add_gray"></i>
			<input type="button" class="button" onmouseover="this.className='button_on';" onmouseout="this.className='button';" />
			<strong>从图文中选择</strong>
		</span>
	</div>
</div>
<div class="tuwenbutton">
	<input type="submit" name="ButtonAdd" value="保存" id="ButtonAdd" style="display:<%=ShowBtn%>" class="ct-btn blue">
</div>
</form>
<%}
  else
  { %>
  <form name="myForm" method="post" action="<%=BasicField.GetUserUrl("wx/wx_TuWenList") %>" id="Form1">
<div id="msgSender_media_1_10">
	<div class="appmsg " style="padding: 0px; margin-top: 0;">
		<div class="appmsg_content">
			<div class="appmsg_thumb_wrp">
				<img src="<%=DefaultPic %>" alt="" class="appmsg_thumb"></div>
			<div class="article_info">
				<h4 class="appmsg_title js_title"><a href="<%= MyCache.GetCacheConfig(5)+BasicField.GetEplistContentUrl(Utils.StrToInt(GetVal("SchoolID")), "action-news,archive-archive,paramID-" + GetVal("id"))  %>" target="_blank"><%=title %></a></h4>
				<div class="appmsg_info">
					<em class="appmsg_date"><%=addDate %></em>
				</div>
				<p class="appmsg_inforin"><%=Intro%></p>
			</div>
			<%=GetDeteleButton()%>
			<div style="clear: both;"></div>
		</div>
	</div>
	<div style="clear: both;"></div>
</div>
</form>
<%} %>





