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 AddDepartForm : Form
    {
        public AddDepartForm()
        {
            InitializeComponent();
        }

        private void textBox1_TextChanged(object sender, EventArgs e)
        {

        }

        private void toolStripButton1_Click(object sender, EventArgs e)
        {
            txt_name.Clear();//清空文本内容
        }
        DataDataContext ddt = new DataDataContext();
        private void toolStripButton2_Click(object sender, EventArgs e)
        {
            T_Department depart = new T_Department();
            if (txt_name.Text != "")
            {
                depart.Depart_Name = txt_name.Text;
                var result = ddt.T_Department.Where(name => name.Depart_Name.ToString().Trim() == txt_name.Text.ToString().Trim());
                if (result.Count() == 0)
                {
                    depart.Parent_ID = Convert.ToInt32(combo_fname.SelectedValue);
                    ddt.T_Department.InsertOnSubmit(depart);
                    ddt.SubmitChanges();
                    MessageBox.Show("添加成功！");
                    txt_name.Clear();

                }
                else
                {
                    MessageBox.Show("你添加的部门已存在，请重新输入！");
                }
            }
            else             
            {
                MessageBox.Show("请输入部门名称！");
            }
            Department dpt = new Department();
            dataGridView1.DataSource = dpt.DepartName();
        }
        /// <summary>
        /// 退出
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void toolStripButton3_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void AddDepartForm_Load(object sender, EventArgs e)
        {
            combo_fname.DataSource = ddt.T_Department;
            combo_fname.DisplayMember = "depart_name";
            combo_fname.ValueMember = "depart_id";
            Department dpt = new Department();
            dataGridView1.DataSource = dpt.DepartName();

        }
    }
}
