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 Project._Common
{
    public partial class fUserInfo : fBase
    {
        public fUserInfo()
        {
            InitializeComponent();
        }

        dsMSSQL.UsersRow dr = null;

        private void fUserInfo_Load(object sender, EventArgs e)
        {
            EnsureVisibleAndUsableSize();
            this.ta.FillID(this.dsMSSQL.Users, FCOMMON.info.Login.no);
            dr = this.dsMSSQL.Users.Rows[0] as dsMSSQL.UsersRow;
            tbKJPath.Text = FCOMMON.DBM.getPathKJ(FCOMMON.info.Login.dept);
            tbKJPath.Tag = tbKJPath.Text;
            if (FCOMMON.info.Login.level > 4) tbKJPath.Enabled = true;
            else tbKJPath.Enabled = false;
            //this.bs.Filter = "id='" + FCOMMON.info.Login.no + "'";

            //공정은 usergroup에서 찾는다.
            var db = new dsMSSQLTableAdapters.EETGW_GroupUserTableAdapter();// EEEntitiesMain();
            var drGroupUser = db.GetbyID(FCOMMON.info.Login.gcode, FCOMMON.info.Login.no).FirstOrDefault();//).EETGW_GroupUser.Where(t => t.gcode == FCOMMON.info.Login.gcode && t.uid == FCOMMON.info.Login.no).FirstOrDefault();
            if (drGroupUser != null)
                this.tbProcess.Text = drGroupUser.Process;
            else this.tbProcess.Text = string.Empty;

            this.tbDept.Text = FCOMMON.info.Login.dept;
        }

        private void button1_Click(object sender, EventArgs e)
        {
            _Common.fNewPassword f = new fNewPassword(FCOMMON.info.Login.no);
            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();
                    //var drv = bs.Current as DataRowView;
                    //drv["password"] = encpassN;
                    //this.bs.EndEdit();
                }
            }
        }

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

        private void button2_Click(object sender, EventArgs e)
        {
            this.Validate();

            dr.name = tbNameK.Text;
            //dr.dept = tbDept.Text;
            dr.email = tbEmail.Text;
            dr.tel = tbTel.Text;
            dr.hp = tbHp.Text;
            dr.indate = tbDateIn.Text;
            dr.outdate = tbDateO.Text;
            dr.nameE = tbNameE.Text;
            dr.grade = tbGrade.Text;
            dr.memo = tbMemo.Text;
            dr.processs = tbProcess.Text;
            dr.level = short.Parse(tbLevel.Text);
            dr.EndEdit();


            this.bs.EndEdit();


            var db2 = new dsMSSQLTableAdapters.EETGW_GroupUserTableAdapter();// EEEntitiesMain();
            var drUserGr = db2.GetbyID(FCOMMON.info.Login.gcode, FCOMMON.info.Login.no).FirstOrDefault();// .EETGW_GroupUser.Where(t => t.gcode == FCOMMON.info.Login.gcode && t.uid == FCOMMON.info.Login.no).FirstOrDefault();
            if (dr != null)
            {
                drUserGr.Process = this.tbProcess.Text;
                drUserGr.EndEdit();
                db2.Update(drUserGr);//
            }



            //     dr.processs = tbProcess.Text;
            //      dr.EndEdit();

            try
            {
                if (tbKJPath.Enabled)
                {
                    if (tbKJPath.Text.Trim() != tbKJPath.Tag.ToString().Trim())
                    {
                        if (!FCOMMON.DBM.setPathKJ(FCOMMON.info.Login.dept, tbKJPath.Text.Trim()))
                        {
                            FCOMMON.Util.MsgE("견적서 폴더 업데이트 실패");
                        }
                        else FCOMMON.Pub.log.AddI("견적서폴더 업뎅트 : " + tbKJPath.Text);
                    }
                }

                var cnt = this.ta.Update(this.dsMSSQL.Users);
                if (cnt == 1)
                {
                    Util.MsgI(string.Format("{0}건의 자료가 업데이트 됨", cnt));
                    DialogResult = System.Windows.Forms.DialogResult.OK;
                }
                else
                    Util.MsgE(string.Format("{0}건의 자료가 업데이트 됨", cnt));

            }
            catch (Exception ex)
            {
                Util.MsgE(ex.Message);
            }


        }

    }
}
