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;
using System.Data.SqlClient;

public partial class WTOSYSadmin_news_piclinkedit : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (Session["WtoRightName"] == null)
        {
            Response.Redirect("~/SYSADMIN/main/error.aspx?err=非法登陆!或系统超时，请刷新后重新登录");
        }
            switch (Request.QueryString["doing"])
            {
                case "del":
                    if (Doadmin.Exec("delete from link where id=" + Request.QueryString["id"]))
                    {
                        Response.Write("<script>alert('删除成功');location.href('piclink.aspx')</script>");
                    }
                    else
                    {
                        Response.Write("<script>alert('此次操作失败');</script>");
                    }
                    break;
                case "open":
                    if (Doadmin.Exec("update link set isopen='y' where id=" + Request.QueryString["id"]))
                    {
                        Response.Write("<script>alert('操作成功');location.href('piclink.aspx')</script>");
                    }
                    else
                    {
                        Response.Write("<script>alert('此次操作失败');</script>");
                    }
                    break;
                case "close":
                    if (Doadmin.Exec("update link set isopen='n' where id=" + Request.QueryString["id"]))
                    {
                        Response.Write("<script>alert('操作成功');location.href('piclink.aspx')</script>");
                    }
                    else
                    {
                        Response.Write("<script>alert('此次操作失败');</script>");
                    }
                    break;
                case "edit":
                    if (!IsPostBack)
                    {
                        SqlConnection conn = CDatabase.wtoconn();
                        conn.Open();
                        SqlCommand smd = new SqlCommand("select * from link where id=" + Request.QueryString["id"], conn);
                        SqlDataReader sdr = smd.ExecuteReader();
                        sdr.Read();
                        if (sdr.GetString(3) == "y")
                        {
                            this.y.Checked = true;
                        }
                        else
                        {
                            this.n.Checked = true;
                        }
                        this.alt.Text = sdr.GetString(6);
                        this.href.Text = sdr.GetString(5);
                        this.Image1.ImageUrl = Server.MapPath("~/images/link/") + sdr.GetString(7).ToString().Trim();
                    }
                    this.Label1.Visible = true;
                    this.LinkButton3.Visible = false;
                    this.LinkButton1.Visible = true;
                    this.Label2.Visible = true;
                    this.Image1.Visible = true;
                    this.LinkButton1.Click += new EventHandler(LinkButton3_Click);
                    break;
            }
        
    }
    protected void LinkButton1_Click(object sender, EventArgs e)
    {
        string isfp, title, href;
        if(y.Checked)
        {
            isfp="y";
        }
        else
        {
             isfp="n";
        }
        href=this.href.Text;
        string picname="";
        string fpath2 = this.img1.PostedFile.FileName;
        string ftype2 = this.img1.PostedFile.ContentType;
        string[] type2 = ftype2.Split('/');
        if (type2[0].ToString() != "image")
        {
            Response.Write("文件格式错误！");
        }
        else
        {
            string S_hz = (fpath2.Substring(fpath2.LastIndexOf(".") + 1)).ToLower().Trim();
            picname = DateTime.Now.ToString("yyyyssssMMddssss")+"." +S_hz;
            string pt2 = Server.MapPath("~/images/link/")+picname;
            this.img1.SaveAs(pt2);
            this.img1.Dispose();
        }


        if (Doadmin.Exec("insert into link (type,name,isopen,href,alt,picurl,addtime) values ('1','图片链接','" + isfp + "','" + href + "','" + this.alt.Text + "','"+picname+"','" + DateTime.Now.ToString() + "')"))
        {
            Response.Write("<script>alert('添加成功');location.href('piclink.aspx')</script>");
        }
        else
        {
            Response.Write("<script>alert('此次操作失败');</script>");
        }
    }
    protected void LinkButton3_Click(object sender, EventArgs e)
    {
        string isfp, title, href,picre="";
        if (y.Checked)
        {
            isfp = "y";
        }
        else
        {
            isfp = "n";
        }
        href = this.href.Text;

        string fpath3 = this.img1.PostedFile.FileName;
        if (fpath3 != "")
        {
            string ftype3 = this.img1.PostedFile.ContentType;
            string[] type3 = ftype3.Split('/');
            if (type3[0].ToString() != "image")
            {
                Response.Write("文件格式错误！");
            }
            else
            {
                string S_hz = (fpath3.Substring(fpath3.LastIndexOf(".") + 1)).ToLower().Trim();
                picre = DateTime.Now.ToString("yyyyssssMMddssss") + "." + S_hz;
                string pt3 = Server.MapPath("~/images/link/") + picre;
                this.img1.SaveAs(pt3);
                this.img1.Dispose();
            }
            if (Doadmin.Exec("update link set type='1',name='图片链接',isopen='" + isfp + "',href='" + href + "',alt='" + this.alt.Text + "',picurl='"+picre+"',addtime='" + DateTime.Now.ToString() + "' where id=" + Request.QueryString["id"]))
            {
                Response.Write("<script>alert('发布成功');location.href('piclink.aspx')</script>");
            }
            else
            {
                Response.Write("<script>alert('此次操作失败');</script>");
            }
        }
        else
        {
            if (Doadmin.Exec("update link set type='1',name='图片链接',isopen='" + isfp + "',href='" + href + "',alt='" + this.alt.Text + "',addtime='" + DateTime.Now.ToString() + "' where id=" + Request.QueryString["id"]))
            {
                Response.Write("<script>alert('发布成功');location.href('piclink.aspx')</script>");
            }
            else
            {
                Response.Write("<script>alert('此次操作失败');</script>");
            }
        }
    }

}
