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 fCustoms : FCOMMON.fBase
    {
        string fn_fpcolsize1 = "";
        string fn_fpcolsize2 = "";
        public fCustoms()
        {
            InitializeComponent();
            Properties.Settings.Default["gwcs"] = FCOMMON.info.CS;
            fn_fpcolsize1 = FCOMMON.Util.MakeFilePath(FCOMMON.Util.CurrentPath, "formSetting", "fp1_" + this.Name + ".ini");
            fn_fpcolsize2 = FCOMMON.Util.MakeFilePath(FCOMMON.Util.CurrentPath, "formSetting", "fp2_" + this.Name + ".ini");
        }
     
        private void __Load(object sender, EventArgs e)
        {
            EnsureVisibleAndUsableSize();

            this.Show();
            Application.DoEvents();

            refreshData();
            refreshDataS();
        
            this.dsMSSQL.Customs.TableNewRow += Customs_TableNewRow;
            this.dsMSSQL.Staff.TableNewRow += Staff_TableNewRow;
        }

        void Staff_TableNewRow(object sender, DataTableNewRowEventArgs e)
        {
            e.Row["wuid"] = FCOMMON.info.Login.no;
            e.Row["wdate"] = DateTime.Now;
            e.Row["cid"] = selcectIDX;
            e.Row["gcode"] = FCOMMON.info.Login.gcode;    
        }

        void Customs_TableNewRow(object sender, DataTableNewRowEventArgs e)
        {
            e.Row["wuid"] = FCOMMON.info.Login.no;
            e.Row["wdate"] = DateTime.Now;
            e.Row["grp"] = "Supply"; 
            e.Row["gcode"] = FCOMMON.info.Login.gcode;           
        }

        void refreshData()
        {
            try
            {
                this.ta.Fill(this.dsMSSQL.Customs,FCOMMON.info.Login.gcode);
               // this.taStaff.Fill(this.dsMSSQL.Staff);
                FPUtil.ColSizeLoad(ref this.fpSpread1, fn_fpcolsize1);
               
            }catch (Exception ex)
            {
                FCOMMON.Util.MsgE(ex.Message);
            }
        }
        void refreshDataS()
        {
            try
            {
               // this.ta.Fill(this.dsMSSQL.Customs);
                this.taStaff.Fill(this.dsMSSQL.Staff, FCOMMON.info.Login.gcode);
                FPUtil.ColSizeLoad(ref this.fpSpread2, fn_fpcolsize2);
            }
            catch (Exception ex)
            {
                FCOMMON.Util.MsgE(ex.Message);
            }
        }
        private void customsBindingNavigatorSaveItem_Click(object sender, EventArgs e)
        {
            this.Validate();
            this.bs.EndEdit();
            int cnt = this.ta.Update(this.dsMSSQL.Customs);
            if (cnt < 1)
            {
                FCOMMON.Util.MsgI("변경된 내역이 없습니다.");
            }
            else FCOMMON.Util.MsgI(string.Format("{0} 건의 자료가 업데이트 되었습니다.",cnt));

        }

        private void toolStripButton1_Click(object sender, EventArgs e)
        {
            refreshData();
        }

        private void autoResizeColummsToolStripMenuItem_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);
        }

        private void btFind_Click(object sender, EventArgs e)
        {
            var search = tbFind.Text.Trim();
            search = search.Replace("*", "x");
            if (tbFind.Text != search) tbFind.Text = search;
            try
            {
                if (search.isEmpty())
                {
                    this.bs.Filter = "";
                    tbFind.BackColor = Color.White;
                }
                else
                {
                    search = search.Replace("'", "''");
                    string filter = "stafftel like ? or grp like ?  or name2 like ? or name like ? or owner like ? or address like ? or tel like ? or email like ? or memo like ?  or staff like ?";
                    filter = filter.Replace("?", "'%" + search + "%'");
                    this.bs.Filter = filter;
                    tbFind.BackColor = Color.Lime;
                }
                tbFind.SelectAll();
                tbFind.Focus();
            }
            catch (Exception ex)
            {
                this.tbFind.BackColor = Color.Tomato;
                FCOMMON.Util.MsgE(ex.Message);
            }
        }

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

        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);
        }

        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)
        {
            FCOMMON.Util.FPColsizeSave(this.fpSpread1, fn_fpcolsize1);
        }

        private void loadToolStripMenuItem_Click(object sender, EventArgs e)
        {
            FCOMMON.Util.FPColSizeLoad(ref this.fpSpread1, fn_fpcolsize1);
        }

        private void bindingNavigatorAddNewItem_Click(object sender, EventArgs e)
        {

        }

        private void bindingNavigatorDeleteItem_Click(object sender, EventArgs e)
        {
            var drv = this.bs.Current as DataRowView;
            if (drv == null) return;
            var dr = drv.Row as dsMSSQL.CustomsRow;
            if (dr.RowState == DataRowState.Added || dr.RowState == DataRowState.Detached || dr.RowState == DataRowState.Deleted) this.bs.RemoveCurrent();

            var taq = new DSQueryTableAdapters.QueriesTableAdapter();
            var cnt = (int)taq.CheckSupplyUsage(dr.idx);
            if(cnt > 0)
            {
                FCOMMON.Util.MsgE("해당 거래처가 연결된 자료가 있어 삭제할 수 없습니다.\n\n" +
                    "개발자에 문의 하세요");
                return;
            }
            var dlg =FCOMMON.Util.MsgQ("다음 거래처를 삭제하시겠습니까?\n"+ 
                dr.name + "\n" + dr.staff);
            if(dlg == System.Windows.Forms.DialogResult.Yes)
            {
                this.bs.RemoveCurrent();
            }
        }

        private int selcectIDX = -1;
        private void bs_CurrentChanged(object sender, EventArgs e)
        {
            //선택되면 staff 를 연결해준다.
            var drv = this.bs.Current as DataRowView;
            if(drv == null)
            {
                selcectIDX = -1;
                this.bsStaff.Filter = "cid=-1";
                bindingNavigator1.Enabled = false;
            }
            else
            {
                selcectIDX = int.Parse(drv["idx"].ToString());
                this.bsStaff.Filter = "cid=" + selcectIDX;
                bindingNavigator1.Enabled = true;
            }
        }

        private void toolStripMenuItem2_Click(object sender, EventArgs e)
        {
            FCOMMON.Util.FPColSizeAuto(this.fpSpread2);
        }

        private void toolStripMenuItem3_Click(object sender, EventArgs e)
        {
            FCOMMON.Util.FPCollSizeReset(this.fpSpread2);
        }

        private void toolStripMenuItem4_Click(object sender, EventArgs e)
        {
            FCOMMON.Util.FPColsizeSave(this.fpSpread2, fn_fpcolsize2);
        }

        private void toolStripMenuItem5_Click(object sender, EventArgs e)
        {
            FCOMMON.Util.FPColsizeSave(this.fpSpread2, fn_fpcolsize2);
        }

        private void toolStripButton2_Click(object sender, EventArgs e)
        {
            refreshDataS();
        }

        private void toolStripButton3_Click(object sender, EventArgs e)
        {
            this.Validate();
            this.bsStaff.EndEdit();
            int cnt = this.taStaff.Update(this.dsMSSQL.Staff);
            if (cnt < 1)
            {
                FCOMMON.Util.MsgI("변경된 내역이 없습니다.");
            }
            else FCOMMON.Util.MsgI(string.Format("{0} 건의 자료가 업데이트 되었습니다.", cnt));
        }
    }
}
