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 fAddNewUser : Form // FCOMMON.fBase
    {
        string p_dept = string.Empty;
        string p_id = string.Empty;

        public fAddNewUser(string dept, string id)
        {
            InitializeComponent();
            p_dept = dept;
            p_id = id;

            this.tbDept.Text = dept;
            this.StartPosition = FormStartPosition.CenterScreen;

            //자료를 불러와서 먼저 불러온다
            if (id.isEmpty() == false)
            {
                tbId.Enabled = false;
                button1.Enabled = false;
                textBox1.Enabled = false;


                var db = new EEEntitiesMain();
                var db_user = db.Users.Where(t => t.id == id).FirstOrDefault();
                var db_guser = db.EETGW_GroupUser.Where(t => t.gcode == FCOMMON.info.Login.gcode && t.uid == id).FirstOrDefault();
                if (db_user != null)
                {
                    tbNameK.Text = db_user.name;
                    tbNameE.Text = db_user.nameE;
                    tbDept.Text = db_user.dept;
                    tbEmail.Text = db_user.email;
                    tbTel.Text = db_user.tel;
                    tbHp.Text = db_user.hp;
                    tbDateIn.Text = db_user.indate;
                    tbDateO.Text = db_user.outdate;
                    tbGrade.Text = db_user.grade;
                    tbMemo.Text = db_user.memo;
                }
                if (db_guser != null)
                {
                    tbProcess.Text = db_guser.Process;
                    tbState.Text = db_guser.state;
                    //this.chkJobReport.Checked = (db_guser.useJobReport == null ? false : (bool)db_guser.useJobReport);
                    this.chkUserSt.Checked = (db_guser.useUserState == null ? false : (bool)db_guser.useUserState);
                }
                this.Text = "사용자 정보 변경";
            }
            else this.Text = "사용자 정보 추가";

        }


        private void fUserInfo_Load(object sender, EventArgs e)
        {

        }
        private void button1_Click(object sender, EventArgs e)
        {
            //입력한 id확인해서 잇으면 해당 자료를 표시해준다.
            var id = this.tbId.Text.Trim();
            if (id.isEmpty())
            {
                FCOMMON.Util.MsgE("id를 입력하세요");
                this.tbId.SelectAll();
                this.tbId.Focus();
                return;
            }

            var db = new EEEntitiesMain();
            var dr_user = db.Users.Where(t => t.id == id).FirstOrDefault();
            if (dr_user == null)
            {
                FCOMMON.Util.MsgE("등록된 자료가 없습니다");
                tbNameK.Focus();
            }
            else
            {
                tbNameK.Text = dr_user.name;
                tbNameE.Text = dr_user.nameE;
                if (tbDept.Text.isEmpty()) tbDept.Text = dr_user.dept;
                tbEmail.Text = dr_user.email;
                tbTel.Text = dr_user.tel;
                tbHp.Text = dr_user.hp;
                tbDateIn.Text = dr_user.indate;
                tbDateO.Text = dr_user.outdate;
                tbGrade.Text = dr_user.grade;
                tbMemo.Text = dr_user.memo;
                tbProcess.Text = string.Empty;

                //부서가잇다면 해당 부서에서 해당 사용자의 공정을 찾는다
                if (tbDept.Text.isEmpty() == false)
                {
                    var drGrp = db.vGroupUser.Where(t => t.id == id).FirstOrDefault();
                    if (drGrp != null)
                    {
                        tbProcess.Text = drGrp.processs;
                    }
                }
            }
        }

        private void button2_Click(object sender, EventArgs e)
        {
            this.Validate();
            var db = new EEEntitiesMain();

            //해당 그룹에 데이터를 추가한다.
            var drDept = db.UserGroup.Where(t => t.dept == tbDept.Text).FirstOrDefault();
            if (drDept == null)
            {
                FCOMMON.Util.MsgE("해당 부서정보가 존재하지 않습니다\n\n부서를 신규로 추가할 수는 없습니다\n관리자에 문의하세요");
                return;
            }

            //해당 그룹에 해당 사용자가 존재하는지 확이한다.
            var gcode = drDept.gcode;
            var uid = (p_id.isEmpty() ?  tbId.Text.Trim() : p_id);
            var drGuser = db.EETGW_GroupUser.Where(t => t.gcode == gcode && t.uid == uid).FirstOrDefault();
            if (drGuser != null)
            {
                drGuser.Process = this.tbProcess.Text.Trim();
                drGuser.state = this.tbState.Text.Trim();
                drGuser.useUserState = chkUserSt.Checked;
               //drGuser.useJobReport = chkJobReport.Checked;
            }
            else
            {
                //불러온경우
                if (tbId.Enabled == false)
                {
                    FCOMMON.Util.MsgE("사용자 ID정보가 없어 진행할 수 없습니다");
                    return;
                }

                drGuser = new EETGW_GroupUser();
                drGuser.wuid = FCOMMON.info.Login.no;
                drGuser.wdate = DateTime.Now;
                drGuser.gcode = gcode;
                drGuser.level = 1;
                drGuser.uid = this.tbId.Text.Trim();
                drGuser.state = this.tbState.Text.Trim();
                drGuser.Process = this.tbProcess.Text.Trim();                
                //drGuser.useJobReport = this.chkJobReport.Checked;
                drGuser.useUserState = this.chkUserSt.Checked;
                db.EETGW_GroupUser.Add(drGuser);
            }



            //사용자 목록에 없다면 추가한다
            var drUser = db.Users.SingleOrDefault(t => t.id == uid);
            if (drUser == null)
            {
                drUser = new Users();
                drUser.wuid = FCOMMON.info.Login.no;
                drUser.wdate = DateTime.Now;
                drUser.gcode = gcode;
                drUser.level = 1;
                drUser.id = tbId.Text.Trim();
                drUser.password = "B6589FC6AB0DC82CF12099D1C2D40AB994E8410C"; //기본값0

                drUser.name = tbNameK.Text.Trim();
                drUser.nameE = tbNameE.Text.Trim();
                drUser.dept = this.tbDept.Text.Trim();
                drUser.email = tbEmail.Text;
                drUser.tel = tbTel.Text;
                drUser.hp = tbHp.Text;
                drUser.indate = tbDateIn.Text;
                drUser.outdate = tbDateO.Text;
                drUser.memo = tbMemo.Text;
                drUser.processs = tbProcess.Text;
                db.Users.Add(drUser);
            }

            else
            {
                if (drUser.password.isEmpty()) drUser.password = "B6589FC6AB0DC82CF12099D1C2D40AB994E8410C";
                drUser.name = tbNameK.Text.Trim();
                drUser.nameE = tbNameE.Text.Trim();
                drUser.dept = this.tbDept.Text.Trim();
                drUser.email = tbEmail.Text;
                drUser.tel = tbTel.Text;
                drUser.hp = tbHp.Text;
                drUser.indate = tbDateIn.Text;
                drUser.outdate = tbDateO.Text;
                drUser.memo = tbMemo.Text;
                drUser.processs = tbProcess.Text;
            }

            db.SaveChanges();
            this.DialogResult = DialogResult.OK;

        }

        private void chkUserSt_CheckedChanged(object sender, EventArgs e)
        {
     
        }

        private void chkJobReport_CheckedChanged(object sender, EventArgs e)
        {
                    }
    }
}
