using System;
using System.Collections;
using System.ComponentModel;
using System.Data;
//using System.Drawing;
using System.Web;
using System.IO;
using System.Web.SessionState;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;
namespace MMManage.Facture
{
 /// <summary>
 /// Facture_Layout 的摘要说明。
 /// </summary>
 public class Facture_Layout : System.Web.UI.Page
 {
  protected System.Web.UI.WebControls.Image Image3;
  protected System.Web.UI.WebControls.Image Image2;
  protected System.Web.UI.WebControls.Image Image1;
  protected System.Web.UI.WebControls.DropDownList Count;
  protected System.Web.UI.WebControls.Label Label1;
  protected System.Web.UI.WebControls.Label Label2;
  protected System.Web.UI.WebControls.Button CreateButton;
  protected System.Web.UI.WebControls.Label Label16;
  protected System.Web.UI.WebControls.Label Label17;
  protected System.Web.UI.WebControls.TextBox MMName;
  protected System.Web.UI.WebControls.Button ViewButton;
  protected static string strCount = "1";
  protected static string szFirstSub="";
  protected static string szFirstP="";
  protected static string szFirstT="";
  protected static string szSub1="";
  protected static string szSubP1="";
  protected static string szSubT1="";
  protected static string szSub2="";
  protected static string szSubP2="";
  protected static string szSubT2="";
  protected static string szSub3="";
  protected static string szSubP3="";
  protected static string szSubT3="";
  protected static string szEndSub="";
  protected static string szEndP="";
  protected static string szEndT="";
  protected static string szEndR="";
  protected static bool bPrev=false;
  protected System.Web.UI.WebControls.TextBox tbFirst;
  protected System.Web.UI.WebControls.TextBox tbSub1;
  protected System.Web.UI.WebControls.TextBox tbSub2;
  protected System.Web.UI.WebControls.TextBox tbSub3;
  protected System.Web.UI.WebControls.TextBox tbEnd;
  protected System.Web.UI.HtmlControls.HtmlInputFile fFirstp;
  protected System.Web.UI.HtmlControls.HtmlInputFile fFirstt;
  protected System.Web.UI.HtmlControls.HtmlInputFile fSubp1;
  protected System.Web.UI.HtmlControls.HtmlInputFile fSubt1;
  protected System.Web.UI.HtmlControls.HtmlInputFile fSubp2;
  protected System.Web.UI.HtmlControls.HtmlInputFile fSubt2;
  protected System.Web.UI.HtmlControls.HtmlInputFile fSubp3;
  protected System.Web.UI.HtmlControls.HtmlInputFile fSubt3;
  protected System.Web.UI.HtmlControls.HtmlInputFile fEndp;
  protected System.Web.UI.HtmlControls.HtmlInputFile fEndt;
  protected System.Web.UI.HtmlControls.HtmlInputFile fEndr;
  protected System.Web.UI.WebControls.Label Label3;
  protected System.Web.UI.WebControls.Label Label4;
  protected System.Web.UI.WebControls.Label Label5;
  protected System.Web.UI.WebControls.Label Label6;
  protected System.Web.UI.WebControls.Label Label7;
  protected System.Web.UI.WebControls.Label Label8;
  protected System.Web.UI.WebControls.Label Label9;
  protected System.Web.UI.WebControls.Label Label10;
  protected System.Web.UI.WebControls.Table tbPreview;
  protected System.Web.UI.WebControls.Image ImgFirst;
  protected System.Web.UI.WebControls.Image ImgEnd;
  protected System.Web.UI.WebControls.Label Label11;
  private void Check()
  {
   if (Page.Session["Name"]==null || Page.Session["Name"].ToString()=="")
   {
    Response.Redirect ("../Unline.aspx");
   }
  }
  private void Page_Load(object sender, System.EventArgs e)
  {
   Check();
   if(!IsPostBack)
   {
    if((int)Page.Session["Power"]==2)
    {
     this.MMName.Text=(string)Page.Session["Name"];
    }
    else
    {
     this.MMName.Text=Request["name"];
    }
    strCount = "1";
    Count.SelectedIndex = 0;
   }
  }
  private void Count_SelectedIndexChanged(object sender, System.EventArgs e)
  {
   
   strCount=Count.SelectedItem.Value;
  }
  
  // 预览
  private void preview()
  {
   String szPath = "../MMBao/";
   String szMapPath = Server.MapPath("../MMBao/");
   //ImgFirst.ImageUrl = szPath+szFirstP;
   //ImgEnd.ImageUrl = szPath+szEndP;
   TableRow tr = new TableRow();
   TableCell tc = new TableCell();
   if (MMName.Text == "")
    tc.Text = "无标题";
   else
    tc.Text = MMName.Text;
   tr.Cells.Add(tc);
   tr.HorizontalAlign = HorizontalAlign.Center;
   tbPreview.Rows.Add(tr);
   // 首页
   tr = new TableRow();
   tc = new TableCell();
   if (tbFirst.Text == "")
    tc.Text = "首页";
   else
    tc.Text = tbFirst.Text;
   tr.Cells.Add(tc);
   tr.HorizontalAlign = HorizontalAlign.Center;
   tbPreview.Rows.Add(tr);
   if (szFirstP != "")
   {
    ImgFirst.ImageUrl = szPath+szFirstP;
    //  预览
    tr = new TableRow();
    tc = new TableCell();
    Image img = new Image();
    img.ImageUrl =szPath+szFirstP;
    tc.Controls.Add(img);
    tr.Cells.Add(tc);
    tr.HorizontalAlign = HorizontalAlign.Center;
    tbPreview.Rows.Add(tr);   
   }
   if (szFirstT != "")
   {
    tr = new TableRow();
    tc = new TableCell();
    using (StreamReader sr = new StreamReader(szMapPath+szFirstT,
System.Text.Encoding.GetEncoding(936)))
    {
      
     String line;
     while ((line = sr.ReadLine()) != null)
      tc.Text += line;
     //lSize += sr.BaseStream.Length;
    }
    tr.Cells.Add(tc);
    tbPreview.Rows.Add(tr);   
   }
   // 可选项
   tr = new TableRow();
   tc = new TableCell();
   if (tbSub1.Text == "")
    tc.Text = "专题1";
   else
    tc.Text = tbSub1.Text;
   tr.Cells.Add(tc);
   tr.HorizontalAlign = HorizontalAlign.Center;
   tbPreview.Rows.Add(tr);
   if (szSubP1 != "")
   {
    Image1.ImageUrl = szPath+szSubP1;
    //  预览
    tr = new TableRow();
    tc = new TableCell();
    Image img = new Image();
    img.ImageUrl =szPath +szSubP1;
    tc.Controls.Add(img);
    tr.Cells.Add(tc);
    tr.HorizontalAlign = HorizontalAlign.Center;
    tbPreview.Rows.Add(tr);   
   }
   if (szSubT1 != "")
   {
    tr = new TableRow();
    tc = new TableCell();
    using (StreamReader sr = new StreamReader(szMapPath+szSubT1,
System.Text.Encoding.GetEncoding(936)))
    {
      
     String line;
     while ((line = sr.ReadLine()) != null)
      tc.Text += line;
     //lSize += sr.BaseStream.Length;
    }
    tr.Cells.Add(tc);
    tbPreview.Rows.Add(tr);   
   }
   // 动态选项
   if (Int32.Parse(Count.SelectedValue) > 1)
   {
    tr = new TableRow();
    tc = new TableCell();
    if (tbSub2.Text == "")
     tc.Text = "专题2";
    else
     tc.Text = tbSub2.Text;
    tr.Cells.Add(tc);
    tr.HorizontalAlign = HorizontalAlign.Center;
    tbPreview.Rows.Add(tr);
    if (szSubP2 != "")
    {
     Image2.ImageUrl = szPath+szSubP2;
     //  预览
     tr = new TableRow();
     tc = new TableCell();
     Image img = new Image();
     img.ImageUrl =szPath +szSubP2;
     tc.Controls.Add(img);
     tr.Cells.Add(tc);
     tr.HorizontalAlign = HorizontalAlign.Center;
     tbPreview.Rows.Add(tr);   
    }
    if (szSubT2 != "")
    {
     tr = new TableRow();
     tc = new TableCell();
     using (StreamReader sr = new StreamReader(szMapPath+szSubT2,
System.Text.Encoding.GetEncoding(936)))
     {
      
      String line;
      while ((line = sr.ReadLine()) != null)
       tc.Text += line;
      //lSize += sr.BaseStream.Length;
     }
     tr.Cells.Add(tc);
     tbPreview.Rows.Add(tr);   
    }
   }
   if (Int32.Parse(Count.SelectedValue) > 2)
   {
    tr = new TableRow();
    tc = new TableCell();
    if (tbSub3.Text == "")
     tc.Text = "专题3";
    else
     tc.Text = tbSub3.Text;
    tr.Cells.Add(tc);
    tr.HorizontalAlign = HorizontalAlign.Center;
    tbPreview.Rows.Add(tr);
    if (szSubP3 != "")
    {
     Image3.ImageUrl = szPath+szSubP3;
     //  预览
     tr = new TableRow();
     tc = new TableCell();
     Image img = new Image();
     img.ImageUrl =szPath +szSubP3;
     tc.Controls.Add(img);
     tr.Cells.Add(tc);
     tr.HorizontalAlign = HorizontalAlign.Center;
     tbPreview.Rows.Add(tr);   
    }
    if (szSubT3 != "")
    {
     tr = new TableRow();
     tc = new TableCell();
     using (StreamReader sr = new StreamReader(szMapPath+szSubT3,
System.Text.Encoding.GetEncoding(936)))
     {
      
      String line;
      while ((line = sr.ReadLine()) != null)
       tc.Text += line;
      //lSize += sr.BaseStream.Length;
     }
     tr.Cells.Add(tc);
     tbPreview.Rows.Add(tr);   
    }
   }
   // 落款
   tr = new TableRow();
   tc = new TableCell();
   if (tbEnd.Text == "")
    tc.Text = "落款";
   else
    tc.Text = tbEnd.Text;
   tr.Cells.Add(tc);
   tr.HorizontalAlign = HorizontalAlign.Center;
   tbPreview.Rows.Add(tr);
   if (szEndP != "")
   {
    ImgEnd.ImageUrl = szPath+szEndP;
    //  预览
    tr = new TableRow();
    tc = new TableCell();
    Image img = new Image();
    img.ImageUrl =szPath +szEndP;
    tc.Controls.Add(img);
    tr.Cells.Add(tc);
    tr.HorizontalAlign = HorizontalAlign.Center;
    tbPreview.Rows.Add(tr);   
   }
   if (szEndT != "")
   {
    tr = new TableRow();
    tc = new TableCell();
    using (StreamReader sr = new StreamReader(szMapPath+szEndT,
System.Text.Encoding.GetEncoding(936)))
    {
      
     String line;
     while ((line = sr.ReadLine()) != null)
      tc.Text += line;
     //lSize += sr.BaseStream.Length;
    }
    tr.Cells.Add(tc);
    tbPreview.Rows.Add(tr);   
   }
   if (szEndR != "")
   {
    tr = new TableRow();
    tc = new TableCell();
    tc.Text = szEndR;
    tr.Cells.Add(tc);
    tbPreview.Rows.Add(tr);   
   }
  
  }
  // 上传文件
  private void uploadfile(bool bPreview)
  {
   int iSize=0;
   String szUpPath;
   string szName;
   string szExtension;
   String szNow = (string)Page.Session["Hzf"] + DateTime.Now.ToString("MMddhhmmss");
   if (bPreview)
    szUpPath = "../MMBao/";
   else
    szUpPath = "../MMPacket/";
   // 检查大小
   if(fFirstp.PostedFile.ContentLength>10240)
   {
    this.Label1.Text="上传图片大小不能大于10K,请重新上传!";
    return;
   }
   iSize +=fFirstp.PostedFile.ContentLength;
   if(fFirstt.PostedFile.ContentLength>10240)
   {
    this.Label2.Text="上传图片大小不能大于10K,请重新上传!";
    return;
   }
   iSize +=fFirstt.PostedFile.ContentLength;
   if(fSubp1.PostedFile.ContentLength>10240)
   {
    this.Label3.Text="上传图片大小不能大于10K,请重新上传!";
    return;
   }
   iSize +=fSubp1.PostedFile.ContentLength;
   if(fSubt1.PostedFile.ContentLength>10240)
   {
    this.Label4.Text="上传图片大小不能大于10K,请重新上传!";
    return;
   }
   iSize +=fSubt1.PostedFile.ContentLength;
   if (Int32.Parse(Count.SelectedValue) > 1)
   {
    if(fSubp2.PostedFile.ContentLength>10240)
    {
     this.Label5.Text="上传图片大小不能大于10K,请重新上传!";
     return;
    }
    iSize +=fSubp2.PostedFile.ContentLength;
    if(fSubt2.PostedFile.ContentLength>10240)
    {
     this.Label6.Text="上传图片大小不能大于10K,请重新上传!";
     return;
    }
    iSize +=fSubt2.PostedFile.ContentLength;
   }
   if (Int32.Parse(Count.SelectedValue) > 2)
   {
    if(fSubp3.PostedFile.ContentLength>10240)
    {
     this.Label7.Text="上传图片大小不能大于10K,请重新上传!";
     return;
    }
    iSize +=fSubp3.PostedFile.ContentLength;
    if(fSubt3.PostedFile.ContentLength>10240)
    {
     this.Label8.Text="上传图片大小不能大于10K,请重新上传!";
     return;
    }
    iSize +=fSubt3.PostedFile.ContentLength;
   }
   if(fEndp.PostedFile.ContentLength>10240)
   {
    this.Label9.Text="上传图片大小不能大于10K,请重新上传!";
    return;
   }
   iSize +=fEndp.PostedFile.ContentLength;
   if(fEndt.PostedFile.ContentLength>10240)
   {
    this.Label10.Text="上传图片大小不能大于10K,请重新上传!";
    return;
   }
   iSize +=fEndt.PostedFile.ContentLength;
   if(fEndr.PostedFile.ContentLength>10240)
   {
    this.Label1.Text="上传图片大小不能大于10K,请重新上传!";
    return;
   }
   iSize +=fEndr.PostedFile.ContentLength;
   // 超出45K则提示并返回,中止当前彩信包制作
   if (iSize > 46080)
   {
    Response.Write("<script language='javascript'>alert('彩信包大小超出有可能50K,请重新制定彩信包
!');</script>");
    return;   
   }
   // 首页
   if (fFirstp.Value != "")
   {
    szName = fFirstp.PostedFile.FileName;
    szExtension = szName.Substring(szName.IndexOf("."));
    szFirstP = szNow+"-fp"+szExtension;
    fFirstp.PostedFile.SaveAs(Server.MapPath(szUpPath +szFirstP));
   }
   if (fFirstt.Value != "")
   {
    szName = fFirstt.PostedFile.FileName;
    szExtension = szName.Substring(szName.IndexOf("."));
    szFirstT = szNow+"-ft"+szExtension;
    fFirstt.PostedFile.SaveAs(Server.MapPath(szUpPath +szFirstT));
   }
   // 可选项
   if (fSubp1.Value != "")
   {
    szName = fSubp1.PostedFile.FileName;
    szExtension = szName.Substring(szName.IndexOf("."));
    szSubP1 = szNow+"-1p"+szExtension;
    fSubp1.PostedFile.SaveAs(Server.MapPath(szUpPath +szSubP1));
   }
   if (fSubt1.Value != "")
   {
    szName = fSubt1.PostedFile.FileName;
    szExtension = szName.Substring(szName.IndexOf("."));
    szSubT1 = szNow+"-1t"+szExtension;
    fSubt1.PostedFile.SaveAs(Server.MapPath(szUpPath +szSubT1));
   }
   //动态选项
   if (Int32.Parse(Count.SelectedValue) > 1)
   {
    if (fSubp2.Value != "")
    {
     szName = fSubp2.PostedFile.FileName;
     szExtension = szName.Substring(szName.IndexOf("."));
     szSubP2 = szNow+"-2p"+szExtension;
     fSubp2.PostedFile.SaveAs(Server.MapPath(szUpPath +szSubP2));
    }
    if (fSubt2.Value != "")
    {
     szName = fSubt2.PostedFile.FileName;
     szExtension = szName.Substring(szName.IndexOf("."));
     szSubT2 = szNow+"-2t"+szExtension;
     fSubt2.PostedFile.SaveAs(Server.MapPath(szUpPath +szSubT2));
    }
   }
   if (Int32.Parse(Count.SelectedValue) > 2)
   {
    if (fSubp3.Value != "")
    {
     szName = fSubp3.PostedFile.FileName;
     szExtension = szName.Substring(szName.IndexOf("."));
     szSubP3 = szNow+"-3p"+szExtension;
     fSubp3.PostedFile.SaveAs(Server.MapPath(szUpPath +szSubP3));
    }
    if (fSubt3.Value != "")
    {
     szName = fSubt3.PostedFile.FileName;
     szExtension = szName.Substring(szName.IndexOf("."));
     szSubT3 = szNow+"-3t"+szExtension;
     fSubt3.PostedFile.SaveAs(Server.MapPath(szUpPath +szSubT3));
    }    
   }
   // 落款
   if (fEndp.Value != "")
   {
    szName = fEndp.PostedFile.FileName;
    szExtension = szName.Substring(szName.IndexOf("."));
    szEndP = szNow+"-ep"+szExtension;
    fEndp.PostedFile.SaveAs(Server.MapPath(szUpPath +szEndP));
   }
   if (fEndt.Value != "")
   {
    szName = fEndt.PostedFile.FileName;
    szExtension = szName.Substring(szName.IndexOf("."));
    szEndT = szNow+"-et"+szExtension;
    fEndt.PostedFile.SaveAs(Server.MapPath(szUpPath +szEndT));
   }
   if (fEndr.Value != "")
   {
    szName = fEndr.PostedFile.FileName;
    szExtension = szName.Substring(szName.IndexOf("."));
    szEndR = szNow+"-er"+szExtension;
    fEndr.PostedFile.SaveAs(Server.MapPath(szUpPath +szEndR));
   }
  
   if (bPreview)
    preview();
   
  }
 
  // 生成专题文件
  private void createSubfile(String szName, String szTxt)
  {
   using (StreamWriter sw = new StreamWriter(szName, true, System.Text.Encoding.GetEncoding(936)))
   {
    // Add some text to the file.
    sw.Write(szTxt);
   }
  }

  private void CreateButton_Click(object sender, System.EventArgs e)
  {
   String szSour = Server.MapPath("../MMBao/");
   String szDest = Server.MapPath("../MMPacket/");
   // 如果没预览过,则直接生成目标文件,否则先上传文件
   if (!bPrev)
   {
    // 上传文件
    uploadfile(false);
   }
   else  // 转移文件路径
   {
    // 设定文件路径
    // 移动文件
    if (szFirstP != "")
     File.Move(szSour+szFirstP, szDest+szFirstP);
    if (szFirstT != "")
     File.Move(szSour+szFirstT, szDest+szFirstT);
    if (szSubP1 != "")
     File.Move(szSour+szSubP1, szDest+szSubP1);
    if (szSubT1 != "")
     File.Move(szSour+szSubT1, szDest+szSubT1);
    if (szSubP2 != "")
     File.Move(szSour+szSubP2, szDest+szSubP2);
    if (szSubT2 != "")
     File.Move(szSour+szSubT2, szDest+szSubT2);
    if (szSubP3 != "")
     File.Move(szSour+szSubP3, szDest+szSubP3);
    if (szSubT3 != "")
     File.Move(szSour+szSubT3, szDest+szSubT3);
    if (szEndP != "")
     File.Move(szSour+szEndP, szDest+szEndP);
    if (szEndT != "")
     File.Move(szSour+szEndT, szDest+szEndT);
    if (szEndR != "")
     File.Move(szSour+szEndR, szDest+szEndR);   
   }
  #region 生成主题文件
   // 生成主题文件
   String szNow = (string)Page.Session["Hzf"] + DateTime.Now.ToString("MMddhhmmss");
   String szTxt;
   String szMMFile="";
   // 首页主题
   if (tbFirst.Text == "")
    szTxt = "首页";
   else
    szTxt = tbFirst.Text;
   szFirstSub = szNow+szTxt+".txt"; // 生成文件名
   createSubfile(szDest+szFirstSub, szTxt);
   szMMFile += szFirstSub;
   if (szFirstP != "")
    szMMFile += ";"+szFirstP;
   if (szFirstT != "")
    szMMFile += ";"+szFirstT;
   // 可选主题
   if (tbSub1.Text == "")
    szTxt = "专题1";
   else
    szTxt = tbSub1.Text;
   szSub1 = szNow+szTxt+".txt"; // 生成文件名
   createSubfile(szDest+szSub1, szTxt);
   szMMFile += ";"+szSub1;
   if (szSubP1 != "")
    szMMFile += ";"+szSubP1;
   if (szSubT1 != "")
    szMMFile += ";"+szSubT1;
   // 动态主题
   if (Int32.Parse(Count.SelectedValue) > 1)
   {
    if (tbSub2.Text == "")
     szTxt = "专题2";
    else
     szTxt = tbSub2.Text;
    szSub2 = szNow+szTxt+".txt"; // 生成文件名
    createSubfile(szDest+szSub2, szTxt);
    szMMFile += ";"+szSub2;
    if (szSubP2 != "")
     szMMFile += ";"+szSubP2;
    if (szSubT2 != "")
     szMMFile += ";"+szSubT2;
   }
   if (Int32.Parse(Count.SelectedValue) > 2)
   {
    if (tbSub3.Text == "")
     szTxt = "专题3";
    else
     szTxt = tbSub3.Text;
    szSub3 = szNow+szTxt+".txt"; // 生成文件名
    createSubfile(szDest+szSub3, szTxt);
    szMMFile += ";"+szSub3;
    if (szSubP3 != "")
     szMMFile += ";"+szSubP3;
    if (szSubT3 != "")
     szMMFile += ";"+szSubT3;
   }
   // 落款主题
   if (tbEnd.Text == "")
    szTxt = "落款";
   else
    szTxt = tbEnd.Text;
   szEndSub = szNow+szTxt+".txt"; // 生成文件名
   createSubfile(szDest+szEndSub, szTxt);
   szMMFile += ";"+szEndSub;
   if (szEndP != "")
    szMMFile += ";"+szEndP;
   if (szEndT != "")
    szMMFile += ";"+szEndT;
   if (szEndR != "")
    szMMFile += ";"+szEndR; 
   #endregion
   // 入库
   
if(CsFuns.setMMInfo(MMName.Text,szMMFile,(string)Page.Session["iType"],(string)Page.Session["Hzf"],(string)Page.Session["Flag
"]))
   {
    CsFuns.WebLogs((string)Page.Session["Name"],"彩信制作成功");
    Response.Write("<script language='javascript'>alert('彩信制作成功');</script>");
   }
   else
   {
    CsFuns.WebLogs((string)Page.Session["Name"],"彩信制作失败");
    Response.Write("<script language='javascript'>alert('彩信制作失败');</script>");
   }
   // 清空处理
   // 变量
   bPrev = false;
   strCount="1";
   szFirstSub="";
   szFirstP="";
   szFirstT="";
   szSub1="";
   szSubP1="";
   szSubT1="";
   szSub2="";
   szSubP2="";
   szSubT2="";
   szSub3="";
   szSubP3="";
   szSubT3="";
   szEndSub="";
   szEndP="";
   szEndT="";
   szEndR="";
   // 控件
   Count.SelectedIndex = 0;
   MMName.Text = (string)Page.Session["Name"];
   ImgFirst.ImageUrl ="view.gif";
   tbFirst.Text = "";
   Image1.ImageUrl = "view.gif";
   tbSub1.Text = "";
   Image2.ImageUrl = "view.gif";
   tbSub2.Text = "";
   Image3.ImageUrl = "view.gif";
   tbSub3.Text = "";
   ImgEnd.ImageUrl = "view.gif";
   tbEnd.Text = "";  
  }
  #region 预览
  private void ViewButton_Click(object sender, System.EventArgs e)
  {
   uploadfile(true);
   bPrev = true;
   // 名字还原
  }
  #endregion