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;
using worm;

public partial class wormadmin_pic : System.Web.UI.Page
{
    sqloper sqloper = new sqloper();
    SqlConnection one = conn.sqlconn();
    string connect_sql = "";       //搜索的sql合拼语句
    int i = 0;
    int tempid=0;
    protected void Page_PreInit(object sender, EventArgs e)
    {
        if (Session["superad"] == null || Session["superpw"] == null)
        {
            Response.Redirect("login.aspx");
        }
        else
        {
            Page.Theme = "SkinFile";
        }
    }
    protected void Page_Load(object sender, EventArgs e)
    {
        connect_sql = tsql.Text;
        if (!IsPostBack)
        {
            if (Request.QueryString["search"] == "1")
            {
                connect_sql = req();
                tsql.Text = connect_sql;
            }
            gvbind("select * from pic " + connect_sql + "  order by id desc", one, myGrid, AspNetPager1.PageSize); 
        }

    }

    //接受搜索页面的值,然后进行sql语句合拼处理
    protected string req()
    {
        string reqstr = "";
        string txttitle = Request["txttitle"];
        string txtclass = Request["txtclass"];
        string bit1 = Request["bit1"];
        string bit2 = Request["bit2"];
        string date1 = Request["date1"];
        string date2 = Request["date2"];
        int cktop;
        if (Request["cktop"] == null)
        {
            cktop = 0;
        }
        else
        {
            cktop = 1;
        }
        string temptitle = Request["temptitle"];
        append_sql apsql = new append_sql();
        reqstr = apsql.sql_str(temptitle, txttitle, txtclass, bit1, bit2, date1,date2, cktop);
        return reqstr;
    }

    //短日期处理
    protected string short_time(object times)
    {
        stringdo shortime = new stringdo();
        return shortime.short_time(times);
    }

    //数据绑定GridView
    //sql:数据库查询语句,conn:数据库连接,gvid:gridview控件的id,pssize:一页中数据的多少
    protected void gvbind(string sql, SqlConnection conn, GridView gvid, int pssize)
    {
            SqlDataAdapter adapter = new SqlDataAdapter(sql, conn);
            DataSet ds = new DataSet();
            PagedDataSource ps = new PagedDataSource();
            try
            {
                conn.Open();
                adapter.Fill(ds, "order");
                ps.DataSource = ds.Tables["order"].DefaultView;              //ps分页数据源设置
                ps.AllowPaging = true;                                       //允许分页
                ps.PageSize = pssize;                                       //页面数据大小
                ps.CurrentPageIndex = AspNetPager1.CurrentPageIndex - 1;    //当前页面指针
                AspNetPager1.RecordCount = ds.Tables["order"].Rows.Count;  //记录总行数赋值给控件AspNetPager1

                gvid.DataSource = ps;
                gvid.DataBind();

            }
            catch (System.Exception ee)
            {
                Response.Write(ee.Message.ToString());
            }
            finally
            {
                ds.Dispose();
                adapter.Dispose();
                conn.Close();
            }
            if (myGrid.Rows.Count > 0)
            {
                ((TextBox)myGrid.FooterRow.FindControl("add_pbit")).Text = "0";
                ((TextBox)myGrid.FooterRow.FindControl("add_pdate")).Text = DateTime.Now.ToShortDateString();
                dlbing();
            }
            else
            {
                add_pbit.Text = "0";
                add_pdate.Text = DateTime.Now.ToShortDateString();
                dlbing();
            }
    }


    //gridview编辑链接
    protected void myedit(object sender, GridViewEditEventArgs e)
    {
        //用NewEidIndex取得当前编辑的行号，然后获取gridviewrow对象

        myGrid.EditIndex = e.NewEditIndex;
        gvbind("select * from pic " + connect_sql + "  order by id desc", one, myGrid, AspNetPager1.PageSize);
        try
        {
            one.Open();
            string sql = "select distinct pclass from picclass";
            SqlCommand cmd = new SqlCommand(sql, one);
            SqlDataReader rs = cmd.ExecuteReader();
            int i = 0;
            while (rs.Read())
            {

                ((DropDownList)myGrid.Rows[e.NewEditIndex].FindControl("dlclass")).Items.Add(new ListItem(rs["pclass"].ToString(), rs["pclass"].ToString()));
                if (((TextBox)myGrid.Rows[e.NewEditIndex].FindControl("tempclass")).Text == rs["pclass"].ToString())
                {
                    ((DropDownList)myGrid.Rows[e.NewEditIndex].FindControl("dlclass")).SelectedIndex = i;
                }
                i = i + 1;
            }
        }
        catch (System.Exception ee)
        {
            Response.Write(ee.Message.ToString());
        }
        finally
        {
            one.Close();
        }
    }

    //图片分类下拉菜单的数据绑定
    protected void dlbing()
    {
        try
        {
            one.Open();
            string sql = "select distinct pclass from picclass";
            SqlCommand cmd = new SqlCommand(sql, one);
            SqlDataReader rs = cmd.ExecuteReader();
            int i = 0;
            if (myGrid.Rows.Count > 0)
            {
                while (rs.Read())
                {
                    ((DropDownList)myGrid.FooterRow.FindControl("add_class")).Items.Add(new ListItem(rs["pclass"].ToString(), rs["pclass"].ToString()));
                }
            }
            else
            {
                while (rs.Read())
                {
                    add_class.Items.Add(new ListItem(rs["pclass"].ToString(), rs["pclass"].ToString()));
                }
            }
            cmd.Dispose();
            rs.Dispose();
        }
        catch (System.Exception ee)
        {
            Response.Write(ee.Message.ToString());
        }
        finally
        {
            one.Close();
        }
    }

    //gridview更新数据事件
    protected void myupdate(object sender, GridViewUpdateEventArgs e)
    {
        
        string wr_pclass = ((DropDownList)myGrid.Rows[e.RowIndex].FindControl("dlclass")).SelectedValue.ToString();
        string wr_pname = ((TextBox)myGrid.Rows[e.RowIndex].FindControl("pname")).Text;
        int wr_pbit = Convert.ToInt16(((TextBox)myGrid.Rows[e.RowIndex].FindControl("pbit")).Text);
        string wr_pdate = ((TextBox)myGrid.Rows[e.RowIndex].FindControl("pdate")).Text;
        int wr_ptop = Convert.ToInt16(((CheckBox)myGrid.Rows[e.RowIndex].FindControl("top")).Checked);

        string sql = "update pic set pclass='" + wr_pclass + "',pname='" + wr_pname + "',pbit=" + wr_pbit + ",pdate='" + wr_pdate + "',ptop=" + wr_ptop + " where id=" + myGrid.DataKeys[e.RowIndex].Value;
        sqloper.dodata(sql, one);
        myGrid.EditIndex = -1;
        ClientScript.RegisterStartupScript(GetType(), "Message", "<SCRIPT LANGUAGE='javascript'>alert('更新成功!');</script>");
        gvbind("select * from pic " + connect_sql + "  order by id desc", one, myGrid, AspNetPager1.PageSize);

    }

    //gridview取消编辑事件
    protected void mycancel(object sender, GridViewCancelEditEventArgs e)
    {
        myGrid.EditIndex = -1;
        gvbind("select * from pic " + connect_sql + "  order by id desc", one, myGrid, AspNetPager1.PageSize);
    }

    //翻页数据绑定事件
    protected void pagego(object sender, EventArgs e)
    {
        gvbind("select * from pic " + connect_sql + "  order by id desc", one, myGrid, AspNetPager1.PageSize);
    }

    //选中删除按钮事件
    protected void btdel_Click(object sender, EventArgs e)
    {
        int i;
        string sql;
        string sql2;
        string myKeyid;
        CheckBox mycheck = new CheckBox();
        for (i = 0; i < myGrid.Rows.Count; i++)
        {
            mycheck = (CheckBox)myGrid.Rows[i].FindControl("del");
            if (mycheck.Checked == true)
            {
                myKeyid = myGrid.DataKeys[i].Value.ToString();
                string delfile = sqloper.loaddb("select * from pic where id=" + myKeyid, "ppath");
                string delfile2 = sqloper.loaddb("select * from pic where id=" + myKeyid, "tpath");
                //删除pic表的记录
                sql = "DELETE FROM pic where id=" + myKeyid;
                sqloper.dodata(sql, one);
                //把图片物理删除
                myupimg files = new myupimg();
                files.DeleteFile(delfile);
                files.DeleteFile(delfile2);
            }
        }
        gvbind("select * from pic " + connect_sql + "  order by id desc", one, myGrid, AspNetPager1.PageSize);
    }

    protected void delall_CheckedChanged(object sender, EventArgs e)
    {
        foreach (GridViewRow gvr in myGrid.Rows)
        {
            CheckBox ch = (CheckBox)gvr.FindControl("del");
            if (((CheckBox)myGrid.HeaderRow.FindControl("delall")).Checked == true)
            {
                ch.Checked = true;
            }
            else
            {
                ch.Checked = false;
            }
        }

    }
    protected void Button1_Click(object sender, EventArgs e)
    {
        foreach (GridViewRow gvr in myGrid.Rows)
        {
            CheckBox ch = (CheckBox)gvr.FindControl("del");
            if (ch.Checked == false)
            {
                ((CheckBox)myGrid.HeaderRow.FindControl("delall")).Checked = true;
                ch.Checked = true;
            }
            else
            {
                ((CheckBox)myGrid.HeaderRow.FindControl("delall")).Checked = false;
                ch.Checked = false;
            }
        }
    }

    #region 图片添加操作-----------------------------------------------------------------
    //上传图片的单击事件
    protected void up_Click(object sender, EventArgs e)
    {
        myupimg up = new myupimg();
        string nowdate=DateTime.Now.ToShortDateString();
        if (myGrid.Rows.Count > 0)
        {
            up._upfilecontrol = ((FileUpload)myGrid.FooterRow.FindControl("fileup"));
        }
        else
        {
            up._upfilecontrol = ((FileUpload)fileup);
        }
        up._topath = "..\\UpLoadFile\\pic\\" + nowdate;
        up._tWidth = 80;
        up._tHeight = 80;
        up._setting = 3;
        string imgpath = up.upfile();
        string[] fenpath = imgpath.Split('|');
        string wr_tpath = fenpath[1];
        string wr_ppath = fenpath[0];
        if (myGrid.Rows.Count > 0)
        {
            ((Image)myGrid.FooterRow.FindControl("upimg")).ImageUrl = wr_tpath;
            ((Image)myGrid.FooterRow.FindControl("upimg2")).ImageUrl = wr_ppath;
            ((Image)myGrid.FooterRow.FindControl("upimg")).Visible = true;
            //ClientScript.RegisterStartupScript(GetType(), "Message", "<SCRIPT LANGUAGE='javascript'>alert('上传成功!');</script>");
        }
        else
        {
            upimg.ImageUrl = wr_tpath;
            upimg2.ImageUrl = wr_ppath;
            upimg.Visible = true;
        }
    }

    //添加图片数据库项的事件
    protected void insert_pic(string ppath,string tpath)
    {
        string wr_pclass;
        string wr_pname;
        int wr_pbit;
        string wr_pdate;
        int wr_ptop;

        if (myGrid.Rows.Count > 0)
        {
             wr_pclass = ((DropDownList)myGrid.FooterRow.FindControl("add_class")).SelectedValue.ToString();
             wr_pname = ((TextBox)myGrid.FooterRow.FindControl("add_pname")).Text;
             wr_pbit = Convert.ToInt16(((TextBox)myGrid.FooterRow.FindControl("add_pbit")).Text);
             wr_pdate = ((TextBox)myGrid.FooterRow.FindControl("add_pdate")).Text;
             wr_ptop = Convert.ToInt16(((CheckBox)myGrid.FooterRow.FindControl("add_top")).Checked);
        }
        else
        {
             wr_pclass = add_class.SelectedValue.ToString();
             wr_pname = add_pname.Text;
             wr_pbit = Convert.ToInt16(add_pbit.Text);
             wr_pdate = add_pdate.Text;
             wr_ptop = Convert.ToInt16(add_top.Checked);
        }
        string wr_tpath = tpath;
        string wr_ppath = ppath;
        string sql = "insert into pic values('" + wr_pclass + "','" + wr_pname + "'," + wr_pbit + ",'" + wr_ppath + "','" + wr_tpath +"','"+wr_pdate+"',"+wr_ptop+")";
        sqloper.dodata(sql, one);
    }

    //实行添加图片数据库项操作,重新绑定pic表
    protected void add_do_Click(object sender, EventArgs e)
    {
        string img;
        string img2;
        if (myGrid.Rows.Count > 0)
        {
             img = ((Image)myGrid.FooterRow.FindControl("upimg")).ImageUrl.ToString();
             img2 = ((Image)myGrid.FooterRow.FindControl("upimg2")).ImageUrl.ToString();
        }
        else
        {
             img = upimg.ImageUrl.ToString();
             img2 = upimg2.ImageUrl.ToString();
        }
        insert_pic(img2,img);
        gvbind("select * from pic " + connect_sql + "  order by id desc", one, myGrid, AspNetPager1.PageSize);
        ClientScript.RegisterStartupScript(GetType(), "Message", "<SCRIPT LANGUAGE='javascript'>alert('添加成功!');</script>");
    }
    #endregion----------------------------------------------------------------------------------
}
