using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data.OleDb;
namespace TelBook
{
    public partial class Main : Form
    {
        public Main()
        {
            InitializeComponent();
        }
        OleDbConnection con = new OleDbConnection("Provider=Microsoft.ACE.OLEDB.12.0;Data Source=|DataDirectory|DATA.ACCDB;JET OLEDB:DATABASE PASSWORD=●●●●●●●●●●●●;");

        public void show()
        {
            try
            {
                string d;
                OleDbDataAdapter adap;
                DataSet ds = new DataSet();
                d = "select * from contact";
                adap = new OleDbDataAdapter(d, con);
                adap.Fill(ds, "contact");
                dgv.DataSource = ds.Tables["contact"];
                {
                    OleDbCommand com = new OleDbCommand("select count(*)from contact", con);
                    OleDbCommand com2 = new OleDbCommand("select count(*)from groups",con);
                    label6.Text = Convert.ToString((int)com.ExecuteScalar()) + " نفر";
                    label8.Text = Convert.ToString((int)com2.ExecuteScalar())+" گروه";

                }
            }
            catch (Exception ex)
            {

                MessageBox.Show(ex.Message, "System Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }
        private void Main_Load(object sender, EventArgs e)
        {
            con.Open();
            show();
            dgv.Columns["id"].HeaderText = "کد";
            dgv.Columns["fil1"].HeaderText = "گروه مخاطب";
            dgv.Columns["fil2"].HeaderText = "نام مخاطب";
            dgv.Columns["fil3"].HeaderText = "نام خانوادگی";
            dgv.Columns["fil4"].HeaderText = "نام مستعار";
            dgv.Columns["fil5"].Visible = false;
            dgv.Columns["fil6"].Visible = false;
            dgv.Columns["fil7"].Visible = false;
            dgv.Columns["fil8"].Visible = false;
            dgv.Columns["fil9"].Visible = false;
            dgv.Columns["fil10"].Visible = false;
            dgv.Columns["fil11"].Visible = false;
            dgv.Columns["fil12"].Visible = false;
            dgv.Columns["fil13"].Visible = false;
            dgv.Columns["id"].Width = 50;
            dgv.Columns["fil1"].Width = 120;
            dgv.Columns["fil2"].Width = 120;
            dgv.Columns["fil3"].Width = 120;
            dgv.Columns["fil4"].Width = 150;
        }

        private void linkLabel1_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
        {
            fmgroup group = new fmgroup(); group.ShowDialog(); show();
        }

        private void linkLabel6_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
        {
            fmnew newcon = new fmnew(); newcon.button1.Text = "ذخیره"; newcon.ShowDialog();
            show();
        }

        private void linkLabel2_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
        {
            fmsettings user = new fmsettings(); user.ShowDialog();
        }

        private void cmsearch_SelectedIndexChanged(object sender, EventArgs e)
        {
            switch (cmsearch.SelectedIndex.ToString())
            {
                case "0":
                    txsearch.Enabled=false;btsearch.Enabled=false;
                    break;
                case "1":
                    txsearch.Enabled = true; btsearch.Enabled = true;
                    break;
                case "2":
                    txsearch.Enabled = true; btsearch.Enabled = true;
                    break;
                case "3":
                    txsearch.Enabled = true; btsearch.Enabled = true;
                    break;
                case "4":
                    txsearch.Enabled = true; btsearch.Enabled = true;
                    break;
                case "5":
                    txsearch.Enabled = true; btsearch.Enabled = true;
                    break;
            }
            
        }

        private void dgv_CellDoubleClick(object sender, DataGridViewCellEventArgs e)
        {
            fmshow n = new fmshow();
            n.l1.Text = dgv.CurrentRow.Cells[1].Value.ToString();
            n.l2.Text = dgv.CurrentRow.Cells[2].Value.ToString() + "  " + dgv.CurrentRow.Cells[3].Value.ToString() + "   -   " + dgv.CurrentRow.Cells[4].Value.ToString();
            n.l3.Text = dgv.CurrentRow.Cells[5].Value.ToString() + "     " + dgv.CurrentRow.Cells[6].Value.ToString();
            n.l4.Text = dgv.CurrentRow.Cells[7].Value.ToString() + "     " + dgv.CurrentRow.Cells[8].Value.ToString();
            n.l5.Text = dgv.CurrentRow.Cells[9].Value.ToString();
            n.l6.Text = dgv.CurrentRow.Cells[10].Value.ToString();
            n.l7.Text = dgv.CurrentRow.Cells[11].Value.ToString();
            n.l8.Text = dgv.CurrentRow.Cells[12].Value.ToString();
            n.l9.Text = dgv.CurrentRow.Cells[13].Value.ToString();
            n.ShowDialog();
        }

        private void linkLabel5_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
        {
            fmnew newcon = new fmnew(); newcon.button1.Text = "ویرایش";
            newcon.label13.Text = dgv.CurrentRow.Cells[1].Value.ToString();
            newcon.fil2.Text = dgv.CurrentRow.Cells[2].Value.ToString();
            newcon.fil3.Text = dgv.CurrentRow.Cells[3].Value.ToString();
            newcon.fil4.Text = dgv.CurrentRow.Cells[4].Value.ToString();
            newcon.fil5.Text = dgv.CurrentRow.Cells[5].Value.ToString();
            newcon.fil6.Text = dgv.CurrentRow.Cells[6].Value.ToString();
            newcon.fil7.Text = dgv.CurrentRow.Cells[7].Value.ToString();
            newcon.fil8.Text = dgv.CurrentRow.Cells[8].Value.ToString();
            newcon.fil9.Text = dgv.CurrentRow.Cells[9].Value.ToString();
            newcon.fil10.Text = dgv.CurrentRow.Cells[10].Value.ToString();
            newcon.fil11.Text = dgv.CurrentRow.Cells[11].Value.ToString();
            newcon.fil12.Text = dgv.CurrentRow.Cells[12].Value.ToString();
            newcon.fil13.Text = dgv.CurrentRow.Cells[13].Value.ToString();
            newcon.textBox1.Text = dgv.CurrentRow.Cells[0].Value.ToString();
            newcon.ShowDialog();
            show();
        }

        private void btsearch_Click(object sender, EventArgs e)
        {
            try
            {
                OleDbCommand com = new OleDbCommand();
                DataSet ds = new DataSet();

                switch (cmsearch.SelectedIndex.ToString())
                {
                    case "0":
                        txsearch.Enabled = false; btsearch.Enabled = false;
                        break;
                    case "1":
                        com.CommandText = "select * from contact where( id LIKE '" + txsearch.Text + "')";
                        break;
                    case "2":
                        com.CommandText = "select * from contact where( fil1 LIKE '" + txsearch.Text + "')";
                        break;
                    case "3":
                        com.CommandText = "select * from contact where( fil2 LIKE '" + txsearch.Text + "')";
                        break;
                    case "4":
                        com.CommandText = "select * from contact where( fil3 LIKE '" + txsearch.Text + "')";
                        break;
                    case "5":
                        com.CommandText = "select * from contact where( fil4 LIKE '" + txsearch.Text + "')";
                        break;
                }
                com.Connection = con;
                OleDbDataAdapter da = new OleDbDataAdapter(com);
                da.Fill(ds);
                dgv.DataSource = ds.Tables[0];
                dgv.Refresh();
            }

            catch (Exception ex)
            {

                MessageBox.Show(ex.Message, "System Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }

        private void linkLabel4_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
        {
            try
            {
                if (MessageBox.Show("آیا قصد دارید مخاطب انتخابی را حذف کنید ؟", "سوال بانک", MessageBoxButtons.YesNo, MessageBoxIcon.Warning) == DialogResult.Yes)
                {
                    OleDbCommand cs = new OleDbCommand("delete from contact where id=(" + dgv.CurrentRow.Cells[0].Value.ToString() + ")", con);
                    cs.ExecuteNonQuery();
                    show();
                    MessageBox.Show("عملیات حذف انجام شد", "System Info", MessageBoxButtons.OK, MessageBoxIcon.Information);
                }
            }
            catch (Exception ex)
            {

                MessageBox.Show(ex.Message, "System Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }

        private void button2_Click(object sender, EventArgs e)
        {
            OleDbCommand com = new OleDbCommand();
            if (cmsearch.SelectedIndex == 1)
            {
                com.CommandText = "select * from contact where( id LIKE '" + txsearch.Text + "')";
            }
            else
                if (cmsearch.SelectedIndex == 2)
                {
                    com.CommandText = "select * from contact where( fil1 LIKE '" + txsearch.Text + "')";
                }
            DataSet ds = new DataSet();
            com.Connection = con;
            OleDbDataAdapter da = new OleDbDataAdapter(com);
            da.Fill(ds);
            dgv.DataSource = ds.Tables[0];
            dgv.Refresh();
        }

        private void linkLabel3_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
        {
            backre br = new backre(); br.ShowDialog();
        }

        private void timer1_Tick(object sender, EventArgs e)
        {
            System.Globalization.PersianCalendar pc = new System.Globalization.PersianCalendar();
            lp2.Text = "امروز : "+pc.GetYear(DateTime.Now) + "/" + pc.GetMonth(DateTime.Now) + "/" + pc.GetDayOfMonth(DateTime.Now) + "     ساعت : " + pc.GetHour(DateTime.Now) + ":" + pc.GetMinute(DateTime.Now) + ":" + pc.GetSecond(DateTime.Now); 

        }

        private void Main_FormClosing(object sender, FormClosingEventArgs e)
        {

                backre bk = new backre();
                bk.ShowDialog();
            }

        private void Main_KeyPress(object sender, KeyPressEventArgs e)
        {

        }

        private void Main_KeyDown(object sender, KeyEventArgs e)
        {
            switch (e.KeyData)
            {
                case Keys.F1:
                    fmgroup fg = new fmgroup(); fg.ShowDialog();
                    break;

            }
        }

        private void linkLabel10_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
        {
            about ab = new about(); ab.ShowDialog();
        }
        }
    }

