using System;
using System.Data;
using System.Collections.Generic;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Text;
using EKETEAM.Data;
using EKETEAM.UserControl;
using EKETEAM.FrameWork;


    public partial class Customs_PlanAnalysis : System.Web.UI.Page
    {
        public string UserArea = "Application";
        public eModel model;
        public eUser user;       
        public string AppId = "";
        public string AppItem = eParameters.Request("AppItem");
        public string ModelID = "";
        string sql = "";

        public string type = "";
        public string baseUrl = "";

        public dynamic View = new eViewObject();
        protected void Page_Load(object sender, EventArgs e)
        {
            user = new eUser(eBase.getUserArea(UserArea));
            user.Check();

            model = new eModel();

            sql = "SELECT Year FROM SalePlans where siteid=" + user["siteid"] + " group by YEAR order by YEAR desc";
            DataTable tb = eBase.DataBase.getDataTable(sql);
            StringBuilder sb = new StringBuilder();
            string year = eParameters.QueryString("year");
            if (year.Length == 0) year = DateTime.Now.Year.ToString();
            baseUrl = "?AppItem=" + AppItem;
            for (int i = 0; i < tb.Rows.Count; i++)
            {
                DataRow dr = tb.Rows[i];
                if (year.Length == 0 && i == 0) year = dr["Year"].ToString();

                sb.Append("<a href=\"?AppItem=" + AppItem + "&year=" + dr["Year"].ToString() + "\"" + (year == dr["Year"].ToString() ? " class=\"cur\"" : "") + ">" + dr["Year"].ToString() + "年</a>");
            }
            litMenu.Text = sb.ToString();

            sql = "SELECT top 1 * FROM SalePlans where Year=" + year + " and siteid=" + user["siteid"].ToString() + " and deltag=0 order by addtime desc";// and Type=0
            View.year = 0;
            for (int i = 1; i < 13; i++)
            {
                View["month" + i.ToString()] = 0;
            }
            tb = eBase.DataBase.getDataTable(sql);
            if (tb.Rows.Count > 0)
            {
                DataRow row = tb.Rows[0];
                for (int i = 1; i < 13; i++)
                {
                    View["month" + i.ToString()] = row["month" + i.ToString()].ToString().Length == 0 ? 0 : Convert.ToDouble(row["month" + i.ToString()]);
                }
                View.year = Convert.ToDouble(row["yearsum"]);
            }
            for (int i = 1; i < 13; i++)
            {
                string temp = eBase.DataBase.getValue("select isnull(sum(Money),0) from Orders where siteid=" + user["siteid"] + " and Year(Time)=" + year + " and MONTH(Time)=" + i.ToString() + " and deltag=0");
                View["month" + i.ToString() + "_over"] = temp.Length == 0 ? 0 : Convert.ToDouble(temp);
            }
            View.year_over = View.month1_over + View.month2_over + View.month3_over + View.month4_over + View.month5_over + View.month6_over + View.month7_over + View.month8_over + View.month9_over + View.month10_over + View.month11_over + View.month12_over;
            
            View.year_bl = View.year == 0 ? 100 : Convert.ToInt32(100 * (View.year_over / (View.year + 0.001)));
            for (int i = 1; i < 13; i++)
            {
                View["month" + i.ToString() + "_bl"] = View["month" + i.ToString()] == 0 ? 100 : Convert.ToInt32(100 * (View["month"+ i.ToString() + "_over"] / (View["month" + i.ToString()] + 0.001)));
            }
            if (View.year == 0 && View.year_over == 0) View.year_bl = 0;
            for (int i = 1; i < 13; i++)
            {
                if (View["month" + i.ToString()] == 0 && View["month" + i.ToString() + "_over"] == 0) View["month" + i.ToString() + "_bl"] = 0;
            }
            View.year_css = "danger";
            if (View.year_bl >= 45) View.year_css = "warning";
            if (View.year_bl >= 80) View.year_css = "success";
            for (int i = 1; i < 13; i++)
            {
                View["month" + i.ToString() + "_css"] = "danger";
                if (View["month" + i.ToString() + "_bl"] >= 45) View["month" + i.ToString() + "_css"] = "warning";
                if (View["month" + i.ToString() + "_bl"] >= 80) View["month" + i.ToString() + "_css"] = "success";
            }
        }
        protected void Page_Load1(object sender, EventArgs e)
        {
            user = new eUser(eBase.getUserArea(UserArea));
            user.Check();

            model = new eModel();

            sql = "SELECT [Year] FROM [SalePlans] where siteid=" + user["siteid"] + " group by YEAR order by YEAR desc";
            DataTable tb = eBase.DataBase.getDataTable(sql);
            StringBuilder sb = new StringBuilder();
            string year = eParameters.QueryString("year");
            if (year.Length == 0) year = DateTime.Now.Year.ToString();
            baseUrl = "?AppItem=" + AppItem;
            for (int i = 0; i < tb.Rows.Count; i++)
            {
                DataRow dr = tb.Rows[i];
                if (year.Length ==0 && i == 0) year = dr["Year"].ToString();

                sb.Append("<a href=\"?AppItem=" + AppItem + "&year=" + dr["Year"].ToString() + "\"" + (year == dr["Year"].ToString() ? " class=\"cur\"" : "") + ">" + dr["Year"].ToString() + "年</a>");
            }
            litMenu.Text = sb.ToString();


           


            sql = "SELECT top 1 * FROM SalePlans where [Year]=" + year + " and siteid=" + user["siteid"].ToString() + " and Type=0 and deltag=0 order by addtime desc";

            View.year = 0;
            View.month1 = 0;
            View.month2 = 0;
            View.month3 = 0;
            View.month4 = 0;
            View.month5 = 0;
            View.month6 = 0;
            View.month7 = 0;
            View.month8 = 0;
            View.month9 = 0;
            View.month10 = 0;
            View.month11 = 0;
            View.month12 = 0;
            tb = eBase.DataBase.getDataTable(sql);
            if (tb.Rows.Count > 0)
            {
                DataRow row = tb.Rows[0];

                View.month1 = Convert.ToDouble(row["month1"]);
                View.month2 = Convert.ToDouble(row["month2"]);
                View.month3 = Convert.ToDouble(row["month3"]);
                View.month4 = Convert.ToDouble(row["month4"]);
                View.month5 = Convert.ToDouble(row["month5"]);
                View.month6 = Convert.ToDouble(row["month6"]);
                View.month7 = Convert.ToDouble(row["month7"]);
                View.month8 = Convert.ToDouble(row["month8"]);
                View.month9 = Convert.ToDouble(row["month9"]);
                View.month10 = Convert.ToDouble(row["month10"]);
                View.month11 = Convert.ToDouble(row["month11"]);
                View.month12 = Convert.ToDouble(row["month12"]);
                View.year = Convert.ToDouble(row["yearsum"]);
            }
            
            string tmp = eBase.DataBase.getValue("select isnull(sum(PayMoney),0) from Orders where siteid=" + user["siteid"] + " and Year(addTime)=" + year + " and MONTH(addTime)=1 and deltag=0");
            View.month1_over = tmp.Length == 0 ? 0 : Convert.ToDouble(tmp);
            tmp = eBase.DataBase.getValue("select isnull(sum(PayMoney),0) from Orders where siteid=" + user["siteid"] + " and Year(addTime)=" + year + " and MONTH(addTime)=2 and deltag=0");
            View.month2_over = tmp.Length == 0 ? 0 : Convert.ToDouble(tmp);
            tmp = eBase.DataBase.getValue("select isnull(sum(PayMoney),0) from Orders where siteid=" + user["siteid"] + " and Year(addTime)=" + year + " and MONTH(addTime)=3 and deltag=0");
            View.month3_over = tmp.Length == 0 ? 0 : Convert.ToDouble(tmp);
            tmp = eBase.DataBase.getValue("select isnull(sum(PayMoney),0) from Orders where siteid=" + user["siteid"] + " and Year(addTime)=" + year + " and MONTH(addTime)=4 and deltag=0");
            View.month4_over = tmp.Length == 0 ? 0 : Convert.ToDouble(tmp);
            tmp = eBase.DataBase.getValue("select isnull(sum(PayMoney),0) from Orders where siteid=" + user["siteid"] + " and Year(addTime)=" + year + " and MONTH(addTime)=5 and deltag=0");
            View.month5_over = tmp.Length == 0 ? 0 : Convert.ToDouble(tmp);
            tmp = eBase.DataBase.getValue("select isnull(sum(PayMoney),0) from Orders where siteid=" + user["siteid"] + " and Year(addTime)=" + year + " and MONTH(addTime)=6 and deltag=0");
            View.month6_over = tmp.Length == 0 ? 0 : Convert.ToDouble(tmp);
            tmp = eBase.DataBase.getValue("select isnull(sum(PayMoney),0) from Orders where siteid=" + user["siteid"] + " and Year(addTime)=" + year + " and MONTH(addTime)=7 and deltag=0");
            View.month7_over = tmp.Length == 0 ? 0 : Convert.ToDouble(tmp);
            tmp = eBase.DataBase.getValue("select isnull(sum(PayMoney),0) from Orders where siteid=" + user["siteid"] + " and Year(addTime)=" + year + " and MONTH(addTime)=8 and deltag=0");
            View.month8_over = tmp.Length == 0 ? 0 : Convert.ToDouble(tmp);
            tmp = eBase.DataBase.getValue("select isnull(sum(PayMoney),0) from Orders where siteid=" + user["siteid"] + " and Year(addTime)=" + year + " and MONTH(addTime)=9 and deltag=0");
            View.month9_over = tmp.Length == 0 ? 0 : Convert.ToDouble(tmp);
            tmp = eBase.DataBase.getValue("select isnull(sum(PayMoney),0) from Orders where siteid=" + user["siteid"] + " and Year(addTime)=" + year + " and MONTH(addTime)=10 and deltag=0");
            View.month10_over = tmp.Length == 0 ? 0 : Convert.ToDouble(tmp);
            tmp = eBase.DataBase.getValue("select isnull(sum(PayMoney),0) from Orders where siteid=" + user["siteid"] + " and Year(addTime)=" + year + " and MONTH(addTime)=11 and deltag=0");
            View.month11_over = tmp.Length == 0 ? 0 : Convert.ToDouble(tmp);
            tmp = eBase.DataBase.getValue("select isnull(sum(PayMoney),0) from Orders where siteid=" + user["siteid"] + " and Year(addTime)=" + year + " and MONTH(addTime)=12 and deltag=0");
            View.month12_over = tmp.Length == 0 ? 0 : Convert.ToDouble(tmp);


            View.year_over = View.month1_over + View.month2_over + View.month3_over + View.month4_over + View.month5_over + View.month6_over + View.month7_over + View.month8_over + View.month9_over + View.month10_over + View.month11_over + View.month12_over;



            View.year_bl = View.year == 0 ? 100 : Convert.ToInt32(100 * (View.year_over / (View.year + 0.001)));           
            View.month1_bl = View.month1 == 0 ? 100 : Convert.ToInt32(100 * (View.month1_over / (View.month1 + 0.001)));            
            View.month2_bl = View.month2 == 0 ? 100 : Convert.ToInt32(100 * (View.month2_over / (View.month2 + 0.001)));
            View.month3_bl = View.month3 == 0 ? 100 : Convert.ToInt32(100 * (View.month3_over / (View.month3 + 0.001)));
            View.month4_bl = View.month4 == 0 ? 100 : Convert.ToInt32(100 * (View.month4_over / (View.month4 + 0.001)));
            View.month5_bl = View.month5 == 0 ? 100 : Convert.ToInt32(100 * (View.month5_over / (View.month5 + 0.001)));
            View.month6_bl = View.month6 == 0 ? 100 : Convert.ToInt32(100 * (View.month6_over / (View.month6 + 0.001)));
            View.month7_bl = View.month7 == 0 ? 100 : Convert.ToInt32(100 * (View.month7_over / (View.month7 + 0.001)));
            View.month8_bl = View.month8 == 0 ? 100 : Convert.ToInt32(100 * (View.month8_over / (View.month8 + 0.001)));
            View.month9_bl = View.month9 == 0 ? 100 : Convert.ToInt32(100 * (View.month9_over / (View.month9 + 0.001)));
            View.month10_bl = View.month10 == 0 ? 100 : Convert.ToInt32(100 * (View.month10_over / (View.month10 + 0.001)));
            View.month11_bl = View.month11 == 0 ? 100 : Convert.ToInt32(100 * (View.month11_over / (View.month11 + 0.001)));
            View.month12_bl = View.month12 == 0 ? 100 : Convert.ToInt32(100 * (View.month12_over / (View.month12 + 0.001)));

            if (View.year == 0 && View.year_over == 0) View.year_bl = 0;
            if (View.month1 == 0 && View.month1_over == 0) View.month1_bl = 0;
            if (View.month2 == 0 && View.month2_over == 0) View.month2_bl = 0;
            if (View.month3 == 0 && View.month3_over == 0) View.month3_bl = 0;
            if (View.month4 == 0 && View.month4_over == 0) View.month4_bl = 0;
            if (View.month5 == 0 && View.month5_over == 0) View.month5_bl = 0;
            if (View.month6 == 0 && View.month6_over == 0) View.month6_bl = 0;
            if (View.month7 == 0 && View.month7_over == 0) View.month7_bl = 0;
            if (View.month8 == 0 && View.month8_over == 0) View.month8_bl = 0;
            if (View.month9 == 0 && View.month9_over == 0) View.month9_bl = 0;
            if (View.month10 == 0 && View.month10_over == 0) View.month10_bl = 0;
            if (View.month11 == 0 && View.month11_over == 0) View.month11_bl = 0;
            if (View.month12 == 0 && View.month12_over == 0) View.month12_bl = 0;

            View.year_css = "danger";
            if (View.year_bl >= 45) View.year_css = "warning";
            if (View.year_bl >= 80) View.year_css = "success";

            View.month1_css = "danger";
            if (View.month1_bl >= 45) View.month1_css = "warning";
            if (View.month1_bl >= 80) View.month1_css = "success";

            View.month2_css = "danger";
            if (View.month2_bl >= 45) View.month2_css = "warning";
            if (View.month2_bl >= 80) View.month2_css = "success";

            View.month3_css = "danger";
            if (View.month3_bl >= 45) View.month3_css = "warning";
            if (View.month3_bl >= 80) View.month3_css = "success";

            View.month4_css = "danger";
            if (View.month4_bl >= 45) View.month4_css = "warning";
            if (View.month4_bl >= 80) View.month4_css = "success";

            View.month5_css = "danger";
            if (View.month5_bl >= 45) View.month5_css = "warning";
            if (View.month5_bl >= 80) View.month5_css = "success";
            View.month6_css = "danger";
            if (View.month6_bl >= 45) View.month6_css = "warning";
            if (View.month6_bl >= 80) View.month6_css = "success";
            View.month7_css = "danger";
            if (View.month7_bl >= 45) View.month7_css = "warning";
            if (View.month7_bl >= 80) View.month7_css = "success";
            View.month8_css = "danger";
            if (View.month8_bl >= 45) View.month8_css = "warning";
            if (View.month8_bl >= 80) View.month8_css = "success";
            View.month9_css = "danger";
            if (View.month9_bl >= 45) View.month9_css = "warning";
            if (View.month9_bl >= 80) View.month9_css = "success";
            View.month10_css = "danger";
            if (View.month10_bl >= 45) View.month10_css = "warning";
            if (View.month10_bl >= 80) View.month10_css = "success";
            View.month11_css = "danger";
            if (View.month11_bl >= 45) View.month11_css = "warning";
            if (View.month11_bl >= 80) View.month11_css = "success";
            View.month12_css = "danger";
            if (View.month12_bl >= 45) View.month12_css = "warning";
            if (View.month12_bl >= 80) View.month12_css = "success";
        }

        protected void Page_PreRender(object sender, EventArgs e)
        {
            if (Master == null) return;
            //Literal lit = (Literal)Master.FindControl("LitTitle");
           // if (lit != null) lit.Text = eConfig.ApplicationTitle(user["SiteID"].ToString());// model.ModelInfo["mc"].ToString() + " - " + 



        }
    }
