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 Project._Common
{
    public partial class fUserList :  FCOMMON.fBase
    {
        public fUserList()
        {
            InitializeComponent();
            this.dsMSSQL.Users.TableNewRow += Users_TableNewRow;
            this.FormClosed += __Closed;
        }

        void __Closed(object sender, FormClosedEventArgs e)
        {
            var form = this as Form;
            Pub.SetFormStatus(ref form, this.Name, false);
        }

        void Users_TableNewRow(object sender, DataTableNewRowEventArgs e)
        {
            e.Row["wuid"] = FCOMMON.info.Login.no;
            e.Row["wdate"] = DateTime.Now;
            e.Row["password"] = Pub.MakePasswordEnc("0");
            e.Row["level"] = 1;            
        }

        private void __Load(object sender, EventArgs e)
        {
            var form = this as Form;
            Pub.SetFormStatus(ref form, this.Name , true);
            this.Show();            
            Application.DoEvents();

            if(FCOMMON.info.Login.level > 9)
            {
                this.btDevel.Visible = true;
                this.dv1.EditMode = DataGridViewEditMode.EditOnKeystrokeOrF2;                
            }
            else
            {
                this.dv1.EditMode = DataGridViewEditMode.EditProgrammatically;
                this.dv1.SelectionMode = DataGridViewSelectionMode.FullRowSelect;
                this.btDevel.Visible = false;
                this.textBox1.Enabled = false;
                this.dvc_id.Visible = false;
                this.dvc_level.Visible = false;
                this.usersBindingNavigatorSaveItem.Enabled = false;
                this.bindingNavigatorAddNewItem.Enabled = false;
                this.bindingNavigatorDeleteItem.Enabled = false;
                
            }
            
            //부서목록 업데이트
            this.cmbdept.Items.Clear();
            var deptList = FCOMMON.DBM.getGroupList("dept", "users");
            foreach (var item in deptList)
                this.cmbdept.Items.Add(item);

            this.cmbdept.Text = FCOMMON.info.Login.dept;
            btRef.PerformClick();
          
        }
        private void usersBindingNavigatorSaveItem_Click(object sender, EventArgs e)
        {
            this.Validate();
            this.bs.EndEdit();
            this.tam.UpdateAll(this.dsMSSQL);

        }

        private void iDChangeToolStripMenuItem_Click(object sender, EventArgs e)
        {
            var drv = this.bs.Current as DataRowView;
            if (drv == null) return;
            var dr = drv.Row as dsMSSQL.UsersRow;
          
            var f = new fNewID(dr.id);
            if (f.ShowDialog() == System.Windows.Forms.DialogResult.OK)
            {
                var newid = f.tbIDN.Text.Trim();
                if(this.dsMSSQL.Users.Select("id='" + newid + "'").Length != 0)
                {
                    Util.MsgE("Exist id - error");
                    return;
                }
                else
                {
                    this.Validate();
                    this.bs.EndEdit();
                    dr.id = newid;
                    dr.EndEdit();
                    this.bs.EndEdit();
                }
            }
        }

        private void passwordChangeToolStripMenuItem_Click(object sender, EventArgs e)
        {
            var drv = this.bs.Current as DataRowView;
            if (drv == null) return;
            var dr = drv.Row as dsMSSQL.UsersRow;
            var currentPassword = dr.password;

            _Common.fNewPassword f = new fNewPassword(dr.id);
            if (f.ShowDialog() == System.Windows.Forms.DialogResult.OK)
            {
                var encpassO = Pub.MakePasswordEnc(f.tbPassO.Text);
                var encpassN = Pub.MakePasswordEnc(f.tbPass1.Text);
                if (currentPassword != encpassO)
                {
                    Util.MsgE("Old password error");
                    return;
                }
                else
                {
                    this.Validate();
                    this.bs.EndEdit();
                    dr.password = encpassN;
                    dr.EndEdit();
                    this.bs.EndEdit();
                }
            }
        }

        private void mailToolStripMenuItem_Click(object sender, EventArgs e)
        {
            var drv = this.bs.Current as DataRowView;
            if (drv == null) return;
            var dr = drv.Row as dsMSSQL.UsersRow;
            if (dr.email.isEmpty()) return;
            string cmd = "mailto:" + dr.email;
            Util.RunExplorer(cmd);
        }

        private void 한글이름분리ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            
            foreach(dsMSSQL.UsersRow dr in  this.dsMSSQL.Users.Rows)
            {
                var name = dr.name;
                var newname = name.Split(' ')[0];
                dr.name = newname;
                dr.EndEdit();
            }
        }

        private void btRef_Click(object sender, EventArgs e)
        {
            //read data
            if (tbProcess.Text.Trim() == "") tbProcess.Text = "%";
            this.dsMSSQL.Users.Clear();
            this.ta.Fill(this.dsMSSQL.Users, this.cmbdept.Text, tbProcess.Text.Trim());
            this.dsMSSQL.Users.AcceptChanges();
        }

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

    }
}
