using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace FCM0000
{
    public partial class fCode : FCOMMON.fBase
    {
        public fCode()
        {
            InitializeComponent();
            this.dsMSSQL.Common.TableNewRow += Common_TableNewRow;
            if (FCOMMON.info.Login.level >= 9) toolStripButton1.Visible = true;
        }

        void Common_TableNewRow(object sender, DataTableNewRowEventArgs e)
        {
            e.Row["gcode"] = FCOMMON.info.Login.gcode;
            e.Row["wuid"] = FCOMMON.info.Login.no;
            e.Row["wdate"] = DateTime.Now;
            string grp = "99";
            if (this.cmbList.SelectedIndex > 0)
            {
                var title = cmbList.Text.Split(']');
                grp = title[0].Substring(1);
            }
            e.Row["grp"] = grp;
        }

        private void __Load(object sender, EventArgs e)
        {
            this.Show();
            Application.DoEvents();

            if (this.WindowState == FormWindowState.Minimized) this.WindowState = FormWindowState.Normal;
            if (this.RestoreBounds.X + this.RestoreBounds.Width < 300 && this.RestoreBounds.X < 0)
                this.Location = new Point(0, 0);


            toolStripButton1.Visible = FCOMMON.info.Login.no == "dev";
            // this.TopMost=true;
            // this.Activate();
            cmbList_SelectedIndexChanged(null, null);
        }
        void RefreshCodeList()
        {
            //공용코드목록을 가져온다.
            cmbList.Items.Clear();
            var codelist = FCOMMON.DBM.getCodeList();
            cmbList.Items.Add("-- Code Group List --");
            foreach (var item in codelist)
            {
                cmbList.Items.Add(string.Format("[{0}] {1}", item.Key, item.Value));
            }
        }
        void RefreshCodeData()
        {
            string grp = "99";
            if (cmbList.SelectedIndex > 0)
            {
                var title = cmbList.Text.Split(']');
                grp = title[0].Substring(1);
            }
            try
            {
                this.ta.Fill(this.dsMSSQL.Common, grp, FCOMMON.info.Login.gcode);
                this.dsMSSQL.Common.AcceptChanges();
            }
            catch (Exception ex)
            {
                FCOMMON.Util.MsgE("Query Error\n\n" + ex.Message);
            }
        }

        private void commonBindingNavigatorSaveItem_Click(object sender, EventArgs e)
        {
            this.Validate();
            this.bs.EndEdit();
            this.tam.UpdateAll(this.dsMSSQL);
            this.dsMSSQL.Common.AcceptChanges();
            if (this.cmbList.SelectedIndex <= 0)
            {
                cmbList_SelectedIndexChanged(null, null);
            }

        }

        private void cmbList_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (this.dsMSSQL.HasChanges())
            {
                var dlg = FCOMMON.Util.MsgQ(
                    "변경된 자료가 손실됩니다.\n\n" +
                    "목록을 새로고침 하시겠습니까?");
                if (dlg != System.Windows.Forms.DialogResult.Yes) return;
            }
            if (cmbList.SelectedIndex <= 0)
            {
                RefreshCodeList();
                RefreshCodeData();
            }
            else if (cmbList.SelectedIndex > 0)
                RefreshCodeData();
        }

        private void 코드일괄생성ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            var drows = this.dsMSSQL.Common.Select(this.bs.Filter, "memo");
            for (int i = 1; i <= drows.Length; i++)
            {
                drows[i - 1]["code"] = i.ToString("000");
            }
        }

        private void title가져오기ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            var f = new FCOMMON.fInputTextBox();
            if (f.ShowDialog() == DialogResult.OK)
            {
                var data = f.textBox1.Text.Trim();
                data = data.Replace("\r", "");
                var lines = data.Split(new string[] { "\n" }, StringSplitOptions.RemoveEmptyEntries);

                var dlg = FCOMMON.Util.MsgQ(lines.Count().ToString() + "건의 자료가 있습니다. 입력할까요?");
                if (dlg == DialogResult.Yes)
                {
                    foreach (var item in lines)
                    {
                        if (item.Trim().Equals("")) continue;

                        if(this.dsMSSQL.Common.Where(t=>t.memo == item.Trim()).Any()==false)
                        {
                            var newdr = this.dsMSSQL.Common.NewCommonRow();
                            newdr.memo = item;
                            this.dsMSSQL.Common.AddCommonRow(newdr);
                        }
                    }
                }


            }
        }

        private void s가져오기ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            var f = new FCOMMON.fInputTextBox();
            if (f.ShowDialog() == DialogResult.OK)
            {
                var data = f.textBox1.Text.Trim();
                data = data.Replace("\r", "");
                var lines = data.Split(new string[] { "\n" }, StringSplitOptions.RemoveEmptyEntries);

                var dlg = FCOMMON.Util.MsgQ(lines.Count().ToString() + "건의 자료가 있습니다. 입력할까요?");
                if (dlg == DialogResult.Yes)
                {
                    int r = 0;
                    foreach (var item in lines)
                    {
                        if(r < this.dsMSSQL.Common.Count)
                            this.dsMSSQL.Common.Rows[r]["svalue"] = item;

                        r += 1;
                        //if (item.Trim().Equals("")) continue;

                        //if (this.dsMSSQL.Common.Where(t => t.memo == item.Trim()).Any() == false)
                        //{
                        //    var newdr = this.dsMSSQL.Common.NewCommonRow();
                        //    newdr.memo = item;
                        //    this.dsMSSQL.Common.AddCommonRow(newdr);
                        //}
                    }
                }


            }
        }
    }
}
