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 fUserGroupDev : FCOMMON.fBase
    {
        string fn_fpcolsize1 = "";
        public fUserGroupDev()
        {
            InitializeComponent();
            Properties.Settings.Default["gwcs"] = FCOMMON.info.CS;
            fn_fpcolsize1 = FCOMMON.Util.MakeFilePath(FCOMMON.Util.CurrentPath, "formSetting", "fp1_" + this.Name + ".ini");
        }

        private void __Load(object sender, EventArgs e)
        {
            EnsureVisibleAndUsableSize();
            refreshData();
            this.dSUser.UserGroup.TableNewRow += Customs_TableNewRow;
        }

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


        void refreshData()
        {
            try
            {
                this.ta.Fill(this.dSUser.UserGroup);
                dv1.AutoResizeColumns();

            }
            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.dSUser.UserGroup);
            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)
        {
            dv1.AutoResizeColumns();
        }

        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 = "dept 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 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 bindingNavigatorDeleteItem_Click(object sender, EventArgs e)
        {
            var drv = this.bs.Current as DataRowView;
            if (drv == null) return;
            var dr = drv.Row as DSUser.UserGroupRow;
            if (dr.RowState == DataRowState.Added || dr.RowState == DataRowState.Detached || dr.RowState == DataRowState.Deleted) this.bs.RemoveCurrent();

            var dlg = FCOMMON.Util.MsgQ("다음 자료를 삭제하시겠습니까?");
            if (dlg == System.Windows.Forms.DialogResult.Yes)
            {
                this.bs.RemoveCurrent();
            }
        }

        //private int selcectIDX = -1;
        private void bs_CurrentChanged(object sender, EventArgs e)
        {

        }

        private void 권한설정ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            var drv = this.bs.Current as DataRowView;
            if (drv == null) return;
            var dr = drv.Row as DSUser.UserGroupRow;
            var f = new fUserGroupPermission(dr.permission);
            if (f.ShowDialog() == System.Windows.Forms.DialogResult.OK)
            {
                dr.permission = f.permission;
                dr.EndEdit();
            }
        }


    }
}
