using System;
using System.Collections.Generic;
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;

using System.Data.SQLite;
using System.IO;
using System.Data;
using System.Security;
using System.Security.Cryptography;

namespace AIRS
{
    /// <summary>
    /// LoginWindow.xaml 的交互逻辑
    /// </summary>
    public partial class LoginWindow : 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 string loginname = "";//登录名
        public string password = "";//密码
        public string entryclerk = "";//录入员
        public LoginWindow()
        {
            InitializeComponent();
        }
        public delegate void DeleTest();
        public static event DeleTest TestEvent;

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

        private void Window_Loaded(object sender, RoutedEventArgs e)
        {
            connstr.DataSource = path + "\\datas.db";
            conn.ConnectionString = connstr.ToString();
            conn.Open();

            cbbLoginname.Focus();
        }

        /// <summary>
        /// 根据账号查询用户信息
        /// </summary>
        /// <param name="loginname"></param>
        private void GetUserByloginname(string strloginname)
        {
            System.Data.SQLite.SQLiteDataAdapter da = new SQLiteDataAdapter("select * from t_username where username='"+ strloginname + "'", conn);
            DataTable ds = new DataTable();
            da.Fill(ds);
            if (ds.Rows.Count>0)
            {
                loginname = ds.Rows[0][0].ToString();
                password = ds.Rows[0][1].ToString();
            }
        }

        private void BtnLogin_Click(object sender, RoutedEventArgs e)
        {
            //this.DialogResult = true;
            //LoginWindow.TestEvent();
            //return;
            //if (cbbLoginname.Text.Trim().Length == 0)
            //{
            //    MessageBox.Show("请填写账号！");
            //    cbbLoginname.Focus();
            //    return;
            //}
            //if (cbbpassword.Password.Trim().Length == 0)
            //{
            //    MessageBox.Show("请填写密码！");
            //    cbbpassword.Focus();
            //    return;
            //}
            //if (cbbcompound.Text.Trim().Length == 0)
            //{
            //    MessageBox.Show("请填写录入员！");
            //    cbbcompound.Focus();
            //    return;
            //}
            GetUserByloginname(cbbLoginname.Text.Trim());

            //if (!loginname.Equals(cbbLoginname.Text.Trim()))
            //{
            //    MessageBox.Show("账号不存在！");
            //    cbbLoginname.Focus();
            //    return;
            //}
            ////if (!password.Equals(cbbpassword.Password.Trim()))
            //{
            //    MessageBox.Show("密码错误！");
            //    cbbpassword.Focus();
            //    return;
            //}
            entryclerk = cbbcompound.Text.Trim();
            this.DialogResult = true;
            LoginWindow.TestEvent();
        }
    }
}
