using CasunCommon.BaseUI;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace PMSUI
{
    public partial class CBehaviorForm : CasunForm
    {
        private int selectRowIndexOfordersDataGridView;         // 记录选中的行号
        private int selectColumnIndexOfordersDataGridView;      // 记录选中的列号
        private int scrollingRowIndexOfordersDataGridView;      // 记录垂直滚动条位置
        private int scrollingColumnIndexOfordersDataGridView;   // 记录水平滚动条的位置
        /// <summary>
        /// 用于事件重复使用
        /// </summary>
        private Dictionary<string, string> DBNameToShowNameDic = new Dictionary<string, string>();
        private PmsCoreDB.Model.PMS_bas_Behavior _oldmodel;  //记录更新前选中行的信息

        enum EBehavior
        {
            Behavior,
            Value,
            Type,
            Standard,
            DisplayMessage,
            remark,
            Reserved1,
            Reserved2,
            Reserved3,
        }

        public CBehaviorForm()
        {
            InitializeComponent();
        }

        private void btHelps_Click(object sender, EventArgs e)
        {
            MessageBox.Show(PMSUI.Config.CConfigManager.gs_FormHelpConfig.GetFormHelp(this.Name));
        }
     
        private bool FormatData( out PmsCoreDB.Model.PMS_bas_Behavior sbehavior )
        {
            sbehavior = new PmsCoreDB.Model.PMS_bas_Behavior();
            if (!String.IsNullOrEmpty(tbBehavior.Text.ToString())
                   )
            {
                sbehavior.Behavior = CasunUILib.StringToInt32(tbBehavior.Text.ToString());
                sbehavior.Value = tbvalue.Text;
                sbehavior.Standard = ckbStandard.Checked;
                sbehavior.DisplayMessage = tbDispalyMessage.Text;
                sbehavior.Type = CasunUILib.StringToInt32(tbType.Text.ToString());
                sbehavior.remark = tbRemark.Text;
                sbehavior.Reserved1 = tbReserved1.Text;
                sbehavior.Reserved2 = tbReserved2.Text;
                sbehavior.Reserved3 = tbReserved3.Text;


                return true;
            }
            else
            {
                return false;
            }
        }

        private void btAdd_Click(object sender, EventArgs e)
        {
            PmsCoreDB.Model.PMS_bas_Behavior sbehavior;
            
            if(FormatData(out sbehavior))
            {
                if (PmsCoreDB.DBHost.Instance.Behavior.Add(sbehavior))
                {
                    MessageBox.Show(CLanguageManager.LanguageChange("添加成功") + "！");
                }
                else
                {
                    MessageBox.Show(CLanguageManager.LanguageChange("添加失败") + "！");
                }
                loadTable();
            }
            else
            {
                MessageBox.Show(CLanguageManager.LanguageChange("添加失败") + "！");
            }
        }

        private void btModify_Click(object sender, EventArgs e)
        {
            PmsCoreDB.Model.PMS_bas_Behavior sbehavior;

            if (FormatData(out sbehavior))
            {
                if (PmsCoreDB.DBHost.Instance.Behavior.Update(sbehavior,_oldmodel))
                {
                    MessageBox.Show(CLanguageManager.LanguageChange("更新成功") + "！");
                }
                else
                {
                    MessageBox.Show(CLanguageManager.LanguageChange("更新失败") + "！");
                }
                loadTable();
            }
            else
            {
                MessageBox.Show(CLanguageManager.LanguageChange("请重新选择要处理的行") + "！");
            }
        }

        private void btDelete_Click(object sender, EventArgs e)
        {
            if (_oldmodel.Behavior != 0)
            {
                string strMsg = CLanguageManager.LanguageChange("删除行为码") + "【" + _oldmodel.Behavior.ToString() + "】，" + CLanguageManager.LanguageChange("是否继续") + "！";
                if (MessageBox.Show(strMsg, CLanguageManager.LanguageChange("删除行为码"), MessageBoxButtons.OKCancel, MessageBoxIcon.Warning) != DialogResult.OK)
                {
                    return;
                }

                if (PmsCoreDB.DBHost.Instance.Behavior.Delete(_oldmodel.Behavior))
                {
                    _oldmodel.Behavior = 0;
                    MessageBox.Show(CLanguageManager.LanguageChange("删除成功") + "！");
                }
                else
                {
                    MessageBox.Show(CLanguageManager.LanguageChange("删除失败") + "！");
                }
                loadTable();
            }
            else
            {
                MessageBox.Show(CLanguageManager.LanguageChange("请重新选择要处理的行") + "！");
            }
        }

        private void CBehaviorForm_Load(object sender, EventArgs e)
        {
            if (!InitDBNameToShowNameDic())
            {
                this.Close();
                return;
            }

            CasunUILib.ButtonHelps(btHelps);

            gp_Opt.Location = new Point(btHelps.Location.X, gp_Opt.Location.Y);

            //CasunUILib.GroupBoxReadModel(gp_Read, PermissionLevel);
            CasunUILib.GroupBoxReadWriteModel(gp_Opt, PermissionLevel);

            CasunUILib.splitContainerModel(splitContainer1);
            InitFormShow();
            InitLabelShow();

            CasunUILib.DgvModel(DgvBehavior);
            loadTable();
        }

        private void loadTable()
        {
            ResetControl();

            if (DgvBehavior.CurrentCell != null && DgvBehavior.RowCount != 0 && DgvBehavior.ColumnCount != 0)
            {
                selectRowIndexOfordersDataGridView = DgvBehavior.CurrentCell.RowIndex;
                selectColumnIndexOfordersDataGridView = DgvBehavior.CurrentCell.ColumnIndex;
                scrollingRowIndexOfordersDataGridView = DgvBehavior.FirstDisplayedScrollingRowIndex;
                scrollingColumnIndexOfordersDataGridView = DgvBehavior.FirstDisplayedScrollingColumnIndex;
            }

            DgvBehavior.DataSource =
                CFormColumnManager.GetDGVDataSource(this.Name, "PMS_bas_Behavior", "order by Behavior");

            int n = DgvBehavior.RowCount;
            int m = DgvBehavior.ColumnCount;

            if (n != 0 && m != 0)
            {
                if (selectRowIndexOfordersDataGridView < n && selectColumnIndexOfordersDataGridView < m &&
                    scrollingRowIndexOfordersDataGridView < n && scrollingColumnIndexOfordersDataGridView < m)
                {
                    DgvBehavior.CurrentCell = DgvBehavior.Rows[selectRowIndexOfordersDataGridView].Cells[selectColumnIndexOfordersDataGridView];
                    DgvBehavior.FirstDisplayedScrollingRowIndex = scrollingRowIndexOfordersDataGridView;
                    DgvBehavior.FirstDisplayedScrollingColumnIndex = scrollingColumnIndexOfordersDataGridView;
                }
            }
        }

        private void ResetControl()
        {
            tbBehavior.Text = "";
            tbDispalyMessage.Text = "";
            tbvalue.Text = "";
            ckbStandard.Checked = false;
            tbType.Text = "";
            tbRemark.Text = "";
            tbReserved1.Text = "";
            tbReserved2.Text = "";
            tbReserved3.Text = "";
        }

        private void InitFormShow()
        {
            foreach (Control con in gp_Opt.Controls)
            {
                if (!(con is Button))
                {
                    con.Visible = false;
                }
            }
            //主键显示
            lbBehavior.Visible = true;
            tbBehavior.Visible = true;
       
            List<string> InitControlList = CFormColumnManager.GetNeedShowDBList(this.Name);
            foreach (string str in InitControlList)
            {
                switch (str)
                {
                    case "Value":
                        lbvalue.Visible = true;
                        tbvalue.Visible = true;
                        break;
                    case "Type":
                        lbType.Visible = true;
                        tbType.Visible = true;
                        break;
                    case "Standard":
                        lbStandard.Visible = true;
                        ckbStandard.Visible = true;
                        break;         
                    case "DisplayMessage":
                        lbDispalyMessage.Visible = true;
                        tbDispalyMessage.Visible = true;
                        break;
                    case "remark":
                        lbRemark.Visible = true;
                        tbRemark.Visible = true;
                        break;
                    case "Reserved1":
                        lbReserved1.Visible = true;
                        tbReserved1.Visible = true;
                        break;
                    case "Reserved2":
                        lbReserved2.Visible = true;
                        tbReserved2.Visible = true;
                        break;
                    case "Reserved3":
                        lbReserved3.Visible = true;
                        tbReserved3.Visible = true;
                        break;
                    default:
                        break;
                }
            }
        }

        private void InitLabelShow()
        {
            ShowLable(EBehavior.Behavior,lbBehavior);
            ShowLable(EBehavior.Value,lbvalue);
            ShowLable(EBehavior.Type, lbType);
            ShowLable(EBehavior.Standard, lbStandard);
            ShowLable(EBehavior.DisplayMessage, lbDispalyMessage);
            ShowLable(EBehavior.remark, lbRemark);
            ShowLable(EBehavior.Reserved1, lbReserved1);
            ShowLable(EBehavior.Reserved2, lbReserved2);
            ShowLable(EBehavior.Reserved2, lbReserved2);
        }
    
        private bool InitDBNameToShowNameDic()
        {
            if (!CFormColumnManager.GetDBNameToShowNameDic(this.Name, out DBNameToShowNameDic))
            {
                MessageBox.Show(CLanguageManager.LanguageChange("加载界面配置失败，请重启尝试恢复。"));
                return false;
            }
            return true;
        }

        private void ShowLable(EBehavior Ekey,Label slable)
        {
            if (DBNameToShowNameDic.ContainsKey(Ekey.ToString()))
                slable.Text = DBNameToShowNameDic[Ekey.ToString()];
        }
        private void ShowText(Control sControl, string strKey, int DGVCurrentRow)
        {
            if (DBNameToShowNameDic.ContainsKey(strKey) && DgvBehavior.Rows[DGVCurrentRow].Cells[DBNameToShowNameDic[strKey]].Value != null)
            {
                sControl.Text = DgvBehavior.Rows[DGVCurrentRow].Cells[DBNameToShowNameDic[strKey]].Value.ToString();
            }
        }

        private void ShowText(CheckBox sControl, string strKey, int DGVCurrentRow)
        {
            if (DBNameToShowNameDic.ContainsKey(strKey) && DgvBehavior.Rows[DGVCurrentRow].Cells[DBNameToShowNameDic[strKey]].Value != null)
            {
                if (DgvBehavior.Rows[DGVCurrentRow].Cells[DBNameToShowNameDic[strKey]].Value.ToString() == "1")
                {
                    sControl.Checked = true;
                }
                else
                {
                    sControl.Checked = false;
                }
            }
        }

        private void DgvBehavior_CellMouseClick(object sender, DataGridViewCellMouseEventArgs e)
        {
            int DGVCurrentRow = e.RowIndex;
            _oldmodel = new PmsCoreDB.Model.PMS_bas_Behavior();

            ResetControl();
            if (DGVCurrentRow != -1 && DgvBehavior.Rows[DGVCurrentRow] != null)
            {
                ShowText(tbBehavior, EBehavior.Behavior.ToString(), DGVCurrentRow);
                _oldmodel.Behavior = CasunUILib.StringToInt32(tbBehavior.Text.ToString());

                ShowText(tbvalue, EBehavior.Value.ToString(), DGVCurrentRow);
                _oldmodel.Value = tbvalue.Text;

                ShowText(ckbStandard, EBehavior.Standard.ToString(), DGVCurrentRow);
                _oldmodel.Standard = ckbStandard.Checked;

                ShowText(tbDispalyMessage, EBehavior.DisplayMessage.ToString(), DGVCurrentRow);
                _oldmodel.DisplayMessage = tbDispalyMessage.Text;

                ShowText(tbType, EBehavior.Type.ToString(), DGVCurrentRow);
                _oldmodel.Type = CasunUILib.StringToInt32(tbType.Text.ToString());

                ShowText(tbRemark, EBehavior.remark.ToString(), DGVCurrentRow);
                _oldmodel.remark = tbRemark.Text;

                ShowText(tbReserved1, EBehavior.Reserved1.ToString(), DGVCurrentRow);
                _oldmodel.Reserved1 = tbReserved1.Text;

                ShowText(tbReserved2, EBehavior.Reserved2.ToString(), DGVCurrentRow);
                _oldmodel.Reserved2 = tbReserved2.Text;

                ShowText(tbReserved3, EBehavior.Reserved3.ToString(), DGVCurrentRow);
                _oldmodel.Reserved3 = tbReserved3.Text;
            }
        }
    }
}
