using AIMS;
using AIMSBLL;
using AIMSExtension;
using AIMSModel;
using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Net;
using System.Windows.Forms;
namespace DataDictionary.UI
{
    public partial class frmOperationRoom : Form
    {
        /// 
        /// 手术间集合
        /// 
        public List list;
        public List listNew = new List();
        public List listOnit = new List();
        public string cboDepartmentText;
        /// 
        /// 声明保存数据时的状态
        /// 
        public EditState _state;
        public frmOperationRoom()
        {
            InitializeComponent();
        }
        private void frmOperationRoom_Load(object sender, EventArgs e)
        {
            list = BOperationRoom.Select("  order by Site,[RoomOrder] asc", new ParameterList(), RecursiveType.None, 0);
            ControlExtension.EnabledControl(panel1, false);
            //listOnit = BDepartment.Select("  id in (select roomtype from operationroom) order by id desc ", new ParameterList(), RecursiveType.None, 0);
            //listOnit.Insert(0, new Department() { Id = -1, Name = "" });
            //this.cboDepartment.Items.AddRange(listOnit.ToArray());
            //cboDepartment.ValueMember = "Id";
            //cboDepartment.DisplayMember = "Name";
            //cboDepartment.Text = cboDepartmentText;
            //cboDepartment.Enabled = true;
            BindDgv(list);
        }
        /// 
        /// 为DataGridView绑定数据
        /// 
        private void BindDgv(List list)
        {
            dgvOperationsRoom.AutoGenerateColumns = false;
            dgvOperationsRoom.Rows.Clear();
            int num = 1;
            foreach (OperationRoom item in list)
            {
                try
                {
                    int index = this.dgvOperationsRoom.Rows.Add();
                    this.dgvOperationsRoom.Rows[index].Cells["Id"].Value = item.Id;
                    this.dgvOperationsRoom.Rows[index].Cells["Index"].Value = num;
                    num++;
                    this.dgvOperationsRoom.Rows[index].Cells["oName"].Value = item.Name;
                    this.dgvOperationsRoom.Rows[index].Cells["type"].Value = item.Site;
                    this.dgvOperationsRoom.Rows[index].Cells["Ip1"].Value = item.Ip;
                    this.dgvOperationsRoom.Rows[index].Cells["Ip2"].Value = item.Ip2;
                    this.dgvOperationsRoom.Rows[index].Cells["Ip3"].Value = item.Ip3;
                    this.dgvOperationsRoom.Rows[index].Cells["OrderBy"].Value = item.RoomOrder;
                    this.dgvOperationsRoom.Rows[index].Cells["IsValid"].Value = item.IsValid == 1 ? "有效" : "无效"; 
                }
                catch (Exception)
                {
                }
            }
        }
        /// 
        /// 退出事件
        /// 
        /// 
        /// 
        private void tsbExit_Click(object sender, EventArgs e)
        {
            this.Close();
        }
        /// 
        /// 新增事件
        /// 
        /// 
        /// 
        private void tsbAdd_Click(object sender, EventArgs e)
        {
            //设置状态为新增
            _state = EditState.ADD;
            ControlExtension.EnabledControl(panel1, true);
            ControlExtension.ClearControl(panel1);
        }
        /// 
        /// 修改事件
        /// 
        /// 
        /// 
        private void tsbModify_Click(object sender, EventArgs e)
        {
            //设置状态为修改
            _state = EditState.EDIT;
            if (!(dgvOperationsRoom.SelectedRows.Count > 0))
            {
                MessageBox.Show("请选择列表中的一项!");
                return;
            }
            ControlExtension.EnabledControl(panel1, true);
            txtName.Text = dgvOperationsRoom.SelectedRows[0].Cells["oName"].Value.ToString();
            if (dgvOperationsRoom.SelectedRows[0].Cells["Ip1"].EditedFormattedValue.ToString() != "")
            {
                txtIp1.Text = dgvOperationsRoom.SelectedRows[0].Cells["Ip1"].Value.ToString();
            }
            if (dgvOperationsRoom.SelectedRows[0].Cells["Ip2"].EditedFormattedValue.ToString() != "")
            {
                txtIp2.Text = dgvOperationsRoom.SelectedRows[0].Cells["Ip2"].Value.ToString();
            }
            if (dgvOperationsRoom.SelectedRows[0].Cells["Ip3"].EditedFormattedValue.ToString() != "")
            {
                txtIp3.Text = dgvOperationsRoom.SelectedRows[0].Cells["Ip3"].Value.ToString();
            }
            cbotype.Text = dgvOperationsRoom.SelectedRows[0].Cells["type"].Value.ToString();
            txtOrderBy.Text = dgvOperationsRoom.SelectedRows[0].Cells["OrderBy"].Value.ToString();
            chkIsValid.Checked = dgvOperationsRoom.SelectedRows[0].Cells["IsValid"].Value.ToString() == "有效" ? true : false;
            //try
            //{
            //    cboDepartment.Text = dgvOperationsRoom.SelectedRows[0].Cells["DepartmentNameColumn"].EditedFormattedValue.ToString();
            //}
            //catch (Exception)
            //{
            //}
        }
        /// 
        /// 取消事件
        /// 
        /// 
        /// 
        private void tsbCancel_Click(object sender, EventArgs e)
        {
            ControlExtension.ClearControl(panel1);
            ControlExtension.EnabledControl(panel1, false);
            cboDepartment.SelectedIndex = -1;
            cboDepartment.Text = "";
        }
        /// 
        /// 保存数据事件
        /// 
        /// 
        /// 
        private void tsbSave_Click(object sender, EventArgs e)
        {
            if (!ValidInput())
            {
                return;
            }
            if (_state == EditState.ADD && dgvOperationsRoom.Rows.Count > 0 &&  ControlExtension.ValidDataGridViewExistsItemName(dgvOperationsRoom, "oName", txtName.Text.Trim()))
            {
                MessageBox.Show("该手术间已存在,请重新输入!");
                txtName.Focus();
                return;
            }
            OperationRoom opr = new OperationRoom();
            opr.Name = txtName.Text.Trim();
            opr.HelpCode = PublicMethod.GetFirstLetter(txtName.Text.Trim());
            opr.Ip = txtIp1.Text.Trim();
            opr.Ip2 = txtIp2.Text.Trim();
            opr.Ip3 = txtIp3.Text.Trim();
            opr.Site = cbotype.Text.Trim();
            opr.RoomOrder = Convert.ToInt32(txtOrderBy.Text.Trim());
            opr.IsValid = chkIsValid.Checked == true ? 1 : 0;
            opr.OperatorName = PublicMethod.OperatorName;
            opr.OperatorNo = PublicMethod.OperatorNo;
            opr.OperateDate = DateTime.Now;
            int num = 0;
            if (_state == EditState.ADD)
            {
                num = BOperationRoom.Insert(opr);
            }
            else if (_state == EditState.EDIT)
            {
                opr.Id = Convert.ToInt32(dgvOperationsRoom.SelectedRows[0].Cells["Id"].Value);
                num = BOperationRoom.Update(opr);
            }
            if (num > 0)
            {
                MessageBox.Show("保存成功!");
                ControlExtension.EnabledControl(panel1, false);
                ControlExtension.ClearControl(panel1);
                list = BOperationRoom.Select("  order by Site,[RoomOrder] asc", new ParameterList(), RecursiveType.None, 0); ;
                BindDgv(list);
            }
        }
        /// 
        /// 输入验证
        /// 
        /// 
        private bool ValidInput()
        {
            bool key = false;
            if (txtName.Text.Trim().Length < 1)
            {
                MessageBox.Show("请输入手术间!");
                txtName.Focus();
            }
            else if (txtIp1.Text.Trim().Length < 1)
            {
                MessageBox.Show("请输入一个Ip地址!");
                txtIp1.Focus();
            }
            else if (txtOrderBy.Text.Trim().Length < 1)
            {
                MessageBox.Show("请输入排序顺序!");
                txtOrderBy.Focus();
            }
            else
            {
                key = true;
            }
            return key;
        }
        /// 
        /// 判断DataGridView是否显示全部记录
        /// 
        /// 
        /// 
        private void chkAll_CheckedChanged(object sender, EventArgs e)
        {
            if (chkAll.Checked == true)
            {
                BindDgv(list);
            }
            else
            {
                var results = from p in list
                              where p.IsValid == 1
                              select p;
                BindDgv(results.ToList());
            }
        }
        private void txtIp1_Leave(object sender, EventArgs e)
        {
            TextBox tb = (TextBox)sender;
            if (tb.Text.Trim().Length == 0)
            {
                return;
            }
            IPAddress ip;
            if (!IPAddress.TryParse(tb.Text.Trim(), out ip))
            {
                MessageBox.Show("非法IP地址,请重新输入!");
                tb.Text = "";
                tb.Focus();
            }
        }
        private void cboDepartment_TextUpdate(object sender, EventArgs e)
        {
            //清空combobox
            this.cboDepartment.Items.Clear();
            //清空listNew
            listNew.Clear();
            //遍历全部备查数据
            listNew = BDepartment.Select("IsValid=1 and (HCode like '%" + cboDepartment.Text + "%' or Name like '%" + cboDepartment.Text + "%' )", new ParameterList(), RecursiveType.None, 0);
            if (listNew.Count <= 0)
            {
                listNew = BDepartment.Select("IsValid=1 ", new ParameterList(), RecursiveType.None, 0);
                listNew.Insert(0, new Department() { Id = -1, Name = "全部科室" });
            }
            //combobox添加已经查到的关键词
            this.cboDepartment.Items.AddRange(listNew.ToArray());
            //设置光标位置,否则光标位置始终保持在第一列,造成输入关键词的倒序排列
            this.cboDepartment.SelectionStart = this.cboDepartment.Text.Length;
            //保持鼠标指针原来状态,有时候鼠标指针会被下拉框覆盖,所以要进行一次设置。
            Cursor = Cursors.Default;
            //自动弹出下拉框
            this.cboDepartment.DroppedDown = true;
        }
        private void tsbClear_Click(object sender, EventArgs e)
        {
        }
    }
}