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;
using HRManagerSystem.Bll;

namespace HRManagerSystem
{
    public partial class UserManagerForm : Form
    {
        public UserManagerForm()
        {
            InitializeComponent();
        }
        DataDataContext ddc = new DataDataContext();
        //User user = new User();
        /// <summary>
        /// 添加用户
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void bt_add_Click(object sender, EventArgs e)
        {
            try
            {
                if (txt_username.Text != "" && txt_pwd.Text != "" && txt_checkpwd.Text != "")
                {
                    if (ddc.T_Users.Where(t => t.User_Name == txt_username.Text ).Count() > 0)
                    {
                                           
                        MessageBox.Show("此用户名已存在！");
                    }
                    else
                    {
                        if (txt_pwd.Text == txt_checkpwd.Text)
                        {
                            T_Users user = new T_Users();
                            user.User_Name = txt_username.Text;
                            user.User_pwd = txt_checkpwd.Text;
                            ddc.T_Users.InsertOnSubmit(user);
                            ddc.SubmitChanges();
                            MessageBox.Show("添加成功！");
                            txt_pwd.Clear();
                            txt_username.Clear();
                            txt_checkpwd.Clear();
                        }
                        else
                        {
                            MessageBox.Show("两次密码不一致！");
                            txt_username.Clear();
                            txt_username.Clear();
                        }
                    }
                }
                else
                {
                    MessageBox.Show("请输入相关信息");
                }
            }
            catch
            {
                MessageBox.Show("输入信息有误！");
            }
           
        }
        /// <summary>
        /// 删除用户
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void bt_delete_Click(object sender, EventArgs e)
        {
            try
            {
                txt_pwd.Clear();
                txt_checkpwd.Clear();                ;
                if (txt_username.Text != "")
                {
                    if (ddc.T_Users.Where(t => t.User_Name == txt_username.Text).Count() > 0)
                    {
                        var dd = ddc.T_Users.Single(m => m.User_Name == txt_username.Text);
                        ddc.T_Users.DeleteOnSubmit(dd);
                        MessageBox.Show("删除成功！");
                        txt_username.Clear();                        
                    }
                    else
                    {
                        MessageBox.Show("此用户不存在！");
                        txt_username.Clear(); 
                    }
                }
                else
                {
                    MessageBox.Show("请输入用户名！");
                }
            }
            catch
            { 
               
            }
          
          
        }
    }
}
