<%@ 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" %>
<%@ Import Namespace="Kesion.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 dr = null;

    public int keyId = 0;
    private string action = string.Empty;
    public DataRow TuDr = null;
    public DataRow ShopDr = null;
    private string AttentionStatus = "1";
    public int keytype = 0;
    public string Content = string.Empty;
    protected void Page_Load(object sender, EventArgs e)
    {
        keyId = Utils.StrToInt(DataFactory.GetOnlyValue("select top 1 id from KS_WeixinKeyWord where iskeyword=2 and TypeFlag="+new TemporaryVar().GroupID+" and userid="+new TemporaryVar().UserID+""));
        AttentionStatus =DataFactory.GetOnlyValue("select top 1 AutomaticReply from KS_weixinepconfig where TypeFlag=" + new TemporaryVar().GroupID + " and userid="+new TemporaryVar().UserID+"");
      
        string DoMethod = BasicField.GetParam("do");
        if (string.IsNullOrEmpty(DoMethod))
        {
            DoMethod = KSCMS.S("action");
        }
        switch (DoMethod)
        {
            case "save":
                Save_Click();
                break;
            case "save2":
                Save_Click2();
                break;
        }
        __info.TxtContent = "";
        if (!IsPostBack)
        {
            __info.RdbStatus = AttentionStatus;
            if (keyId == 0)
            {
            }
            else
            {
                #region 修改

                using (DataTable dt = DataFactory.GetDataTable("select top 1 * from  KS_WeixinKeyWord where iskeyword=2 and TypeFlag="+new TemporaryVar().GroupID+" "))
                {
                    if (dt.Rows.Count > 0)
                    {
                        DataRow dr = dt.Rows[0];
                        string[] typename = string.Concat(dr["typename"].ToString(), "§").Split('§');
                        keyId = Utils.StrToInt(dr["id"].ToString());
                        __info.TxtContent = dr["content"].ToString();
                        if (!string.IsNullOrEmpty(dr["TuWenID"].ToString()))
                        {
                            keytype = 0;
                        }
                        else if (!string.IsNullOrEmpty(dr["content"].ToString()))
                        {
                            keytype = 1;
                        }
                        else if (!string.IsNullOrEmpty(dr["ShopID"].ToString()))
                        {
                            keytype = 2;
                        }
                        else if (!string.IsNullOrEmpty(dr["sucaiid"].ToString()))
                        {
                            keytype = 3;
                        }
                        else if (!string.IsNullOrEmpty(dr["courseid"].ToString()))
                        {
                            keytype = 4;
                        }
                    }
                }
                #endregion
            }
        }
    }
    private void GetShop(string ShopID)
    {
        #region 根据商品类型得到商品的信息
        if (!string.IsNullOrEmpty(ShopID))
        {
            string[] shop = string.Concat(ShopID, "§").Split('§');

            using (DataTable dt2 = DataFactory.GetDataTable("select Top 1 * from " + string.Format("[" + MyCache.GetCacheModel(Utils.StrToInt(shop[1]), 3) + "]") + " where infoid=" + shop[2] + " and ChannelID=" + shop[1] + ""))
            {
                if (dt2.Rows.Count > 0)
                {
                    ShopDr = dt2.Rows[0];
                }
            }
        }
        #endregion
    }

    private void GetTuWen(string TuWenID)
    {
        #region 根据图文类型得到图文的信息

        if (!string.IsNullOrEmpty(TuWenID))
        {
            string[] tuwen = string.Concat(TuWenID, "§").Split('§');
            using (DataTable dt2 = DataFactory.GetDataTable("select Top 1 * from " + string.Format("[" + MyCache.GetCacheModel(Utils.StrToInt(tuwen[1]), 3) + "]") + " where infoid=" + tuwen[2] + " and ChannelID=" + tuwen[1] + ""))
            {
                if (dt2.Rows.Count > 0)
                {
                    TuDr = dt2.Rows[0];
                }
            }
        }
        #endregion
    }

    protected void Save_Click()
    {
        Hashtable ht = new Hashtable();
        ht.Add("KeyWord", "消息自动回复");
        ht.Add("Type", 0);
        ht.Add("Content", Utils.ClearHtml(KSCMS.S("TxtContent")));
        ht.Add("Title", "消息自动回复");
        string site = KSCMS.S("TxtTuWenID");
        if (!string.IsNullOrEmpty(site))
        {
            ht.Add("TuWenID", site);
        }
        if (!string.IsNullOrEmpty(KSCMS.S("TxtCourseID")))
        {
            ht.Add("CourseID", KSCMS.S("TxtCourseID"));
            ht.Add("Costype", Utils.StrToInt(KSCMS.S("costype")));
        }
        if (!string.IsNullOrEmpty(KSCMS.S("TxtSuCaiID")))
        {
            ht.Add("sucaiid", KSCMS.S("TxtSuCaiID"));
        }
        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", 2);
            ht.Add("TypeFlag", new TemporaryVar().GroupID);
            CommonData.Insert("KS_WeixinKeyWord", ht);
        }
        DataFactory.ExecuteNonQuery("update KS_WeixinEpConfig set AutomaticReply=1 where userid=" + new TemporaryVar().UserID + "");
        Kesion.Weixin.ks_weixin.ClearKeyWordCache();
        Msg.Alert("恭喜，保存成功！", KSCMS.GetComeUrl());
    }
    protected void Save_Click2()
    {
        string content = "keyword";
        string picmsg = "picid";
        System.Web.Caching.Cache keywordCache = System.Web.HttpRuntime.Cache;
        keywordCache.Remove(picmsg);
        keywordCache.Remove(content);
        DataFactory.ExecuteNonQuery("update KS_WeixinEpConfig set AutomaticReply=0 where userid=" + new TemporaryVar().UserID + "");

        Msg.Alert("恭喜，保存成功！",BasicField.GetUserUrl("wx/wx_AutomaticReplyMsg"));
    }

</script>
<%
	if (!isLoadPage) return;
%>
<link href="/SysImg/teacher/css/newswx.css" rel="stylesheet" />
<div class="t_tybox">
    <div class="tytitle"><h4>微信关注回复</h4></div>
	<div class="tabs">
		<ul class="clearfix">
			<li>
				<a href="<%=BasicField.GetUserUrl("wx/wx_ReplyMsg") %>">关注回复  </a>
			</li>
			<li class="puton">
				<a href="<%=BasicField.GetUserUrl("wx/wx_AutomaticReplyMsg") %>">消息自动回复  </a>
			</li>
			<li>
				<a href="<%=BasicField.GetUserUrl("wx/wx_KeyWord") %>">关键词自动回复  </a>
			</li>
		</ul>
	</div>
	<div class="clear"></div>

	<script>
		jQuery(document).ready(function () {
			showStatus();
		});

		function showStatus() {
			var checkrad = jQuery("#RdbStatus :radio[checked]").val();
			if (checkrad == "1") {
				jQuery("#detail").show();
				jQuery("#visiblebutton").hide();
				// jQuery("#weixinstatus").css("display", "");
			} else {

				jQuery("#detail").hide();
				jQuery("#visiblebutton").show();
			}
		}
		$(function () {
			aa(<%=keytype%>);
   		$(".menu1box ul li").click(function () {
   			var k = $(this).index();
   			aa(k);
   		});
   	});
		function aa(k) {

			$(".menu1box ul li").each(function (index, domEle) {
				$(this).attr("class", "");
			});
			$(".menu1box ul li").each(function (index, domEle) {
				if (k == index) {
					$("#typekey").val(k);

					if (k == 0) {
						$('#iframemain0').attr('src', '<%=BasicField.GetUserUrl("wx/wx_TuWenList","frame-no,action-AutomaticReply,keyId-"+keyId+"") %>');
					} else if (k == 1) {
						$('#mybox2').show();
						$('#mybox1').hide();

					} else if (k == 2) {
						$('#iframemain0').attr('src', '<%=BasicField.GetUserUrl("wx/wx_ShopList","frame-no,action-AutomaticReply,keyId-"+keyId+"") %>');
					}
					else if (k == 3) {
						$('#iframemain0').attr('src', '<%=BasicField.GetUserUrl("wx/wx_SuCaiList","frame-no,action-AutomaticReply,keyId-"+keyId+"") %>');
					}
					$(this).attr("class", "hit");
				}
			});
		}
                  var box = null;
                  var isreload = false;
                  function openWin(title, url, reload, width, height) {
                  	if (width == null) width = 760;
                  	if (height == null) height = 450;
                  	isreload = reload;
                  	box = $.dialog.open(url, {
                  		id: 'topbox', lock: true, title: title, width: width, height: height, close: function () {
                  			if (isreload) {
                  				top.frames['main'].location.reload();
                  			}
                  		}
                  	});
                  }
	</script>
    <div class="console_body">
        <form name="myForm" method="post" action="<%=BasicField.GetUserUrl("wx/wx_AutomaticReplyMsg") %>" id="myForm">
        <input name="action" value="save" type="hidden" id="action" />
            <div class="openKey" >
                <span class="txt" style="float: left;">是否要开启消息回复:</span>
                <input type="hidden" name="keyid" id="keyid" value="<%=keyId%>">
                <table id="RdbStatus" onclick="showStatus()" border="0" style="margin-right: 5px;">
                    <tr>
                        <td><span style="margin-right: 10px;">
                            <input id="RdbStatus_0" type="radio" name="RdbStatus" <%=__info.RdbStatus=="1"?"checked":"" %> value="1" /><label for="RdbStatus_0">开启</label></span></td>
                        <td>
                            <input id="RdbStatus_1" type="radio" name="RdbStatus" value="0" <%=__info.RdbStatus=="0"?"checked":"" %> /><label for="RdbStatus_1"> 关闭</label></td>
                    </tr>
                </table>
                <span class="tips" style="font-weight: normal; padding-left: 30px;">Tips:如果有启用多客服系统，这里设置的消息回复会失效；客服系统的优先级高于消息回复；</span>
                <div class="clear"></div>
            </div>
    
            <div id="detail" style="display: none">
                <div id="Panel1">
                    <input id="keytype" type="hidden" value="<%=keytype%>" />
                    <input id="TxtTuWenID" type="hidden" name="TxtTuWenID" />
                    <input id="TxtCourseID" type="hidden" name="TxtCourseID" />
                    <input id="CosType" type="hidden" value="0" name="CosType" />
                    <textarea id="ShowTxtTuWen0" style="width: 300px; height: 200px; display: none"></textarea>
                    <textarea id="ShowTxtShop0" style="width: 300px; height: 200px; display: none"></textarea>
                    <input id="TxtSuCaiID" type="hidden" name="TxtSuCaiID" />
                    <textarea id="ShowTxtSuCai0" style="width: 300px; height: 200px; display: none"></textarea>
                    <div class="phone_right">
                        <div class="cz_list">
                            <div class="tabs1">
                                <div class="menu1box clearfix">
                                    <ul class="menu1">
                                        <li class="hit">
                                            <a href="<%=BasicField.GetUserUrl("wx/wx_TuWenList","frame-no,action-AutomaticReply,keyId-"+keyId+"") %>" onclick="$('#mybox1').show();$('#mybox2').hide();" target="iframemain0">
                                                <i class="sm_tab tw2"></i>图文
                                            </a>
                                        </li>
                                        <li>
                                            <a href="javascript:;" onclick="$('#mybox2').show();$('#mybox1').hide();">
                                                <i class="sm_tab wz"></i>文字
                                            </a>
                                        </li>
                                        <%if (BaseFun.CheckModulePower("mall")) { %>
                                        <li>
                                            <a href="<%=BasicField.GetUserUrl("wx/wx_CourseList","frame-no,action-AutomaticReply,keyId-"+keyId+"") %>" onclick="$('#mybox2').hide();$('#mybox1').show();" target="iframemain0">
                                                <i class="sm_tab sp"></i>课程
                                            </a>
                                        </li>
                                        <%} %>
                                        <li>
                                            <a href="<%=BasicField.GetUserUrl("wx/wx_SuCaiList","frame-no,action-AutomaticReply,keyId-"+keyId+"") %>" onclick="$('#mybox1').show();$('#mybox2').hide();" target="iframemain0">
                                                <i class="sm_tab tw2"></i>素材
                                            </a>
                                        </li>
                                        <div class="clear">
                                        </div>
                                    </ul>
                                </div>
                                <div class="main1box">
                                    <div class="main">
                                        <dt class="mbimg" style="display: block;" id="mybox1">
                                            <iframe class="editArea-ifrm" frameborder="no" border="0" src="<%=BasicField.GetUserUrl("wx/wx_TuWenList","frame-no,action-AutomaticReply,keyId-"+keyId+"") %>" style="width: 100%;" name="iframemain0" id="iframemain0"></iframe>
                                        </dt>
                                        <dt id="mybox2" class="com_form msgBox" style="display: none; margin-bottom: 10px;">
                                            <div><textarea name="TxtContent" rows="2" cols="20" id="TxtContent" style="height: 200px; width:100%; background: #fff; background-color: #fff; border: 1px solid #ddd;"><%=__info.TxtContent %>  </textarea></div>
                                        </dt>
                                    </div>
                                </div>
                            </div>
                        </div>
                    </div>
                    <div class="opr1">
                        <br />
                        <br />
                        <input type="submit" name="ButtonAdd" value="保存" id="Submit1" class="btn btn_primary Js_rule_save" />
                    </div>
                </div>
            </div>
            <div id="visiblebutton" class="clearfix" style="display: none">
                <input type="submit" name="Button1" value="保存" id="Submit2" onclick="$('#action').val('save2')" class="ct-btn blue" style="float: left" />
            </div>
        </form>
    </div>
</div>


