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;

namespace JuSNS.Web.home.bill
{
    public partial class create : JuSNS.UI.Page.UserPage
    {
        protected string r = string.Empty;
        JuSNS.Home.Bill bll = new JuSNS.Home.Bill();
        protected string isp = string.Empty;
        protected void Page_Load(object sender, EventArgs e)
        {
            GetIsNotEmail();
            if (!IsPostBack)
            {
                r = Request.QueryString["r"];
                string a = Request.QueryString["a"];
                if (a == "edit")
                {
                    if (!JuSNS.Common.Input.IsInteger(r))
                    {
                        PageError("错误的参数", rooDir + "/home/bill/?r=my");
                    }
                    IDataReader dr = bll.GetBillInfo(UserID, Convert.ToInt32(r));
                    if (dr.Read())
                    {
                        this.bName.Text = dr["bName"].ToString();
                        this.bmoney.Text = Convert.ToDouble(dr["bmoney"].ToString()).ToString();
                        this.b_BillTime.Text = DateTime.Parse(dr["BillTime"].ToString()).ToString("yyyy-MM-dd");
                        this.content.Text = dr["content"].ToString();
                        GetClassList(dr["classID"].ToString());
                        GetFamilyList(dr["fid"].ToString());
                        isp = dr["isPub"].ToString();
                        for (int k = 0; k < this.isPub.Items.Count; k++)
                        {
                            if (this.isPub.Items[k].Value == isp)
                            {
                                this.isPub.Items[k].Selected = true;
                            }
                        }
                        string btype = dr["btype"].ToString();
                        for (int i= 0; i < this.btype.Items.Count; i++)
                        {
                            if (this.btype.Items[i].Value == btype)
                            {
                                this.btype.Items[i].Selected = true;
                            }
                        }
                    }
                    dr.Close();
                    this.bID.Value = r;
                }
                else
                {
                    GetClassList("0");
                    GetFamilyList("0");
                    this.b_BillTime.Text = DateTime.Now.ToShortDateString();
                    this.bID.Value = "0";
                }
            }
        }

        public void GetClassList(string ID)
        {
            IDataReader dr = bll.GetClassList(UserID);
            while (dr.Read())
            {
                ListItem it = new ListItem();
                string stxt = "";
                it.Value = dr["ID"].ToString();
                it.Text = stxt + dr["className"].ToString();
                if (ID == dr["ID"].ToString())
                {
                    it.Selected = true;
                }
                this.classID.Items.Add(it);
            }
            dr.Close();
        }

        public void GetFamilyList(string ID)
        {
            IDataReader dr = bll.GetFamilyList(UserID);
            while (dr.Read())
            {
                ListItem it = new ListItem();
                string stxt = "";
                it.Value = dr["ID"].ToString();
                it.Text = stxt + dr["fName"].ToString();
                if (ID == dr["ID"].ToString())
                {
                    it.Selected = true;
                }
                this.fid.Items.Add(it);
            }
            dr.Close();
        }

        protected void send_Click(object sender, EventArgs e)
        {
            if (Page.IsValid)                       //判断是否验证成功
            {
                int bID = Convert.ToInt32(this.bID.Value);
                string bName = this.bName.Text;
                int bType =Convert.ToInt16(this.btype.SelectedValue);
                double bmoney = Convert.ToDouble(this.bmoney.Text);
                int ClassID = Convert.ToInt32(Request.Form["classID"]);
                int fID = Convert.ToInt32(this.fid.SelectedValue);
                string content = this.content.Text;
                int isPub = Convert.ToInt16(this.isPub.SelectedValue);
                DateTime BillTime = DateTime.Now;
                if (!JuSNS.Common.Input.IsDate(this.b_BillTime.Text))
                {
                    this.succ_div.Visible = false;
                    this.eorror_div.Visible = true;
                    this.eorror_div.InnerHtml = "收入或支出时间必须为正确的日期格式！格式为：2008-10-10";
                    return;
                }
                else
                {
                    BillTime = DateTime.Parse(this.b_BillTime.Text);
                }
                if (bll.addBill(bID, UserID, bName, bType, bmoney,BillTime, ClassID, fID, content, isPub) > 0)
                {
                    PageRight("保存记账成功！", rooDir + "/home/bill/?r=my");
                }
                else
                {
                    this.succ_div.Visible =false ;
                    this.eorror_div.Visible = true;
                    this.eorror_div.InnerHtml = "保存记账发生错误！";
                    return;
                }
            }
        }
    }
}
