using Newtonsoft.Json;
using AIMSExtension;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Windows.Forms;
using DevComponents.DotNetBar.Controls;
using Newtonsoft.Json.Linq;
using System.Drawing;
using AIMS.Infrastructure.CA;
using System.Data;
using DevComponents.WinForms.Drawing;
namespace DrawGraph
{
    [Serializable, JsonObject(MemberSerialization.OptOut)]
    public class BaseInfoBottomManage : AreaManageBase
    {
        private OperationRecord myOpeRecord = null;
        public BaseInfoBottomManage() { }
        public BaseInfoBottomManage(object _operationRecor, ZedGraphControl _zedControl, TemplateManage _template, string _name) : base(_operationRecor, _zedControl, _template, _name)
        {
            init();
        }
        public void init()
        {
            //自己要用的手术对象
            myOpeRecord = OpeRecord as OperationRecord;
        }
        #region 重写的事件
        /// 
        /// 鼠标点击画板
        /// 
        /// 
        /// 
        public override void MouseDown(ZedGraphControl sender, MouseEventArgs e)
        {
            //if (e.Button == System.Windows.Forms.MouseButtons.Left)
            //{
            //    MessageBox.Show(this.GetType().Name + "is Click Left Button");
            //}
        }
        public override void MouseMove(ZedGraphControl sender, MouseEventArgs e)
        {
        }
        public override void MouseUp(ZedGraphControl sender, MouseEventArgs e)
        {
        }
        public override void MouseDoubleClick(ZedGraphControl sender, MouseEventArgs e)
        {
        }
        public override void KeyUp(ZedGraphControl sender, KeyEventArgs e)
        {
        }
        public SelectWorkerValue aSyncSelectDict = null;
        public override void Bind()
        {
            init();
            aSyncSelectDict = SelectWorkerValue.CreateInstance();
            List ables = PackManage.ListPob.Where(s => s is AbleEditPackObj).ToList();
            foreach (PackObjBase pack in ables)
            {
                AbleEditPackObj ableEdit = pack as AbleEditPackObj;
                SetAbleEditView(ableEdit);
            }
        }
        /// 
        /// 设置可编辑组件的显示样式
        /// 
        /// 
        private void SetAbleEditView(AbleEditPackObj ableEdit)
        {
            string span = "";
            double spanSum = Math.Round((float)(ableEdit.OneUnitCount / 12));
            for (int i = 0; i < spanSum; i++)
            {
                span += " ";
            }
            string text = "", value = "";
            if (ableEdit != null)
            {
                switch (ableEdit.ControlType)
                {
                    case EControlType.Directory:
                        value = ableEdit.PackValue;
                        text = DBManage.GetDictionaryValuesById(ableEdit.PackValue, ableEdit.ControlTitleText);
                        if (ableEdit.ControlTitleText.Contains("CA") || ableEdit.ControlTitleText.Contains("签名"))
                        {
                            if (ableEdit.PackValue != null && ableEdit.PackValue != "")
                            {
                                SetCAAbleEditPackObj(ableEdit);
                            }
                        }
                        //设置属性的值
                        template.SetObjValue(OpeRecord, ableEdit.ClassDataSourceName, text, value);
                        break;
                    case EControlType.Directorys:
                        value = ableEdit.PackValue;
                        if (ableEdit.ClassDataSourceName == "OperationRecord.OperationDoctor" && myOpeRecord.OpeRecordInfo.Extend4 != null && myOpeRecord.OpeRecordInfo.Extend4 != "" && myOpeRecord.OpeRecordInfo.Extend4 != "|")
                        {
                            text = DBManage.GetDictionaryValuesById(ableEdit.PackValue, ableEdit.ControlTitleText, myOpeRecord.OpeRecordInfo.Extend4);
                        }
                        else if (ableEdit.ClassDataSourceName == "OperationRecord.AnesthesiaDoctor" && myOpeRecord.OpeRecordInfo.Extend5 != null && myOpeRecord.OpeRecordInfo.Extend5 != "" && myOpeRecord.OpeRecordInfo.Extend5 != "|")
                        {
                            text = DBManage.GetDictionaryValuesById(ableEdit.PackValue, ableEdit.ControlTitleText, myOpeRecord.OpeRecordInfo.Extend5);
                        }
                        else
                        {
                            text = DBManage.GetDictionaryValuesById(ableEdit.PackValue, ableEdit.ControlTitleText);
                        }
                        if (ableEdit.ControlTitleText == "人员" && text == "")
                        {
                            text = "无";
                        }
                        TokenEditorManage.SetAutoSizeRowSpan(template, ableEdit, text);
                        //设置属性的值
                        template.SetObjValue(OpeRecord, ableEdit.ClassDataSourceName, text, value);
                        break;
                    default:
                        break;
                }
            }
        }
        #endregion 重写的事件结束
        /// 
        /// 响应可编辑区域的点击事件
        /// 
        /// 
        /// 
        public override void editAr_Click(object sender, EventArgs e)
        {
            try
            {
                //传过来的数据是不是可编辑的
                AbleEditPackObj aEdit1S = sender as AbleEditPackObj;
                if (aEdit1S == null) return;
                //先把所有WINFORM组件隐藏
                foreach (PackObjBase pack in PackManage.ListPob)
                {
                    AbleEditPackObj aEdit = pack as AbleEditPackObj;
                    if (aEdit != null)
                    {
                        aEdit.IsVisible = false;
                    }
                }
                SelectWorkerValue.Hidden();
                //这句话很重要,只操作自己管理器里的组件
                AbleEditPackObj aEdit1 = PackManage.ListPob.FirstOrDefault(s => s.PackTag == aEdit1S.PackTag) as AbleEditPackObj;
                //指定的组件显示
                if (aEdit1 != null)
                {
                    Control conl = aEdit1.CControl;
                    conl.Leave -= new EventHandler(txt_Leave);
                    aEdit1.IsVisible = !aEdit1.IsVisible;
                    aEdit1S.CControl.MouseDown -= new MouseEventHandler(CControl_MouseDown);
                    aEdit1S.CControl.KeyUp -= new KeyEventHandler(CControl_KeyUp);
                    aEdit1S.CControl.TextChanged -= new EventHandler(CControl_TextChanged);
                    aEdit1S.CControl.KeyPress -= new KeyPressEventHandler(CControl_KeyPress); ;
                    //根据数据源名称进行不同的事件处理
                    switch (aEdit1.ClassDataSourceName)
                    {
                        case "OperationRecord.AnesthesiaDoctor": //麻醉医生
                            Worker_Click(aEdit1, e, 2);
                            break;
                        case "OperationRecord.SignName": //麻醉医生签名
                            Workerqm_Click(aEdit1, e, 2);
                            break;
                        case "OperationRecord.OperationDoctor":
                            Worker_Click(aEdit1, e, 0);
                            break;
                        case "OperationRecord.InstrumentNurse":
                            Worker_Click(aEdit1, e, 3);
                            break;
                        case "OperationRecord.Assistant1":
                            Worker_Click(aEdit1, e, 0);
                            break;
                        case "OperationRecord.AnaesthesiaMethodName":
                            Anaes_Click(aEdit1, e);
                            break;
                        case "OperationRecord.OperationSiteId": //手术体位
                            TYZD_Click(aEdit1, e);
                            break;
                        case "OperationRecord.OperationPositionId": //手术体位
                            TYZD_Click(aEdit1, e);
                            break;
                        case "OperationRecord.TourNurse":
                            Worker_Click(aEdit1, e, 3);
                            break;
                        case "OperationRecord.Operation": //手术名称
                            ope_Click(aEdit1, e);
                            break;
                        case "OperationRecord.Diagnose": //手术名称
                            opeDisease_Click(aEdit1, e);
                            break;
                        case "OperationRecord.OrtherDoctorId":
                            Worker_Click(aEdit1, e, 4);
                            break;
                        case "OperationRecord.OpeRecoverOutInfo.Extend1":
                            ((TextBox)conl).BorderStyle = BorderStyle.Fixed3D;
                            ((TextBox)conl).Focus();
                            ((TextBox)conl).Select(((TextBox)conl).Text.Length, 1);
                            conl.Leave += new EventHandler(txt_Leave);
                            break;
                        case "OperationRecord.OpeRecoverOutInfo.Extend2":
                            ((TextBox)conl).BorderStyle = BorderStyle.Fixed3D;
                            ((TextBox)conl).Focus();
                            ((TextBox)conl).Select(((TextBox)conl).Text.Length, 1);
                            conl.Leave += new EventHandler(txt_Leave);
                            break;
                            //default:
                            //    //在此处写日志
                            //    aEdit1.IsVisible = !aEdit1.IsVisible;
                            //    MessageBox.Show("没找到-" + aEdit1.ClassDataSourceName + "-属性的事件");
                            //    break;
                    }
                }
            }
            catch (Exception exp)
            {
                MessageBox.Show(exp.Message);
            }
            finally
            {
            }
        }
        private void txt_Leave(object sender, EventArgs e)
        {
            Control control = (Control)sender;
            if (control == null) return;
            AbleEditPackObj ableEdit = control.Tag as AbleEditPackObj;
            if (ableEdit == null) return;
            try
            {
                string DataSourceName = ableEdit.ClassDataSourceName;
                bool updateOk = true;
                string text = "";
                string value = "";
                if (!updateOk)
                {
                    if (ableEdit.ControlType == EControlType.RadioButtonList)
                        ableEdit.IsVisible = true;
                    else
                        ableEdit.IsVisible = !ableEdit.IsVisible;
                }
                else
                {
                    text = control.Text.Trim();
                    value = control.Text.Trim();
                    //设置属性的值
                    ableEdit.IsVisible = !ableEdit.IsVisible;
                    template.SetObjValue(OpeRecord, DataSourceName, text, value, true);
                    ((TextBox)control).BorderStyle = BorderStyle.None;
                }
            }
            catch (Exception ex)
            {
                //写日志
                PublicMethod.WriteLog(ex);
            }
            finally
            {
            }
        }
        #region 可编辑区域响应事件
        private void Worker_Click(AbleEditPackObj sender, EventArgs e, int _workersType)
        {
            try
            {
                AbleEditPackObj ableEdit = sender;
                if (ableEdit == null) return;
                if (myOpeRecord == null) return;
                if (ableEdit.ControlType == EControlType.Directory)
                {
                    if (ableEdit.ControlTitleText.Contains("CA") || ableEdit.ControlTitleText.Contains("签名"))
                    {
                        Workerqm_Click(ableEdit, e, _workersType);
                    }
                    else
                    {
                        ableEdit.CControl.MouseDown -= new MouseEventHandler(CControl_MouseDown);
                        ableEdit.CControl.MouseDown += new MouseEventHandler(CControl_MouseDown);
                        ableEdit.CControl.KeyUp -= new KeyEventHandler(CControl_KeyUp);
                        ableEdit.CControl.KeyUp += new KeyEventHandler(CControl_KeyUp);
                        ableEdit.CControl.KeyPress -= new KeyPressEventHandler(CControl_KeyPress);
                        ableEdit.CControl.KeyPress += new KeyPressEventHandler(CControl_KeyPress);
                        SelectWorkerValue.ChageText = "";
                        if (myOpeRecord != null)
                        {
                            Control conl = sender.CControl;
                            if (conl is TextBox)
                            {
                                ((TextBox)conl).BorderStyle = BorderStyle.Fixed3D;
                                ((TextBox)conl).Focus();
                                if (sender.PackValue != null && sender.PackValue.Trim() != "")
                                {
                                    if (((TextBox)conl).Text.LastIndexOf(',') != sender.PackText.Length)
                                        ((TextBox)conl).Text = ((TextBox)conl).Text + ",";
                                    ((TextBox)conl).Select(((TextBox)conl).Text.Length, 1);
                                }
                            }
                            ableEdit.CControl.TextChanged -= new EventHandler(CControl_TextChanged);
                            ableEdit.CControl.TextChanged += new EventHandler(CControl_TextChanged);
                            aSyncSelectDict.Show(template, OpeRecord, sender, _workersType.ToString());
                        }
                    }
                }
                else if (ableEdit.ControlType == EControlType.Directorys)
                {
                    SetDirectorysMethod(ableEdit);
                }
            }
            catch (Exception ex)
            {
                AIMSExtension.PublicMethod.WriteLog(ex);
            }
        }
        private void CControl_TextChanged(object sender, EventArgs e)
        {
            if (myOpeRecord != null)
            {
                Control conl = sender as Control;
                aSyncSelectDict.ShowSel(conl, OpeRecord);
            }
        }
        private void CControl_KeyUp(object sender, KeyEventArgs e)
        {
            if (myOpeRecord != null)
            {
                Control conl = sender as Control;
                if (e.KeyCode == Keys.Delete || e.KeyCode == Keys.Back)
                {
                    if (conl.Text != SelectWorkerValue.Value && SelectWorkerValue.ChageText == "")
                    {
                        aSyncSelectDict.DelSel(conl, OpeRecord);
                        SelectDictValue.ChageText = "";
                    }
                }
                else if (e.KeyCode == Keys.Down)
                {
                    if (SelectWorkerValue.dgvZd.CurrentRow.Index + 1 != SelectWorkerValue.dgvZd.Rows.Count - 1)
                    {
                        SelectWorkerValue.dgvZd.CurrentCell = SelectWorkerValue.dgvZd.Rows[SelectWorkerValue.dgvZd.CurrentRow.Index + 1].Cells[1];
                    }
                }
                else if (e.KeyCode == Keys.Up)
                {
                    if (SelectWorkerValue.dgvZd.CurrentRow.Index != 0)
                    {
                        SelectWorkerValue.dgvZd.CurrentCell = SelectWorkerValue.dgvZd.Rows[SelectWorkerValue.dgvZd.CurrentRow.Index - 1].Cells[1];
                    }
                }
            }
        }
        private void CControl_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (e.KeyChar.ToString() == "\r")
            {
                if (SelectWorkerValue.dgvZd.SelectedRows.Count > 0)
                {
                    SelectWorkerValue.SetContent(SelectWorkerValue.dgvZd.SelectedRows[0].Index);
                }
            }
        }
        private void CControl_MouseDown(object sender, MouseEventArgs e)
        {
            if (myOpeRecord != null)
            {
                TextBox conl = sender as TextBox;
                string leftStr = conl.Text.Substring(0, conl.SelectionStart);
                string rightStr = conl.Text.Substring(conl.SelectionStart, conl.Text.Length - conl.SelectionStart);
                int startIndex = 0;
                int endLength = conl.Text.Length;
                if (leftStr.Contains(","))
                {
                    int beginindex = leftStr.LastIndexOf(',');
                    startIndex = beginindex + 1;
                    if (rightStr.Contains(","))
                    {
                        endLength = rightStr.IndexOf(",") + (leftStr.Length - beginindex);
                    }
                }
                else if (leftStr.Contains("+"))
                {
                    int beginindex = leftStr.LastIndexOf('+');
                    startIndex = beginindex + 1;
                    if (rightStr.Contains("+"))
                    {
                        endLength = rightStr.IndexOf("+") + (leftStr.Length - beginindex);
                    }
                }
                else
                {
                    if (rightStr.Contains(","))
                    {
                        endLength = rightStr.IndexOf(",") + leftStr.Length + 1;
                    }
                    else if (rightStr.Contains("+"))
                    {
                        endLength = rightStr.IndexOf("+") + leftStr.Length + 1;
                    }
                }
                conl.Select(startIndex, endLength);
            }
        }
        private void ope_Click(AbleEditPackObj sender, EventArgs e)
        {
            try
            {
                AbleEditPackObj ableEdit = sender;
                if (ableEdit == null) return;
                if (myOpeRecord == null) return;
                if (ableEdit.ControlType == EControlType.Directory)
                {
                    SelDict(sender, e, ableEdit.ControlTitleText);
                }
                else if (ableEdit.ControlType == EControlType.Directorys)
                {
                    SetDirectorysMethod(ableEdit);
                }
            }
            catch (Exception ex)
            {
                AIMSExtension.PublicMethod.WriteLog(ex);
            }
        }
        private void opeDisease_Click(AbleEditPackObj sender, EventArgs e)
        {
            try
            {
                AbleEditPackObj ableEdit = sender;
                if (ableEdit == null) return;
                if (myOpeRecord == null) return;
                if (ableEdit.ControlType == EControlType.Directory)
                {
                    SelDict(sender, e, ableEdit.ControlTitleText);
                }
                else if (ableEdit.ControlType == EControlType.Directorys)
                {
                    SetDirectorysMethod(ableEdit);
                }
            }
            catch (Exception ex)
            {
                AIMSExtension.PublicMethod.WriteLog(ex);
            }
        }
        private void Anaes_Click(AbleEditPackObj sender, EventArgs e)
        {
            try
            {
                AbleEditPackObj ableEdit = sender;
                if (ableEdit == null) return;
                if (myOpeRecord == null) return;
                if (ableEdit.ControlType == EControlType.Directory)
                {
                    SelDict(sender, e, ableEdit.ControlTitleText);
                }
                else if (ableEdit.ControlType == EControlType.Directorys)
                {
                    SetDirectorysMethod(ableEdit);
                }
            }
            catch (Exception ex)
            {
                AIMSExtension.PublicMethod.WriteLog(ex);
            }
        }
        //通用字典分组窗体打开
        private void TYZD_Click(AbleEditPackObj sender, EventArgs e, bool isRadio = false)
        {
            try
            {
                AbleEditPackObj ableEdit = sender;
                if (ableEdit == null) return;
                if (myOpeRecord == null) return;
                if (ableEdit.ControlType == EControlType.Directory)
                {
                    SelDict(sender, e, ableEdit.ControlTitleText);
                }
                else if (ableEdit.ControlType == EControlType.Directorys)
                {
                    SetDirectorysMethod(ableEdit);
                }
            }
            catch (Exception ex)
            {
                AIMSExtension.PublicMethod.WriteLog(ex);
            }
        }
        private void SelDict(AbleEditPackObj sender, EventArgs e, string _DictType)
        {
            AbleEditPackObj ableEdit = sender;
            if (ableEdit == null) return;
            ableEdit.CControl.MouseDown -= new MouseEventHandler(CControl_MouseDown);
            ableEdit.CControl.MouseDown += new MouseEventHandler(CControl_MouseDown);
            ableEdit.CControl.KeyUp -= new KeyEventHandler(CControl_KeyUp);
            ableEdit.CControl.KeyUp += new KeyEventHandler(CControl_KeyUp);
            ableEdit.CControl.KeyPress -= new KeyPressEventHandler(CControl_KeyPress);
            ableEdit.CControl.KeyPress += new KeyPressEventHandler(CControl_KeyPress);
            if (myOpeRecord != null)
            {
                Control conl = sender.CControl;
                if (conl is TextBox)
                {
                    ((TextBox)conl).BorderStyle = BorderStyle.Fixed3D;
                    ((TextBox)conl).Focus();
                    if (sender.PackValue != null && sender.PackValue.Trim() != "")
                    {
                        if (ableEdit.ClassDataSourceName == "OperationRecord.AnaesthesiaMethodName")
                        {
                            if (((TextBox)conl).Text.LastIndexOf('+') != sender.PackText.Length)
                                ((TextBox)conl).Text = ((TextBox)conl).Text + "+";
                            ((TextBox)conl).Select(((TextBox)conl).Text.Length, 1);
                        }
                        else
                        {
                            if (((TextBox)conl).Text.LastIndexOf(',') != sender.PackText.Length)
                                ((TextBox)conl).Text = ((TextBox)conl).Text + ",";
                            ((TextBox)conl).Select(((TextBox)conl).Text.Length, 1);
                        }
                    }
                }
                ableEdit.CControl.TextChanged -= new EventHandler(CControl_TextChanged);
                ableEdit.CControl.TextChanged += new EventHandler(CControl_TextChanged);
                aSyncSelectDict.Show(template, OpeRecord, sender, _DictType);
            }
        }
        #endregion
        private void SetDirectorysMethod(AbleEditPackObj ableEdit)
        {
            DevComponents.DotNetBar.Controls.TokenEditor tokenEditor = ableEdit.CControl as DevComponents.DotNetBar.Controls.TokenEditor;
            tokenEditor.EditTextBox.DoubleClick -= EditTextBox_DoubleClick;
            tokenEditor.EditTextBox.DoubleClick += EditTextBox_DoubleClick;
            tokenEditor.EditTextBox.TextChanged -= EditTextBox_TextChanged;
            tokenEditor.EditTextBox.TextChanged += EditTextBox_TextChanged;
            tokenEditor.Leave -= TokenEditor_Leave;
            tokenEditor.Leave += TokenEditor_Leave;
            if ((PublicMethod.RoleName.Contains("增加诊断") && ableEdit.ControlTitleText == "诊断") || (AIMSExtension.PublicMethod.RoleName.Contains("增加手术") && ableEdit.ControlTitleText == "手术"))
            {
                tokenEditor.ValidateToken -= new DevComponents.DotNetBar.Controls.ValidateTokenEventHandler(this.tokenEditor_ValidateToken);
                tokenEditor.ValidateToken += new DevComponents.DotNetBar.Controls.ValidateTokenEventHandler(this.tokenEditor_ValidateToken);
                tokenEditor.DoubleClick -= EditTextBox2_DoubleClick;
                tokenEditor.DoubleClick += EditTextBox2_DoubleClick;
            }
            if (ableEdit.ClassDataSourceName == "OperationRecord.OperationDoctor" || ableEdit.ClassDataSourceName == "OperationRecord.AnesthesiaDoctor")
            {
                tokenEditor.ValidateToken -= new DevComponents.DotNetBar.Controls.ValidateTokenEventHandler(this.tokenEditor_ValidateToken1);
                tokenEditor.ValidateToken += new DevComponents.DotNetBar.Controls.ValidateTokenEventHandler(this.tokenEditor_ValidateToken1);
                tokenEditor.Leave -= TokenEditor_Leave;
                tokenEditor.Leave -= TokenEditor_Leave2;
                tokenEditor.Leave += TokenEditor_Leave2;
            }
            if (ableEdit.ControlTitleText == "人员" && tokenEditor.SelectedTokens.Count > 0)
            {
                for (int i = tokenEditor.SelectedTokens.Count - 1; i >= 0; i--)
                {
                    if (tokenEditor.SelectedTokens[i].Text == "无")
                    {
                        tokenEditor.SelectedTokens.Remove(tokenEditor.SelectedTokens[i]);
                    }
                }
            }
            EditTextBox_DoubleClick(tokenEditor.EditTextBox, null);
            tokenEditor.EditTextBox.Select();
        }
        private void EditTextBox_DoubleClick(object sender, EventArgs e)
        {
            TextBox txt = sender as TextBox;
            TokenEditorManage.SelectDictList(OpeRecord as OperationRecord, txt);
        }
        private void EditTextBox_TextChanged(object sender, EventArgs e)
        {
            TextBox txt = sender as TextBox;
            TokenEditorManage.SelectDictList(OpeRecord as OperationRecord, txt);
        }
        private void tokenEditor_ValidateToken(object sender, DevComponents.DotNetBar.Controls.ValidateTokenEventArgs ea)
        {
            DevComponents.DotNetBar.Controls.TokenEditor tokenEditor = sender as DevComponents.DotNetBar.Controls.TokenEditor;
            AbleEditPackObj aEdit = tokenEditor.Tag as AbleEditPackObj;
            if (ea.IsNewToken)
            {
                tokenEditor.EditTextBox.TextChanged -= EditTextBox_TextChanged;
                string id = TokenEditorManage.GetOperationItemId(ea.Token.Value, aEdit);
                tokenEditor.SelectedTokens.Add(new EditToken(id, ea.Token.Value));
                ea.Token = null;
                tokenEditor.EditTextBox.Text = "";
                tokenEditor.IsPopupOpen = false;
                tokenEditor.EditTextBox.TextChanged += EditTextBox_TextChanged;
            }
        }
        private void tokenEditor_ValidateToken1(object sender, DevComponents.DotNetBar.Controls.ValidateTokenEventArgs ea)
        {
            DevComponents.DotNetBar.Controls.TokenEditor tokenEditor = sender as DevComponents.DotNetBar.Controls.TokenEditor;
            AbleEditPackObj aEdit = tokenEditor.Tag as AbleEditPackObj;
            if (ea.IsNewToken)
            {
                if (tokenEditor.SelectedTokens.Count > 0)
                {
                    tokenEditor.EditTextBox.TextChanged -= EditTextBox_TextChanged;
                    EditToken token = tokenEditor.SelectedTokens[tokenEditor.SelectedTokens.Count - 1];
                    string Tokentxt = ea.Token.Value.Replace(":", ":");
                    token.Text += Tokentxt;
                    ea.Token = null;
                    tokenEditor.EditTextBox.Text = "";
                    tokenEditor.IsPopupOpen = false;
                    tokenEditor.EditTextBox.TextChanged += EditTextBox_TextChanged;
                }
            }
        }
        private void TokenEditor_Leave(object sender, EventArgs e)
        {
            DevComponents.DotNetBar.Controls.TokenEditor tokenEditor = sender as DevComponents.DotNetBar.Controls.TokenEditor;
            AbleEditPackObj aEdit = tokenEditor.Tag as AbleEditPackObj;
            TokenEditorManage.SaveDictItem(template, OpeRecord as OperationRecord, tokenEditor, aEdit, true);
        }
        private void TokenEditor_Leave2(object sender, EventArgs e)
        {
            DevComponents.DotNetBar.Controls.TokenEditor tokenEditor = sender as DevComponents.DotNetBar.Controls.TokenEditor;
            AbleEditPackObj aEdit = tokenEditor.Tag as AbleEditPackObj;
            TokenEditorManage.SaveDictItem2(template, OpeRecord as OperationRecord, tokenEditor, aEdit, true);
        }
        private void EditTextBox2_DoubleClick(object sender, EventArgs e)
        {
            DevComponents.DotNetBar.Controls.TokenEditor tokenEditor = ((DevComponents.DotNetBar.Controls.TokenEditor)sender);
            string text = tokenEditor.Text;
            if (text.Trim() != "")
            {
                tokenEditor.SelectedTokens.Clear();
                tokenEditor.EditTextBox.TextChanged -= EditTextBox_TextChanged;
                tokenEditor.EditTextBox.Text = text;
                tokenEditor.IsPopupOpen = false;
                tokenEditor.EditTextBox.TextChanged += EditTextBox_TextChanged;
            }
        }
        private void Workerqm_Click(AbleEditPackObj sender, EventArgs e, int _workersType)
        {
            AbleEditPackObj ableEdit = sender;
            if (ableEdit == null) return;
            if (myOpeRecord != null)
            {
                KeyValuePair _controlName = new KeyValuePair(ableEdit.PackValue, ableEdit.PackText);
                KeyValuePair keyValuePairs = SelectWorker(ableEdit.PackTag, _controlName, _workersType, PublicMethod.DepId, true);
                if (keyValuePairs.Key != null)
                {
                    string value = keyValuePairs.Key.ToString();
                    string text = keyValuePairs.Value.ToString();
                    ableEdit.PackValue = value;
                    ableEdit.PackText = text;
                    SetCAAbleEditPackObj(ableEdit);
                    //设置属性的值
                    ableEdit.IsVisible = !ableEdit.IsVisible;
                    template.SetObjValue(OpeRecord, ableEdit.ClassDataSourceName, text, value, true);
                    ZedControl.Refresh();
                }
                else
                {
                    ableEdit.IsVisible = !ableEdit.IsVisible;
                }
            }
        }
        private void SetCAAbleEditPackObj(AbleEditPackObj ableEdit)
        {
            DataTable Person = null;
            try
            {
                if (!string.IsNullOrWhiteSpace(ableEdit.PackValue))
                    Person = DBManage.GetPerson(int.Parse(ableEdit.PackValue));
            }
            catch (Exception)
            { 
            }
            if (Person != null && Person.Rows.Count > 0)
            {
                string Id = Person.Rows[0][3].ToString();
                string Name = Person.Rows[0][0].ToString();
                string Contact = Person.Rows[0][1].ToString();
                string SignatureStr = Person.Rows[0][2].ToString();
                string SignDataStr = PublicMethod.ImgToBase64Str(ZedControl.GetImage());
                if (SignatureStr == null || SignatureStr == "")
                    SignatureStr = new SignManage().GetSignStr(SignDataStr, "麻醉记录单", Id, Name, Contact);
                if (SignatureStr != null && SignatureStr != "")
                {
                    Image img = PublicMethod.BytesToImage(Convert.FromBase64String(SignatureStr));
                    if (img != null)
                        ableEdit.PickImage = img;
                }
            }
            if (ableEdit != null && string.IsNullOrWhiteSpace(ableEdit.PackValue))
            {
                ableEdit.PickImage = null;
                ableEdit.PackText = "";
            }
        }
        /// 
        /// 选择人员
        /// 
        /// 传递取值控件
        /// 人员类型(0医生,1护士,2麻醉医生,3,麻醉护士,9其他)
        /// 科室Id
        /// 是否单选
        /// CA签名数据信息 
        public KeyValuePair SelectWorker(string Tag, KeyValuePair sender, int _workersType, int _deptId, bool isRadio = false)
        {
            KeyValuePair _controlName = new KeyValuePair();
            SelectPerson personDoc = new SelectPerson();
            personDoc.PersonType = _workersType == 2 ? "麻醉医生" : _workersType == 4 ? "麻醉护士" : "手术室护士";
            DialogResult dialogResult = personDoc.ShowDialog();
            if (dialogResult == DialogResult.OK)
            {
                if (personDoc.SelectPersonData != null && personDoc.SelectPersonData != "")
                {
                    string PersonId = personDoc.SelectPersonData.ToString();
                    _controlName = new KeyValuePair(PersonId, personDoc.SelectPersonName);
                }
                else
                {
                    _controlName = new KeyValuePair("", "");
                }
            }
            return _controlName;
        }
    }
}