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 CWarehouseTypeForm : CasunForm
    {
        private int selectRowIndexOfordersDataGridView;         // 记录选中的行号
        private int selectColumnIndexOfordersDataGridView;      // 记录选中的列号
        private int scrollingRowIndexOfordersDataGridView;      // 记录垂直滚动条位置
        private int scrollingColumnIndexOfordersDataGridView;   // 记录水平滚动条的位置
        //public PmsCoreDB.Model.PMS_bas_WarehouseType oldmodel = new PmsCoreDB.Model.PMS_bas_WarehouseType();
        private PmsCoreDB.Model.PMS_bas_WarehouseType oldmodel;  //记录更新前选中行的信息

        public CWarehouseTypeForm()
        {
            InitializeComponent();
        }

        private enum EDGVColumns
        {
            WarehouseType,
            TemplateID,
            TemplateBehavior,
            Remark
        }

        private void InitDgv()
        {
            CasunUILib.DgvModel(DGVWarehouseTypes);
        }

        private void CWarehouseTypeForm_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();

            InitDgv();
            #region 表格数值添加
            loadTable();
            #endregion
        }

        private void loadTable()
        {
            ResetControl();

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

            DGVWarehouseTypes.DataSource = 
                CFormColumnManager.GetDGVDataSource(this.Name, "PMS_bas_WarehouseType", "order by WarehouseType ,BookTemplateBehavior");

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

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

        private void ResetControl()
        {
            tbBookTemplateBehavior.Text = "";
            tbWarehousetype.Text = "";
            cbStepTemplateID.Text = "";
            tbRemark.Text = "";
            tbReserved1.Text = "";
            tbReserved2.Text = "";
            tbReserved3.Text = "";
        }

        private void btAdd_Click(object sender, EventArgs e)
        {
            if (!String.IsNullOrEmpty(tbWarehousetype.Text.ToString())
                && !String.IsNullOrEmpty(cbStepTemplateID.Text.ToString())
                && !String.IsNullOrEmpty(tbBookTemplateBehavior.Text.ToString())
                )
            {
                PmsCoreDB.Model.PMS_bas_WarehouseType RTV = new PmsCoreDB.Model.PMS_bas_WarehouseType();
                RTV.WarehouseType = tbWarehousetype.Text.ToString();
                RTV.StepBookTemplateID = cbStepTemplateID.Text.ToString();
                RTV.BookTemplateBehavior = Convert.ToInt32(tbBookTemplateBehavior.Text.ToString());
                RTV.Remark = tbRemark.Text.ToString();
                RTV.Reserved1 = tbReserved1.Text.ToString();
                RTV.Reserved2 = tbReserved2.Text.ToString();
                RTV.Reserved3 = tbReserved3.Text.ToString();
                if (PmsCoreDB.DBHost.Instance.WarehouseType.Add(RTV))
                {
                    MessageBox.Show(CLanguageManager.LanguageChange("添加成功") + "！");
                }
                else
                {
                    MessageBox.Show(CLanguageManager.LanguageChange("添加失败") + "！");
                }
                loadTable();
            }
        }

        private void btModfiy_Click(object sender, EventArgs e)
        {
            if (!String.IsNullOrEmpty(tbWarehousetype.Text.ToString()) 
                && !String.IsNullOrEmpty(cbStepTemplateID.Text.ToString())
                && !String.IsNullOrEmpty(tbBookTemplateBehavior.Text.ToString())
                )
            {
                PmsCoreDB.Model.PMS_bas_WarehouseType RTV = new PmsCoreDB.Model.PMS_bas_WarehouseType();
                RTV.WarehouseType = tbWarehousetype.Text.ToString();
                RTV.StepBookTemplateID = cbStepTemplateID.Text.ToString();
                RTV.BookTemplateBehavior = Convert.ToInt32(tbBookTemplateBehavior.Text.ToString());
                RTV.Remark = tbRemark.Text.ToString();
                RTV.Reserved1 = tbReserved1.Text.ToString();
                RTV.Reserved2 = tbReserved2.Text.ToString();
                RTV.Reserved3 = tbReserved3.Text.ToString();
                if (PmsCoreDB.DBHost.Instance.WarehouseType.Update(RTV, oldmodel))
                {
                    MessageBox.Show(CLanguageManager.LanguageChange("修改成功") + "！");
                }
                else
                {
                    MessageBox.Show(CLanguageManager.LanguageChange("修改失败") + "！");
                }
                loadTable();
            }
        }

        private void btDelete_Click(object sender, EventArgs e)
        {
            if (!String.IsNullOrEmpty(tbWarehousetype.Text.ToString())
                && !String.IsNullOrEmpty(cbStepTemplateID.Text.ToString())
                && !String.IsNullOrEmpty(tbBookTemplateBehavior.Text.ToString())
                )
            {
                string strMsg = CLanguageManager.LanguageChange("删除库位类型") + "【" + tbWarehousetype.Text.ToString() + "】，" + CLanguageManager.LanguageChange("是否继续") + "！";
                if (MessageBox.Show(strMsg, CLanguageManager.LanguageChange("删除库位类型"), MessageBoxButtons.OKCancel, MessageBoxIcon.Warning) != DialogResult.OK)
                {
                    return;
                }

                string WarehouseType = tbWarehousetype.Text.ToString();
                string StepBookTemplateID = cbStepTemplateID.Text.ToString();
                int BookTemplateBehavior = Convert.ToInt32(tbBookTemplateBehavior.Text.ToString());

                if (PmsCoreDB.DBHost.Instance.WarehouseType.Delete(WarehouseType, StepBookTemplateID, BookTemplateBehavior))
                {
                    MessageBox.Show(CLanguageManager.LanguageChange("删除成功") + "！");
                }
                else
                {
                    MessageBox.Show(CLanguageManager.LanguageChange("删除失败") + "！");
                }
                loadTable();
            }
        }

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

            lbStepTemplateID.Visible = true;
            cbStepTemplateID.Visible = true;

            #region 绑定模板ID下拉框
            List<string> LTemplateID = PmsCoreDB.DBHost.Instance.StepBookTemplate.GetTemplateID();
            cbStepTemplateID.Items.Clear();
            cbStepTemplateID.Items.Add("");
            cbStepTemplateID.Items.AddRange(LTemplateID.ToArray());
            if (LTemplateID.Count > 0)
            {
                cbStepTemplateID.SelectedIndex = 0;
            }
            #endregion

            lbBookTemplateBehavior.Visible = true;
            tbBookTemplateBehavior.Visible = true;

            List<string> InitControlList = CFormColumnManager.GetNeedShowDBList(this.Name);
            foreach (string str in InitControlList)
            {
                switch (str)
                {
                    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("WarehouseType"))
                lbWarehousetype.Text = DBNameToShowNameDic["WarehouseType"];
            if (DBNameToShowNameDic.ContainsKey("StepBookTemplateID"))
                lbStepTemplateID.Text = DBNameToShowNameDic["StepBookTemplateID"];
            if (DBNameToShowNameDic.ContainsKey("BookTemplateBehavior"))
                lbBookTemplateBehavior.Text = DBNameToShowNameDic["BookTemplateBehavior"];
            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 DGVWarehouseTypes_CellMouseClick(object sender, DataGridViewCellMouseEventArgs e)
        {
            int DGVCurrentRow = e.RowIndex;
            oldmodel = new PmsCoreDB.Model.PMS_bas_WarehouseType();
            ResetControl();
            if (DGVCurrentRow != -1 && DGVWarehouseTypes.Rows[DGVCurrentRow] != null)
            {
                ShowText(tbWarehousetype, "WarehouseType", DGVCurrentRow);
                oldmodel.WarehouseType = tbWarehousetype.Text;

                ShowText(cbStepTemplateID, "StepBookTemplateID", DGVCurrentRow);
                oldmodel.StepBookTemplateID = cbStepTemplateID.Text;

                ShowText(tbBookTemplateBehavior, "BookTemplateBehavior", DGVCurrentRow);
                oldmodel.BookTemplateBehavior = Convert.ToInt32(tbBookTemplateBehavior.Text);

                ShowText(tbRemark, "Remark", DGVCurrentRow);
                oldmodel.Remark = tbRemark.Text;

                ShowText(tbReserved1, "Reserved1", DGVCurrentRow);
                oldmodel.Reserved1 = tbReserved1.Text;

                ShowText(tbReserved2, "Reserved2", DGVCurrentRow);
                oldmodel.Reserved2 = tbReserved2.Text;

                ShowText(tbReserved3, "Reserved3", DGVCurrentRow);
                oldmodel.Reserved3 = tbReserved3.Text;
            }
        }

        private void ShowText(Control sControl, string strKey, int DGVCurrentRow)
        {
            if (DBNameToShowNameDic.ContainsKey(strKey) && DGVWarehouseTypes.Rows[DGVCurrentRow].Cells[DBNameToShowNameDic[strKey]].Value != null)
            {
                sControl.Text = DGVWarehouseTypes.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));

        }
    }
}
