using System;
using System.Collections.Generic;
using System.Data;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using EKETEAM.Data;
using EKETEAM.FrameWork;
using EKETEAM.UserControl;
using System.Text;
using System.Xml;
using System.Xml.Serialization;
using ICSharpCode.SharpZipLib.Zip;
using ICSharpCode.SharpZipLib.Checksums;
using System.IO;

namespace eFrameWork.Manage
{
    public partial class Styles : System.Web.UI.Page
    {
        public string id = eParameters.Request("id");
        public string act = eParameters.Request("act");
        public string sql = "";
        public eForm edt;
        public eUser user;
        public eList elist;
        protected void Page_Load(object sender, EventArgs e)
        {
            user = new eUser("Manage");
            edt = new eForm("a_eke_sysStyles", user);

            if (act.Length == 0)
            {
                List();
                return;
            }
            if (act == "export")
            {
                MemoryStream mstream = new MemoryStream();
                ZipOutputStream zstream = new ZipOutputStream(mstream);

                string ids = eParameters.QueryString("styleids");
                ids = "'" + ids.Replace(",", "','") + "'";

                eList a_eke_sysStyles = new eList("a_eke_sysStyles");
                a_eke_sysStyles.Where.Add("deltag=0");
                a_eke_sysStyles.Where.Add("StyleID in (" + ids + ")");

                DataTable dt = a_eke_sysStyles.getDataTable();
                if (dt.Columns.Contains("addTime")) dt.Columns.Remove("addTime");

                for (int i = 0; i < dt.Rows.Count; i++)
                {
                    DataRow dr = dt.Rows[i];
                    string filename = dr["name"].ToString() + ".efw";
                    ZipEntry zen = new ZipEntry(filename);
                    zstream.PutNextEntry(zen);
                    zstream.SetLevel(8);

                    MemoryStream ms = getXMLFile(dr);
                    byte[] bytes = ms.ToArray();
                    zstream.Write(bytes, 0, bytes.Length);
                }

                zstream.Finish();
                zstream.Close();

                string fileName = "样式.zip";
                string userAgent = HttpContext.Current.Request.UserAgent == null ? "" : HttpContext.Current.Request.UserAgent.Replace(" ", "").ToLower();
                if (userAgent.IndexOf("msie") > -1) fileName = HttpUtility.UrlEncode(fileName, System.Text.Encoding.UTF8);  //IE需要编码
                HttpContext.Current.Response.Clear();
                HttpContext.Current.Response.ContentType = "application/octet-stream";
                HttpContext.Current.Response.AddHeader("Content-Disposition", "attachment; filename=" + fileName);
                HttpContext.Current.Response.BinaryWrite(mstream.ToArray());
                HttpContext.Current.Response.Flush();
                HttpContext.Current.Response.End();
            }
            if (act == "export2")
            {
                #region 导出V1
                string xml = ExportStyles();
                //eBase.WriteHTML(xml);
                byte[] buffer = Encoding.UTF8.GetBytes(xml);
                byte[] outBuffer = new byte[buffer.Length + 3];
                outBuffer[0] = (byte)0xEF;
                outBuffer[1] = (byte)0xBB;
                outBuffer[2] = (byte)0xBF;
                Array.Copy(buffer, 0, outBuffer, 3, buffer.Length);

                string modelName = "样式";
                string fileName = modelName + ".efw";
                string userAgent = HttpContext.Current.Request.UserAgent == null ? "" : HttpContext.Current.Request.UserAgent.Replace(" ", "").ToLower();
                if (userAgent.IndexOf("msie") > -1) fileName = HttpUtility.UrlEncode(fileName, System.Text.Encoding.UTF8);  //IE需要编码
                //Response.ContentType = "text/xml";
                Response.ContentType = "application/octet-stream";
                Response.AddHeader("Accept-Ranges", "bytes");
                Response.AddHeader("Content-Disposition", "attachment;filename=\"" + fileName + "\"");
                Response.Write(Encoding.UTF8.GetString(outBuffer));
                Response.End();
                #endregion
            }
            if (act == "del")
            {
                string id = eParameters.QueryString("id");
                eBase.DataBase.Execute("delete from a_eke_sysStyles where StyleID='" + id + "'");

                eBase.clearDataCache(); //清除所有缓存
                if (Request.ServerVariables["HTTP_REFERER"] == null)
                {
                    Response.Redirect("Styles.aspx", true);
                }
                else
                {
                    Response.Redirect(Request.ServerVariables["HTTP_REFERER"].ToString(), true);
                }
            }
            #region 信息添加、编辑

           
            edt.AddControl(eFormControlGroup);
            edt.onChange += new eFormTableEventHandler(edt_onChange);
            edt.Handle();
            #endregion
        }
        private MemoryStream getXMLFile(DataRow dr)
        {
            MemoryStream ms = new MemoryStream();
            DataTable Styles = dr.toDataTable();
            XmlDocument doc = new XmlDocument();
            Styles.ExtendedProperties.Add("name", "a_eke_sysStyles");
            doc.appendData(Styles);

            string xml = doc.InnerXml;
            byte[] buffer = Encoding.UTF8.GetBytes(xml);
            byte[] outBuffer = new byte[buffer.Length + 3];
            outBuffer[0] = (byte)0xEF;
            outBuffer[1] = (byte)0xBB;
            outBuffer[2] = (byte)0xBF;
            Array.Copy(buffer, 0, outBuffer, 3, buffer.Length);

            ms.Write(outBuffer, 0, outBuffer.Length);
            return ms;
        }
        private string ExportStyles()
        {
            XmlDocument doc = new XmlDocument();
            string ids = eParameters.QueryString("styleids");
            ids = "'" + ids.Replace(",", "','") + "'";

            eList a_eke_sysStyles = new eList("a_eke_sysStyles");
            a_eke_sysStyles.Where.Add("deltag=0");
            a_eke_sysStyles.Where.Add("StyleID in (" + ids + ")");



            DataTable Styles = a_eke_sysStyles.getDataTable();
            if (Styles.Columns.Contains("addTime")) Styles.Columns.Remove("addTime");
   

            #region 生成XML
            Styles.ExtendedProperties.Add("name", "a_eke_sysStyles");
            doc.appendData(Styles);
            #endregion

            return doc.InnerXml;
        }
        public void edt_onChange(object sender, eFormTableEventArgs e)
        {
            if (e.eventType == eFormTableEventType.Inserting)
            {
                //if (user["ServiceID"].Length > 0) edt.Fields.Add("ServiceID", user["ServiceID"]);
            }
        }
        private void List()
        {
            elist = new eList("a_eke_sysStyles");
            elist.Where.Add("delTag=0");
            elist.Where.Add(eSearchControlGroup);
            elist.OrderBy.Add("addTime desc");
            elist.Bind(Rep, ePageControl1);
        }
        protected void Page_PreRender(object sender, EventArgs e)
        {
            if (Master == null) return;
            Literal lit = (Literal)Master.FindControl("LitTitle");
            if (lit != null)
            {
                lit.Text = "样式管理 - " + eConfig.manageName();
            }
        }
    }
}