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 HostManage_AddHost : System.Web.UI.Page
{
    HostManage hostmanage = new HostManage();
    ModuleManage modulemanage = new ModuleManage();
    ImageManage imagemanage = new ImageManage();
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            ddlModule.DataSource = modulemanage.GetAllModule("tb_Module").Tables[0].DefaultView;
            ddlModule.DataTextField = "版块名称";
            ddlModule.DataBind();
            ddlPhoto.DataSource = imagemanage.GetAllPhoto("tb_Image").Tables[0].DefaultView;
            ddlPhoto.DataTextField = "编号";
            ddlPhoto.DataBind();
            imagemanage.PhotoID = ddlPhoto.SelectedValue;
            imgPhoto.ImageUrl = imagemanage.FindPhotoByID(imagemanage, "tb_Image").Tables[0].Rows[0][1].ToString();
        }
    }
    protected void btnTest_Click(object sender, EventArgs e)
    {
        if (txtName.Text == string.Empty)
        {
            Response.Write("<script language=javascript>alert('版主名不能为空！')</script>");
        }
        else
        {
            hostmanage.HostName = txtName.Text;
            DataSet ds = hostmanage.FindHostByName(hostmanage, "tb_Host");
            if (ds.Tables[0].Rows.Count > 0)
            {
                Response.Write("<script language=javascript>alert('该版主已经存在！')</script>");
                txtName.Text = string.Empty;
                txtName.Focus();
            }
            else
                Response.Write("<script language=javascript>alert('您可以添加该版主！')</script>");
        }
    }
    protected void btnSelDate_Click(object sender, EventArgs e)
    {
        calDate.Visible = true;
    }
    protected void calDate_SelectionChanged(object sender, EventArgs e)
    {
        txtBirthday.Text = calDate.SelectedDate.ToShortDateString();
        calDate.Visible = false;
    }
    protected void ddlPhoto_SelectedIndexChanged(object sender, EventArgs e)
    {
        imagemanage.PhotoID = ddlPhoto.SelectedValue;
        imgPhoto.ImageUrl = imagemanage.FindPhotoByID(imagemanage, "tb_Image").Tables[0].Rows[0][1].ToString();
    }
    protected void btnAdd_Click(object sender, EventArgs e)
    {
        if (txtName.Text == string.Empty)
        {
            Response.Write("<script language=javascript>alert('版主名不能为空！')</script>");
        }
        else
        {
            hostmanage.HostName = txtName.Text;
            DataSet ds = hostmanage.FindHostByName(hostmanage, "tb_Host");
            if (ds.Tables[0].Rows.Count > 0)
            {
                Response.Write("<script language=javascript>alert('该版主已经存在！')</script>");
                txtName.Text = string.Empty;
                txtName.Focus();
            }
            else
            {
                modulemanage.ModuleName = ddlModule.SelectedValue;
                hostmanage.ModuleID = modulemanage.FindModuleByName(modulemanage, "tb_Module").Tables[0].Rows[0][0].ToString();
                hostmanage.HostPwd = txtPwd.Text;
                hostmanage.TName = txtTName.Text;
                if (ddlSex.SelectedIndex == 0)
                    hostmanage.Sex = true;
                if (ddlSex.SelectedIndex == 1)
                    hostmanage.Sex = false;
                hostmanage.Birthday = DateTime.Parse(txtBirthday.Text);
                hostmanage.Tel = txtTel.Text;
                hostmanage.Mobile = txtMobile.Text;
                hostmanage.QQ = Int32.Parse(txtQQ.Text);
                imagemanage.PhotoID = ddlPhoto.SelectedValue;
                hostmanage.Photo = imagemanage.FindPhotoByID(imagemanage, "tb_Image").Tables[0].Rows[0][1].ToString();
                hostmanage.Email = txtEmail.Text;
                hostmanage.FAddress = txtHAddress.Text;
                hostmanage.RAddress = txtRAddress.Text;
                hostmanage.Index = txtIndex.Text;
                hostmanage.AddHost(hostmanage);
                Response.Write("<script language=javascript>alert('版主添加成功！')</script>");
                txtName.Text = txtPwd.Text = txtSPwd.Text = txtTName.Text = txtBirthday.Text = txtTel.Text
                    = txtMobile.Text = txtQQ.Text = txtEmail.Text = txtHAddress.Text = txtRAddress.Text = txtIndex.Text = string.Empty;
            }
        }
    }
    protected void btnCancel_Click(object sender, EventArgs e)
    {
        txtName.Text = txtPwd.Text = txtSPwd.Text = txtTName.Text = txtBirthday.Text = txtTel.Text
            = txtMobile.Text = txtQQ.Text = txtEmail.Text = txtHAddress.Text = txtRAddress.Text = txtIndex.Text = string.Empty;
    }
}
