using CasunCommon.BaseUI;
using Pms.Common.Utility;
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 CStationIDRelatedForm : CasunForm
    {
        public CStationIDRelatedForm()
        {
            InitializeComponent();
        }

        private int selectRowIndexOfordersDataGridView;         // 记录选中的行号
        private int selectColumnIndexOfordersDataGridView;      // 记录选中的列号
        private int scrollingRowIndexOfordersDataGridView;      // 记录垂直滚动条位置
        private int scrollingColumnIndexOfordersDataGridView;   // 记录水平滚动条的位置

        private void Init()
        {
            CasunUILib.DgvModel(DGVNodeIDRelated);
        }

        private void btAdd_Click(object sender, EventArgs e)
        {
            if (!String.IsNullOrEmpty(tbStationID.Text.ToString()))
            {
                PmsCoreDB.Model.PMS_bas_StationIDRelated sStationIDRelated = new PmsCoreDB.Model.PMS_bas_StationIDRelated();

                sStationIDRelated.StationID = CasunUILib.StringToInt32(tbStationID.Text.ToString());

                sStationIDRelated.RelatedStationID1 = CasunUILib.StringToInt32(tbRelatedStationID1.Text.ToString());
                sStationIDRelated.RelatedVertex1 = CasunUILib.StringToInt32(tbRelatedVertex1.Text.ToString());

                sStationIDRelated.RelatedStationID2 = CasunUILib.StringToInt32(tbRelatedStationID2.Text.ToString());
                sStationIDRelated.RelatedVertex2 = CasunUILib.StringToInt32(tbRelatedVertex2.Text.ToString());

                sStationIDRelated.RelatedStationID3 = CasunUILib.StringToInt32(tbRelatedStationID3.Text.ToString());
                sStationIDRelated.RelatedVertex3 = CasunUILib.StringToInt32(tbRelatedVertex3.Text.ToString());

                sStationIDRelated.Remark = tbRemark.Text.ToString();
                sStationIDRelated.Reserved1 = tbReserved1.Text.ToString();
                sStationIDRelated.Reserved2 = tbReserved2.Text.ToString();
                sStationIDRelated.Reserved3 = tbReserved3.Text.ToString();

                //if (CasunModelDB.CDBManager.Instance.PMS_bas_StationIDRelated.Add(sStationIDRelated))
                if (PmsCoreDB.DBHost.Instance.StationIDRelated.Add(sStationIDRelated))
                {
                    string strOptLog = string.Format("【{3}{4}】:{5}={0},{4}{5}={1},{4}{6}={2}"
                            , sStationIDRelated.StationID
                            , sStationIDRelated.RelatedStationID1
                            , sStationIDRelated.RelatedVertex1
                            , CLanguageManager.LanguageChange("增加活动站点")
                            , CLanguageManager.LanguageChange("关联")
                            , CLanguageManager.LanguageChange("活动站点")
                            , CLanguageManager.LanguageChange("导航点")
                     );
                    COperateLog.Instance.InsertOperaterLogAsync(UserID, strOptLog, DateTime.Now);
                    MessageBox.Show(CLanguageManager.LanguageChange("添加成功") + "！");
                }
                else
                {
                    MessageBox.Show(CLanguageManager.LanguageChange("添加失败") + "！");
                }
                loadTable();
            }
        }

        private void btModfiy_Click(object sender, EventArgs e)
        {
            if (!String.IsNullOrEmpty(tbStationID.Text.ToString()))
            {
                PmsCoreDB.Model.PMS_bas_StationIDRelated sStationIDRelated = new PmsCoreDB.Model.PMS_bas_StationIDRelated();
                sStationIDRelated.StationID = CasunUILib.StringToInt32(tbStationID.Text.ToString());
                sStationIDRelated.RelatedStationID1 = CasunUILib.StringToInt32(tbRelatedStationID1.Text.ToString());
                sStationIDRelated.RelatedVertex1 = CasunUILib.StringToInt32(tbRelatedVertex1.Text.ToString());
                sStationIDRelated.RelatedStationID2 = CasunUILib.StringToInt32(tbRelatedStationID2.Text.ToString());
                sStationIDRelated.RelatedVertex2 = CasunUILib.StringToInt32(tbRelatedVertex2.Text.ToString());
                sStationIDRelated.RelatedStationID3 = CasunUILib.StringToInt32(tbRelatedStationID3.Text.ToString());
                sStationIDRelated.RelatedVertex3 = CasunUILib.StringToInt32(tbRelatedVertex3.Text.ToString());
                sStationIDRelated.Remark = tbRemark.Text.ToString();
                sStationIDRelated.Reserved1 = tbReserved1.Text.ToString();
                sStationIDRelated.Reserved2 = tbReserved2.Text.ToString();
                sStationIDRelated.Reserved3 = tbReserved3.Text.ToString();
                if (PmsCoreDB.DBHost.Instance.StationIDRelated.Update(sStationIDRelated))
                {
                    string strOptLog = string.Format("【{3}】:{5}={0},{4}{5}={1},{4}{6}={2}"
                            , sStationIDRelated.StationID
                            , sStationIDRelated.RelatedStationID1
                            , sStationIDRelated.RelatedVertex1
                            , CLanguageManager.LanguageChange("修改站点关联")
                            , CLanguageManager.LanguageChange("关联")
                            , CLanguageManager.LanguageChange("活动站点")
                            , CLanguageManager.LanguageChange("导航点")
                           );
                    COperateLog.Instance.InsertOperaterLogAsync(UserID, strOptLog, DateTime.Now);
                    MessageBox.Show(CLanguageManager.LanguageChange("修改成功") + "！");
                }
                else
                {
                    MessageBox.Show(CLanguageManager.LanguageChange("修改失败") + "！");
                }

                loadTable();
            }
        }

        private void btDelete_Click(object sender, EventArgs e)
        {
            if (!String.IsNullOrEmpty(tbStationID.Text.ToString()))
            {
                PmsCoreDB.Model.PMS_bas_StationIDRelated sNodeIDRelated = new PmsCoreDB.Model.PMS_bas_StationIDRelated();
                sNodeIDRelated.StationID = CasunUILib.StringToInt32(tbStationID.Text.ToString());

                string strMsg = CLanguageManager.LanguageChange("删除站点关联") + "【" + tbStationID.Text.ToString() + "】，" + CLanguageManager.LanguageChange("是否继续") + "！";
                if (MessageBox.Show(strMsg, CLanguageManager.LanguageChange("删除站点关联"), MessageBoxButtons.OKCancel, MessageBoxIcon.Warning) != DialogResult.OK)
                {
                    return;
                }

                if(PmsCoreDB.DBHost.Instance.StationIDRelated.Delete(sNodeIDRelated.StationID))
                {
                    string strOptLog = string.Format("【{0}】:{1}={2}"
                        , CLanguageManager.LanguageChange("删除站点关联")
                        , CLanguageManager.LanguageChange("活动站点")
                        , sNodeIDRelated.StationID
                       );
                    COperateLog.Instance.InsertOperaterLogAsync(UserID, strOptLog, DateTime.Now);
                    MessageBox.Show(CLanguageManager.LanguageChange("删除成功") + "！");
                }
                else
                {
                    MessageBox.Show(CLanguageManager.LanguageChange("删除失败") + "！");
                }
                loadTable();
            }
        }

        private void CStationIDRelatedForm_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);
            Init();
            loadTable();
            InitFormShow();
            InitLabelShow();
        }

        private void loadTable()
        {
            if (DGVNodeIDRelated.CurrentCell != null && DGVNodeIDRelated.RowCount != 0 && DGVNodeIDRelated.ColumnCount != 0)
            {
                selectRowIndexOfordersDataGridView = DGVNodeIDRelated.CurrentCell.RowIndex;
                selectColumnIndexOfordersDataGridView = DGVNodeIDRelated.CurrentCell.ColumnIndex;
                scrollingRowIndexOfordersDataGridView = DGVNodeIDRelated.FirstDisplayedScrollingRowIndex;
                scrollingColumnIndexOfordersDataGridView = DGVNodeIDRelated.FirstDisplayedScrollingColumnIndex;
            }
            
            DGVNodeIDRelated.DataSource = CFormColumnManager.GetDGVDataSource(this.Name, "PMS_bas_StationIDRelated");

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

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

        private void InitFormShow()
        {
            foreach (Control con in gp_Opt.Controls)
            {
                if (!(con is Button))
                {
                    con.Visible = false;
                }
            }
            //主键显示
            lbStation.Visible = true;
            tbStationID.Visible = true;


            List<string> InitControlList = CFormColumnManager.GetNeedShowDBList(this.Name);
            foreach (string str in InitControlList)
            {
                switch (str)
                {
                    case "RelatedStationID1":
                        lbRelatedStationID1.Visible = true;
                        tbRelatedStationID1.Visible = true;
                        break;
                    case "RelatedVertex1":
                        lbRelatedVertex1.Visible = true;
                        tbRelatedVertex1.Visible = true;
                        break;
                    case "RelatedStationID2":
                        lbRelatedStationID2.Visible = true;
                        tbRelatedStationID2.Visible = true;
                        break;
                    case "RelatedVertex2":
                        lbRelatedVertex2.Visible = true;
                        tbRelatedVertex2.Visible = true;
                        break;
                    case "RelatedStationID3":
                        lbRelatedStationID3.Visible = true;
                        tbRelatedStationID3.Visible = true;
                        break;
                    case "RelatedVertex3":
                        lbRelatedVertex3.Visible = true;
                        tbRelatedVertex3.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()
        {
            if(DBNameToShowNameDic.ContainsKey("StationID"))
                lbStation.Text = DBNameToShowNameDic["StationID"];
            if (DBNameToShowNameDic.ContainsKey("RelatedStationID1"))
                lbRelatedStationID1.Text = DBNameToShowNameDic["RelatedStationID1"];
            if (DBNameToShowNameDic.ContainsKey("RelatedVertex1"))
                lbRelatedVertex1.Text = DBNameToShowNameDic["RelatedVertex1"];
            if (DBNameToShowNameDic.ContainsKey("RelatedStationID2"))
                lbRelatedStationID2.Text = DBNameToShowNameDic["RelatedStationID2"];
            if (DBNameToShowNameDic.ContainsKey("RelatedVertex2"))
                lbRelatedVertex2.Text = DBNameToShowNameDic["RelatedVertex2"];
            if (DBNameToShowNameDic.ContainsKey("RelatedStationID3"))
                lbRelatedStationID3.Text = DBNameToShowNameDic["RelatedStationID3"];
            if (DBNameToShowNameDic.ContainsKey("RelatedVertex3"))
                lbRelatedVertex3.Text = DBNameToShowNameDic["RelatedVertex3"];
            if (DBNameToShowNameDic.ContainsKey("Remark"))
                lbRemark.Text = DBNameToShowNameDic["Remark"];
            if (DBNameToShowNameDic.ContainsKey("Reserved1"))
                lbReserved1.Text = DBNameToShowNameDic["Reserved1"];
            if (DBNameToShowNameDic.ContainsKey("Reserved2"))
                lbReserved2.Text = DBNameToShowNameDic["Reserved2"];
            if (DBNameToShowNameDic.ContainsKey("Reserved3"))
                lbReserved3.Text = DBNameToShowNameDic["Reserved3"];
        }

        /// <summary>
        /// 用于事件重复使用
        /// </summary>
        private Dictionary<string, string> DBNameToShowNameDic = new Dictionary<string, string>();
              
        private bool InitDBNameToShowNameDic()
        {
            if(!CFormColumnManager.GetDBNameToShowNameDic(this.Name,out DBNameToShowNameDic))
            {
                MessageBox.Show(CLanguageManager.LanguageChange("加载界面配置失败，请重启尝试恢复。"));
                return false;
            }
            return true;
        }

        private void DGVNodeIDRelated_CellMouseClick(object sender, DataGridViewCellMouseEventArgs e)
        {
            try
            {
                int DGVCurrentRow = e.RowIndex;
                if (DGVCurrentRow != -1 && DGVNodeIDRelated.Rows[DGVCurrentRow] != null)
                {
                    ShowText(tbStationID, "StationID", DGVCurrentRow);
                    ShowText(tbRelatedStationID1, "RelatedStationID1", DGVCurrentRow);
                    ShowText(tbRelatedVertex1, "RelatedVertex1", DGVCurrentRow);
                    ShowText(tbRelatedStationID2, "RelatedStationID2", DGVCurrentRow);
                    ShowText(tbRelatedVertex2, "RelatedVertex2", DGVCurrentRow);
                    ShowText(tbRelatedStationID3, "RelatedStationID3", DGVCurrentRow);
                    ShowText(tbRelatedVertex3, "RelatedVertex3", DGVCurrentRow);
                    ShowText(tbRemark, "Remark", DGVCurrentRow);
                    ShowText(tbReserved1, "Reserved1", DGVCurrentRow);
                    ShowText(tbReserved2, "Reserved2", DGVCurrentRow);
                    ShowText(tbReserved3, "Reserved3", DGVCurrentRow);    
                }
            }
            catch
            {
                MessageBox.Show(CLanguageManager.LanguageChange("更改过设置后请重新打开该子界面"));
            }        
        }
        private void ShowText(Control sControl, string strKey, int DGVCurrentRow)
        {
            if (DBNameToShowNameDic.ContainsKey(strKey) && DGVNodeIDRelated.Rows[DGVCurrentRow].Cells[DBNameToShowNameDic[strKey]].Value != null)
            {
                sControl.Text = DGVNodeIDRelated.Rows[DGVCurrentRow].Cells[DBNameToShowNameDic[strKey]].Value.ToString();
            }
        }

        private void btHelps_Click(object sender, EventArgs e)
        {
            MessageBox.Show(PMSUI.Config.CConfigManager.gs_FormHelpConfig.GetFormHelp(this.Name));

        }
    }
}
