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 FPJ0000
{
    public partial class fSPMaster :FCOMMON.fBase
    {
        string fn_fpcolsize = "";
        public int qty = 0;

        string search = string.Empty;
        int idx = -1;
        public fSPMaster(string search_)
        {
            InitializeComponent();

            fn_fpcolsize = System.IO.Path.Combine(
                AppDomain.CurrentDomain.BaseDirectory,
                "formSetting", 
                "fp_" + this.Name + ".ini");

            this.WindowState = FormWindowState.Normal;
            this.StartPosition = FormStartPosition.CenterScreen;
            this.search = search_;
            this.tbFind.Text = search_;
            this.KeyDown += (s1, e1) => {
                if (e1.KeyCode == Keys.Escape) this.Close();
            };
        }

        private void __Load(object sender, EventArgs e)
        {
            this.Show();
            this.bs.Filter = "Storage <> 'B-CENTER'";
            Application.DoEvents();
            button1.PerformClick();
            
        }
        void refreshData(string div)
        {
            this.ta.Fill(this.dsMSSQL.SPMaster, "%" + div + "%");
            FPUtil.ColSizeLoad(ref this.fpSpread1, fn_fpcolsize);
            tbFind.Focus();
       //     tbFind.SelectAll();
        }
    

        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 dsPRJ.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)
        {
            this.refreshData(this.search);
        }

        //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;
        //        FCOMMON.Util.MsgE(ex.Message);
        //    }
        //}

        private void button1_Click(object sender, EventArgs e)
        {
            refreshData(tbFind.Text.Trim());
        }

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

        private void exportListToolStripMenuItem_Click(object sender, EventArgs e)
        {
            SaveFileDialog sd = new SaveFileDialog();
            sd.Filter = "excel|*.xls";
            var fn = "pmplist";            
            sd.FileName = fn + ".xls";
            if (sd.ShowDialog() == System.Windows.Forms.DialogResult.OK)
            {
                fpSpread1.SaveExcel(sd.FileName,
                    FarPoint.Excel.ExcelSaveFlags.SaveAsViewed
                    | FarPoint.Excel.ExcelSaveFlags.SaveAsFiltered
                    | FarPoint.Excel.ExcelSaveFlags.NoFormulas
                    | FarPoint.Excel.ExcelSaveFlags.SaveCustomColumnHeaders);
            }
        }

        private void autoToolStripMenuItem_Click(object sender, EventArgs e)
        {
            this.fpSpread1.ActiveSheet.DataAutoSizeColumns = true;

            for (int i = 0; i < this.fpSpread1.ActiveSheet.Rows.Count; i++)
                this.fpSpread1.ActiveSheet.SetRowHeight(i, 25);
            //dv1.AutoResizeColumns();
        }

        private void resetToolStripMenuItem_Click(object sender, EventArgs e)
        {
            foreach (FarPoint.Win.Spread.Column col in this.fpSpread1.ActiveSheet.Columns)
            {
                col.Width = 100;
            }
        }

        private void saveToolStripMenuItem_Click(object sender, EventArgs e)
        {
            FPUtil.ColsizeSave(this.fpSpread1, fn_fpcolsize);
        }

        private void loadToolStripMenuItem_Click(object sender, EventArgs e)
        {
            FPUtil.ColSizeLoad(ref this.fpSpread1, fn_fpcolsize);
        }

        private void chkZeroCount_Click(object sender, EventArgs e)
        {
            if (chkZeroCount.Checked)
                this.bs.Filter = "CurrentQty > 0 and Storage <> 'B-CENTER'";
            else
                this.bs.Filter = "Storage <> 'B-CENTER'";
        }
    }
}
