<%@ Control Language="C#" %>
<%@ Import Namespace="Kesion.Cache" %>
<%@ Import Namespace="Kesion.Publics" %>
<%@ Import Namespace="Kesion.BLL" %>
<%@ Import Namespace="Kesion.Config" %>
<%@ Import Namespace="Kesion.Components" %>
<%@ Import Namespace="Kesion.HtmlTags" %>
<%@ Import Namespace="Kesion.UCenter" %>
<%@ Import Namespace="Kesion.XML" %>
<%@ Import Namespace="System.Data" %>
<%@ Import Namespace="System.Xml" %>
<script runat="server" type="text/C#">

    dynamic __user = HttpContext.Current.Items["user"];                //初始化用户信息
    bool isLoadPage = true;  //定义页面是否加载


    int recordcount, pagecount;
    int CurrPage = new BasicField().GetCurrPage;
    int PageSize =10;

    public static int formId = 0;
    public DataTable dtField = null;
    public StringBuilder sbb = new StringBuilder();
    public StringBuilder sbbTitle = new StringBuilder();
    public int isPay = 0;
    public int isReply = 0;
    public int showColNum = 8;
    protected void Page_Load(object sender, EventArgs e)
    {
        formId = Utils.StrToInt(KSCMS.S("formid"));
        if (KSCMS.S("action") == "del")
        {
            isLoadPage = false;
            doDelete();
            return;
        }

        initialList(1);
    }

    private void initialList(int PageIndex)
    {
        if (formId > 0)
        {

            string noShowField = "formid,recordid,UserName,userip";
            if (!DataFactory.Exists("KS_Form", "formid=" + formId))
            {
                KSCMS.Die("出错！");
            }
            else
            {
                isPay = FormManage.GetFormSubjectInfo(formId).IsPay;
                isReply = FormManage.GetFormSubjectInfo(formId).IsReply;
                if (isPay != 1)
                {
                    noShowField += ",ispay,paymoney,paytime";
                }
                if (isReply != 1)
                {
                    noShowField += ",IsReply,ReplyUser,ReplyContent,ReplyTime";
                }


                dtField = LabelSource.getTableFiled(0, string.Empty, "KS_FormRecord" + formId);

                if (dtField != null)
                {
                    int num = 0;
                    foreach (DataRow dr in dtField.Rows)
                    {
                        if (Utils.InArray(dr["COLUMN_NAME"].ToString().ToLower(), noShowField.ToLower(), ",") == false)
                        {
                            string fieldTitle = getFieldTitle(dr["COLUMN_NAME"].ToString());
                            sbbTitle.AppendLine(@"<td style=""text-align:center"">" + fieldTitle + "</td>");
                            num++;
                            if (num == showColNum) break;
                        }
                    }

                }

            }


            using (DataTable dt = DataFactory.GetPage("*", "[KS_FormRecord" + formId + "] Where UserName='" + Utils.ChkSQL((new TemporaryVar()).UserName) + "'", "recordid", "order by recordid desc", CurrPage, PageSize, out recordcount, out pagecount))
            {
                if (dt.Rows.Count > 0)
                {
                    int nn = (PageIndex-1)*PageSize;

                    foreach (DataRow dr in dt.Rows)
                    {
                        nn++;
                        sbb.Append("<tr>");
                        sbb.Append("<td><div style='text-align:center;'>" + nn + "</div></td>");
                        if (dtField != null)
                        {
                            int num = 0;
                            foreach (DataRow drr in dtField.Rows)
                            {
                                if (Utils.InArray(drr["COLUMN_NAME"].ToString().ToLower(), noShowField.ToLower(), ",") == false)
                                {
                                    string v = dr[drr["COLUMN_NAME"].ToString()].ToString();
                                    sbb.Append("<td>");
                                    switch (drr["COLUMN_NAME"].ToString().ToLower())
                                    {
                                        case "adddate":
                                            sbb.Append("" + Convert.ToDateTime(v).ToShortDateString() + "");
                                            break;
                                        case "ispay":
                                            if (v == "1")
                                            {
                                                sbb.Append("<div style='text-align:center;color:green'>已支付</div>");
                                            }
                                            else
                                            {
                                                sbb.Append("<div style='text-align:center;color:red'>未支付</div>");
                                            }
                                            break;
                                        case "isreply":
                                            if (v == "1")
                                            {
                                                sbb.Append("<div style='text-align:center;color:green'>已回复</div>");
                                            }
                                            else
                                            {
                                                sbb.Append("<div style='text-align:center;color:red'>未回复</div>");
                                            }
                                            break;
                                        case "paymoney":
                                            sbb.Append("<div style='text-align:center;'>" + Kesion.APPCode.Public.ReturnShopPrice(v) + "</div>");
                                            break;
                                        case "status":
                                            if (v == "0")
                                            {
                                                sbb.Append("<div style='text-align:center;color:red'>待审核</div>");
                                            }
                                            else if (v == "1")
                                            {
                                                sbb.Append("<div style='text-align:center;color:green'>采纳</div>");
                                            }
                                            else
                                            {
                                                sbb.Append("<div style='text-align:center;color:#999'>作废</div>");
                                            }
                                            break;
                                        default:
                                            sbb.Append("" + v + "");
                                            break;
                                    }
                                    sbb.Append("</td>");
                                    num++;
                                    if (num == showColNum) break;
                                }
                            }

                        }
                        sbb.Append(@"<td style=""text-align:center"">");
                        sbb.Append("<a href='"+BasicField.GetUserUrl("other/form","action-show,formid-" + formId + ",recordid-" + dr["recordid"].ToString()) + "'>详情</a>");
                        if (isPay == 0 || (isPay == 1 && Utils.StrToInt(dr["ispay"].ToString())== 0))
                        {
                            sbb.Append(" <a href='"+BasicField.GetUserUrl("other/form","action-del,formid-" + formId + ",recordid-" + dr["recordid"].ToString()) + @"' onclick=""return(confirm('确认删除吗？'))"">删除</a>");
                        }
                        sbb.Append("</td>");
                        sbb.Append("</tr>");

                    }
                }
                else
                {
                    sbb.Append(@"<tr class=""tdbg emptycss"">");
                    sbb.Append("<td colspan=10>没有提交数据!</td>");
                    sbb.Append("</tr>");

                }

            }
        }


    }

    public static string getFieldTitle(string field)
    {
        string fieldTitle = field.ToString();
        switch (fieldTitle.ToLower())
        {
            case "isreply":
                fieldTitle = "是否回复";
                break;
            case "replycontent":
                fieldTitle = "回复内容";
                break;
            case "replyuser":
                fieldTitle = "回复人";
                break;
            case "replytime":
                fieldTitle = "回复时间";
                break;
            case "status":
                fieldTitle = "状态";
                break;
            case "adddate":
                fieldTitle = "提交时间";
                break;
            case "ispay":
                fieldTitle = "是否支付";
                break;
            case "paymoney":
                fieldTitle = "支付金额";
                break;
            case "paytime":
                fieldTitle = "支付时间";
                break;
            case "username":
                fieldTitle = "用户名";
                break;
            case "userip":
                fieldTitle = "用户IP";
                break;
            default:
                fieldTitle = XmlObject.GetStrXmlNode(FormManage.GetFormSubjectInfo(formId).FieldsXML, string.Format("//form/field[@name='{0}']/fieldname", fieldTitle));
                break;
        }
        return fieldTitle;
    }

    private void doDelete()
    {
        #region 删除操作
        using (DataTable dt = DataFactory.GetDataTable("select top 1 * From KS_Form Where ShowOnUser=1 and FormID=" + formId))
        {
            if (dt.Rows.Count > 0)
            {
                if (dt.Rows[0]["ispay"].ToString() == "1")
                {
                    DataFactory.ExecuteNonQuery("Delete From KS_FormRecord" + formId + " Where Ispay=0 And UserName='" + __user.UserName + "' and recordid=" + Utils.StrToInt(KSCMS.S("recordid")));
                }
                else
                {
                    DataFactory.ExecuteNonQuery("Delete From KS_FormRecord" + formId + " Where UserName='" + __user.UserName + "' and recordid=" + Utils.StrToInt(KSCMS.S("recordid")));

                }
                Msg.Alert("恭喜，删除成功!", KSCMS.GetComeUrl());
            }
            else
            {
                Msg.Alert("出错，不允许操作!", KSCMS.GetComeUrl());
            }
        }
        #endregion

    }
 </script>
<%
    if (!isLoadPage) return;
%>


 <div class="r_tybox nomar">
        <%
            int n = 0;
            StringBuilder sb = new StringBuilder();
            using(DataTable dt=DataFactory.GetDataTable("select * From KS_Form Where ShowOnUser=1 order by formid desc"))
            {
                if (dt.Rows.Count > 0)
                {

                    foreach (DataRow dr in dt.Rows)
                    {
                        if (formId == 0 && n==0)
                        {
                            Response.Redirect(BasicField.GetUserUrl("other/form","formid-"+ Utils.StrToInt(dr["formid"].ToString())));
                        }
                        sb.AppendLine(string.Format(@"<li{0}><a href=""{1}"">{2}</a></li>",  (dr["formid"].ToString() == formId.ToString()) ? @" class=""puton""" : string.Empty, BasicField.GetUserUrl("other/form","formid-"+dr["formid"].ToString()), dr["subject"].ToString()));
                        n++;
                    }

                }
            }
          %>
        
    <%if (n == 0)
        { %>
         <table id="tablist" width="100%" border="0" align="center" cellpadding="2" cellspacing="1" class="border">
             <tr class="tdbg emptycss"><td>没有可供管理的表单数据哦！</td></tr>
         </table>
        <%
                return;
            }
            else { %>
        <div class="tabs">	
              <ul>
                 <%=sb.ToString() %> 
              </ul>
        </div> 
    	<div class="clear"></div>
        <br />
    <%}
     %>

     <table id="tablist" width="100%" border="0" align="center" cellpadding="2" cellspacing="1" class="border">
     <%
         if (KSCMS.S("action") == "show")
         {
             using (DataTable dt = DataFactory.GetDataTable("select top 1 * From KS_FormRecord" + formId + " Where Username='" + __user.UserName + "' and recordid=" + Utils.StrToInt(KSCMS.S("recordid"))))
             {
                 if (dt.Rows.Count > 0)
                 {
                     DataRow drr = dt.Rows[0];
                     int IsPay = FormManage.GetFormSubjectInfo(formId).IsPay;
                     if (dtField != null)
                     {
                         foreach (DataRow dr in dtField.Rows)
                         {
                             string noShowColumn = "recordid,formid,replycontent,replyuser,replytime";
                             if (IsPay != 1)
                             {
                                 noShowColumn += ",ispay,paymoney,paytime";
                             }
                             if (isReply!=1)
                             {
                                 noShowColumn += ",isreply";
                             }
                             string colName = dr["COLUMN_NAME"].ToString();
                             if (Utils.InArray(colName.ToLower(),noShowColumn , ",") == false)
                             {
                                 #region  显示所有字段内容
                                 Response.Write(@"<tr class=""tdbg""><td style=""width:120px"" class=""lefttd"">" + getFieldTitle(dr["COLUMN_NAME"].ToString()) + "：</td>");
                                 Response.Write("<td>");
                                 string vv = drr[dr["COLUMN_NAME"].ToString()].ToString();
                                 switch (colName.ToLower())
                                 {

                                     case "ispay":
                                         if (vv == "1")
                                         {
                                             vv=("<div style='color:green'>已支付</div>");
                                         }
                                         else
                                         {
                                             vv=("<div style='color:red'>未支付</div>");
                                         }
                                         break;
                                     case "isreply":
                                         if (vv == "1")
                                         {
                                             vv=("<div style='color:green'>已回复</div>");
                                             vv += "回复人：" + drr["replyuser"].ToString();
                                             vv += "<br/>回复时间：" + drr["replytime"].ToString();
                                             vv += "<br/>回复内容：" + drr["replycontent"].ToString();
                                         }
                                         else
                                         {
                                             vv=("<div style='color:red'>未回复</div>");
                                         }
                                         break;
                                     case "paymoney":
                                         vv=("<div>" + Kesion.APPCode.Public.ReturnShopPrice(vv) + "</div>");
                                         break;
                                     case "status":
                                         if (vv == "0")
                                         {
                                             vv=("<div style='color:red'>待审核</div>");
                                         }
                                         else if (vv == "1")
                                         {
                                             vv=("<div style='color:green'>采纳</div>");
                                         }
                                         else
                                         {
                                             vv=("<div style='color:#999'>作废</div>");
                                         }
                                         break;
                                     case "paytime":
                                         if (string.IsNullOrEmpty(vv))
                                         {
                                             vv = "未支付";
                                         }
                                         break;
                                     default:
                                         break;
                                 }
                                 Response.Write(vv);
                                 Response.Write("</td>");
                                 Response.Write("</tr>");
                                 #endregion

                                 #region 支付平台接口
                                 if (IsPay==1 && dr["COLUMN_NAME"].ToString().ToLower()=="paymoney" && drr["ispay"].ToString()=="0")
                                 {
                                     Response.Write(@"<tr class=""tdbg""><td style=""width:120px"" class=""lefttd"">支付平台：</td>");
                                     Response.Write("<td>");
                                     Response.Write(@"<div class=""paylist"">");
                                     Response.Write(@" <ul>");
                                     StringBuilder sbp = new StringBuilder();
                                     using (DataTable dtp = CommonData.GetDataTable(4))
                                     {
                                         if (dtp.Rows.Count > 0)
                                         {
                                             foreach (DataRow drp in dtp.Rows)
                                             {
                                                 if (Utils.IsShowPayMentOnDevice(drp["showdevice"].ToString()))
                                                 {
                                                     sbp.AppendLine(string.Format(@"<li{2} payid=""{3}""><img src=""{0}"" title=""{1}"" alt=""{1}""/></li>", drp["logo"].ToString(), drp["platname"].ToString(), drp["isdefault"].ToString() == "1" ? @" class=""paycurr""" : string.Empty, drp["id"].ToString()));
                                                 }
                                             }
                                         }
                                     }
                                     Response.Write(sbp.ToString());
                                     Response.Write(@" </ul>");
                                     Response.Write(@"</div>");
                                     Response.Write("</td>");
                                     Response.Write("</tr>");

                                 }
                                 #endregion

                             }
                         }
                     }
                     %>
         <tr class="tdbg">
             <td></td>
         <td class="splittd" style="height:50px;border-bottom:0px;">
             <%if (IsPay == 1 && drr["ispay"].ToString()=="0")
                 { %>
             <input type="button" value="确认支付" class="button" onclick="return(doFormPay())" />
              <script>
                  $(document).ready(function(){
                      $(".paylist").find("li").click(function(){
                          $(".paylist").find("li").attr("style","");
                          $(".paylist").find("li").removeClass("paycurr");
                          $(this).addClass("paycurr");
                      });
                  });

                  function doFormPay() {
                      var pid = $(".paylist").find(".paycurr").attr("payid");
                      window.open('/plus/pay/PayConfirm.aspx?from=form&formid=<%=formId%>&OrderId=<%=Utils.StrToInt(KSCMS.S("recordid"))%>&PlatID=' + pid);
                   }
               </script>
             <%} %>
             <input type="button" value=" 返 回 " class="button" onclick="history.back();" />
         </td>
     </tr>

            <%

                 }
             }
             
         }
         else {
     %>
      <tr class="title">
        	<td width="50" align="center">序号</td>
           <%
                  Response.Write(sbbTitle.ToString());
            %>
          <td align="center" width="70">操作</td>
        </tr>
         <%
                  Response.Write(sbb.ToString());
         %>
      <%} %>
    </table>
      <%
            #region 分页
           if (string.IsNullOrEmpty(KSCMS.S("action")))
            {
                string PageStr = "[KS:PageStr3]";
                BasicField.ReplaceFilterPage(ref PageStr, CurrPage, PageSize, pagecount, recordcount);
                KSCMS.W(PageStr);
            }
            #endregion
         %>
</div>
  
