using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

public partial class etwuser_Intentionadd : System.Web.UI.Page
{
    public string usernameid = string.Empty;
    protected void Page_Load(object sender, EventArgs e)
    {
        HttpCookie ChatCookie = Request.Cookies["cookChatuser"];
        HttpCookie GetCookie = Request.Cookies["cookEtwuser"];
        if (Request.Cookies["cookChatuser"] == null)
        {
            if (Request.Cookies["cookEtwuser"] == null)
            {
                Response.Redirect("../index.aspx");
            }
            else
            {
                DataSet ds = DbHelperSQL.Query("select * from etw_user where username='" + GetCookie["username"] + "'");
                if (ds.Tables[0].Rows.Count > 0)
                {
                    usernameid = ds.Tables[0].Rows[0]["userid"].ToString();
                }
            }
        }
        else
        {
            DataSet dss = DbHelperSQL.Query("select * from chat_user where username='" + ChatCookie["username"] + "'");
            if (dss.Tables[0].Rows.Count > 0)
            {
                usernameid = dss.Tables[0].Rows[0]["userid"].ToString();
            }
        }
        Fun.Funrecord(Request.Url.AbsoluteUri, Page.Request.UserHostAddress, usernameid);
        if (!IsPostBack)
        {
            Bind();
        }
    }
    private void Bind()
    {
        DataSet ds = DbHelperSQL.Query("select * from etw_Intention where usernameid=" + usernameid + " or usernameid=0 order by id");
        if (ds.Tables[0].Rows.Count > 0)
        {
            dlstType.DataSource = ds.Tables[0].DefaultView;
            dlstType.DataBind();
        }
    }
    protected void dlstType_CancelCommand(object source, DataListCommandEventArgs e)
    {
        dlstType.EditItemIndex = -1;
        Bind();
    }
    protected void dlstType_DeleteCommand(object source, DataListCommandEventArgs e)
    {
        int id = Convert.ToInt32(dlstType.DataKeys[e.Item.ItemIndex]);
        etw.intention_news inlead = new etw.intention_news();
        etw.etw_Intention lead = new etw.etw_Intention();
        lead.id = id;
        lead.GetModel();
        if (lead.usernameid == 0)
        {

        }
        else
        {
            lead.Delete();
        }
        Fun.JsAlerT("删除成功", "Intentionadd.aspx");
    }
    protected void dlstType_EditCommand(object source, DataListCommandEventArgs e)
    {
        dlstType.EditItemIndex = e.Item.ItemIndex;
        Bind();
    }
    protected void dlstType_UpdateCommand(object source, DataListCommandEventArgs e)
    {
        int id = Convert.ToInt32(dlstType.DataKeys[e.Item.ItemIndex]);
        string name = ((TextBox)e.Item.FindControl("txtName")).Text;
        etw.intention_news inlead = new etw.intention_news();
        etw.etw_Intention lead = new etw.etw_Intention();
        lead.id = id;
        lead.GetModel();
        if (lead.usernameid == 0)
        {
            DataSet ds = DbHelperSQL.Query("select id from intention_news where Intenid=" + id + " and usernameid=" + usernameid + "");
            if (ds.Tables[0].Rows.Count > 0)
            {
                inlead.id = Convert.ToInt32(ds.Tables[0].Rows[0]["id"].ToString());
                inlead.name = name;
                inlead.Upname();
                dlstType.EditItemIndex = -1;
                Fun.JsAlerT("修改成功", "Intentionadd.aspx");
            }
            else
            {
                inlead.name = name;
                inlead.Intenid = Convert.ToInt32(id);
                inlead.usernameid = Convert.ToInt32(usernameid);
                inlead.Add();
                dlstType.EditItemIndex = -1;
                Fun.JsAlerT("修改成功", "Intentionadd.aspx");
            }
        }
        else
        {
            lead.name = name;
            lead.Upname();
            dlstType.EditItemIndex = -1;
            Fun.JsAlerT("修改成功", "Intentionadd.aspx");
        }
    }
    protected void btnSave_Click(object sender, EventArgs e)
    {
        if (txtname.Text != "")
        {
            etw.etw_Intention lead = new etw.etw_Intention();
            lead.name = Fun.Encode(txtname.Text);
            lead.usernameid = Convert.ToInt32(usernameid);
            lead.Add();
            Response.Write("<script language=\"javascript\">window.location='Intentionadd.aspx'</" + "script>");
        }
    }
    public string Cutname(string Iid,string Iname)
    {
        string namestr = Iname;
        DataSet ds = DbHelperSQL.Query("select name from intention_news where usernameid=" + usernameid + " and Intenid=" + Iid + "");
        if (ds.Tables[0].Rows.Count > 0)
        {
            namestr = ds.Tables[0].Rows[0]["name"].ToString();
        }
        return namestr;
    }
    public string CutVisible(string Iid)
    {
        string visiblestr = string.Empty;
        DataSet ds = DbHelperSQL.Query("select usernameid from etw_Intention where id=" + Iid + "");
        if (ds.Tables[0].Rows.Count > 0)
        {
            if (ds.Tables[0].Rows[0]["usernameid"].ToString() != "0")
            {
                visiblestr = "【<a href=\"Intentdel.aspx?Iid=" + Iid + "\" onclick=\"return confirm('真的要删除选中项吗？');\">删除</a>】";
            }
        }
        return visiblestr;
    }
}

