<%@ 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;  //定义页面是否加载
    public int keyId = 0;
    public DataRow TuDr = null;
    public DataRow ShopDr = null;
    public DataRow dr = null;
    private string AttentionStatus = "1";
    public int keytype = 0;
    public string Content = string.Empty;

    protected void Page_Load(object sender, EventArgs e)
    {
        string DoMethod = BasicField.GetParam("do");
        if (string.IsNullOrEmpty(DoMethod))
        {
            DoMethod = KSCMS.S("action");
        }
        keyId = Utils.StrToInt(KSCMS.S("keyid"));
        __info.TxtContent = "";
        __info.TxtKeyWord = "";
        __info.TxtTitle = "";
        switch (DoMethod)
        {
            case "del":
                __info.IsShow1 = false;
                __info.IsShow2 = false;
                __info.IsShow3 = true;
                DoDel(KSCMS.S("ids"));
                break;
            case "addrules":
                __info.IsShow1 = false;
                __info.IsShow2 = true;
                __info.IsShow3 = false;
                break;
            case "modify":
                __info.Button1 = "none";
                Modify();
                __info.IsShow1 = false;
                __info.IsShow2 = true;
                __info.IsShow3 = false;
                break;
            case "save":
                ButtonAdd_Click();
                break;
            default:
                __info.IsShow1 = true;
                __info.IsShow2 = false;
                __info.IsShow3 = true;
                __info.Button1 = "";
                break;
        }

    }
    protected void ButtonAdd_Click()
    {
        if (string.IsNullOrEmpty(KSCMS.S("TxtTitle")))
        {
            Msg.Alert("请输入规则标题");
            return;
        }
        if (string.IsNullOrEmpty(KSCMS.S("TxtKeyWord")))
        {
            Msg.Alert("请输入关键词");
            return;
        }
        Hashtable ht = new Hashtable();
        ht.Add("Content",Utils.RemoveXss(KSCMS.S("TxtContent")));
        ht.Add("Title", KSCMS.S("TxtTitle"));
        ht.Add("keyword", KSCMS.S("TxtKeyWord"));
        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"));
        }
        ht.Add("IsKeyWord", 0);

        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("TypeFlag", new TemporaryVar().GroupID);
            CommonData.Insert("KS_WeixinKeyWord", ht);
        }
        Kesion.Weixin.ks_weixin.ClearKeyWordCache();
        Msg.Alert("恭喜，保存成功！",BasicField.GetUserUrl("wx/wx_KeyWord"));
        isLoadPage = false;
    }
    public void DoDel(string id)
    {
        if (id != "")
        {
            string ids = id.Replace(" ", "");
            string[] id_arr = ids.Split(',');
            Hashtable ht = new Hashtable();
            foreach (string id2 in id_arr)
            {
                ht.Add("id", id2);
                CommonData.Delete("KS_WeixinKeyWord", ht);
                ht.Clear();
            }
            Msg.Alert("删除成功！", KSCMS.GetComeUrl());
            isLoadPage = false;
        }

    }
    private void Modify()
    {

        using (DataTable dt = DataFactory.GetDataTable("select top 1 * from KS_WeixinKeyWord where id=" + keyId))
        {
            if (dt.Rows.Count > 0)
            {
                dr = dt.Rows[0];
                keyId = Utils.StrToInt(dr["id"].ToString());
                __info.TxtContent = dr["content"].ToString();
                __info.TxtKeyWord = dr["keyword"].ToString();
                __info.TxtTitle = dr["title"].ToString();
                //this.DrType.SelectedValue = dr["type"].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;
                }
            }

        }
    }
    public string GetContent(string content)
    {
        string str = string.Empty;
        str = "<span class=\"content\">【内容】</span><div class=\"appmsg_content\"><h4 class=\"appmsg_title js_title\">" + Utils.CutStr(content, 30) + "</h4></div>";
        return str;

    }
    public string GetContentDetail(string TuWenID, string CourseID, string SucaiId,int CosType)
    {
        string str = string.Empty;

        if (!string.IsNullOrEmpty(TuWenID))
        {
            #region 图文
            string[] tuwen = string.Concat(TuWenID, "§").Split('§');

            using (DataTable dt2 = DataFactory.GetDataTable("select Top 1 * from KS_Enterprisenews where id=" + tuwen[2] + " and userid=" + new TemporaryVar().UserID + ""))
            {
                if (dt2.Rows.Count > 0)
                {
                    DataRow dr = dt2.Rows[0];
                    str = string.Format(@"<span class=""content"">【图文】</span><div class=""appmsg_content""> <h4 class=""appmsg_title js_title""><a href=""{1}"" target=""_blank"">{0}</a></h4>
           </div>", Utils.CutStr(dr["title"].ToString(), 30), BasicField.GetEplistContentUrl(Utils.StrToInt(dr["SchoolID"].ToString()), "action-news,archive-archive,paramID-" + dr["id"].ToString()));
                }
            }
            #endregion
        }

        if (!string.IsNullOrEmpty(CourseID))
        {
            #region 课程
            string[] CourseIDs = string.Concat(CourseID, "§").Split('§');
            string tableName = "KS_Course";
            if (CosType == 0)
            {
                tableName = "KS_CourseFace";
            }
            using (DataTable dt2 = DataFactory.GetDataTable("select Top 1 * from " + tableName + " where courseid=" + CourseIDs[2] + ""))
            {
                if (dt2.Rows.Count > 0)
                {
                    DataRow dr = dt2.Rows[0];

                    str += string.Format(@"<span class=""content"">【课程】</span><div class=""appmsg_content"">
              <h4 class=""appmsg_title js_title""><a href=""{1}"" target=""_blank"">{0}</a></h4></div>", Utils.CutStr(dr["name"].ToString(), 30), GetUrl(Utils.StrToInt(dr["courseid"].ToString()), CosType));
                }
            }
            #endregion
        }
        if (!string.IsNullOrEmpty(SucaiId))
        {
            #region 素材
            using (DataTable dt2 = DataFactory.GetDataTable("select Top 1 * from  KS_WeixinPigmsg where id=" + Utils.StrToInt(SucaiId)))
            {
                if (dt2.Rows.Count > 0)
                {
                    DataRow dr = dt2.Rows[0];
                    str += string.Format(@"<span class=""content"">【素材】</span><div class=""appmsg_content"">
              <h4 class=""appmsg_title js_title""><a href=""javascript:(0)"" >{0}</a></h4></div>", Utils.CutStr(dr["title"].ToString(), 30));
                }
            }
            #endregion
        }
        return str;
    }
    public string GetUrl(int courseId,int cosType)
    {
        string value = "courseview";
        if (cosType == 0)
        {
            value = "faceview";
        }
        else if (cosType == 5)
        {
            value = "liveview";
        }
        return BasicField.GetRewriteUrl(value, courseId);
    }
</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>
				<a href="<%=BasicField.GetUserUrl("wx/wx_AutomaticReplyMsg") %>">消息自动回复  </a>
			</li>
			<li class="puton">
				<a href="<%=BasicField.GetUserUrl("wx/wx_KeyWord") %>">关键词自动回复  </a>
			</li>
		</ul>
   	</div>
    <div class="clear"></div>
  
     <script>
     	$(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-keyword,keyId-"+keyId+"") %>');

     				} else if (k == 1) {
     					$('#mybox2').show();
     					$('#mybox1').hide();

     				} else if (k == 2) {
     					$('#iframemain0').attr('src', '<%=BasicField.GetUserUrl("wx/wx_CourseList","frame-no,action-keyword,keyId-"+keyId+"") %>');
     				}
     				else if (k == 3) {
     					$('#iframemain0').attr('src', '<%=BasicField.GetUserUrl("wx/wx_SuCaiList","frame-no,action-keyword,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" id="div_replyContent">
        <form name="myForm" method="post" action="<%=BasicField.GetUserUrl("wx/wx_KeyWord") %>" id="Form1">
        <input type="hidden" name="action" id="action" value="" />
        <%if(__info.IsShow1){ %>
                <div ID="Panel1">
                    <div class="search_group">
                      <div class="search">
                          <input name="TxtKey" type="text" id="TxtKey" class="search_box" />
                          <input type="submit" name="buttonsearch" value="" id="buttonsearch" class="search_btn" />
                      </div>
                      <div class="btn_wrp" style="float:right;line-height: 32px;">
                         <span class="quanxuan"><input type="checkbox" name="chkall" id="chkall" onClick="CheckAll(this.form);" />全选</span>
                        <a onclick="if (GetIDS('del')){$('#action').val('del'); $('#Form1').submit()}else{return false};" id="delbutton" class="key_font_css"><b></b>删除关键字</a>
                         <input type="submit" name="Button1" value="+添加规则" style="display:<%=__info.Button1%>" id="Button1" onclick="$('#action').val('addrules');" class="delete_fans" />
                      </div>
                      <div class="clear"></div>
                   </div>
               </div>
               <%} %>
               <%if (__info.IsShow2)
              { %>
                <div ID="Panel2">
               <input id="keytype" type="hidden" value="<%=keytype%>"/>
               <input  type="hidden" name="keyid" value="<%=keyId%>"/>
               <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>
               <ul id="Js_ruleList" class="keywords_rule_list2"  >
                  <li class="keywords_rule_item open">
                      <!--<div class="keywords_rule_hd no_extra dropdown_area Js_detail_switch dropdown_opened">
                          <div class="info">
                              <em class="keywords_rule_num">
                                  <strong>新规则</strong>
                              </em>
                          </div>
                      </div>-->
                      <div class="keywords_rule_bd keywords_rule_detail border">
                          <div class="rule_name_area">
                              <div class="frm_control_group" >
                                  <div class="frm_controls">
                                      <span class="frm_input_box">
                                         <label for="" class="frm_label">
                                             <strong>规则名</strong>
                                         </label>
                                         <input name="TxtTitle" type="text" maxlength="50" id="TxtTitle" class="textbox" value="<%=__info.TxtTitle %>" style="width:40%;" />
                                      </span>
                                      <span class="tips frm_tips">
                                          规则名最多50个字
                                      </span>
                                  </div>
                              </div>
                          </div>
                          <div class="keywords_tap keywords no_data">
                              <div>
                                  <div class="info"></div>
                                  <div class="opr">
                                      <label>
                                          <strong>关键字</strong>
                                      </label>
                                      <input name="TxtKeyWord" type="text" maxlength="50" id="TxtKeyWord" class="textbox" value="<%=__info.TxtKeyWord %>" style="width:40%;" />
          
                                      <span class="tips keyword" style="color: red;">
                                           关键字最多可输入50字，请用&quot;|&quot;进行分隔，例如&quot;男装|女装"。
                                      </span>
                                  </div>
                              </div>
                          </div>
                          <div class="keywords_tap reply no_data" style="margin-top: 20px;">
                              <div class="keywords_tap_hd">
                                  <div class="info">
                                      <h4>
                                          回复
                                      </h4>
                                  </div>
                              </div>
                              <div class="keywords_tap_bd">
                                  <div class="phone_right" style="margin-left:0; margin-right:0;">
                                  <div class="cz_list" style=" padding:0;">
                                      <div class="tabs1">
                                          <div class="menu1box">
                                             <ul class="menu1">
                                                <li class="hit">
                                                    <a href="<%=BasicField.GetUserUrl("wx/wx_TuWenList","frame-no,action-keyword,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-keyword,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-keyword,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" style="min-height:auto;">
                                                <dt 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-keyword,keyId-"+keyId+"") %>" style="width:100%;" name="iframemain0" id="iframemain0" > </iframe>
                                                  </dt>
                                                   <dt id="mybox2" class="com_form msgBox" style="display:none;margin-bottom: 30px;">
                                                  <div><textarea name="TxtContent" rows="2" cols="20" id="TxtContent" style="height: 200px; width: 98%; background: #fff; background-color: #fff; border: 1px solid #ddd;"><%=__info.TxtContent %>  </textarea></div>
                                                   </dt>
                                              </div>
                                          </div>
                                      </div>
                                  </div>
                              </div>
                              </div>
                          </div>
                      </li>
                  </ul>
                  <div class="main-button">
                      <input type="submit" name="ButtonAdd" value="保存" onclick="$('#action').val('save')" id="Submit1" class="button" />
                  </div>
            
              </div>
              <%} %>
                 <%if (__info.IsShow3)
                { %>
                <div ID="Panel3">
                <ul  class="keywords_rule_list"  >
             <%
                 int recordcount, pagecount;
                 int CurrPage = new BasicField().GetCurrPage;
                 int PageSize = 10;
                 string sqlParam = "ks_weixinkeyword where TypeFlag="+new TemporaryVar().GroupID+" and userid="+new TemporaryVar().UserID+"";
                 if (!string.IsNullOrEmpty(KSCMS.S("TxtKey")))
                     sqlParam += " and (KeyWord like N'%" + KSCMS.S("TxtKey") + "%' or Title like N'%" + KSCMS.S("TxtKey") + "%')";
                 #region 日期限制
                 if (!string.IsNullOrEmpty(KSCMS.S("adddate")))
                 {
                     if (Utils.IsDateTime(KSCMS.S("adddate")))
                     {
                         sqlParam += " and [AddTime]>='" + KSCMS.S("adddate") + "'";
                     }
                 }
                 #endregion
        
                 using (DataTable dt = DataFactory.GetPage("*", sqlParam, "id", "order by id desc", CurrPage, PageSize, out recordcount, out pagecount))
                 {
                     if (dt.Rows.Count > 0)
                     {
                         int i = 0;
                         foreach (DataRow dr in dt.Rows)
                         {
                             KSCMS.W(" <li class=\"keywords_rule_item open\" >" +
                 "<div class='keywords_ruleBox'><div class=\"keywords_rule_hd no_extra dropdown_area Js_detail_switch dropdown_opened\" data-reply=\"empty\" style=\"-webkit-transition: all, 1s;\">" +
               " <div class=\"operating\">" +
                  "<a href=\"" + BasicField.GetUserUrl("wx/wx_KeyWord", "action-modify,keyId-" + dr["id"].ToString()) + " \"><img src=\"/admin/weixin/images/updata.png\" /></a>" +
                  "<a href=\"" + BasicField.GetUserUrl("wx/wx_KeyWord", "do-del,keyId-" + dr["id"].ToString()) + ",ids-" + dr["id"].ToString() + "\"><img src=\"/admin/weixin/images/delete.png\" /></a></div>" +
               " <div class=\"info\">" +
                    "<input type=\"checkbox\"  name=\"ids\" id=\"ids\" value=\"" + dr["id"].ToString() + "\" style=\"height:15px; width:15px; position:absolute;top: 15px;left: 11px;vertical-align: middle;\" /> " +
                    "<em class=\"keywords_rule_num\" style=\"margin-left:20px;\"> 规则" + (((CurrPage - 1) * PageSize) + i) + ":</em>" +
                    "<strong class=\"keywords_rule_name\">" + dr["Title"].ToString() + " </strong></div></div>");
        
                             KSCMS.W("<div class=\"keywords_rule_bd keywords_rule_detail\"><div class=\"keywords_tap keywords\"><div class=\"keywords_tap_hd\">" +
                       " <div class=\"info\"><h4 style=\"color:#333;\"><font style=\"font-size:20px; color:#ccc;\">·</font>关键字</h4></div></div>" +
                    "<div class=\"keywords_tap_bd\"><ul class=\"keywords_list\" ><li data-index=\"0\" ><div class=\"desc\">   &nbsp;&nbsp;" + dr["KeyWord"].ToString() + "</div> </li> </ul></div> </div>" +
                    "<div class=\"keywords_tap reply\"><div class=\"keywords_tap_hd\"><div class=\"info\"> <h4 style=\"color:#333;\">" +
                               " <font style=\"font-size:20px; color:#ccc;\">·</font>回复</h4></div></div><div class=\"keywords_tap_bd huifu\"><div>" +
                    (Convert.ToString(dr["Content"].ToString()) == "" ? "" : GetContent(dr["Content"].ToString())) +
                    GetContentDetail(dr["TuWenID"].ToString(), dr["CourseID"].ToString(), dr["sucaiid"].ToString(),Utils.StrToInt(dr["costype"].ToString())) + "</div></div></div> </div></div></li>");
        
                         }
                     }
                     else
                     {
                         KSCMS.W(" <div class=\"norecord\" style=\" text-align:center; margin:20px 10px;\"> <span style=\"text-align:center\"> 暂无规则</span></div>");
                     }
                 }
               %>
                </ul>
                    <div class="clear"></div>
            <div class="fy_box">
			<%
                    #region 分页
                    string PageStr = "[KS:PageStr1]";
                    BasicField.ReplaceFilterPage(ref PageStr, CurrPage, PageSize, pagecount, recordcount);
                    KSCMS.W(PageStr);
                    #endregion
                 %>
            </div>  
            <div class="clear"></div>   
        </div>
          <%} %>
        </form>
     </div>

</div>

