using AIMS.OremrUserControl;
using AIMS.PublicUI;
using AIMSBLL;
using AIMSModel;
using DataDictionary;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
namespace AIMS.OperationFront.UI
{
    public partial class frmPrintingArrangemen : Form
    {
        /// 
        /// 病历号
        /// 
        public string patCaseNO = "";
        /// 
        /// 患者姓名
        /// 
        public string patname = "";
        public DateTime beginTime;
        public DateTime endTime;
        public DataTable dt;
        public List ArrangePrinting;
        private bool IsSelectAll = false;
        public ToolTip toolTip1 = new System.Windows.Forms.ToolTip();
        public frmPrintingArrangemen()
        {
            InitializeComponent();
            this.toolTip1.AutomaticDelay = 0;
            this.toolTip1.OwnerDraw = true;
            this.toolTip1.ShowAlways = true;
            this.toolTip1.Draw += new System.Windows.Forms.DrawToolTipEventHandler(this.toolTip1_Draw);
        }
        private void frmPrintingArrangemen_Load(object sender, EventArgs e)
        {
            Initial();
            dtpTime.Value = beginTime;
            cboOperationRoom.SelectedIndexChanged += new EventHandler(cboOperationRoom_SelectedIndexChanged);
            flowLayoutPanel1.Focus();
        }
        private void cboOperationRoom_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (cboOperationRoom.SelectedIndex > 0)
            {
                dt = BOperationApply.SelectPlanedOpeByRoom("OrderOperationTime>='" + dtpTime.Value.ToString("yyyy-MM-dd 00:00:00") + "' and OrderOperationTime<='" + dtpTime.Value.ToString("yyyy-MM-dd 23:59:59") + "'  and OperationRoom = '" + cboOperationRoom.Text + "' Order By OperationRoomID ,PlanOrder");
            }
            else if (cboOperationRoom.SelectedIndex <= 0)
            {
                dt = BOperationApply.SelectPlanedOpeByRoom("OrderOperationTime>='" + dtpTime.Value.ToString("yyyy-MM-dd 00:00:00") + "' and OrderOperationTime<='" + dtpTime.Value.ToString("yyyy-MM-dd 23:59:59") + "'  Order By OperationRoomID ,PlanOrder");
            }
            if (CboOperationSite.Visible == true && CboOperationSite.SelectedIndex > 0)
            {
                dt = AIMSExtension.PublicMethod.GetNewDataTable(dt, " OperationSite ='" + CboOperationSite.SelectedValue + "' ", "");
            }
            if (dt == null) return;
            FullDgv(dt);
            dgvApplyOrDoctor.ClearSelection();
        }
        private void ReportData()
        {
            for (int i = 0; i < dt.Rows.Count; i++)
            {
                //UCArrangePrinting UCap = new UCArrangePrinting();
                UCSurgicalNotice UCap = new UCSurgicalNotice();
                UCap.panel1.Height = 430;
                UCap.panel10.Height = 430;//475
                UCap.panel4.Location = new System.Drawing.Point(4, 515);
                try
                {
                    UCap.labHospitalName.Text = "手术通知单";
                    UCap.labName.Text = dt.Rows[i]["PatientName"].ToString();
                    UCap.labAge.Text = dt.Rows[i]["Age"].ToString();
                    UCap.labSex.Text = dt.Rows[i]["Sex"].ToString();
                    UCap.labOperationRoom.Text = dt.Rows[i]["OperationRoom"].ToString();
                    UCap.labDepartments.Text = dt.Rows[i]["ApplyDepName"].ToString();
                    UCap.labBed.Text = dt.Rows[i]["SickBed"].ToString();
                    UCap.labDate.Text =   dt.Rows[i]["OrderOperationTime"].ToString()  ;
                    UCap.labDiagnosis.Text = dt.Rows[i]["ApplyDiagnoseInfoName"].ToString();
                    UCap.labOperation.Text = dt.Rows[i]["ApplyOperationInfoName"].ToString();
                    UCap.labHospitalNo.Text = dt.Rows[i]["MdrecNo"].ToString();
                    UCap.labDoctor.Text = dt.Rows[i]["OperationDoctor"].ToString();
                    UCap.labAnaesthesiaMethod.Text = dt.Rows[i]["AnaesthesiaMethodName"].ToString();
                    UCap.labAssistant1.Text = dt.Rows[i]["Assistant1"].ToString();
                    UCap.labAssistant2.Text = dt.Rows[i]["Assistant2"].ToString();
                    UCap.labAnesthesiaDoctor.Text = dt.Rows[i]["AnesthesiaDoctor"].ToString();
                    if (dt.Rows[i]["InstrumentNurse"].ToString() != "")
                    {
                        UCap.labInstrument.Text = dt.Rows[i]["InstrumentNurse"].ToString();
                    }
                    else
                    {
                        UCap.labInstrument.Text = "";
                    }
                    if (dt.Rows[i]["TourNurse"].ToString() != "")
                    {
                        UCap.labTourNurse.Text = dt.Rows[i]["TourNurse"].ToString();
                    }
                    else
                    {
                        UCap.labTourNurse.Text = "";
                    }
                    UCap.labRemark.Text = dt.Rows[i]["Remark"].ToString();
                    if (dt.Rows[i]["OperationType"].ToString() == "急诊")
                    {
                        UCap.BindWaterMark2("急", 600, 20, Color.Red);
                    }
                    if (dt.Rows[i]["Contagion"].ToString() != "" && dt.Rows[i]["Contagion"].ToString() != "无" && dt.Rows[i]["Contagion"].ToString() != "未报")
                    {
                        UCap.BindWaterMark2(dt.Rows[i]["Contagion"].ToString() , 650, 20, Color.Purple);
                    }
                    UCap.lblOpeLevel.Text = dt.Rows[i]["OperationLevelName"].ToString();
                }
                catch (Exception ex)
                {
                    MessageBox.Show(ex.Message);
                }
                Application.DoEvents();
                flowLayoutPanel1.Controls.Add(UCap);
            }
            //int pcheight = new UCSurgicalNotice().Height;
            //flowLayoutPanel1.Height = dt.Rows.Count * pcheight + 30;
            //this.Height = flowLayoutPanel1.Height + panel1.Height;
        }
        private void tsbNewOperationApply_Click(object sender, EventArgs e)
        {
            flowLayoutPanel1.AutoScroll = false;
            int pcheight = new UCSurgicalNotice().Height;
            flowLayoutPanel1.Height = dt.Rows.Count * pcheight + 30;
            this.Height = flowLayoutPanel1.Height + panel1.Height;
            currentY = 0;
            printDocument1.Print();
            flowLayoutPanel1.AutoScroll = true;
        }
        int currentY = 0; //打印panel2内容时的当前Y坐标,X坐标都是从0开始
        private void printDocument1_PrintPage(object sender, System.Drawing.Printing.PrintPageEventArgs e)
        {
            //待打印区域的宽高
            int awidth = flowLayoutPanel1.Width;
            int aheight = flowLayoutPanel1.Height;
            System.Drawing.Printing.PrintDocument pd = sender as System.Drawing.Printing.PrintDocument;
            //页宽高,Bounds自动会考虑到打印页是横向还是纵向打印
            int pheight = 1140;
            if (currentY < aheight)
            {
                //整个待打印区域
                Bitmap bmp = new Bitmap(awidth, aheight);
                flowLayoutPanel1.DrawToBitmap(bmp, new Rectangle(0, 0, bmp.Width, bmp.Height));
                //打印指定位图的指定区域,可根据实际情况调用Graphics的不同重载方法DrawImage
                e.Graphics.DrawImage(bmp,
                    0, //水平居中
                    0,
                    new RectangleF(0, currentY, bmp.Width, bmp.Height), //打印指定区域
                    GraphicsUnit.Pixel);
                //将当前打印内容的Y坐标设置为打印页高(如有多页,每次都会从当前(0,currentY)开始打印
                currentY += pheight;
                //当整个待打印区域超出了打印页,将表示有多页
                if (aheight - currentY > 0)
                {
                    e.HasMorePages = true;
                }
            }
            else
            {
                e.HasMorePages = false;
            }
        }
        private void dgvApplyOrDoctor_MouseUp(object sender, MouseEventArgs e)
        {
            if (dgvApplyOrDoctor.SelectedRows == null)
                return;
            foreach (DataGridViewRow item in dgvApplyOrDoctor.SelectedRows)
            {
                DataGridViewCell senderdgvr = item.Cells[0];
                senderdgvr.Value = (senderdgvr.EditedFormattedValue.ToString() == "True") ? false : true;
            }
            List SelectIds = GetSelectIds();
            if (SelectIds.Count > 0)
            {
                string Ids = string.Join(",", SelectIds);
                dt = null;
                flowLayoutPanel1.Controls.Clear();
                dt = BOperationApply.SelectPlanedOpeByRoom(" [ApplyId] in (" + Ids + ") ");
                ReportData();
            }
            else
            {
                cboOperationRoom_SelectedIndexChanged(null, null);
            }
        }
        private void dgvApplyOrDoctor_CellMouseEnter(object sender, DataGridViewCellEventArgs e)
        {
            if (e.RowIndex < 0 || e.ColumnIndex < 0)
            {
                return;
            }
            else
            {
                Point mousePos = PointToClient(MousePosition);
                int ei = e.RowIndex;
                string remark = "";
                if (dgvApplyOrDoctor["Operation", e.RowIndex].Value.ToString() != "")
                    remark = "  备注:" + dgvApplyOrDoctor["Remark", e.RowIndex].Value;
                this.toolTip1.Hide(this.dgvApplyOrDoctor);
                this.toolTip1.Show(("科室:" + dgvApplyOrDoctor["DepartmentId", e.RowIndex].Value + "  医师:" + dgvApplyOrDoctor["OperationDoctor", e.RowIndex].Value + "  预约时间:" + dgvApplyOrDoctor["OrderOperationTime", e.RowIndex].Value + "  姓名:" + dgvApplyOrDoctor["PatientName", e.RowIndex].Value + "  住院号:" + dgvApplyOrDoctor["InHospitalNo", e.RowIndex].Value + "  拟施麻醉:" + dgvApplyOrDoctor["AnaesthesiaMethodId", e.RowIndex].Value + "  手术:" + dgvApplyOrDoctor["Operation", e.RowIndex].Value + remark + " \n  \n "), this.dgvApplyOrDoctor, new Point((mousePos.X < 120 ? 120 : mousePos.X), mousePos.Y - 90));
            }
        }
        private void toolTip1_Draw(object sender, DrawToolTipEventArgs e)
        {
            e.Graphics.FillRectangle(Brushes.AliceBlue, e.Bounds);
            e.Graphics.DrawRectangle(Pens.Chocolate, new Rectangle(0, 0, e.Bounds.Width - 1, e.Bounds.Height - 1));
            e.Graphics.DrawString(e.ToolTipText, new System.Drawing.Font("微软雅黑", 12.5F), Brushes.Red, e.Bounds);
        }
        private void dgvApplyOrDoctor_CellMouseLeave(object sender, DataGridViewCellEventArgs e)
        {
            /*鼠标移出单元格后隐藏提示工具*/
            this.toolTip1.Hide(this.dgvApplyOrDoctor);
        }
        private List GetSelectIds()
        {
            List opas = new List();
            foreach (DataGridViewRow item in dgvApplyOrDoctor.Rows)
            {
                bool isChoose = bool.Parse(item.Cells[0].EditedFormattedValue.ToString());
                if (isChoose)
                {
                    string SelectId = item.Cells[1].Value.ToString();// as string;
                    if (SelectId != "")
                        opas.Add(Convert.ToInt32(SelectId));
                }
            }
            return opas;
        }
        /// 
        /// 初始化ComboBox
        /// 
        private void Initial()
        {
            List rooms = BOperationRoom.GetOperationRooms("IsValid=1 and Site='手术室'");
            rooms.Insert(0, new OperationRoom() { Id = -1, Name = "" });
            cboOperationRoom.DataSource = rooms;
            cboOperationRoom.DisplayMember = "Name";
            cboOperationRoom.ValueMember = "Id";
            ControlExtension.GetOperationSite(labelSite,CboOperationSite);
            this.CboOperationSite.SelectedIndexChanged += new System.EventHandler(this.CboOperationSite_SelectedIndexChanged);
        }
        private void FullDgv(DataTable operationApplys)
        {
            if (operationApplys != null)
            {
                dgvApplyOrDoctor.AutoGenerateColumns = false;
                dgvApplyOrDoctor.DataSource = null;
                dgvApplyOrDoctor.Rows.Clear();
                int num = 1;
                foreach (DataRow dr in operationApplys.Rows)
                {
                    int index = this.dgvApplyOrDoctor.Rows.Add();
                    this.dgvApplyOrDoctor.Rows[index].Cells["Id"].Value = dr["ApplyId"].ToString();
                    this.dgvApplyOrDoctor.Rows[index].Cells["Index"].Value = num;
                    num++;
                    this.dgvApplyOrDoctor.Rows[index].Cells["InHospitalNo"].Value = dr["MdrecNo"].ToString();
                    this.dgvApplyOrDoctor.Rows[index].Cells["PlanOperationRoomName"].Value = dr["OperationRoom"].ToString();
                    this.dgvApplyOrDoctor.Rows[index].Cells["PatientName"].Value = dr["PatientName"].ToString() + "(" + dr["Sex"].ToString() + "," + dr["BirthDay"].ToString() + ")";
                    this.dgvApplyOrDoctor.Rows[index].Cells["DepartmentId"].Value = dr["ApplyDepName"].ToString();
                    this.dgvApplyOrDoctor.Rows[index].Cells["Operation"].Value = dr["ApplyOperationInfoName"].ToString();
                    //this.dgvApplyOrDoctor.Rows[index].Cells["OperationDoctor"].Value = dr["OperationDoctor"].ToString();
                    this.dgvApplyOrDoctor.Rows[index].Cells["AnaesthesiaMethodId"].Value = dr["AnaesthesiaMethodName"].ToString();
                    string OrderOperationTime = dr["OrderOperationTime"].ToString();
                    this.dgvApplyOrDoctor.Rows[index].Cells["OrderOperationTime"].Value = DateTime.Parse(OrderOperationTime).ToString("HH:mm");
                    if (dr["OperationType"].ToString().Equals("急诊"))
                    {
                        this.dgvApplyOrDoctor.Rows[index].DefaultCellStyle.BackColor = Color.Red;
                    }
                    this.dgvApplyOrDoctor.Rows[index].Cells["Remark"].Value = dr["Remark"].ToString();
                    //this.dgvApplyOrDoctor.Rows[index].Cells["OperationSite"].Value = dr["OperationSite"].ToString();
                }
            }
        }
        private void btnBeforeDay_Click(object sender, EventArgs e)
        {
            dtpTime.Value = dtpTime.Value.AddDays(-1);
        }
        private void btnAfterDay_Click(object sender, EventArgs e)
        {
            dtpTime.Value = dtpTime.Value.AddDays(1);
        }
        private void dtpTime_ValueChanged(object sender, EventArgs e)
        {
            beginTime = dtpTime.Value.Date;
            endTime = dtpTime.Value.Date.AddHours(23).AddMinutes(59);
            cboOperationRoom_SelectedIndexChanged(null, null);
        }
        private void btnPreview_Click(object sender, EventArgs e)
        {
            if (dgvApplyOrDoctor.SelectedRows == null)
                return;
            List SelectIds = GetSelectIds();
            if (SelectIds.Count > 0)
            {
                for (int i = 0; i < SelectIds.Count; i++)
                {
                    string Ids = string.Join(",", SelectIds);
                    dt = null;
                    flowLayoutPanel1.Controls.Clear();
                    dt = BOperationApply.SelectPlanedOpeByRoom(" [OperationApplyId] in (" + Ids + ") Order By OperationRoom.OrderBy,TableIndex");
                    ReportData();
                }
            }
            else
            {
                cboOperationRoom_SelectedIndexChanged(null, null);
            }
        }
        private void dgvApplyOrDoctor_ColumnHeaderMouseClick(object sender, DataGridViewCellMouseEventArgs e)
        {
            IsSelectAll = IsSelectAll == false ? true : false;
            if (e.ColumnIndex == 0)
            {
                foreach (DataGridViewRow item in dgvApplyOrDoctor.Rows)
                {
                    DataGridViewCell senderdgvr = item.Cells[0];
                    senderdgvr.Value = IsSelectAll;
                }
            }
        }
        private void rboall_CheckedChanged(object sender, EventArgs e)
        {
            cboOperationRoom_SelectedIndexChanged(null, null);
        }
        private void CboOperationSite_SelectedIndexChanged(object sender, EventArgs e)
        {
            //ControlExtension.SetOperationSite(CboOperationSite);
            cboOperationRoom_SelectedIndexChanged(null, null); 
        }
    }
}