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

public partial class AccountType_ExpenditureTypeInfo : PageBase
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if(!this.Page.IsPostBack)
        {
            BindPage();
        }
    }
    #region 功能列表
    protected void btnExpenditureType_Click(object sender, EventArgs e)//支出类型
    {
        Response.Redirect("ExpenditureType.aspx");
    }
    protected void btnIncomeType_Click(object sender, EventArgs e)//收入类型
    {
        Response.Redirect("IncomeType.aspx");
    }
    protected void btnProjectType_Click(object sender, EventArgs e)//项目
    {
        Response.Redirect("ProjectType.aspx");
    }
    protected void btnSeller_Click(object sender, EventArgs e)//商家/付款方
    {
        Response.Redirect("Seller.aspx");
    }
    #endregion
    /// <summary>
    /// 绑定页面数据
    /// </summary>
    public void BindPage()
    {
        if (!string.IsNullOrEmpty(Request["etid"]))
        {
            DataSet ds = SqlHelper.GetDataSet("SELECT ExpenditureTypeName FROM dbo.tabA_Type_Expenditure WHERE ExpenditureTypeId=" + ConvertHelper.GetInteger(Request["etid"]) + " AND State=1", "constring");
            if(ds.Tables[0].Rows.Count>0)
            {
                txtExpenditureTypeName.Text = ConvertHelper.GetString(ds.Tables[0].Rows[0]["ExpenditureTypeName"]);//支出类型名称
            }
        }
    }
    protected void btnSubmit_Click(object sender, EventArgs e)
    {
        if (!string.IsNullOrEmpty(Request["etid"]))//修改支出类型
        {
            string expenditureTypeName = "";//支出类型名
            DataSet dsExpenditureType = SqlHelper.GetDataSet("SELECT ExpenditureTypeName FROM dbo.tabA_Type_Expenditure WHERE ExpenditureTypeId=" + ConvertHelper.GetInteger(Request["etid"]) + " AND State=1", "constring");
            if (dsExpenditureType.Tables[0].Rows.Count > 0)
            {
                expenditureTypeName = ConvertHelper.GetString(dsExpenditureType.Tables[0].Rows[0]["ExpenditureTypeName"]);//支出类型名称
            }
            if (txtExpenditureTypeName.Text.Trim() != expenditureTypeName)
            {
                DataSet ds = SqlHelper.GetDataSet("SELECT ExpenditureTypeId FROM dbo.tabA_Type_Expenditure WHERE State=1 AND ExpenditureTypeName='" + txtExpenditureTypeName.Text.Trim() + "'", "constring");
                if (ds.Tables[0].Rows.Count <= 0)//检查支出类型是否存在
                {
                    int flag = SqlHelper.Operation("UPDATE dbo.tabA_Type_Expenditure SET ExpenditureTypeName='" + txtExpenditureTypeName.Text.Trim() + "' WHERE ExpenditureTypeId=" + ConvertHelper.GetInteger(Request["etid"]) + "", "constring");
                    if (flag > 0)
                    {
                        Response.Redirect("ExpenditureType.aspx");
                    }
                    else
                    {
                        Page.ClientScript.RegisterClientScriptBlock(this.GetType(), "", "<script language='javascript' defer>alert('修改失败，请联系管理员')</script>");
                    }
                }
                else
                {
                    ScriptHelper.AlertMessage(this.Page, "该支出类型已存在！");
                    BindPage();
                }
            }
            else//支出类型未做出修改
            {
                Response.Redirect("ExpenditureType.aspx");
            }
        }
        else//添加支出类型
        {
            DataSet ds = SqlHelper.GetDataSet("SELECT ExpenditureTypeId FROM dbo.tabA_Type_Expenditure WHERE State=1 AND ExpenditureTypeName='" + txtExpenditureTypeName.Text.Trim() + "'", "constring");
            if (ds.Tables[0].Rows.Count > 0)//检查支出类型是否存在
            {
                ScriptHelper.AlertMessage(this.Page, "该支出类型已存在！");
                txtExpenditureTypeName.Text = "";
            }
            else
            {
                int flag = SqlHelper.Operation("INSERT INTO dbo.tabA_Type_Expenditure(ExpenditureTypeName,CreateTime) VALUES('" + txtExpenditureTypeName.Text.Trim() + "','" + System.DateTime.Now + "')", "constring");
                if (flag > 0)
                {
                    Response.Redirect("ExpenditureType.aspx");
                }
                else
                {
                    Page.ClientScript.RegisterClientScriptBlock(this.GetType(), "", "<script language='javascript' defer>alert('添加失败，请联系管理员！')</script>");
                }
            }
        }
    }

    protected void btnCancel_Click(object sender, EventArgs e)//取消事件
    {
        Response.Redirect("ExpenditureType.aspx");
    }
}
