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 CMenuSettingForm : CasunForm
    {
        private enum MenuDGVMenu
        {
            MenuName,
            DisplayMenuName,
            ModuleName,
            IsShow,
            ChildMenuSort,
            ParentMenuSort
        }
        private enum MenuDGVColumn
        {
            FormName,
            DBColumnName,
            ShowColumnName,
            IsShow,
            Sort
        }

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

        private int selectRowIndexOfordersDataGridView1;         // 记录选中的行号
        private int selectColumnIndexOfordersDataGridView1;      // 记录选中的列号
        private int scrollingRowIndexOfordersDataGridView1;      // 记录垂直滚动条位置
        private int scrollingColumnIndexOfordersDataGridView1;   // 记录水平滚动条的位置

        public CMenuSettingForm()
        {
            InitializeComponent();
        }


        private void Init()
        {
            CasunUILib.DgvModel(dgvMenuSetting);
            
            CasunUILib.DgvModel(dgvFormColumnSetting);
        }

        bool checkIsNumber(string text)
        {
            int result = 0;
            bool re = int.TryParse(text, out result);
            if (re && result > 0)
            {
                return true;
            }
            return false;
        }

        private void CMenuSettingForm_Load(object sender, EventArgs e)
        {
            CasunUILib.ButtonHelps(btHelpsForm);
            CasunUILib.ButtonHelps(btHelpsMenu);
            Init();
            loadMenuTable();
            loadColumnTable();
            InitComboBox();
        }

        private void loadMenuTable()
        {

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

            ResetMenuControl();
            string strwhere = "1=1 order by cast(ParentMenuSort as int),cast(ChildMenuSort as int)";
            DataSet ds = PmsCoreDB.DBHost.Instance.MenuSetting.GetListWithColumnName(strwhere);
            dgvMenuSetting.DataSource = ds.Tables[0];

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

            if (n != 0 && m != 0)
            {
                if (selectRowIndexOfordersDataGridView < n && selectColumnIndexOfordersDataGridView < m &&
                    scrollingRowIndexOfordersDataGridView < n && scrollingColumnIndexOfordersDataGridView < m)
                {
                    dgvMenuSetting.CurrentCell = dgvMenuSetting.Rows[selectRowIndexOfordersDataGridView].Cells[selectColumnIndexOfordersDataGridView];
                    dgvMenuSetting.FirstDisplayedScrollingRowIndex = scrollingRowIndexOfordersDataGridView;
                    dgvMenuSetting.FirstDisplayedScrollingColumnIndex = scrollingColumnIndexOfordersDataGridView;
                }
            }        
        }
        private void loadColumnTable()
        {
            string formName = cbWhichForm.Text.ToString();

            if (dgvFormColumnSetting.CurrentCell != null && dgvFormColumnSetting.RowCount != 0 && dgvFormColumnSetting.ColumnCount != 0)
            {
                selectRowIndexOfordersDataGridView1 = dgvFormColumnSetting.CurrentCell.RowIndex;
                selectColumnIndexOfordersDataGridView1 = dgvFormColumnSetting.CurrentCell.ColumnIndex;
                scrollingRowIndexOfordersDataGridView1 = dgvFormColumnSetting.FirstDisplayedScrollingRowIndex;
                scrollingColumnIndexOfordersDataGridView1 = dgvFormColumnSetting.FirstDisplayedScrollingColumnIndex;
            }


            ResetColumnControl();
            string strwhere = "";
            if (formName == "")
            {
                strwhere = "1=1 order by FormName,cast(Sort as int)";
            }
            else
            {
                strwhere = "FormName = '" + formName + "' order by FormName,cast(Sort as int)";
            }
            DataSet ds = PmsCoreDB.DBHost.Instance.ColumnSetting.GetAllListWithColumnName(strwhere);
            dgvFormColumnSetting.DataSource = ds.Tables[0];

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

            if (n != 0 && m != 0)
            {
                if (selectRowIndexOfordersDataGridView1 < n && selectColumnIndexOfordersDataGridView1 < m &&
                    scrollingRowIndexOfordersDataGridView1 < n && scrollingColumnIndexOfordersDataGridView1 < m)
                {
                    dgvFormColumnSetting.CurrentCell = dgvFormColumnSetting.Rows[selectRowIndexOfordersDataGridView1].Cells[selectColumnIndexOfordersDataGridView1];
                    dgvFormColumnSetting.FirstDisplayedScrollingRowIndex = scrollingRowIndexOfordersDataGridView1;
                    dgvFormColumnSetting.FirstDisplayedScrollingColumnIndex = scrollingColumnIndexOfordersDataGridView1;
                }
            }         
        }
        private void InitComboBox()
        {
            List<string> AllFormList = PmsCoreDB.DBHost.Instance.ColumnSetting.GetAllFormName();
            cbWhichForm.Items.Clear();
            cbWhichForm.Items.Add("");
            cbWhichForm.Items.AddRange(AllFormList.ToArray());
        }

        private void ResetMenuControl()
        {
            tbMenuName.Text = "";
            tbMenuModule.Text = "";
            ckbIsMenuShow.Checked = false;
            tbMenuSort.Text = "";
            tbModuleSort.Text = "";
        }
        private void ResetColumnControl()
        {
            tbFormName.Text = "";
            tbDBColumnName.Text = "";
            tbShowName.Text = "";
            ckbShowColumn.Checked = false;
            tbColumnSort.Text = "";
            //cbWhichForm.Text = "";
        }

        private void dgvMenuSetting_CellMouseClick(object sender, DataGridViewCellMouseEventArgs e)
        {
            int DGVCurrentRow = e.RowIndex;
            if (DGVCurrentRow != -1 && dgvMenuSetting.Rows[DGVCurrentRow] != null)
            {
                if (dgvMenuSetting.Rows[DGVCurrentRow].Cells[(int)MenuDGVMenu.MenuName].Value != null)
                {
                    tbMenuName.Text = dgvMenuSetting.Rows[DGVCurrentRow].Cells[(int)MenuDGVMenu.MenuName].Value.ToString();
                }
                if (dgvMenuSetting.Rows[DGVCurrentRow].Cells[(int)MenuDGVMenu.DisplayMenuName].Value != null)
                {
                    tbDisplayMenuName.Text = dgvMenuSetting.Rows[DGVCurrentRow].Cells[(int)MenuDGVMenu.DisplayMenuName].Value.ToString();
                }
                
                if (dgvMenuSetting.Rows[DGVCurrentRow].Cells[(int)MenuDGVMenu.ModuleName].Value != null)
                {
                    tbMenuModule.Text = dgvMenuSetting.Rows[DGVCurrentRow].Cells[(int)MenuDGVMenu.ModuleName].Value.ToString();
                }
                if (dgvMenuSetting.Rows[DGVCurrentRow].Cells[(int)MenuDGVMenu.IsShow].Value != null)
                {
                    //cbIsMenuShow.Text = dgvMenuSetting.Rows[DGVCurrentRow].Cells[(int)MenuDGVMenu.IsShow].Value.ToString();
                    ckbIsMenuShow.Checked = (bool)dgvMenuSetting.Rows[DGVCurrentRow].Cells[(int)MenuDGVMenu.IsShow].Value;
                }
                if (dgvMenuSetting.Rows[DGVCurrentRow].Cells[(int)MenuDGVMenu.ChildMenuSort].Value != null)
                {
                    tbMenuSort.Text = dgvMenuSetting.Rows[DGVCurrentRow].Cells[(int)MenuDGVMenu.ChildMenuSort].Value.ToString();
                }
                if (dgvMenuSetting.Rows[DGVCurrentRow].Cells[(int)MenuDGVMenu.ParentMenuSort].Value != null)
                {
                    tbModuleSort.Text = dgvMenuSetting.Rows[DGVCurrentRow].Cells[(int)MenuDGVMenu.ParentMenuSort].Value.ToString();
                }
            }
        }

        private void dgvFormColumnSetting_CellMouseClick(object sender, DataGridViewCellMouseEventArgs e)
        {
            int DGVCurrentRow = e.RowIndex;
            if (DGVCurrentRow != -1 && dgvFormColumnSetting.Rows[DGVCurrentRow] != null)
            {
                if (dgvFormColumnSetting.Rows[DGVCurrentRow].Cells[(int)MenuDGVColumn.FormName].Value != null)
                {
                    tbFormName.Text = dgvFormColumnSetting.Rows[DGVCurrentRow].Cells[(int)MenuDGVColumn.FormName].Value.ToString();
                }
                if (dgvFormColumnSetting.Rows[DGVCurrentRow].Cells[(int)MenuDGVColumn.DBColumnName].Value != null)
                {
                    tbDBColumnName.Text = dgvFormColumnSetting.Rows[DGVCurrentRow].Cells[(int)MenuDGVColumn.DBColumnName].Value.ToString();
                }
                if (dgvFormColumnSetting.Rows[DGVCurrentRow].Cells[(int)MenuDGVColumn.ShowColumnName].Value != null)
                {
                    tbShowName.Text = dgvFormColumnSetting.Rows[DGVCurrentRow].Cells[(int)MenuDGVColumn.ShowColumnName].Value.ToString();
                }
                if (dgvFormColumnSetting.Rows[DGVCurrentRow].Cells[(int)MenuDGVColumn.IsShow].Value != null)
                {
                    ckbShowColumn.Checked = (bool)dgvFormColumnSetting.Rows[DGVCurrentRow].Cells[(int)MenuDGVColumn.IsShow].Value;
                }
                if (dgvFormColumnSetting.Rows[DGVCurrentRow].Cells[(int)MenuDGVColumn.Sort].Value != null)
                {
                    tbColumnSort.Text = dgvFormColumnSetting.Rows[DGVCurrentRow].Cells[(int)MenuDGVColumn.Sort].Value.ToString();
                }
            }
        }

        private void btMenuAdd_Click(object sender, EventArgs e)
        {
            if (!String.IsNullOrEmpty(tbMenuName.Text.ToString()) && !String.IsNullOrEmpty(tbMenuModule.Text.ToString())
                && !String.IsNullOrEmpty(tbMenuModule.Text.ToString()) && checkIsNumber(tbMenuSort.Text) && checkIsNumber(tbModuleSort.Text))
            {
                PmsCoreDB.Model.PMS_sys_MenuSetting model = new PmsCoreDB.Model.PMS_sys_MenuSetting();
                model.MenuName = tbMenuName.Text.ToString();
                model.DisplayMenuName = tbDisplayMenuName.Text.ToString();
                model.ModuleName = tbMenuModule.Text;
                model.IsShow = ckbIsMenuShow.Checked;
                model.ChildMenuSort = Convert.ToInt32(tbMenuSort.Text);
                model.ParentMenuSort = Convert.ToInt32(tbModuleSort.Text);

                if (PmsCoreDB.DBHost.Instance.MenuSetting.Add(model))
                {
                    MessageBox.Show(CLanguageManager.LanguageChange("添加成功") + "！");
                }
                else
                {
                    MessageBox.Show(CLanguageManager.LanguageChange("添加失败") + "！");
                }
                loadMenuTable();
            }
            else
            {
                MessageBox.Show("请检查输入");
            }
        }
        private void btMenuModfiy_Click(object sender, EventArgs e)
        {
            if (!String.IsNullOrEmpty(tbMenuName.Text.ToString()) && !String.IsNullOrEmpty(tbMenuModule.Text.ToString())
                && !String.IsNullOrEmpty(tbMenuModule.Text.ToString()) && checkIsNumber(tbMenuSort.Text) && checkIsNumber(tbModuleSort.Text))
            {
                PmsCoreDB.Model.PMS_sys_MenuSetting model = new PmsCoreDB.Model.PMS_sys_MenuSetting();
                model.MenuName = tbMenuName.Text.ToString();
                model.DisplayMenuName = tbDisplayMenuName.Text.ToString();
                model.ModuleName = tbMenuModule.Text;
                model.IsShow = ckbIsMenuShow.Checked;
                model.ChildMenuSort = Convert.ToInt32(tbMenuSort.Text);
                model.ParentMenuSort = Convert.ToInt32(tbModuleSort.Text);

                if (PmsCoreDB.DBHost.Instance.MenuSetting.Update(model))
                {
                    MessageBox.Show(CLanguageManager.LanguageChange("修改成功") + "！");
                }
                else
                {
                    MessageBox.Show(CLanguageManager.LanguageChange("修改失败") + "！");
                }
                loadMenuTable();
            }
            else
            {
                MessageBox.Show("请检查输入");
            }
        }
        private void btMenuDelete_Click(object sender, EventArgs e)
        {
            if (!String.IsNullOrEmpty(tbMenuName.Text.ToString()))
            {
                string strMsg = CLanguageManager.LanguageChange("删除菜单设置") + "【" + tbMenuName.Text.ToString() + "】，" + CLanguageManager.LanguageChange("是否继续") + "！";
                if (MessageBox.Show(strMsg, CLanguageManager.LanguageChange("删除菜单设置"), MessageBoxButtons.OKCancel, MessageBoxIcon.Warning) != DialogResult.OK)
                {
                    return;
                }

                if (PmsCoreDB.DBHost.Instance.MenuSetting.Delete(tbMenuName.Text.ToString()))
                {
                    MessageBox.Show(CLanguageManager.LanguageChange("删除成功") + "！");
                }
                else
                {
                    MessageBox.Show(CLanguageManager.LanguageChange("删除失败") + "！");
                }
                loadMenuTable();
            }
            else
            {
                MessageBox.Show("请检查输入");
            }
        }
        private void btColSetAdd_Click(object sender, EventArgs e)
        {
            if (!String.IsNullOrEmpty(tbFormName.Text.ToString()) && !String.IsNullOrEmpty(tbDBColumnName.Text.ToString())
                && !String.IsNullOrEmpty(tbShowName.Text.ToString())  && checkIsNumber(tbColumnSort.Text))
            {
                PmsCoreDB.Model.PMS_sys_FormColumnSetting model = new PmsCoreDB.Model.PMS_sys_FormColumnSetting();
                model.FormName = tbFormName.Text.ToString();
                model.DBColumnName = tbDBColumnName.Text;
                model.ShowColumnName = tbShowName.Text;
                model.IsShow = ckbShowColumn.Checked;
                model.Sort = Convert.ToInt32(tbColumnSort.Text);

                if (PmsCoreDB.DBHost.Instance.ColumnSetting.Add(model))
                {
                    MessageBox.Show(CLanguageManager.LanguageChange("添加成功") + "！");
                }
                else
                {
                    MessageBox.Show(CLanguageManager.LanguageChange("添加失败") + "！");
                }
                loadColumnTable();
            }
            else
            {
                MessageBox.Show("请检查输入");
            }
        }
        private void btColSetModify_Click(object sender, EventArgs e)
        {
            if (!String.IsNullOrEmpty(tbFormName.Text.ToString()) && !String.IsNullOrEmpty(tbDBColumnName.Text.ToString())
                && !String.IsNullOrEmpty(tbShowName.Text.ToString())  && checkIsNumber(tbColumnSort.Text))
            {
                PmsCoreDB.Model.PMS_sys_FormColumnSetting model = new PmsCoreDB.Model.PMS_sys_FormColumnSetting();
                model.FormName = tbFormName.Text.ToString();
                model.DBColumnName = tbDBColumnName.Text;
                model.ShowColumnName = tbShowName.Text;
                model.IsShow = ckbShowColumn.Checked;
                model.Sort = Convert.ToInt32(tbColumnSort.Text);


                if (PmsCoreDB.DBHost.Instance.ColumnSetting.Update(model))
                {
                    MessageBox.Show(CLanguageManager.LanguageChange("修改成功") + "！");
                }
                else
                {
                    MessageBox.Show(CLanguageManager.LanguageChange("修改失败") + "！");
                }
                loadColumnTable();
            }
            else
            {
                MessageBox.Show("请检查输入");
            }
        }
        private void btColSetDel_Click(object sender, EventArgs e)
        {
            if (!String.IsNullOrEmpty(tbFormName.Text.ToString())  && !String.IsNullOrEmpty(tbDBColumnName.Text.ToString()))
            {
                string strMsg = CLanguageManager.LanguageChange("删除界面列显示设置") + "【" + tbFormName.Text.ToString() + "】，" + CLanguageManager.LanguageChange("是否继续") + "！";
                if (MessageBox.Show(strMsg, CLanguageManager.LanguageChange("删除界面列显示设置"), MessageBoxButtons.OKCancel, MessageBoxIcon.Warning) != DialogResult.OK)
                {
                    return;
                }

                if (PmsCoreDB.DBHost.Instance.ColumnSetting.Delete(tbFormName.Text.ToString(), tbDBColumnName.Text.ToString()))
                {
                    MessageBox.Show(CLanguageManager.LanguageChange("删除成功") + "！");
                }
                else
                {
                    MessageBox.Show(CLanguageManager.LanguageChange("删除失败") + "！");
                }
                loadColumnTable();
            }
            else
            {
                MessageBox.Show("请检查输入");
            }
        }

        private void cbWhichForm_SelectedValueChanged(object sender, EventArgs e)
        {
            loadColumnTable();
        }

        private void btHelpsForm_Click(object sender, EventArgs e)
        {
            MessageBox.Show(PMSUI.Config.CConfigManager.gs_FormHelpConfig.GetFormHelp(CasunUILib.EForms.FormSetting.ToString()));

        }

        private void btHelpsMenu_Click(object sender, EventArgs e)
        {
            MessageBox.Show(PMSUI.Config.CConfigManager.gs_FormHelpConfig.GetFormHelp(CasunUILib.EForms.MenuSetting.ToString()));
        }
    }
}
