using AIMSBLL;
using AIMSExtension;
using AIMSModel;
using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Windows.Forms;
namespace AIMS.OperationAanesthesia
{
    public partial class frmInstrumentRegistration : Form
    {
        /// 
        /// 事件集合
        /// 
        public List list;
        /// 
        /// 声明保存数据时的状态
        /// 
        public EditState _state;
        public frmInstrumentRegistration()
        {
            InitializeComponent();
        }
        private void frmInstrumentRegistration_Load(object sender, EventArgs e)
        {
            dtpOperatorTime.Value = DateTime.Now;
            dgvInstrumentType.AutoGenerateColumns = false;
            dgvInstrument.AutoGenerateColumns = false;
            BindControl();
            BindDgv();
            BinddgvInstrument();
        }
        private void BindControl()
        {
            cboPerformance.Items.Add("");
            cboPerformance.Items.Add("完好");
            cboPerformance.Items.Add("异常");
            cboIntact.Items.Add("");
            cboIntact.Items.Add("是");
            cboIntact.Items.Add("否");
            cboClean.Items.Add("");
            cboClean.Items.Add("是");
            cboClean.Items.Add("否");
        }
        /// 
        /// 为DataGridView绑定数据
        /// 
        private void BindDgv()
        {
            DataTable dt = BInstrumentRegistration.SelectGroupByName(" group by name,numbers ");
            dgvInstrumentType.DataSource = null;
            dgvInstrumentType.DataSource = dt;
        }
        private void BinddgvInstrument()
        {
            list = BInstrumentRegistration.Select("  order by OperatorTime desc ", new ParameterList(), RecursiveType.None, 0);//name='" + selname + "' and numbers='" + selNumbers + "'
            dgvInstrument.DataSource = null;
            if (list.Count <= 0)
            {
                BindDgv();
                selname = "";
                selNumbers = "";
                etid = null;
                dgvInstrumentType.ClearSelection();
            }
            else
            {
                dgvInstrument.DataSource = list;
                dgvInstrument.ClearSelection();
            }
        }
        /// 
        /// 保存
        /// 
        /// 
        /// 
        private void tsbSave_Click(object sender, EventArgs e)
        {
            InstrumentRegistration ir = new InstrumentRegistration();
            ir.Name = cboName.Text.Trim();
            ir.Numbers = cboNumbers.Text.Trim();
            ir.Performance = cboPerformance.Text.Trim();
            ir.Intact = cboIntact.Text.Trim();
            ir.OperatorName = txtOperatorName.Text.Trim();
            ir.OperatorTime = DateTime.Parse(dtpOperatorTime.Value.ToString("yyyy-MM-dd 00:00:00"));
            ir.Remark = txtRemark.Text.Trim();
            ir.IsClean = cboClean.Text.Trim();
            int num = 0;
            List UpdateIr = BInstrumentRegistration.Select(" name='" + ir.Name + "' and numbers='" + ir.Numbers + "' and OperatorTime ='" + ir.OperatorTime + "' ", new ParameterList(), RecursiveType.None, 0);
            if (UpdateIr != null && UpdateIr.Count > 0)
                ir.Id = UpdateIr[0].Id;
            if (ir.Id != null && ir.Id != 0)
                num = BInstrumentRegistration.Update(ir);
            else
                num = BInstrumentRegistration.Insert(ir);
            if (num > 0)
            {
                MessageBox.Show("保存成功!");
                if (selname != "" && selNumbers != "" && selname == ir.Name && selNumbers == ir.Numbers)
                {
                    BinddgvInstrument();
                }
                else
                {
                    BindDgv();
                    selname = ir.Name;
                    selNumbers = ir.Numbers;
                    etid = null;
                    BinddgvInstrument();
                    dgvInstrument.ClearSelection();
                }
                cboPerformance.Text = "";
                cboIntact.Text = "";
                txtOperatorName.Text = "";
                txtRemark.Text = "";
                cboClean.Text = "";
            }
        }
        /// 
        /// 删除
        /// 
        /// 
        /// 
        private void tsbExit_Click(object sender, EventArgs e)
        {
            if (etid != null)
            {
                InstrumentRegistration ir = BInstrumentRegistration.SelectSingle(etid, RecursiveType.None, 0);
                string messang = string.Format("确定要删除:{0}吗?", ir.Name);
                DialogResult dr = MessageBox.Show(messang, "删除提示", MessageBoxButtons.OKCancel, MessageBoxIcon.Warning);
                if (dr == System.Windows.Forms.DialogResult.OK)
                {
                    BInstrumentRegistration.Delete(ir);
                    BindDgv();
                    BinddgvInstrument();
                    etid = null;
                    cboPerformance.Text = "";
                    cboIntact.Text = "";
                    txtOperatorName.Text = "";
                    txtRemark.Text = "";
                    cboClean.Text = "";
                }
            }
            else if (selname != "")
            {
                string messang = string.Format("确定要删除:{0}吗?", selname + " " + selNumbers);
                DialogResult dr = MessageBox.Show(messang, "删除提示", MessageBoxButtons.OKCancel, MessageBoxIcon.Warning);
                if (dr == System.Windows.Forms.DialogResult.OK)
                {
                    BInstrumentRegistration.Delete("  name='" + selname + "' and numbers='" + selNumbers + "'  ", null);
                    BindDgv();
                    BinddgvInstrument();
                    selname = "";
                    selNumbers = "";
                    cboName.Text = "";
                    cboNumbers.Text = "";
                    cboPerformance.Text = "";
                    cboIntact.Text = "";
                    txtOperatorName.Text = "";
                    txtRemark.Text = "";
                    cboClean.Text = "";
                }
            }
        }
        private void toolStripButton1_Click(object sender, EventArgs e)
        {
            Close();
        }
        string selname;
        string selNumbers;
        private void dgvInstrumentType_CellClick(object sender, DataGridViewCellEventArgs e)
        {
            if (dgvInstrumentType.SelectedRows == null || dgvInstrumentType.SelectedRows.Count <= 0) return;
            selname = dgvInstrumentType.SelectedRows[0].Cells[0].Value.ToString();
            selNumbers = dgvInstrumentType.SelectedRows[0].Cells[1].Value.ToString();
            //BinddgvInstrument();
            cboName.Text = selname;
            cboNumbers.Text = selNumbers;
            etid = null;
            cboPerformance.Text = "";
            cboIntact.Text = "";
            txtOperatorName.Text = "";
            txtRemark.Text = "";
            cboClean.Text = "";
        }
        public int? etid;
        private void dgvInstrument_CellClick(object sender, DataGridViewCellEventArgs e)
        {
            if (!(dgvInstrument.SelectedRows.Count > 0))
            {
                return;
            }
            cboPerformance.Text = "";
            cboIntact.Text = "";
            txtOperatorName.Text = "";
            txtRemark.Text = "";
            etid = Convert.ToInt32(dgvInstrument.SelectedRows[0].Cells["Id"].Value);
            cboName.Text = dgvInstrument.SelectedRows[0].Cells["irnanme"].Value.ToString();
            cboNumbers.Text = dgvInstrument.SelectedRows[0].Cells["Numbers"].Value.ToString();
            cboPerformance.Text = dgvInstrument.SelectedRows[0].Cells["Performance"].Value.ToString();
            cboIntact.Text = dgvInstrument.SelectedRows[0].Cells["Intact"].Value.ToString();
            txtOperatorName.Text = dgvInstrument.SelectedRows[0].Cells["OperatorName"].Value.ToString();
            dtpOperatorTime.Value = DateTime.Parse(DateTime.Parse(dgvInstrument.SelectedRows[0].Cells["OperatorTime"].Value.ToString()).ToString("yyyy-MM-dd 00:00:00"));
            txtRemark.Text = dgvInstrument.SelectedRows[0].Cells["Remark"].Value.ToString();
            cboClean.Text = dgvInstrument.SelectedRows[0].Cells["IsClean"].Value.ToString();
        }
        private void tspPrint_Click(object sender, EventArgs e)
        {
        }
    }
}