using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Data.SqlClient;

namespace T_FaceRecognizer
{
    public partial class FormVisiting : Form
    {
        public FormVisiting(int selectedUserID,string UserName)
        {
            InitializeComponent();
            SelectedUserID = selectedUserID;
            LblUserName.Text = UserName;
        }
        #region <Переменные>
        int SelectedUserID = -1;

        SqlConnection SCon = new SqlConnection("Data Source=213.155.192.79,3002;Initial Catalog=FaceTrackApp;Persist Security Info=True;User ID=u20teresh;Password=bfg2");
        #endregion
        private void FormVisiting_Load(object sender, EventArgs e)
        {
            GetVisitingForSelectedUser(SelectedUserID);
        }
        /// <summary>
        /// получение списка посещений для выбранного пользователя
        /// </summary>
        /// <param name="UserID">Идентификатор пользователя</param>
        private void GetVisitingForSelectedUser(int UserID)
        {
            DgbVisitingForSelectedUser.Rows.Clear();
            SCon.Open();
            string QueryOnGetVisitsForSelectedUser = $@"set dateformat dmy
select UsersID,FORMAT(cast([Date] as date),N'dd\.MM\.yyyy') as VisitDate,FORMAT(cast(TimeEntrance as time), N'hh\:mm\:ss') as timeIn,FORMAT(cast(TimeExit as time),N'hh\:mm\:ss')as timeEx
from Visits
where UsersID = '{UserID}' 
";
            if(CbxFilterDate.Checked)
            {
                QueryOnGetVisitsForSelectedUser += $@"  and [Date] = '{DtpVisitDate.Value.Date}'";
            }
            QueryOnGetVisitsForSelectedUser += "  order by VisitDate";

            SqlCommand Cmd = new SqlCommand(QueryOnGetVisitsForSelectedUser, SCon);
            SqlDataReader Res = Cmd.ExecuteReader();
            if(!Res.HasRows)
            {
                MessageBox.Show("При выбранных параметрых сортировки посещения отсутствуют!", "FaceTrack", MessageBoxButtons.OK, MessageBoxIcon.Error);
                SCon.Close();
                return;
            }

            while (Res.Read())
            {
                DgbVisitingForSelectedUser.Rows.Add(Res["VisitDate"].ToString(), Res["timeIn"].ToString(), Res["timeEx"].ToString());
            }
            SCon.Close();
        }

        private void CbxFilterDate_CheckedChanged(object sender, EventArgs e)
        {
            GetVisitingForSelectedUser(SelectedUserID);
        }

        private void DtpVisitDate_ValueChanged(object sender, EventArgs e)
        {
            GetVisitingForSelectedUser(SelectedUserID);
        }
    }
}
