using AIMSBLL;
using AIMSExtension;
using DrawGraph;
using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Windows.Forms;
namespace AIMS.PublicUI.UI
{
    public partial class frmBloodGasAnalysisDict : Form
    {
        bool isChecked = false;
        private string name = string.Empty;
        /// 
        /// 状态
        /// 
        private EditState CurrentState;
        public int _dictid;
        public frmBloodGasAnalysisDict()
        {
            InitializeComponent();
        }
        /// 
        /// 窗体加载
        /// 
        /// 
        /// 
        private void frmBloodGasAnalysisDict_Load(object sender, EventArgs e)
        {
            CurrentState = EditState.BROWSE;
            ControlExtension.ClearControl(panel2);
            ControlExtension.EnabledControl(panel2, false);
            chkAll.Enabled = true;
            chkAll.Checked = isChecked;
            txtOrderBy.Text = "0";
            txtName.Enabled = true;
            Binddgv();
            txtName.TextChanged += new EventHandler(txtName_TextChanged);
            label8.Visible = false;
            label9.Visible = false;
            label10.Visible = false;
            label11.Visible = false;
            txtLow.Visible = false;
            txtHeight.Visible = false;
        }
        /// 
        /// 添加
        /// 
        /// 
        /// 
        private void tsbAdd_Click(object sender, EventArgs e)
        {
            CurrentState = EditState.ADD;
            txtName.TextChanged -= new EventHandler(txtName_TextChanged);
            chkAll.CheckedChanged -= new EventHandler(chkAll_CheckedChanged);
            txtName.TextChanged += new EventHandler(txtName_TextChanged_1);
            ControlExtension.ClearControl(panel2);
            ControlExtension.EnabledControl(panel2, true);
            chkIsValid.Checked = true;
            chkAll.Enabled = true;
            chkAll.Checked = isChecked;
            txtOrderBy.Text = "0";
        }
        /// 
        /// 修改
        /// 
        /// 
        /// 
        private void tsbModify_Click(object sender, EventArgs e)
        {
            if (dgvBloodGasAnalysisDict.RowCount > 0)
            {
                CurrentState = EditState.EDIT;
                txtName.TextChanged -= new EventHandler(txtName_TextChanged);
                txtName.TextChanged -= new EventHandler(txtName_TextChanged);
                chkAll.CheckedChanged -= new EventHandler(chkAll_CheckedChanged);
                txtName.TextChanged += new EventHandler(txtName_TextChanged_1);
                ControlExtension.ClearControl(panel2);
                ControlExtension.EnabledControl(panel2, true);
                chkAll.Enabled = true;
                chkAll.Checked = isChecked;
                _dictid = Convert.ToInt32(dgvBloodGasAnalysisDict.SelectedRows[0].Cells["IdColumn"].Value);
                txtcode.Text = dgvBloodGasAnalysisDict.SelectedRows[0].Cells["Code"].Value == null ? "" : dgvBloodGasAnalysisDict.SelectedRows[0].Cells["Code"].Value.ToString();
                txtEname.Text = dgvBloodGasAnalysisDict.SelectedRows[0].Cells["Ename"].Value.ToString();
                txtshowname.Text = dgvBloodGasAnalysisDict.SelectedRows[0].Cells["showname"].Value.ToString();
                txtName.Text = dgvBloodGasAnalysisDict.SelectedRows[0].Cells["NameColumn"].Value.ToString();
                txtRemark.Text = dgvBloodGasAnalysisDict.SelectedRows[0].Cells["RemarkColumn"].Value == null ? "" : dgvBloodGasAnalysisDict.SelectedRows[0].Cells["RemarkColumn"].Value.ToString();
                txtHCode.Text = dgvBloodGasAnalysisDict.SelectedRows[0].Cells["HCodeColumn"].Value == null ? "" : dgvBloodGasAnalysisDict.SelectedRows[0].Cells["HCodeColumn"].Value.ToString();
                txtOrderBy.Text = dgvBloodGasAnalysisDict.SelectedRows[0].Cells["OrderByColumn"].Value == null ? "" : dgvBloodGasAnalysisDict.SelectedRows[0].Cells["OrderByColumn"].Value.ToString();
                txtLow.Text = dgvBloodGasAnalysisDict.SelectedRows[0].Cells["LowLimit"].Value == null ? "" : dgvBloodGasAnalysisDict.SelectedRows[0].Cells["LowLimit"].Value.ToString();
                txtHeight.Text = dgvBloodGasAnalysisDict.SelectedRows[0].Cells["HighLimit"].Value == null ? "" : dgvBloodGasAnalysisDict.SelectedRows[0].Cells["HighLimit"].Value.ToString();
                if (dgvBloodGasAnalysisDict.SelectedRows[0].Cells["IsValidColumn"].Value.ToString() == "有效")
                {
                    chkIsValid.Checked = true;
                }
                else
                {
                    chkIsValid.Checked = false;
                }
                if (dgvBloodGasAnalysisDict.SelectedRows[0].Cells["isshow"].Value.ToString() == "是")
                {
                    chkisshow.Checked = true;
                }
                else
                {
                    chkisshow.Checked = false;
                }
                if (dgvBloodGasAnalysisDict.SelectedRows[0].Cells["iswarning"].Value.ToString() == "是")
                {
                    chkiswar.Checked = true;
                }
                else
                {
                    chkiswar.Checked = false;
                }
            }
        }
        /// 
        /// 取消
        /// 
        /// 
        /// 
        private void tsbCancel_Click(object sender, EventArgs e)
        {
            CurrentState = EditState.BROWSE;
            ControlExtension.ClearControl(panel2);
            ControlExtension.EnabledControl(panel2, false);
            txtName.TextChanged -= new EventHandler(txtName_TextChanged_1);
            txtName.TextChanged -= new EventHandler(txtName_TextChanged);
            txtName.TextChanged += new EventHandler(txtName_TextChanged);
            chkAll.CheckedChanged += new EventHandler(chkAll_CheckedChanged);
            txtName.Enabled = true;
            chkAll.Enabled = true;
            chkAll.Checked = isChecked;
            Binddgv();
        }
        /// 
        /// 保存
        /// 
        /// 
        /// 
        private void tsbSave_Click(object sender, EventArgs e)
        {
            try
            {
                if (!ValidInput())
                {
                    return;
                }
                BloodGasAnalysisDict BloodGasAnalysisDictObj = new BloodGasAnalysisDict();
                BloodGasAnalysisDictObj.Name = txtName.Text.Trim();
                BloodGasAnalysisDictObj.EName = txtEname.Text.Trim();
                BloodGasAnalysisDictObj.ShowName = txtshowname.Text.Trim();
                BloodGasAnalysisDictObj.Code = txtcode.Text.Trim();
                BloodGasAnalysisDictObj.HCode = txtHCode.Text.Trim();
                BloodGasAnalysisDictObj.IsValid = chkIsValid.Checked == true ? 1 : 0;
                BloodGasAnalysisDictObj.IsShow = chkisshow.Checked == true ? 1 : 0;
                BloodGasAnalysisDictObj.IsWarning = chkiswar.Checked == true ? 1 : 0;
                try
                {
                    if (txtOrderBy.Text.Trim() != "") BloodGasAnalysisDictObj.OrderBy = Convert.ToInt32(txtOrderBy.Text.Trim());
                    else BloodGasAnalysisDictObj.OrderBy = 0;
                    if (txtLow.Text.Trim() != "") BloodGasAnalysisDictObj.LowLimit = Convert.ToInt32(txtLow.Text.Trim());
                    if (txtHeight.Text.Trim() != "") BloodGasAnalysisDictObj.HighLimit = Convert.ToInt32(txtHeight.Text.Trim());
                }
                catch (Exception)
                {
                }
                BloodGasAnalysisDictObj.Remark = txtRemark.Text.Trim();
                if (CurrentState == EditState.ADD)
                {
                    BBloodGasAnalysisDict.Insert(BloodGasAnalysisDictObj);
                    MessageBox.Show("增加成功!");
                }
                if (CurrentState == EditState.EDIT)
                {
                    BloodGasAnalysisDictObj.Id = _dictid;
                    BBloodGasAnalysisDict.Update(BloodGasAnalysisDictObj);
                    MessageBox.Show("修改成功!");
                }
            }
            catch (Exception exp)
            {
                PublicMethod.WriteLog(exp, "");
            }
            tsbCancel_Click(null, null);
        }
        /// 
        /// 输入验证
        /// 
        /// 
        private bool ValidInput()
        {
            bool key = false;
            if (txtName.Text.Trim().Length < 1)
            {
                MessageBox.Show("请输入名称!", "系统提示");
            }
            else if (txtHCode.Text.Trim().Length < 1)
            {
                MessageBox.Show("请输入助记码!", "系统提示");
            }
            else
            {
                key = true;
            }
            if (CurrentState == EditState.ADD)
            {
                List dis = BBloodGasAnalysisDict.Select("Name='" + txtName.Text + "'", null);
                if (dis != null && dis.Count > 0)
                {
                    MessageBox.Show("该名称已存在!", "系统提示");
                    key = false;
                }
                List dis1 = BBloodGasAnalysisDict.Select("showName='" + txtshowname.Text + "'", null);
                if (dis1 != null && dis1.Count > 0)
                {
                    MessageBox.Show("该显示名称已存在!", "系统提示");
                    key = false;
                }
            }
            //if (chkiswar.Checked == true && (txtLow.Text == "" || txtHeight.Text == ""))
            //{
            //    MessageBox.Show("请输入正确的上下限!", "系统提示");
            //    key = false;
            //}
            if (txtLow.Text != "" && txtHeight.Text != "")
            {
                if (decimal.Parse(txtLow.Text) >= decimal.Parse(txtHeight.Text))
                {
                    MessageBox.Show("报警值下线不能高于报警值上限,请重新输入!");
                    txtHeight.Focus();
                    key = false;
                }
            }
            return key;
        }
        /// 
        /// 退出
        /// 
        /// 
        /// 
        private void tsbExit_Click(object sender, EventArgs e)
        {
            Close();
        }
        /// 
        /// 检索
        /// 
        /// 
        /// 
        private void txtName_TextChanged(object sender, EventArgs e)
        {
            Binddgv();
        }
        private void txtName_TextChanged_1(object sender, EventArgs e)
        {
            txtHCode.Text = PublicMethod.GetFirstLetter(txtName.Text);
        }
        /// 
        ///绑定数据 
        /// 
        private void Binddgv()
        {
            chkAll_CheckedChanged(null, null);
        }
        /// 
        /// 显示全部
        /// 
        /// 
        /// 
        private void chkAll_CheckedChanged(object sender, EventArgs e)
        {
            dgvBloodGasAnalysisDict.Rows.Clear();
            List BloodGasAnalysisDictObjList = BBloodGasAnalysisDict.SelectAllDate(txtName.Text);
            isChecked = chkAll.Checked;
            try
            {
                if (chkAll.Checked)
                {
                    for (int i = 0; i < BloodGasAnalysisDictObjList.Count; i++)
                    {
                        int index = this.dgvBloodGasAnalysisDict.Rows.Add();
                        this.dgvBloodGasAnalysisDict.Rows[index].Cells["IdColumn"].Value = BloodGasAnalysisDictObjList[i].Id;
                        this.dgvBloodGasAnalysisDict.Rows[index].Cells["Index"].Value = i + 1;
                        this.dgvBloodGasAnalysisDict.Rows[index].Cells["Code"].Value = BloodGasAnalysisDictObjList[i].Code;
                        this.dgvBloodGasAnalysisDict.Rows[index].Cells["NameColumn"].Value = BloodGasAnalysisDictObjList[i].Name;
                        this.dgvBloodGasAnalysisDict.Rows[index].Cells["EName"].Value = BloodGasAnalysisDictObjList[i].EName;
                        this.dgvBloodGasAnalysisDict.Rows[index].Cells["ShowName"].Value = BloodGasAnalysisDictObjList[i].ShowName;
                        this.dgvBloodGasAnalysisDict.Rows[index].Cells["IsValidColumn"].Value = BloodGasAnalysisDictObjList[i].IsValid == 1 ? "有效" : "无效";
                        this.dgvBloodGasAnalysisDict.Rows[index].Cells["isshow"].Value = BloodGasAnalysisDictObjList[i].IsShow == 1 ? "是" : "否";
                        this.dgvBloodGasAnalysisDict.Rows[index].Cells["IsWarning"].Value = BloodGasAnalysisDictObjList[i].IsWarning == 1 ? "是" : "否";
                        this.dgvBloodGasAnalysisDict.Rows[index].Cells["OrderByColumn"].Value = BloodGasAnalysisDictObjList[i].OrderBy;
                        this.dgvBloodGasAnalysisDict.Rows[index].Cells["RemarkColumn"].Value = BloodGasAnalysisDictObjList[i].Remark;
                        this.dgvBloodGasAnalysisDict.Rows[index].Cells["HCodeColumn"].Value = BloodGasAnalysisDictObjList[i].HCode;
                        this.dgvBloodGasAnalysisDict.Rows[index].Cells["LowLimit"].Value =  BloodGasAnalysisDictObjList[i].LowLimit.ToString().Replace(".00","");
                        this.dgvBloodGasAnalysisDict.Rows[index].Cells["HighLimit"].Value = BloodGasAnalysisDictObjList[i].HighLimit.ToString().Replace(".00", ""); 
                    }
                }
                else
                {
                    var results = from p in BloodGasAnalysisDictObjList
                                  where p.IsValid == 1
                                  select p;
                    List BloodGasAnalysisDictObjListTemp = results.ToList();
                    for (int i = 0; i < BloodGasAnalysisDictObjListTemp.Count; i++)
                    {
                        int index = this.dgvBloodGasAnalysisDict.Rows.Add();
                        this.dgvBloodGasAnalysisDict.Rows[index].Cells["IdColumn"].Value = BloodGasAnalysisDictObjList[i].Id;
                        this.dgvBloodGasAnalysisDict.Rows[index].Cells["Index"].Value = i + 1;
                        this.dgvBloodGasAnalysisDict.Rows[index].Cells["Code"].Value = BloodGasAnalysisDictObjList[i].Code;
                        this.dgvBloodGasAnalysisDict.Rows[index].Cells["NameColumn"].Value = BloodGasAnalysisDictObjList[i].Name;
                        this.dgvBloodGasAnalysisDict.Rows[index].Cells["EName"].Value = BloodGasAnalysisDictObjList[i].EName;
                        this.dgvBloodGasAnalysisDict.Rows[index].Cells["ShowName"].Value = BloodGasAnalysisDictObjList[i].ShowName;
                        this.dgvBloodGasAnalysisDict.Rows[index].Cells["IsValidColumn"].Value = BloodGasAnalysisDictObjList[i].IsValid == 1 ? "有效" : "无效";
                        this.dgvBloodGasAnalysisDict.Rows[index].Cells["isshow"].Value = BloodGasAnalysisDictObjList[i].IsShow == 1 ? "是" : "否";
                        this.dgvBloodGasAnalysisDict.Rows[index].Cells["IsWarning"].Value = BloodGasAnalysisDictObjList[i].IsWarning == 1 ? "是" : "否";
                        this.dgvBloodGasAnalysisDict.Rows[index].Cells["OrderByColumn"].Value = BloodGasAnalysisDictObjList[i].OrderBy;
                        this.dgvBloodGasAnalysisDict.Rows[index].Cells["RemarkColumn"].Value = BloodGasAnalysisDictObjList[i].Remark;
                        this.dgvBloodGasAnalysisDict.Rows[index].Cells["HCodeColumn"].Value = BloodGasAnalysisDictObjList[i].HCode;
                        this.dgvBloodGasAnalysisDict.Rows[index].Cells["LowLimit"].Value = BloodGasAnalysisDictObjList[i].LowLimit.ToString().Replace(".00", ""); ;
                        this.dgvBloodGasAnalysisDict.Rows[index].Cells["HighLimit"].Value = BloodGasAnalysisDictObjList[i].HighLimit.ToString().Replace(".00", ""); ;
                    }
                }
            }
            catch (Exception ex)
            {
                PublicMethod.WriteLog(ex);
            }
        }
        /// 
        /// 选择要修改id血气
        /// 
        /// 
        private List GetSchedulingApplyID()
        {
            List opas = new List();
            for (int i = 0; i < dgvBloodGasAnalysisDict.Rows.Count; i++)
            {
                bool isChoose = bool.Parse(dgvBloodGasAnalysisDict.Rows[i].Cells[0].EditedFormattedValue.ToString());
                if (isChoose)
                {
                    int applyID = Convert.ToInt32(dgvBloodGasAnalysisDict.Rows[i].Cells[1].Value.ToString());
                    opas.Add(applyID);
                }
            }
            if (opas.Count == 0 && dgvBloodGasAnalysisDict.SelectedRows.Count > 0)
            {
                opas.Add(Convert.ToInt32(dgvBloodGasAnalysisDict.SelectedRows[0].Cells[1].Value.ToString()));
            }
            return opas;
        }
        /// 
        /// 是否报警
        /// 
        /// 
        /// 
        private void chkiswar_CheckedChanged(object sender, EventArgs e)
        {
            if (chkiswar.Checked == true)
            {
                label8.Visible = true;
                label9.Visible = true;
                label10.Visible = true;
                label11.Visible = true;
                txtLow.Visible = true;
                txtHeight.Visible = true;
            }
            else
            {
                label8.Visible = false;
                label9.Visible = false;
                label10.Visible = false;
                label11.Visible = false;
                txtLow.Visible = false;
                txtHeight.Visible = false;
            }
        }
        private void txtLow_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (e.KeyChar < 48 || e.KeyChar > 57)
            {
                if (e.KeyChar != 46 && e.KeyChar != 8 && e.KeyChar != 13)
                {
                    e.Handled = true;
                }
            }
            TextBox tb = sender as TextBox;
            if (e.KeyChar == 46)
            {
                int n = tb.Text.LastIndexOf(".");
                if (n > 0) e.Handled = true;
            }
        }
    }
}