using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.IO;
using HySoft.BaseCallCenter.Web.WebReferenceFax;

namespace HySoft.BaseCallCenter.Web.faxmanage
{
    public partial class faxreceiveshow : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            Response.Expires = -1;
            if (!Page.IsPostBack)
            {
                Response.Expires = -1;
                if (!Page.IsPostBack)
                {
                    if (!string.IsNullOrEmpty(Request.QueryString["singleFlage"]))
                    {
                        Initialize(Request.QueryString["singleFlage"]);
                    }
                }
            }
        }

        #region 信息初始化

        void Initialize(string id)
        {
            try
            {
                Obj = new BLL.T_Fax_RecvFax().GetModel(Convert.ToInt32(id));
            }
            catch (Exception ex)
            {
                Common.SysLog.WriteLog(ex);
            }
        }
        #endregion
        
        /// <summary>
        /// 显示多页tif图片
        /// </summary>
        /// <param name="fielname">文件名</param>
        /// <param name="index">显示页码</param>
        public void showMulTif(string fielname)
        {
            try
            {
                HttpContext context = HttpContext.Current;
                string bgFilePath = context.Server.MapPath(fielname);
                System.Drawing.Image imgObj = System.Drawing.Image.FromFile(bgFilePath);

                //获得第index页的图片
                Guid objGuid = (imgObj.FrameDimensionsList[0]);
                System.Drawing.Imaging.FrameDimension objDimension = new System.Drawing.Imaging.FrameDimension(objGuid);
                //总页数
                int totFrame;
                totFrame = imgObj.GetFrameCount(objDimension);
                txtnowpage.Value = "1";
                txtnowpage1.Value = "1";
                txtpath.Value = fielname;
                viewArea.Src = "faxtoimage.aspx?path=" + fielname + "&page=0";
                spanallcount.InnerHtml = totFrame.ToString();
                spanallcount1.InnerHtml = totFrame.ToString();
                if (totFrame == 1)
                {
                    span2.Disabled = true;
                    span4.Disabled = true;
                    span1.Disabled = true;
                    span3.Disabled = true;
                }
            }
            catch
            { }
        }

        protected void Button1_Click(object sender, EventArgs e)
        {
            try
            {
                HttpContext context = HttpContext.Current;
                string path = context.Server.MapPath(txtpath.Value);

                int start = path.LastIndexOf("\\") + 1;
                int end = path.Length;
                string filename = path.Substring(start, end - start).Replace("|", "");
                FileStream fs = new FileStream(path, FileMode.Open, FileAccess.Read, FileShare.Read);

                byte[] data = new byte[fs.Length];

                fs.Read(data, 0, data.Length);

                Response.Buffer = true;
                Response.Clear();
                Response.ClearContent();
                Response.ClearHeaders();
                Response.ContentType = "image/tiff";
                Response.Charset = "utf-8";
                Response.ContentEncoding = System.Text.Encoding.GetEncoding("GB2312");
                Response.AddHeader("Content-Disposition", "attachment;filename=" + filename + "");
                Response.BinaryWrite(data);
                Response.Flush();
                Response.Close();
            }
            catch
            { }
        }

        #region 公共属性
        private Model.T_Fax_RecvFax Obj
        {
            get
            {
                return null;
            }
            set
            {
                if (value != null)
                {
                    if (value.State == 0)
                    {
                        int i = new BLL.T_Fax_RecvFax().UpdateState(value.FaxID, 1);
                    }
                    this.txtPhone.Value = value.TelNum;
                    this.txtContents.Value = value.Remark;
                    this.txtRecvTime.Value = value.RecvTime.ToString();
                    this.txtName.Value = value.F_Name;

                    string requestpath = "";
                    if (value.FileState == 1)
                    {
                        requestpath = value.TifFilePath.Trim();
                        try
                        {
                            //下载文件、存入到Web服务器返回路径、更新数据库WebFilePath
                            byte[] file;
                            WebReferenceFax.FaxService ws = new WebReferenceFax.FaxService();
                            file = ws.DownloadFile(requestpath);
                            if (file != null)
                            {
                                string _FaxFileName = "";
                                string _Path = "\\FaxFile\\" + DateTime.Now.ToString("yyyyMM");
                                string _SavePath = AppDomain.CurrentDomain.BaseDirectory + _Path;
                                if (!Directory.Exists(_SavePath))
                                {
                                    Directory.CreateDirectory(_SavePath);
                                }
                                string _xdPath = _Path + "\\" + Guid.NewGuid().ToString().Replace("-", "") + "." + requestpath.Substring(requestpath.LastIndexOf(".") + 1);
                                _FaxFileName = AppDomain.CurrentDomain.BaseDirectory + _xdPath;
                                using (FileStream fs = new FileStream(_FaxFileName, FileMode.Create, FileAccess.Write))
                                {
                                    fs.Write(file, 0, file.Length);
                                    fs.Flush();
                                    fs.Close();
                                }
                                Model.T_Fax_RecvFax model = new Model.T_Fax_RecvFax();
                                model.FaxID = value.FaxID;
                                model.FilePath = _xdPath;
                                int r = new BLL.T_Fax_RecvFax().UpdateWebFilePath(model);
                                if (r > 0)
                                {
                                    showMulTif(Server.UrlEncode(_xdPath.Trim()));
                                }
                            }
                        }
                        catch
                        { }
                    }
                    else
                    {
                    }
                }
            }
        }
        #endregion
    }
}