using System;
using System.Collections.Generic;
using System.Data;
using System.Data.SQLite;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;

namespace AIRS
{
    /// <summary>
    /// UpUser.xaml 的交互逻辑
    /// </summary>
    public partial class UpUser : Window
    {
        public UpUser()
        {
            InitializeComponent();
        }

        /// <summary>
        /// //需要修改的username  为空时是添加
        /// </summary>
        public string username { get; set; }
        /// <summary>
        /// 数据库连接对象
        /// </summary>
        public SQLiteConnection conn { get; set; }
        /// <summary>
        /// 退出
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void BtnExit_Click(object sender, RoutedEventArgs e)
        {
            try
            {
                conn.Close();
            }
            catch
            {
            }
            this.DialogResult = false;
        }

        /// <summary>
        /// 修改/添加
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void BtnUp_Click(object sender, RoutedEventArgs e)
        {
            if (this.cbbLoginname.Text.Trim().Length==0)
            {
                MessageBox.Show("请填写检察员！");
                return;
            }
            if (IsExis())
            {
                MessageBox.Show("输入检察员已存在！");
                return;
            }
            if (this.BtnLogin.Content.Equals("添加"))
            {
                //添加
                if (Add())
                {
                    this.DialogResult = true;
                }
                else
                {
                    MessageBox.Show("添加失败！");
                }
            }
            else
            {
                //修改
                if (Up())
                {
                    this.DialogResult = true;
                }
                else
                {
                    MessageBox.Show("修改失败！");
                }
            }
        }

        /// <summary>
        /// 加载成功后执行
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void Window_Loaded(object sender, RoutedEventArgs e)
        {
        }

        /// <summary>
        /// 是否存在相同账号
        /// </summary>
        /// <returns></returns>
        private bool IsExis()
        {
            bool success = false;
            try
            {
                string strSql = "select * from t_username where username='" + this.cbbLoginname.Text.Trim() + "'";
                SQLiteDataAdapter da = new SQLiteDataAdapter(strSql, conn);
                DataTable dt = new DataTable();
                da.Fill(dt);
                if (dt.Rows.Count > 0)
                {
                    success = true;
                }
            }
            catch (Exception ex)
            {

            }
            return success;
        }
        /// <summary>
        /// 添加用户
        /// </summary>
        /// <returns></returns>
        private bool Add()
        {
            bool success = false;
            try
            {                
                SQLiteCommand cmd = conn.CreateCommand();
                cmd.CommandText = "insert into t_username values (@username,'123456')";
                cmd.Parameters.Add(new SQLiteParameter("@username"));
                cmd.Parameters["@username"].Value = this.cbbLoginname.Text.Trim();
                if (cmd.ExecuteNonQuery() > 0)
                {
                    success = true;
                }
            }
            catch (Exception ex)
            {
            }
            return success;
        }
        /// <summary>
        /// 修改
        /// </summary>
        private bool Up()
        {
            bool success = false;
            try
            {

                SQLiteCommand cmd = conn.CreateCommand();
                cmd.CommandText = "UPDATE t_username SET username=@username1 where username=@username";
                cmd.Parameters.Add(new SQLiteParameter("@username"));
                cmd.Parameters.Add(new SQLiteParameter("@username1"));
                cmd.Parameters["@username"].Value = username;
                cmd.Parameters["@username1"].Value = this.cbbLoginname.Text;
                if (cmd.ExecuteNonQuery() > 0)
                {
                    success = true;
                }
            }
            catch (Exception ex)
            {
            }
            return success;            
        }
    }
}
