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 FPM0000
{
    public partial class fSPMaster :FCOMMON.fBase
    {
        int idx = -1;
        public fSPMaster()
        {
            InitializeComponent();
            this.dsMSSQL.SPMaster.TableNewRow += minutes_TableNewRow;
        }

        void minutes_TableNewRow(object sender, DataTableNewRowEventArgs e)
        {
            //e.Row["wuid"] = FCOMMON.info.Login.no;
            //e.Row["wdate"] = DateTime.Now;
            //e.Row["stime"] = DateTime.Now;
            //e.Row["etime"] = DateTime.Now.AddHours(1);
            //e.Row["title"] = "Subject";
        }

        private void __Load(object sender, EventArgs e)
        {
            this.Show();
            Application.DoEvents();
            refreshDivision();
        }
        void refreshData(string div)
        {
            
            //refresh data
           
          //  this.dv1.AutoResizeColumns();
        }
        void refreshDivision()
        {
            //모든 디비젼을 가져와서 우측을 다시 생성한다
            var taDiv = new DSMssqlTableAdapters.DivListTableAdapter();
            var dt = taDiv.GetData();

            this.listView1.Items.Clear();
            
            var bta  = this.listView1.Items.Add( "전체보기");
            bta.BackColor = Color.HotPink;
            bta.Tag = "";

            foreach(DSMssql.DivListRow dr in dt.Rows)
            {
                var bt = this.listView1.Items.Add(dr.Division);
                bt.Tag = dr.Division;
            }

        }

        private void listView1_SelectedIndexChanged(object sender, EventArgs e)
        {
            //지정된 Division을 필터링한다
            if (listView1.FocusedItem == null) return;

            ListViewItem bt = this.listView1.FocusedItem ;
            if (bt.Tag.ToString() == "")
            {
                var dlg = Util.MsgQ("전체 보기는 데이터 통신량과 시간이 오래 소요됩니다\n" +
                    "Division을 알고 있다면 아래 Division 목록을 먼저 선택하고 검색 하세요");
                if (dlg != DialogResult.Yes) return;

                label2.Text = "전체보기";
                label2.Tag = "";
                this.ta.FillByAll(this.dsMSSQL.SPMaster);
            }
            else
            {
                label2.Text = bt.Text; 
                this.ta.Fill(this.dsMSSQL.SPMaster, bt.Tag.ToString());
            }

            label2.Tag = bt.Tag;
            tbFind.Focus();
            tbFind.SelectAll();
        }
      

        private void autoColumnSizeToolStripMenuItem_Click(object sender, EventArgs e)
        {
          //  this.dv1.AutoResizeColumns();
        }

        private void toolStripButton1_Click(object sender, EventArgs e)
        {
            if(label2.Tag != null)
            refreshData(label2.Tag.ToString());
        }

        private void dv1_DoubleClick(object sender, EventArgs e)
        {
           
        }

        private void bs_CurrentChanged(object sender, EventArgs e)
        {
            idx = -1;
            var drv = bs.Current as DataRowView;
            if (drv == null) return;
            var dr = drv.Row as DSMssql.SPMasterRow;//.minutesRow;
            idx = dr.ID;
        }

        private void toolStripButton2_Click(object sender, EventArgs e)
        {
            try
            {
                var cnt = this.ta.Update(this.dsMSSQL.SPMaster);
            }
            catch (Exception ex)
            {
                FCOMMON.Util.MsgE(ex.Message);
            }
        }

        private void 목록새로고침ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            refreshDivision();
        }

        void Find()
        {
            var txt = tbFind.Text.Trim();
            if (txt == "")
            {
                if(bs.Filter != "")
                {
                    bs.Filter = "";
                    tbFind.BackColor = Color.WhiteSmoke;
                }
                tbFind.Focus();
                return;
            }

            //검색어가 잇으므로 검색을 수행한다
            var cols = new string[] { "SIDNo","Storage","Location",
            "PartName","PartNo","Memo","CostCenter","Enrollee"};
            string filter = "";
            foreach(var col in cols)
            {
                if (filter != "") filter += " or ";
                filter += col + " like '%"+ txt.Replace("'","''") +"%' ";
            }
            try
            {
                this.bs.Filter = filter;
                tbFind.BackColor = Color.Lime;
                tbFind.SelectAll();
            }catch (Exception ex)
            {
                this.bs.Filter = "";
                tbFind.BackColor = Color.Red;
                Util.MsgE(ex.Message);
            }
        }

        private void button1_Click(object sender, EventArgs e)
        {
            Find();
        }

        private void tbFind_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
                Find();
        }

    }
}
