using System;
using System.Collections.Generic;
using System.Data.SQLite;
using System.IO;
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>
    /// SetPwdWindow.xaml 的交互逻辑
    /// </summary>
    public partial class SetPwdWindow : Window
    {
        private string path = Directory.GetCurrentDirectory();          //程序工作目录
        System.Data.SQLite.SQLiteConnection conn = new System.Data.SQLite.SQLiteConnection();
        System.Data.SQLite.SQLiteConnectionStringBuilder connstr = new System.Data.SQLite.SQLiteConnectionStringBuilder();

        public SetPwdWindow()
        {
            InitializeComponent();
        }

        public string username = "";//账号
        public string password = "";//密码
        /// <summary>
        /// 确认按钮
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void BtnOK_Click(object sender, RoutedEventArgs e)
        {
            try
            {
                if (lodpassword.Password.Trim().Length==0)
                {
                    MessageBox.Show("请填写原密码");
                    lodpassword.Focus();
                    return;
                }
                if (newpassword.Password.Trim().Length == 0)
                {
                    MessageBox.Show("请填写新密码");
                    newpassword.Focus();
                    return;
                }
                if (confirmpassword.Password.Trim().Length == 0)
                {
                    MessageBox.Show("请填写确认密码");
                    confirmpassword.Focus();
                    return;
                }
                if (!lodpassword.Password.Trim().Equals(password))
                {
                    MessageBox.Show("密码错误！");
                    lodpassword.Focus();
                    return;
                }
                if (!newpassword.Password.Trim().Equals(confirmpassword.Password.Trim()))
                {
                    MessageBox.Show("新密码与确认密码不一致!");
                    //lodpassword.Focus();
                    return;
                }
                connstr.DataSource = path + "\\datas.db";
                conn.ConnectionString = connstr.ToString();
                conn.Open();
                SQLiteCommand cmd = conn.CreateCommand();
                cmd.CommandText = "update t_username set password=@password where username=@username";
                cmd.Parameters.Add(new SQLiteParameter("@password"));
                cmd.Parameters.Add(new SQLiteParameter("@username"));
                cmd.Parameters["@password"].Value = newpassword.Password.Trim();
                cmd.Parameters["@username"].Value = username;
                if (cmd.ExecuteNonQuery() > 0)
                {
                    this.DialogResult = true;
                }
                else
                {
                    MessageBox.Show("修改密码失败！");
                }
            }
            catch (Exception ex)
            {
                
            }
        }

        private void BtnCancle_Click(object sender, RoutedEventArgs e)
        {
            this.DialogResult = false;
        }
    }
}
