using AIMSBLL;
using AxNsoOfficeLib;
using System;
using System.Collections.Generic;
using System.Data;
using System.Drawing;
using System.Windows.Forms;
namespace AIMS.OperationFront.UI
{
    public partial class frmSelectiveOperationsPrint : Form
    {
        /// 
        /// 病历号
        /// 
        public string patCaseNO = "";
        /// 
        /// 患者姓名
        /// 
        public string patname = "";
        public DateTime beginTime;
        public DateTime endTime;
        public DataTable dt;
        public string funcMenuName = string.Empty;
        public frmSelectiveOperationsPrint()
        {
            InitializeComponent();
        }
        private void QuietOperationRecordSheet_Load(object sender, EventArgs e)
        {
            ControlExtension.GetOperationSite(labelSite, CboOperationSite);
            this.CboOperationSite.SelectedIndexChanged += new System.EventHandler(this.CboOperationSite_SelectedIndexChanged);
            dtpTime.Value = beginTime;
            cboOperationRoom.SelectedIndexChanged += new EventHandler(cboOperationRoom_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;
                    this.dgvApplyOrDoctor.Rows[index].Cells["PlanOperationRoomName"].Value = dr["OperationRoom"].ToString();
                    this.dgvApplyOrDoctor.Rows[index].Cells["PlanOrder"].Value = dr["PlanOrder"].ToString();
                    this.dgvApplyOrDoctor.Rows[index].Cells["OrderOperationTime"].Value = DateTime.Parse(dr["OrderOperationTime"].ToString() ).ToString("HH:mm");
                    this.dgvApplyOrDoctor.Rows[index].Cells["DepartmentId"].Value = dr["ApplyDepName"].ToString();
                    this.dgvApplyOrDoctor.Rows[index].Cells["SickBed"].Value = dr["SickBed"].ToString();
                    this.dgvApplyOrDoctor.Rows[index].Cells["InHospitalNo"].Value = dr["MdrecNo"].ToString();
                    this.dgvApplyOrDoctor.Rows[index].Cells["PatientName"].Value = dr["PatientName"].ToString() + "(" + dr["Sex"].ToString() + "," + dr["Age"].ToString() + ")";
                    this.dgvApplyOrDoctor.Rows[index].Cells["ApplyDiagnoseInfoName"].Value = dr["ApplyDiagnoseInfoName"].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["Assistant"].Value = dr["Assistant"].ToString(); 
                    this.dgvApplyOrDoctor.Rows[index].Cells["AnaesthesiaMethodId"].Value = dr["AnaesthesiaMethodName"].ToString(); 
                    this.dgvApplyOrDoctor.Rows[index].Cells["AnesthesiaDoctor"].Value = dr["AnesthesiaDoctor"].ToString(); 
                    this.dgvApplyOrDoctor.Rows[index].Cells["InstrumentNurse"].Value = dr["InstrumentNurse"].ToString(); 
                    this.dgvApplyOrDoctor.Rows[index].Cells["TourNurse"].Value = dr["TourNurse"].ToString(); 
                    this.dgvApplyOrDoctor.Rows[index].Cells["Remark"].Value = dr["Remark"].ToString(); 
                    if (dr["OperationType"].ToString().Equals("急诊"))
                    {
                        this.dgvApplyOrDoctor.Rows[index].DefaultCellStyle.BackColor = Color.Red;
                    }
                    num++;
                }
            }
        }
        private void btnBeforeDay_Click(object sender, EventArgs e)
        {
            dtpTime.Value = dtpTime.Value.AddDays(-1);
            checkBoxX1.Checked = true;
            checkBoxX1_CheckedChanged(null, null);
        }
        private void btnAfterDay_Click(object sender, EventArgs e)
        {
            dtpTime.Value = dtpTime.Value.AddDays(1);
            checkBoxX1.Checked = true;
            checkBoxX1_CheckedChanged(null, null); 
        }
        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 cboOperationRoom_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (cboOperationRoom.SelectedIndex > 0)
            {
                dt = BOperationReview.GetOperationPlanDataTable("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 = BOperationReview.GetOperationPlanDataTable("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);
            checkBoxX1.Checked= true;
            dgvApplyOrDoctor.ClearSelection();
        }
        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;
            }
        }
        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;
        }
        private void rboall_CheckedChanged(object sender, EventArgs e)
        {
            cboOperationRoom_SelectedIndexChanged(null, null);
        }
        private void btnPrint_Click(object sender, EventArgs e)
        {
            GoldPrinter.ExcelAccess excel = new GoldPrinter.ExcelAccess();
            string strFileName = "手术排程模板.xlt"; //模板文件名  
            string strExcelTemplateFile = AppDomain.CurrentDomain.BaseDirectory;
            strExcelTemplateFile += @"\Template\" + strFileName;
            excel.Open(strExcelTemplateFile); //用模板文件
            int rowNum = 4;
            DateTime dtBegin = reBeginTime(dtpTime.Value, "begin");
            DateTime dtEnd = reBeginTime(dtpTime.Value, "end");
            DataTable dtt = new DataTable();
            List SelectIds = GetSelectIds();
            if (SelectIds.Count > 0)
            {
                string Ids = string.Join(",", SelectIds);
                dtt = BOperationReview.GetOperationPlanDataTable(" of1.[ApplyId] in (" + Ids + ") ");
            }
            else
            {
                dtt = BOperationReview.GetOperationPlanDataTable(beginTime, endTime, "");
            }
            excel.SetCellText(3, "L", dtpTime.Value.ToShortDateString());
            for (int j = 0; j < dtt.Rows.Count; j++)
            {
                rowNum++;
                DataRow temp = dtt.Rows[j];
                excel.GetRange(rowNum, "A", rowNum, "R").RowHeight = 24.75;
                excel.GetRange(rowNum, "A", rowNum, "R").Borders.LineStyle = Microsoft.Office.Interop.Excel.XlLineStyle.xlContinuous;
                excel.GetRange(rowNum, "A", rowNum, "R").Value = new string[]{
                    temp[1].ToString(),
                    temp[2].ToString(),
                    temp[3].ToString(),
                    temp[4].ToString(),
                    temp[5].ToString(),
                    temp[6].ToString(),
                    temp[7].ToString(),
                    temp[8].ToString(),
                    temp[9].ToString(),
                    temp[10].ToString(),
                    temp[11].ToString(),
                    temp[12].ToString(),
                    temp[13].ToString(),
                    temp[14].ToString(),
                    temp[15].ToString(),
                    temp[16].ToString(),
                    temp[17].ToString(),
                    temp[18].ToString()
                };
            }
            excel.Print();
            this.Focus();
            excel.Close();
        }
        private void btnReview_Click(object sender, EventArgs e)
        { 
            GoldPrinter.ExcelAccess excel = new GoldPrinter.ExcelAccess();
            string strFileName = "手术排程模板.xlt"; //模板文件名   
            string strExcelTemplateFile = AppDomain.CurrentDomain.BaseDirectory;
            strExcelTemplateFile += @"\Template\" + strFileName;
            excel.Open(strExcelTemplateFile); //用模板文件
            int rowNum = 4;
            DateTime dtBegin = reBeginTime(dtpTime.Value, "begin");
            DateTime dtEnd = reBeginTime(dtpTime.Value, "end");
            DataTable dtt = new DataTable();
            List SelectIds = GetSelectIds();
            if (SelectIds.Count > 0)
            {
                string Ids = string.Join(",", SelectIds);
                dtt = BOperationReview.GetOperationPlanDataTable(" of1.[ApplyId] in (" + Ids + ") ");
            }
            else
            {
                dtt = BOperationReview.GetOperationPlanDataTable(beginTime, endTime, "");
            }
            excel.SetCellText(3, "L", dtpTime.Value.ToShortDateString());
            for (int j = 0; j < dtt.Rows.Count; j++)
            {
                rowNum++;
                DataRow temp = dtt.Rows[j];
                excel.GetRange(rowNum, "A", rowNum, "R").RowHeight = 26;
                excel.GetRange(rowNum, "A", rowNum, "R").WrapText = true;
                excel.GetRange(rowNum, "A", rowNum, "R").Borders.LineStyle = Microsoft.Office.Interop.Excel.XlLineStyle.xlContinuous;
                excel.GetRange(rowNum, "A", rowNum, "R").Value = new string[]{
                    temp[1].ToString(),
                    temp[2].ToString(),
                    temp[3].ToString(),
                    temp[4].ToString(),
                    temp[5].ToString(),
                    temp[6].ToString(),
                    temp[7].ToString(),
                    temp[8].ToString(),
                    temp[9].ToString(),
                    temp[10].ToString(),
                    temp[11].ToString(),
                    temp[12].ToString(),
                    temp[13].ToString(),
                    temp[14].ToString(),
                    temp[15].ToString(),
                    temp[16].ToString(),
                    temp[17].ToString(),
                    temp[18].ToString()
                };
            }
            excel.PrintPreview();
            this.Focus();
            excel.Close(); 
        }
        /// 
        /// 返回指定格式的日期
        /// 
        /// 传入的日期
        /// 标记,begin,end,其它任务标记全返回输入的日期
        /// 
        public DateTime reBeginTime(DateTime dt, string flag)
        {
            if (flag == "begin") return dt.Date.AddSeconds(1);
            if (flag == "end") return dt.Date.AddHours(23).AddMinutes(59).AddSeconds(59);
            return dt;
        }
        private void checkBoxX1_CheckedChanged(object sender, EventArgs e)
        {
            if (checkBoxX1.Checked==true )
            { 
                foreach (DataGridViewRow item in dgvApplyOrDoctor.Rows)
                {
                    item.Cells[0].Value = true;
                }
            }
            else
            {
                foreach (DataGridViewRow item in dgvApplyOrDoctor.Rows)
                {
                    item.Cells[0].Value = false;
                } 
            }
        }
        private void CboOperationSite_SelectedIndexChanged(object sender, EventArgs e)
        {
            //ControlExtension.SetOperationSite(CboOperationSite);
            cboOperationRoom_SelectedIndexChanged(null, null);
        }
    }
}