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 DisplayLanguageForm : CasunForm
    {
        private int selectRowIndexOfordersDataGridView;         // 记录选中的行号
        private int selectColumnIndexOfordersDataGridView;      // 记录选中的列号
        private int scrollingRowIndexOfordersDataGridView;      // 记录垂直滚动条位置
        private int scrollingColumnIndexOfordersDataGridView;   // 记录水平滚动条的位置

        private enum DgvColumn
        {
            ID,
            Language,
            Keyword,
            Displayword,
            Remark,
            Reserved1,
            Reserved2,
            Reserved3
        }

        public DisplayLanguageForm()
        {
            InitializeComponent();
        }

        private void DisplayLanguageForm_Load(object sender, EventArgs e)
        {
            CasunUILib.ButtonHelps(btHelps);

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

            CasunUILib.GroupBoxReadModel(gp_Read, PermissionLevel);
            CasunUILib.GroupBoxReadWriteModel(gp_Opt, PermissionLevel);
            CasunUILib.GroupBoxReadWriteModel(gp_Opt1, PermissionLevel);
            CasunUILib.GroupBoxReadWriteModel(gp_Opt2, PermissionLevel);

            CasunUILib.splitContainerModel(splitContainer1);
            CasunUILib.DgvModel(DGVLanguage);
            InitTable();
            Init();
        }
        public void InitTable()
        {
            DGVLanguage.Columns.Clear();
            foreach (DgvColumn sDgv in Enum.GetValues(typeof(DgvColumn)))
            {
                DataGridViewTextBoxColumn dc = new DataGridViewTextBoxColumn();
                dc.HeaderText = sDgv.ToString();
                DGVLanguage.Columns.Add(dc);
            }
            int width = DGVLanguage.Width;

            DGVLanguage.Columns[(int)DgvColumn.ID].Width = 5;
            //DGVLanguage.Columns[(int)DgvColumn.Permission].Width = width * 10 / 100;
        }
        private void Init()
        {

            List<string> LanguageList = PmsCoreDB.DBHost.Instance.Language.GetLanguage();
            //List<string> LanguageList = CasunModelDB.CDBManager.Instance.PMS_sys_Language.GetLanguage();

            cbLanguage.Items.AddRange(LanguageList.ToArray());
            if (LanguageList.Count > 0)
            {
                cbLanguage.SelectedIndex = 0;
            }
        }

        private void btModfiyStep_Click(object sender, EventArgs e)
        {
            if (DGVLanguage.CurrentRow == null)
            {
                MessageBox.Show(CLanguageManager.LanguageChange("请选择需要处理的行") + "！");
                return;
            }

            int currentIndex = DGVLanguage.CurrentRow.Index;
            if (currentIndex != -1)
            {
                PmsCoreDB.Model.PMS_sys_Language smodel = new PmsCoreDB.Model.PMS_sys_Language();

                smodel.Language = DGVLanguage.Rows[currentIndex].Cells[(int)DgvColumn.Language].Value.ToString();
                smodel.Keyword = DGVLanguage.Rows[currentIndex].Cells[(int)DgvColumn.Keyword].Value.ToString();
                smodel.Displayword = tbDisplayword.Text.ToString();

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

                //CasunModelDB.CDBManager.Instance.PMS_sys_Language.Update(smodel);
                PmsCoreDB.DBHost.Instance.Language.Update(smodel);

                string strOptLog = string.Format("【{7}】:{8}={0},{9}={1},{10}={2},remark={3},reserved1={4},reserved2={5},reserved3={6}"
                    , smodel.Language
                    , smodel.Keyword
                    , smodel.Displayword
                    , smodel.Remark
                    , smodel.Reserved1
                    , smodel.Reserved2
                    , smodel.Reserved3
                    , CLanguageManager.LanguageChange("修改语言")
                    , CLanguageManager.LanguageChange("语言")
                    , CLanguageManager.LanguageChange("关键字")
                    , CLanguageManager.LanguageChange("显示信息")
                    );
                COperateLog.Instance.InsertOperaterLogAsync(UserID, strOptLog, DateTime.Now);

                LanguageSelectChange(cbLanguage.SelectedItem.ToString());
                MessageBox.Show(CLanguageManager.LanguageChange("修改成功") + "！");
            }
        }

        private void LanguageSelectChange(string strLanguage)
        {
            if (DGVLanguage.CurrentCell != null && DGVLanguage.RowCount != 0 && DGVLanguage.ColumnCount != 0)
            {
                selectRowIndexOfordersDataGridView = DGVLanguage.CurrentCell.RowIndex;
                selectColumnIndexOfordersDataGridView = DGVLanguage.CurrentCell.ColumnIndex;
                scrollingRowIndexOfordersDataGridView = DGVLanguage.FirstDisplayedScrollingRowIndex;
                scrollingColumnIndexOfordersDataGridView = DGVLanguage.FirstDisplayedScrollingColumnIndex;
            }
            DGVLanguage.Rows.Clear();

            string strWhere = " Language = '" + strLanguage + "' order BY Keyword ";
            //List<CasunModelDB.Model.PMS_sys_LanguageVo> LanguageSetList
            //    = CasunModelDB.CDBManager.Instance.PMS_sys_Language.GetModelList(strWhere);

            List<PmsCoreDB.Model.PMS_sys_Language> LanguageSetList
                = PmsCoreDB.DBHost.Instance.Language.GetModelList(strWhere);

            foreach (PmsCoreDB.Model.PMS_sys_Language sLanguage in LanguageSetList)
            {
                string[] rowData = new string[8];
                rowData[(int)DgvColumn.Language] = sLanguage.Language.ToString();
                rowData[(int)DgvColumn.Keyword] = sLanguage.Keyword.ToString();
                rowData[(int)DgvColumn.Displayword] = sLanguage.Displayword.ToString();

                rowData[(int)DgvColumn.Remark] = sLanguage.Remark.ToString();
                rowData[(int)DgvColumn.Reserved1] = sLanguage.Reserved1.ToString();
                rowData[(int)DgvColumn.Reserved2] = sLanguage.Reserved2.ToString();
                rowData[(int)DgvColumn.Reserved3] = sLanguage.Reserved3.ToString();
                DGVLanguage.Rows.Add(rowData);
            }

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

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


        private void btAddPermission_Click(object sender, EventArgs e)
        {

            string strLanguage = cbLanguage.Text.ToString();
            if (!cbLanguage.Items.Contains(strLanguage))
            {
                cbLanguage.Enabled = false;
                foreach (string str in CLanguageManager.LanguageKey)
                {
                    PmsCoreDB.Model.PMS_sys_Language smodel = new PmsCoreDB.Model.PMS_sys_Language();

                    //CasunModelDB.Model.PMS_sys_LanguageVo smodel = new CasunModelDB.Model.PMS_sys_LanguageVo();
                    smodel.Language = strLanguage;
                    smodel.Keyword = str;

                    //CasunModelDB.CDBManager.Instance.PMS_sys_Language.Add(smodel);
                    PmsCoreDB.DBHost.Instance.Language.Add(smodel);
                }
                string strOptLog = string.Format("【{0}】:{1}={2}"
                , CLanguageManager.LanguageChange("添加语言")
                , CLanguageManager.LanguageChange("语言")
                , strLanguage
                );
                COperateLog.Instance.InsertOperaterLogAsync(UserID, strOptLog, DateTime.Now);
                cbLanguage.Items.Add(cbLanguage.Text);
                cbLanguage.Enabled = true;
            }
            else
            {
                string strMsg = CLanguageManager.LanguageChange("语言") + "：【" + cbLanguage.Text.ToString() + "】" + CLanguageManager.LanguageChange("已存在") + "！";
                MessageBox.Show(strMsg);
            }
        }

        private void btDelete_Click(object sender, EventArgs e)
        {
            if (cbLanguage.SelectedIndex == -1)
            {
                return;
            }

            string strLanguage = cbLanguage.Text.ToString();
            string strMsg = CLanguageManager.LanguageChange("是否删除语言") + "：【" + strLanguage + "】\r\n" + CLanguageManager.LanguageChange("请谨慎操作") + "！";

            if (MessageBox.Show(strMsg, CLanguageManager.LanguageChange("删除语言"), MessageBoxButtons.OKCancel, MessageBoxIcon.Warning) == DialogResult.OK)
            {
                //if (CasunModelDB.CDBManager.Instance.PMS_sys_Language.DeleteLanguage(strLanguage))
                if (PmsCoreDB.DBHost.Instance.Language.DeleteLanguage(strLanguage))
                {
                    cbLanguage.Items.Remove(strLanguage);
                    DGVLanguage.Rows.Clear();

                    string strOptLog = string.Format("【{0}】:{1}={2}"
                        , CLanguageManager.LanguageChange("删除语言")
                        , CLanguageManager.LanguageChange("语言")
                        , strLanguage
                        );

                    COperateLog.Instance.InsertOperaterLogAsync(UserID, strOptLog, DateTime.Now);
                }
                else
                {

                }
            }
        }

        private void btDeleteKeyword_Click(object sender, EventArgs e)
        {
            CLanguageManager.DeleteKeyword(tbKeywordOpt.Text.Trim());

            LanguageSelectChange(cbLanguage.SelectedItem.ToString());
        }

        private void cbLanguage_SelectedIndexChanged(object sender, EventArgs e)
        {

            if (cbLanguage.SelectedIndex != -1)
            {
                LanguageSelectChange(cbLanguage.SelectedItem.ToString());

                tbDisplayword.Text = "";
                tbKeyword.Text = "";
                tbRemark.Text = "";
                tbReserved1.Text = "";
                tbReserved2.Text = "";
                tbReserved3.Text = "";
            }
        }

        private void DGVLanguage_CellMouseClick(object sender, DataGridViewCellMouseEventArgs e)
        {

            if (DGVLanguage.CurrentRow == null)
            {
                return;
            }
            int currentIndex = DGVLanguage.CurrentRow.Index;
            if (currentIndex != -1)
            {
                tbKeyword.Text = DGVLanguage.Rows[currentIndex].Cells[(int)DgvColumn.Keyword].Value.ToString();
                tbKeywordOpt.Text = DGVLanguage.Rows[currentIndex].Cells[(int)DgvColumn.Keyword].Value.ToString();
                tbDisplayword.Text = DGVLanguage.Rows[currentIndex].Cells[(int)DgvColumn.Displayword].Value.ToString();
                tbRemark.Text = DGVLanguage.Rows[currentIndex].Cells[(int)DgvColumn.Remark].Value.ToString();
                tbReserved1.Text = DGVLanguage.Rows[currentIndex].Cells[(int)DgvColumn.Reserved1].Value.ToString();
                tbReserved2.Text = DGVLanguage.Rows[currentIndex].Cells[(int)DgvColumn.Reserved2].Value.ToString();
                tbReserved3.Text = DGVLanguage.Rows[currentIndex].Cells[(int)DgvColumn.Reserved3].Value.ToString();
            }
        }

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

        }

        private void btnAdd_Click(object sender, EventArgs e)
        {

            PmsCoreDB.Model.PMS_sys_Language smodel = new PmsCoreDB.Model.PMS_sys_Language();

            smodel.Language = "zh-CN";
            smodel.Keyword = tbKeyword.Text.ToString();
            smodel.Displayword = tbDisplayword.Text.ToString();

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


            //CasunModelDB.CDBManager.Instance.PMS_sys_Language.Update(smodel);
            if (PmsCoreDB.DBHost.Instance.Language.Add(smodel))
            {
                LanguageSelectChange("zh-CN");
                MessageBox.Show(CLanguageManager.LanguageChange("添加成功") + "！");
            }
        }

    }
}
