using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.IO;
using System.Configuration;

namespace BeginScreen
{
    public partial class PatientDocument : System.Web.UI.Page
    {
        public List<PatientDoc> imgdatas;
        public string InHospitalNo;
        public string DocName;
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!this.IsPostBack)
            {
                //PatientDocument.aspx?InHospitalNo=1404780
                //http://10.10.100.36:8090/PatientDocument.aspx?PatId=1592225&Isvalid=2
                //InHospitalNo = Request.QueryString["InHospitalNo"];
                InHospitalNo = Request.QueryString["PatId"];
                string Isvalid = Request.QueryString["Isvalid"];
                DocName = Request.QueryString["DocName"];
                InHospitalNo = InHospitalNo + "_" + Isvalid;
                string str = BindTreeView(InHospitalNo, DocName);
                Response.Write(str);
            }
        }

        public string BindTreeView(string InHospitalNo, string DocName)
        {
            if (InHospitalNo == null) return "";
            string result = "";
            string s1 = "";
            imgdatas = new List<PatientDoc>();
            string PatientDocumentFilePath = ConfigurationManager.AppSettings["PatientDocumentFilePath"].ToString();
            if (string.IsNullOrWhiteSpace(PatientDocumentFilePath))
                PatientDocumentFilePath = @"D:\\PatientDocumentFiles";
            DirectoryInfo TheFolder = new DirectoryInfo(PatientDocumentFilePath);
            //遍历文件
            foreach (FileInfo NextFile in TheFolder.GetFiles())
            {
                if (NextFile.Name.Contains(InHospitalNo))
                {
                    if (DocName != null && DocName != "" && !NextFile.Name.Contains(DocName)) continue;
                    string name = NextFile.Name;
                    if (NextFile.Name.IndexOf('_', 1) > 0)
                        name = NextFile.Name.Substring(0, NextFile.Name.IndexOf('_', 1));
                    PatientDoc pc = new PatientDoc()
                    {
                        Id = imgdatas.Count + 1,
                        InHospitalNo = InHospitalNo,
                        DocName = name,
                        ImgDate = NextFile.Name
                    };
                    imgdatas.Add(pc);
                    s1 = "<img id = 'CurImg' src = '/" + NextFile.Name + "' height = 'auto' width = '780' /><br />";
                    result += "<a href='javascript:void(0);' onclick='fun(this)'>" + NextFile.Name + "</a><br />";
                }
            }
            return "<input type='button' id='print' name='print' value='打印' onclick='javascript: print_page()' /><br />" + result + s1;

        }

        //public void BindTreeView(string InHospitalNo)
        //{
        //    if (InHospitalNo == null) return;
        //    imgdatas = new List<PatientDoc>();
        //    DirectoryInfo TheFolder = new DirectoryInfo(@"D:\\PatientDocumentFiles");
        //    //遍历文件
        //    foreach (FileInfo NextFile in TheFolder.GetFiles())
        //    {
        //        if (NextFile.Name.Contains(InHospitalNo))
        //        {
        //            //FileStream stream = NextFile.OpenRead();
        //            //byte[] imgdata = new byte[stream.Length];
        //            //stream.Read(imgdata, 0, imgdata.Length);
        //            //stream.Close();
        //            string name = NextFile.Name;
        //            if (NextFile.Name.IndexOf('_', 1) > 0)
        //                name = NextFile.Name.Substring(0, NextFile.Name.IndexOf('_', 1));
        //            PatientDoc pc = new PatientDoc()
        //            {
        //                Id = imgdatas.Count + 1,
        //                InHospitalNo = InHospitalNo,
        //                DocName = name,
        //                ImgDate = NextFile.Name
        //            };
        //            imgdatas.Add(pc);
        //        }
        //    }

        //    foreach (var item in imgdatas)
        //    {
        //        TreeNode td = new TreeNode();
        //        td.Text = item.DocName;
        //        td.Value = item.ImgDate;
        //        TreeViewPic.Nodes.Add(td);
        //    }
        //}
        public class PatientDoc
        {
            public int Id { get; set; }
            public string InHospitalNo { get; set; }
            public string DocName { get; set; }
            public string ImgDate { get; set; }
        }

        //protected void TreeViewPic_SelectedNodeChanged(object sender, EventArgs e)
        //{
        //    if (TreeViewPic.SelectedNode.Value != null)
        //    {
        //        Image1.ImageUrl = TreeViewPic.SelectedNode.Value; // "../PatientDocumentFiles/" + TreeViewPic.SelectedNode.Value;
        //        Image1.Visible = true;
        //    }
        //}
    }
}