using System;
using System.Collections.Generic;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using TopWinCMS.Model;
using TopWinCMS.BLL;
using TopWinCMS.Common;

namespace TopWinCMS.WebUI.Member
{
    public partial class Answer : TopWinCMS.BLL.PageFactory.Pages.ClientPublicPage
    {
        SQLModel s_model = new SQLModel();
        string Action = null;
        protected void Page_Load(object sender, EventArgs e)
        {
            Page.Title = Config.SiteName + "→ 会员中心 → 我回答的问题";
            Action = Request["Action"];
            if (!IsPostBack)
            {
                Bind();
            }
        }

        private void Bind()
        {
            if (Action == "Pass")
            {
                s_model.WHERE = " UserName='" + MemberLoginInfo.UserName + "' and pass=1";
                GV_List.Columns[GV_List.Columns.Count - 1].Visible = false;
            }
            else
            {
                s_model.WHERE = " UserName='" + MemberLoginInfo.UserName + "' and pass=0";
            }
            new Public_Answer().BindList(GV_List, PageNavigation1, s_model);
            if (GV_List.Rows.Count <= 0)
            {
                LB_NoShow.Text = "<img src='/Images/NoData.gif'><br/>";
            }
            else
            {
                LB_NoShow.Visible = false;
            }

        }

        protected string Answer_Content(object obj)
        {
            return TopWinCMS.Common.MyString.Left(TopWinCMS.Common.HTML.NoHTML(obj.ToString()), 200);
        }

        protected void PageNavigation1_PageChanging(object src, TopWinCMS.WebControls.PageChangingEventArgs e)
        {
            s_model.PageNo = e.NewPageIndex;
            Bind();
        }

        protected void GV_List_RowEditing(object sender, GridViewEditEventArgs e)
        {
            GV_List.EditIndex = e.NewEditIndex;
            Bind();


            string ID = GV_List.DataKeys[GV_List.EditIndex].Value.ToString();
            AnswerInfo model = new AnswerManage().EditLoad(ID);
            if (model != null)
            {
                TextBox tb = ((TextBox)GV_List.Rows[GV_List.EditIndex].Cells[1].Controls[1]);
                tb.Text = model.Content.Trim();
            }
        }

        protected void GV_List_RowUpdating(object sender, GridViewUpdateEventArgs e)
        {
            TextBox tb = ((TextBox)GV_List.Rows[GV_List.EditIndex].Cells[1].Controls[1]);

            AnswerInfo model = new AnswerInfo();

            model.ID = (int)GV_List.DataKeys[GV_List.EditIndex].Value;
            model.Content = Server.HtmlEncode(tb.Text.Trim());

            string Err = null;
            new AnswerManage().Add(model, ref Err);

            if (string.IsNullOrEmpty(Err) == false)
            {
                MsgBox.Alert(Err, UpdatePanel1);
            }
            else
            {
                GV_List.EditIndex = -1;
                Bind();
            }


        }

        protected void GV_List_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)
        {
            GV_List.EditIndex = -1;
            Bind();
        }
    }
}
