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.Globalization;

namespace GymSystem
{
    public partial class FrmSavabeq : Form
    {

        DbGymEntities2 database = new DbGymEntities2();

        public FrmSavabeq()
        {
            InitializeComponent();
        }

        private void FrmSavabeq_Load(object sender, EventArgs e)
        {

            GrdList.DataSource = database.TblTarakoneshes.ToList();
            CmbSearch.SelectedIndex = 0;




        }


        private void searching()
        {


            
            if (TxtSearchDate1.TextLength != 10 || TxtSearchDate2.TextLength != 10 )
            {
                var query = (from tarakonesh in database.TblTarakoneshes where tarakonesh.CustomerID.Contains(TxtSearchCode.Text) && tarakonesh.CustomerName.Contains(TxtSearchName.Text) select tarakonesh);
                GrdList.DataSource = query.ToList();
            }
            else
            {

                var query = (from tarakonesh in database.TblTarakoneshes where tarakonesh.CustomerID.Contains(TxtSearchCode.Text) && tarakonesh.CustomerName.Contains(TxtSearchName.Text) && string.Compare(tarakonesh.TarakoneshDate, TxtSearchDate1.Text) >= 0 && string.Compare(tarakonesh.TarakoneshDate, TxtSearchDate2.Text) <= 0 select tarakonesh);
                GrdList.DataSource = query.ToList();
            }



        }






        private void TxtSearchCode_TextChanged(object sender, EventArgs e)
        {
            Class1.ChangeNum(TxtSearchCode);
            searching();
        }

        private void TxtSearchName_TextChanged(object sender, EventArgs e)
        {
            searching();
        }

        private void TxtSearchCode_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (Convert.ToInt32(e.KeyChar) >= 48 && Convert.ToInt32(e.KeyChar) <= 57 || Convert.ToInt32(e.KeyChar) >= 1776 && Convert.ToInt32(e.KeyChar) <= 1785 || Convert.ToInt32(e.KeyChar) == 8)
            {

            }
            else
            {
                e.KeyChar = Convert.ToChar(Keys.None);
            }
        }


        public DataTable ConvertToDataTable<T>(IList<T> data)
        {
            PropertyDescriptorCollection properties =
               TypeDescriptor.GetProperties(typeof(T));
            DataTable table = new DataTable();
            foreach (PropertyDescriptor prop in properties)
                table.Columns.Add(prop.Name, Nullable.GetUnderlyingType(prop.PropertyType) ?? prop.PropertyType);
            foreach (T item in data)
            {
                DataRow row = table.NewRow();
                foreach (PropertyDescriptor prop in properties)
                    row[prop.Name] = prop.GetValue(item) ?? DBNull.Value;
                table.Rows.Add(row);
            }
            return table;

        }




        private void PicPrint_Click(object sender, EventArgs e)
        {
            

            if (TxtSearchDate1.TextLength != 10 || TxtSearchDate2.TextLength != 10)
            {
                var query = (from tarakonesh in database.TblTarakoneshes where tarakonesh.CustomerID.Contains(TxtSearchCode.Text) && tarakonesh.CustomerName.Contains(TxtSearchName.Text) select tarakonesh);
                DataTable dt = new DataTable();
                dt = ConvertToDataTable(query.ToList());

                stiReport1.RegData(dt);
                stiReport1.Load("stiReportTr.mrt");
                stiReport1.Show();
            }
            else
            {
                var query = (from tarakonesh in database.TblTarakoneshes where tarakonesh.CustomerID.Contains(TxtSearchCode.Text) && tarakonesh.CustomerName.Contains(TxtSearchName.Text) && string.Compare(tarakonesh.TarakoneshDate, TxtSearchDate1.Text) >= 0 && string.Compare(tarakonesh.TarakoneshDate, TxtSearchDate2.Text) <= 0 select tarakonesh);
                DataTable dt = new DataTable();
                dt = ConvertToDataTable(query.ToList());

                stiReport1.RegData(dt);
                stiReport1.Load("stiReportTr.mrt");
                stiReport1.Show();
            }
            



        }

        private void groupPanel1_Click(object sender, EventArgs e)
        {

        }

        private void TxtSearchDate1_TextChanged(object sender, EventArgs e)
        {
            Class1.ChangeNum(TxtSearchDate1);
            searching();
        }

        private void TxtSearchDate2_TextChanged(object sender, EventArgs e)
        {
            Class1.ChangeNum(TxtSearchDate1);
            searching();
        }

        private void pictureBox1_Click(object sender, EventArgs e)
        {
            DateTime today = DateTime.Now;
            PersianCalendar pc = new PersianCalendar();

            TxtSearchDate1.Text = pc.GetYear(today).ToString("0000") + "/" + pc.GetMonth(today).ToString("00") + "/" + pc.GetDayOfMonth(today).ToString("00");
           
        }

        private void pictureBox2_Click(object sender, EventArgs e)
        {
            DateTime today = DateTime.Now;
            PersianCalendar pc = new PersianCalendar();
            
            TxtSearchDate2.Text = pc.GetYear(today).ToString("0000") + "/" + pc.GetMonth(today).ToString("00") + "/" + pc.GetDayOfMonth(today).ToString("00");
           
        }

        private void CmbSearch_SelectedIndexChanged(object sender, EventArgs e)
        {
            DateTime today = DateTime.Now;
            PersianCalendar pc = new PersianCalendar();

            
            
            if (CmbSearch.SelectedIndex == 0)
            {
                TxtSearchDate1.Text = "";
                TxtSearchDate2.Text = "";
            }
            else if (CmbSearch.SelectedIndex == 1)
            {
                TxtSearchDate2.Text = pc.GetYear(today).ToString("0000") + "/" + pc.GetMonth(today).ToString("00") + "/" + pc.GetDayOfMonth(today).ToString("00");
                today = today.AddDays(-7);
                TxtSearchDate1.Text = pc.GetYear(today).ToString("0000") + "/" + pc.GetMonth(today).ToString("00") + "/" + pc.GetDayOfMonth(today).ToString("00");
            }
            else if (CmbSearch.SelectedIndex == 2)
            {
                TxtSearchDate2.Text = pc.GetYear(today).ToString("0000") + "/" + pc.GetMonth(today).ToString("00") + "/" + pc.GetDayOfMonth(today).ToString("00");
                today = today.AddDays(-30);
                TxtSearchDate1.Text = pc.GetYear(today).ToString("0000") + "/" + pc.GetMonth(today).ToString("00") + "/" + pc.GetDayOfMonth(today).ToString("00");
            }
        }
    }
}
