using System;
using System.Text;
using System.Data;
using System.Collections.Generic;
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 JuSNS.Common;
using JuSNS.Home;
using JuSNS.Model;
using JuSNS.Config;
using JuSNS.UI.Control;

namespace JuSNS.Web.home.bill
{
    public partial class index : JuSNS.UI.Page.UserPage
    {
        public string r = string.Empty;
        protected string uName = "朋友的";
        User us = new User();
        protected void Page_Load(object sender, EventArgs e)
        {
            string a = Request.QueryString["a"];
            if (a == "del")
            {
                string id = Request.QueryString["id"];
                Bill bll = new Bill();
                if (bll.DelBillInfo(UserID, Convert.ToInt32(id)) > 0)
                {
                    this.succ_div.Visible = true;
                    this.succ_div.InnerHtml = "帐务删除成功！";
                }
            }
            r = Request.QueryString["r"];
            if (!string.IsNullOrEmpty(r))
            {
                if (r == "my" || r == this.UserID.ToString())
                {
                    this.FriendBill.Attributes.Add("class", "");
                    this.MyBill.Attributes.Add("class", "current");
                    this.AllBill.Attributes.Add("class", "");
                    this.pNames.InnerHtml = "我的账本";
                    this.ptitle.InnerText = "我的账本";
                }
                else if (r == "all")
                {
                    this.FriendBill.Attributes.Add("class", "");
                    this.MyBill.Attributes.Add("class", "");
                    this.AllBill.Attributes.Add("class", "current");
                    this.pNames.InnerHtml = "所有人的账本";
                    this.ptitle.InnerText = "所有人的账本";
                }
                else if (JuSNS.Common.Input.IsInteger(r))
                {
                    UserInfo mdl = new UserInfo();
                    mdl = us.GetUserInfo(Convert.ToInt32(r));
                    uName = mdl.Username + "的";
                    this.FriendBill.Attributes.Add("class", "current");
                    this.MyBill.Attributes.Add("class", "");
                    this.AllBill.Attributes.Add("class", "");
                    this.pNames.InnerHtml = mdl.Username + "的账本";
                    this.ptitle.InnerText = mdl.Username + "的账本";
                }
            }
            else
            {
                this.FriendBill.Attributes.Add("class", "current");
                this.MyBill.Attributes.Add("class", "");
                this.AllBill.Attributes.Add("class", "");
                this.pNames.InnerHtml = "朋友们的账本";
                this.ptitle.InnerText = "朋友们的账本";
            }
            BillList();
        }

        protected void BillList()
        {
            Bill bll = new Bill();
            int ReCount, PgCount;
            int PageIndex = 1;
            if (Request.QueryString["Page"] != null)
            {
                PageIndex = Convert.ToInt32(Request.QueryString["Page"]);
            }
            int t = -1;
            if (JuSNS.Common.Input.IsInteger(Request.QueryString["t"]))
            {
                t = Convert.ToInt32(Request.QueryString["t"]);
            }
            if (string.IsNullOrEmpty(r))
            {
                r = "f";
            }
            int classID = 0;
            if (JuSNS.Common.Input.IsInteger(Request.QueryString["c"]))
            {
                classID = Convert.ToInt32(Request.QueryString["c"]);
            }
            DataTable dt = bll.BillPage(UserID, t, r, Request.QueryString["f"], classID, PageIndex, 20, out ReCount, out PgCount);
            string PageUrl = "../bill/?r=" + r + "&t=" + Request.QueryString["t"] + "&c=" + Request.QueryString["c"];
            DataTable data = new DataTable();
            data.Columns.Add("d", typeof(string));
            JuSNS.Home.Notepad np = new JuSNS.Home.Notepad();
            JuSNS.Home.User u = new JuSNS.Home.User();
            this.noBill1.Visible = true;
            this.noBill1.InnerHtml = ReCount + "个记录 <span class=\"pipe\">|</span> <a href=\"creat.aspx\">我要记账</a> <span class=\"pipe\">|</span>";
            for (int i = 0; i < dt.Rows.Count; i++)
            {
                StringBuilder sb = new StringBuilder();
                DataRow dr = data.NewRow();
                sb.Append("<div class=\"billtc\">");
                string cont = "<span id=\"s_" + dt.Rows[i]["ID"].ToString() + "\"><a href=\"javascript:showNoteContent('" + dt.Rows[i]["ID"].ToString() + "')\"><img src=\"../../template/" + JuSNS.Config.UiConfig.SkinStyle + "/images/site/note.gif\" title=\"点击查看详情\" align=\"absmiddle\" /></a></span>";
                string fstr = string.Empty;
                if (dt.Rows[i]["fid"].ToString() == "0")
                {
                    fstr = "<a href=\"index.aspx?r=" + r + "&t=" + t + "&f=" + dt.Rows[i]["fid"].ToString() + "\">我自己</a>";
                }
                else
                {
                    fstr = "<a href=\"index.aspx?r=" + r + "&t=" + t + "&f=" + dt.Rows[i]["fid"].ToString() + "\">" + GetF(Convert.ToInt32(dt.Rows[i]["fID"].ToString())) + "</a>";
                }

                if (dt.Rows[i]["UserID"].ToString() == UserID.ToString())
                {
                    sb.Append("<div class=\"name\"><a title=\"点击修改\" href=\"creat.aspx?a=edit&r=" + dt.Rows[i]["ID"].ToString() + "\">" + dt.Rows[i]["bname"].ToString() + "</a> " + cont + "</div>");
                    sb.Append("<div class=\"fname\">" + fstr + "</div>");
                }
                else
                {
                    sb.Append("<div class=\"name1 bold\">" + dt.Rows[i]["bname"].ToString() + " " + cont + "</div>");
                }
                string btype = dt.Rows[i]["bType"].ToString();
                if (btype == "0")
                {
                    btype = "<a href=\"../bill/?r=" + r + "&t=" + btype + "\">收入</a>";
                }
                else
                {
                    btype = "<span class=\"reshow\"><a href=\"../bill/?r=" + r + "&t=" + btype + "\">支出</a></span>";
                }
                sb.Append("<div class=\"money\">" + Convert.ToDouble(dt.Rows[i]["bmoney"].ToString()) + "</div>");
                sb.Append("<div class=\"billTime\">" + DateTime.Parse(dt.Rows[i]["billTime"].ToString()).ToString("yy-MM-dd") + "</div>");
                sb.Append("<div class=\"type\">" + btype + "</div>");
                string cid = dt.Rows[i]["ClassID"].ToString();
                sb.Append("<div class=\"sort\"><a href=\"../bill/?r=" + r + "&t=" + t + "&c=" + cid + "\">" + GetClass(Convert.ToInt32(cid)) + "</a></div>");
                if (dt.Rows[i]["UserID"].ToString() == UserID.ToString())
                {
                    sb.Append("<div class=\"posttime\">" + JuSNS.Common.Public.getTimeEXYearSpan(DateTime.Parse(dt.Rows[i]["CreatTime"].ToString())) + "</div>");
                    sb.Append("<div class=\"del\"><span class=\"delete1\"><a href=\"javascript:void(0);\" onclick=\"javascript:Confirm(this,'<ul><li>确定要删除吗？</li></ul>','删除流水',function(){window.location.href='index.aspx?r=" + r + "&id=" + dt.Rows[i]["ID"].ToString() + "&a=del'})\">删除</a></span></div>");
                }
                else
                {
                    sb.Append("<div class=\"posttime1\">" + JuSNS.Common.Public.getTimeEXYearSpan(DateTime.Parse(dt.Rows[i]["CreatTime"].ToString())) + "</div>");
                    //sb.Append("<div class=\"del\"></div>");
                }
                if (!string.IsNullOrEmpty(dt.Rows[i]["Content"].ToString()))
                {
                    sb.Append("<div id=\"c_" + dt.Rows[i]["ID"].ToString() + "\" style=\"display:block;margin-left:0px;\" class=\"cbbg nl\">" + dt.Rows[i]["Content"].ToString() + "</div>\n");
                }
                else
                {
                    sb.Append("<div id=\"c_" + dt.Rows[i]["ID"].ToString() + "\" style=\"display:none;margin-left:0px;\" class=\"cbbg nl\">" + dt.Rows[i]["Content"].ToString() + "</div>\n");
                }
                sb.Append("</div>");
                dr["d"] = sb.ToString();
                data.Rows.Add(dr);
            }
            dt.Clear(); dt.Dispose();
            this.R_BillList.Visible = true;
            this.R_BillList.DataSource = data;
            this.R_BillList.DataBind();
            PageUrl += "?r=" + r;
            if (ReCount > 50)
            {
                this.DivPage.InnerHtml = PageNavigator.Pagination(PageIndex, PgCount, PageUrl);
            }
            else
            {
                this.DivPage.Visible = false;
            }
        }

        public string GetClass(int ClassID)
        {
            string cStr = string.Empty;
            if (ClassID == 0)
            {
                cStr = "所有";
            }
            else
            {
                Bill bll = new Bill();
                cStr = bll.GetClassName(ClassID,UserID);
            }
            return cStr;
        }

        public string GetF(int fid)
        {
            string cStr = string.Empty;
            if (fid == 0)
            {
                cStr = "所有";
            }
            else
            {
                Bill bll = new Bill();
                cStr = bll.GetFamily(fid, UserID);
            }
            return cStr;
        }
    }
}
