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

namespace eFrameWork.Application
{
    public partial class Check_Plans_NotResult : System.Web.UI.Page
    {
        public string UserArea = "Application";
        public string AppItem = eParameters.Request("AppItem");
        public eModel model;
        public eList elist;
        public eUser user;


        public string PlanID = eParameters.Request("PlanID");
        public DataRow row;
        string sql = "";
        public string pid = eParameters.QueryString("pid");
        public string allids = "";
        private DataTable _organizationals;
        private DataTable Organizationals
        {
            get
            {
                _organizationals = eBase.getDataCache("Organizationals");
                return _organizationals;
            }
        }
        public string code = eParameters.QueryString("code");
        private string tablename = ""; 
        protected void Page_Load(object sender, EventArgs e)
        {
            user = new eUser(eBase.getUserArea(UserArea));
            eModelInfo customModel = new eModelInfo(user);
            model = customModel.Model;
            sql="select * from CheckPlans where delTag=0 and CheckPlanID='" + PlanID+ "'";
            DataTable dt = eOleDB.getDataTable(sql);
            if (dt.Rows.Count == 0)
            {
                Response.Write("该批次不存在,或已被删除.");
                Response.End();
            }
            row = dt.Rows[0];
            tablename = "Check_plan_" + row["BaseName"].ToString();
            tablename = "Check_plan";
            if (Request.QueryString["value"] != null)
            {
                string sfzh = eParameters.QueryString("sfzh");
                string value = eParameters.QueryString("value");
                eOleDB.Execute("update " + tablename + " set Reason='" + value + "' where CheckPlanID='" + PlanID + "' and sfzh='" + sfzh + "'");
                Response.Write("设置成功!");
                Response.End();
            }
            string act = eParameters.QueryString("act");
            if (act.Length > 0)
            {
            }
            else
            {
                LitOrgList.Text = getTree(user["code"]);
                List();
            }
        }
        private string getTree(string Code, int level = 0)
        {
            StringBuilder sb = new StringBuilder();
            Code = Code.Replace(",", "','");
            DataRow[] rows = Organizationals.Select("code in ('" + Code + "')", "PX,addTime");
            if (level == 0)
            {
                sb.Append("<ul id=\"etree\" class=\"etree\" PID=\"NULL\">\r\n");
            }
            else
            {
                sb.Append("<ul>\r\n");
            }
            foreach (DataRow dr in rows)
            {
                string ct = eOleDB.getValue("select count(*) from " + tablename + " where CheckPlanID='" + PlanID + "' and orgCode like '" + dr["code"].ToString() + "%'"); //计划
                string ct1 = eOleDB.getValue("select count(*) from " + tablename + " where CheckPlanID='" + PlanID + "' and orgCode like '" + dr["code"].ToString() + "%' and result=0"); //未完成
                DataRow[] rs = Organizationals.Select("ParentCode='" + dr["Code"].ToString() + "'", "PX,addTime");
                sb.Append("<li onclick=\"treeselect_click(this);\" class=\"open\">");
                sb.Append("<div><a href=\"" + "?" + "AppItem=" + AppItem + "&PlanID=" + PlanID + "&code=" + dr["Code"].ToString() + "\"" + (Convert.ToInt32(ct1) > 0 && Convert.ToInt32(ct1) < Convert.ToInt32(ct) ? " style=\"color:#ff0000;\"" : "") + ">" + dr["MC"].ToString() + " (" + ct1 + " / " + ct + ")</a>");
                sb.Append("</div>");
                foreach (DataRow _dr in rs)
                {
                    sb.Append(getTree(_dr["Code"].ToString(), level + 1));
                }
                sb.Append("</li>\r\n"); 
            }
            sb.Append("</ul>\r\n");
            return sb.ToString();
        }
        private void List()
        {
            elist = new eList("T_ryxx");


            string sql = "select a.CJFS,a.Reason,a.CJSJ,b.xm,b.xb,b.sfzh,b.LXDH,b.mz,b.dz from " + tablename + " a ";
            sql += " inner join T_ryxx b on a.sfzh=b.sfzh";
            sql += " where a. CheckPlanID='" + PlanID + "' and a.result=0 and b.delTag=0 and b.Multiple=0";
            if (code.Length > 0) sql += " and a.OrgCode like '" + code + "%'";

            elist.SQL = sql;
            elist.Bind(eDataTable, ePageControl1);
            if (ePageControl1.RecordsCount == 0) LitNoResords.Text = "<div style=\"padding:15px;text-align:center\">暂无数据!</div>";
        }
        private void List1()
        {


            elist = new eList("T_ryxx");
            elist.Where.Add("delTag=0");
            //elist.Where.Add("SiteID='" + user["siteid"] + "'");
            string userCond = eParameters.Replace(model.UserCondition, null, user);//替换用户、URL等参数
            elist.Where.Add(userCond);//用户条件
           // elist.Where.Add(s1); //按姓名查询
            elist.OrderBy.Default = "addTime desc";//默认排序



            elist.Bind(eDataTable, ePageControl1);
        }
    }
}