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_Export : System.Web.UI.Page
    {
        public string UserArea = "Application";
        public string AppItem = eParameters.Request("AppItem");
        public eModel model;
        public eUser user;


        public string PlanID = eParameters.Request("PlanID");
        public DataRow row;
        string sql = "";

        public string type = eParameters.QueryString("type");
        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];

            eList list;
            string fileName = "";
            string tablename = "";
            switch (type)
            {
                case "1": 
                    #region 计划采集导出
                    fileName = "计划采集.xls";
                    tablename = "Check_plan_" + row["BaseName"].ToString();
                    tablename = "Check_plan";
                    list = new eList("T_ryxx");
                    //,a.resultTime
                    sql = "select a.CJFS,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 b.delTag=0 and b.Multiple=0";
                    //if (code.Length > 0) sql += " and a.OrgCode like '" + code + "%'";
                    list.SQL = sql;
                    list.Bind(Repeater1);
                    #endregion
                    break;
                case "2":
                    #region 已采集导出
                    fileName = "已采集.xls";
                    tablename = "Check_plan_" + row["BaseName"].ToString();
                    tablename = "Check_plan";
                    list = new eList("T_ryxx");
                    sql = "select a.CJFS,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.CheckPlanID='" + PlanID + "' and a.sfzh=b.sfzh";
                    sql += " where a.result=1 and b.delTag=0 and b.Multiple=0";
                    //if (code.Length > 0) sql += " and a.OrgCode like '" + code + "%'";
                    //eBase.Writeln(sql);
                    list.SQL = sql;
                    list.Bind(Repeater2);
                    #endregion
                    break;
                case "3":
                    #region 未采集导出
                    fileName = "未采集.xls";
                    tablename = "Check_plan_" + row["BaseName"].ToString();
                    tablename = "Check_plan";

                    list = new eList("T_ryxx");
                    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.CheckPlanID='" + PlanID + "' and a.sfzh=b.sfzh";
                    sql+=" where a.result=0 and b.delTag=0 and b.Multiple=0";
                    //if (code.Length > 0) sql += " and a.OrgCode like '" + code + "%'";
                    list.SQL = sql;
                    list.Bind(Repeater3);
                    #endregion
                    break;
                case "4":
                    #region 计划外导出
                    fileName = "计划外.xls";
                    tablename = "Check_other_" + row["BaseName"].ToString();
                    tablename = "Check_other";
                    list = new eList("T_ryxx");
                    sql = "select a.CJFS,a.CJSJ,a.xm,a.xb,a.sfzh,b.LXDH,b.mz,b.dz from " + tablename + " a ";
                    sql += " left join T_ryxx b on a.sfzh=b.sfzh and b.delTag=0 and b.Multiple=0";
                    sql += " where a.CheckPlanID='" + PlanID + "'";
                    //if (code.Length > 0) sql += " and a.OrgCode like '" + code + "%'";
                   // eBase.Writeln(sql);
                    list.SQL = sql;
                    list.Bind(Repeater4);
                    #endregion
                    break;
            }
            

           // if (HttpContext.Current.Request.ServerVariables["HTTP_USER_AGENT"].ToString().ToLower().IndexOf("msie") > -1) fileName = HttpUtility.UrlEncode(fileName, System.Text.Encoding.UTF8);  //IE需要编码
           // HttpContext.Current.Response.ContentType = "application/octet-stream";
           // HttpContext.Current.Response.AddHeader("Accept-Ranges", "bytes");
           // HttpContext.Current.Response.AddHeader("Content-Disposition", "attachment;filename=\"" + fileName + "\"");
        }
    }
}