using FCOMMON;
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 fLovProject : fBase
    {
        public string val_Title { get; set; }
        public string val_OrderNo { get; set; }
        public int val_Index { get; set; }

        string keyword = string.Empty;
        string keyword2 = string.Empty;
        bool layoutmode = false;
        public fLovProject(string search_, string stat_ = "", bool layout = false)
        {
            InitializeComponent();
            Properties.Settings.Default["gwcs"] = FCOMMON.info.CS;

            val_Title = string.Empty;
            val_Index = -1;
            layoutmode = layout;

            this.keyword = search_;
            this.keyword2 = stat_;
            if (this.keyword2.isEmpty()) keyword2 = "%";
            this.KeyPreview = true;
            this.KeyDown += (s1, e1) =>
            {
                if (e1.KeyCode == Keys.Escape) this.Close();
            };

        }

        private void fLovItem_Load(object sender, EventArgs e)
        {
            EnsureVisibleAndUsableSize();
            // var dt = this.ta.GetSearch(this.keyword);
            var statekey = this.keyword2;

            if (layoutmode)
            {
                this.ta.FillByLayout(this.dsMSSQL.Projects,
                "%" + this.keyword + "%",
                FCOMMON.info.Login.gcode);
            }
            else
            {
                this.ta.FillSearch(this.dsMSSQL.Projects,
                "%" + this.keyword + "%",
                FCOMMON.info.Login.gcode,
                 this.keyword2);
            }
            this.Show();
            Application.DoEvents();

            this.StartPosition = FormStartPosition.CenterScreen;
            bs.Filter = "status <> '취소'";

            this.dv.AutoResizeColumns();
        }

        private void bs_CurrentChanged(object sender, EventArgs e)
        {

        }

        private void dv_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
            {

                btOK.PerformClick();
            }
        }

        private void itemsBindingNavigatorSaveItem_Click(object sender, EventArgs e)
        {
            var drv = bs.Current as DataRowView;
            if (drv == null)
            {
                val_Title = string.Empty;
                val_OrderNo = string.Empty;
                val_Index = -1;
                return;
            }
            else
            {
                var dr = drv.Row as dsMSSQL.ProjectsRow;
                val_Index = dr.idx;
                val_Title = dr.name;
                val_OrderNo = dr.orderno;
            }

            if (val_Title.isEmpty() || val_Index == -1) DialogResult = System.Windows.Forms.DialogResult.Cancel;
            else DialogResult = System.Windows.Forms.DialogResult.OK;
        }

        private void dv_DoubleClick(object sender, EventArgs e)
        {
            btOK.PerformClick();
        }

        private void toolStripTextBox1_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter) btFGind.PerformClick();
        }

        private void btFGind_Click(object sender, EventArgs e)
        {
            var search = tbFind.Text.Trim();
            if (search.isEmpty())
            {
                bs.Filter = "status <> '취소'";

                tbFind.BackColor = SystemColors.Control;
            }
            else
            {
                if (search.IsNumeric()) bs.Filter = "idx=" + search;
                else
                {
                    var Filter = "(name like ? or reqstaff like ? or usermanager like ?) and status <> '취소'";
                    Filter = Filter.Replace("?", "'%" + search.Replace("'", "''") + "%'");
                    bs.Filter = Filter;
                    tbFind.BackColor = Color.Lime;
                }
            }
            tbFind.Focus();
            tbFind.SelectAll();
        }

        private void pdateLabel_Click(object sender, EventArgs e)
        {

        }
    }
}
