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 admin_ad : Fm.Bussiness.BasePage
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            Fm.Bussiness.public_select ps = new Fm.Bussiness.public_select();
            Drop_adclass.DataSource = ps.Getps("Fm_adclass", "*", "");
            Drop_adclass.DataTextField = "acname";
            Drop_adclass.DataValueField = "acid";
            Drop_adclass.DataBind();
            edittab.Visible = false;
           
        }
        GetPage("");
    }
    //查询数据并且分页
    void GetPage(string where)
    {
        FormPaging1.TableName = "Fm_adclass_ad";
        FormPaging1.ColName = "adid";
        FormPaging1.Where = where;
        FormPaging1.Gv = GridView_ad;
    }
    protected void btn_save_Click(object sender, EventArgs e)
    {
        string path = Server.MapPath("../");
        DateTime time = DateTime.Now;

        string newpath = "/upload/ad/" + time.Year.ToString() + time.Month.ToString() + time.Day.ToString() + time.Hour.ToString() + time.Minute.ToString() + time.Second.ToString() + System.IO.Path.GetExtension(FileUpload1.FileName);
        FileUpload1.SaveAs(path + newpath);

        txt_pic.Text = newpath;
    }
    protected void ImageButton1_Click(object sender, ImageClickEventArgs e)
    {
        if (Drop_adclass.Items.Count > 0)
        {
            Fm.EntityClass.ad ad = new Fm.EntityClass.ad();
            ad.acid = int.Parse(Drop_adclass.SelectedValue);
            ad.adletter = txt_letter.Text;
            ad.adpic = txt_pic.Text;
            ad.adjs = txt_js.Text;
            ad.adurl = txt_url.Text;
            ad.adid = int.Parse(adid.Value);
            Fm.Bussiness.ad add = new Fm.Bussiness.ad();
            add.Update(ad);

            Response.Redirect("Erro.aspx?eid=广告信息编辑完成!&url=ad.aspx", false);
        }
        else
        {
            Response.Write("<script>alert('请先添加广告分类!');</script>");
        }
    }
    //浏览
    protected void GridView_ad_SelectedIndexChanging(object sender, GridViewSelectEventArgs e)
    {
        Fm.Bussiness.public_select ps = new Fm.Bussiness.public_select();
        DataView dv=ps.Getps("Fm_ad","*","adid="+GridView_ad.Rows[e.NewSelectedIndex].Cells[0].Text);
        //启用TAB
        edittab.Visible = true;
        //禁掉所有操作功能
        btn_save.Visible = false;
        FileUpload1.Visible = false;
        ImageButton1.Visible = false;
        Drop_adclass.Enabled = false;
        foreach (ListItem item in Drop_adclass.Items)
        {
            if (item.Value == dv[0]["acid"].ToString())
            {
                item.Selected = true;
            }
        }
        txt_letter.Enabled = false;
        txt_letter.Text = dv[0]["adletter"].ToString();
        txt_pic.Enabled = false;
        txt_pic.Text = dv[0]["adpic"].ToString();
        txt_js.Enabled = false;
        txt_js.Text = dv[0]["adjs"].ToString();
        txt_url.Enabled = false;
        txt_url.Text = dv[0]["adurl"].ToString();
    }
    //删除
    protected void GridView_ad_RowDeleting(object sender, GridViewDeleteEventArgs e)
    {
        Fm.Bussiness.public_select ps = new Fm.Bussiness.public_select();
        if (ps.Queryps("delete from Fm_ad where adid=" + GridView_ad.Rows[e.RowIndex].Cells[0].Text))
        {
            Response.Redirect("Erro.aspx?eid=该广告删除成功!&url=ad.aspx", false);
        }
    }
    //编辑
    protected void GridView_ad_RowEditing(object sender, GridViewEditEventArgs e)
    {
        //启用TAB
        edittab.Visible = true;

        Fm.Bussiness.public_select ps = new Fm.Bussiness.public_select();
        DataView dv = ps.Getps("Fm_ad", "*", "adid=" + GridView_ad.Rows[e.NewEditIndex].Cells[0].Text);
        
        //禁掉所有操作功能
        adid.Value = dv[0]["adid"].ToString();
        btn_save.Visible = true;
        FileUpload1.Visible = true;
        ImageButton1.Visible = true;
        Drop_adclass.Enabled = true;
        foreach (ListItem item in Drop_adclass.Items)
        {
            if (item.Value == dv[0]["acid"].ToString())
            {
                item.Selected = true;
            }
        }
        txt_letter.Enabled = true;
        txt_letter.Text = dv[0]["adletter"].ToString();
        txt_pic.Enabled = true;
        txt_pic.Text = dv[0]["adpic"].ToString();
        txt_js.Enabled = true;
        txt_js.Text = dv[0]["adjs"].ToString();
        txt_url.Enabled = true;
        txt_url.Text = dv[0]["adurl"].ToString();
    }
   
}
