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 : FCOMMON.fBase
    {
        public fUserInfo()
        {
            InitializeComponent();
        }


        private void fUserInfo_Load(object sender, EventArgs e)
        {
            // TODO: 이 코드는 데이터를 'dsMSSQL.Users' 테이블에 로드합니다. 필요한 경우 이 코드를 이동하거나 제거할 수 있습니다.
            this.ta.FillID(this.dsMSSQL.Users,FCOMMON.info.Login.no);
            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 + "'";
        }

        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();
                    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();
            this.bs.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 Pub.log.AddI("견적서폴더 업뎅트 : " + tbKJPath.Text);
                    }
                }

                var cnt = this.tam.UpdateAll(this.dsMSSQL);
                Util.MsgI(string.Format("{0}건의 자료가 업데이트 됨", cnt));
                DialogResult = System.Windows.Forms.DialogResult.OK;
            }
            catch (Exception ex)
            {
                Util.MsgE(ex.Message);
            }


        }

    }
}
