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.SqlClient;
using System.Globalization;
using Stimulsoft.Report.Dictionary;
using Stimulsoft.Report;
using DevComponents.DotNetBar;
using DevComponents.DotNetBar.Rendering;
using System.IO;

namespace Library_Red_Diamonds
{
    public partial class registermember : Form
    {
        static SqlConnection con = new SqlConnection(Connection.con);
        public registermember()
        {
            InitializeComponent();
        }

        SqlCommand cmd = new SqlCommand();
        SqlDataAdapter adp = new SqlDataAdapter();
        DataSet ds = new DataSet();
        DataTable dt = new DataTable();
        PersianCalendar pc = new PersianCalendar();
        OpenFileDialog op = new OpenFileDialog();
        MemoryStream ms = new MemoryStream();
        MyDate md = new MyDate();
        Random rnd = new Random();


        private void clear()
        {
            txt_search_member_number.Clear();
            txt_search_national_code.Clear();
            msk_onedate.Text = "";
            msk_date1.Text = "";
            msk_date2.Text = "";
            txt_code.Clear();
            txt_member_number.Clear();
            txt_name_family.Clear();
            txt_father_name.Clear();
            txt_national_code.Clear();
            msk_date_birth.Text = "";
            cmb_education.Text = "";
            txt_tel.Clear();
            txt_mob.Clear();
            txt_email.Clear();
            txt_sponsor_name.Clear();
            txt_tel_sponsor.Clear();
            txt_mob_sponsor.Clear();
            msk_start_member.Text = "";
            msk_end_member.Text = "";
            txt_debit.Clear();
            msk_regdate.Text = "";
            txt_address.Clear();
            pictureBox.Image = Properties.Resources.image_not_available;
        }



        private void returns()
        {
            txt_code.Text = dgv_member.CurrentRow.Cells[0].Value.ToString();
            txt_member_number.Text = dgv_member.CurrentRow.Cells[1].Value.ToString();
            txt_name_family.Text = dgv_member.CurrentRow.Cells[2].Value.ToString();
            txt_father_name.Text = dgv_member.CurrentRow.Cells[3].Value.ToString();
            txt_national_code.Text = dgv_member.CurrentRow.Cells[4].Value.ToString();
            msk_date_birth.Text = dgv_member.CurrentRow.Cells[5].Value.ToString();
            selectrbtn(dgv_member.CurrentRow.Cells[6].Value.ToString());
            cmb_education.Text = dgv_member.CurrentRow.Cells[7].Value.ToString();
            txt_tel.Text = dgv_member.CurrentRow.Cells[8].Value.ToString();
            txt_mob.Text = dgv_member.CurrentRow.Cells[9].Value.ToString();
            txt_email.Text = dgv_member.CurrentRow.Cells[10].Value.ToString();
            txt_sponsor_name.Text = dgv_member.CurrentRow.Cells[11].Value.ToString();
            txt_tel_sponsor.Text = dgv_member.CurrentRow.Cells[12].Value.ToString();
            txt_mob_sponsor.Text = dgv_member.CurrentRow.Cells[13].Value.ToString();
            msk_start_member.Text = dgv_member.CurrentRow.Cells[14].Value.ToString();
            msk_end_member.Text = dgv_member.CurrentRow.Cells[15].Value.ToString();
            txt_debit.Text = dgv_member.CurrentRow.Cells[16].Value.ToString();
            msk_regdate.Text = dgv_member.CurrentRow.Cells[17].Value.ToString();
            txt_address.Text = dgv_member.CurrentRow.Cells[18].Value.ToString();
            var data = (byte[])(dgv_member.CurrentRow.Cells[19].Value);
            var stream = new MemoryStream(data);
            pictureBox.Image = Image.FromStream(stream);
            lbl_id.Text = dgv_member.CurrentRow.Cells[20].Value.ToString();
        }

        public void show()
        {
            dt.Clear();
            adp = new SqlDataAdapter("AllMembers", con);
            adp.SelectCommand.CommandType = CommandType.StoredProcedure;
            adp.Fill(dt);
            dgv_member.DataSource = dt;
        }

        private byte[] GetByte(PictureBox picturbox)
        {
            byte[] arrPic = null;
            try
            {
                if (picturbox.Image != null)
                {

                    picturbox.Image.Save(ms, picturbox.Image.RawFormat);
                    arrPic = ms.GetBuffer();
                    ms.Close();
                }
            }
            catch (Exception)
            {
 
            }
            return arrPic;
        }

        private string selectsex()
        {
            string _status = string.Empty;
            if (rb_man.IsChecked)
                _status = "مرد";
            else if (rb_woman.IsChecked)
                _status = "زن";
            return _status;
        }

        private void selectrbtn(string status)
        {
            if (status == "مرد")
                rb_man.IsChecked = true;
            else if (status == "زن")
                rb_woman.IsChecked = true;
        }

       

        //--------------start searches ----------------------------------------//

        public void search_national_code()
        {
            adp.SelectCommand.Connection = con;
            adp.SelectCommand.CommandText = "select * from members where code='" + txt_search_national_code.Text + "'";
            adp.Fill(ds, "t1");
            dgv_member.DataSource = ds;
            dgv_member.DataMember = "t1";
        }

        public void search_one()
        {
            adp.SelectCommand.Connection = con;
            adp.SelectCommand.CommandText = "select * from members where regdate='" + msk_onedate.Text + "'";
            adp.Fill(ds, "t1");
            dgv_member.DataSource = ds;
            dgv_member.DataMember = "t1";
        }

        public void search_two()
        {
            adp.SelectCommand.Connection = con;
            adp.SelectCommand.CommandText = "select * from members where regdate between '" + msk_date1.Text + "' and '" + msk_date2.Text + "'";
            adp.Fill(ds, "t1");
            dgv_member.DataSource = ds;
            dgv_member.DataMember = "t1";
        }

        public void search_member_number()
        {
            adp.SelectCommand.Connection = con;
            adp.SelectCommand.CommandText = "select * from members where nationalcode='" + txt_search_member_number.Text + "'";
            adp.Fill(ds, "t1");
            dgv_member.DataSource = ds;
            dgv_member.DataMember = "t1";
        }

        //--------------end searches ----------------------------------------//

        private void add()
        {
            cmd = new SqlCommand("AddMembers", con);
            cmd.CommandType = CommandType.StoredProcedure;

            cmd.Parameters.AddWithValue("@code", txt_code.Text);
            cmd.Parameters.AddWithValue("@membernumber", txt_member_number.Text);
            cmd.Parameters.AddWithValue("@namefamily", txt_name_family.Text);
            cmd.Parameters.AddWithValue("@fathername", txt_father_name.Text);
            cmd.Parameters.AddWithValue("@nationalcode", txt_national_code.Text);
            cmd.Parameters.AddWithValue("@datebirth", msk_date_birth.Text);
            cmd.Parameters.AddWithValue("@sex", selectsex());
            cmd.Parameters.AddWithValue("@education", cmb_education.Text);
            cmd.Parameters.AddWithValue("@tel", txt_tel.Text);
            cmd.Parameters.AddWithValue("@mob", txt_mob.Text);
            cmd.Parameters.AddWithValue("@email", txt_email.Text);
            cmd.Parameters.AddWithValue("@sponsorname", txt_sponsor_name.Text);
            cmd.Parameters.AddWithValue("@telsponsor", txt_tel_sponsor.Text);
            cmd.Parameters.AddWithValue("@mobsponsor", txt_mob_sponsor.Text);
            cmd.Parameters.AddWithValue("@startmember", msk_start_member.Text);
            cmd.Parameters.AddWithValue("@endmember", msk_end_member.Text);
            cmd.Parameters.AddWithValue("@debit", txt_debit.Text);
            cmd.Parameters.AddWithValue("@regdate", msk_regdate.Text);
            cmd.Parameters.AddWithValue("@addres", txt_address.Text);
            cmd.Parameters.AddWithValue("@images", GetByte(pictureBox));

            con.Open();
            cmd.ExecuteNonQuery();
            con.Close();
        }

        private void edit()
        {
            cmd = new SqlCommand("EditMembers", con);
            cmd.CommandType = CommandType.StoredProcedure;
            cmd.Parameters.AddWithValue("@id", lbl_id.Text);
            cmd.Parameters.AddWithValue("@code", txt_code.Text);
            cmd.Parameters.AddWithValue("@membernumber", txt_member_number.Text);
            cmd.Parameters.AddWithValue("@namefamily", txt_name_family.Text);
            cmd.Parameters.AddWithValue("@fathername", txt_father_name.Text);
            cmd.Parameters.AddWithValue("@nationalcode", txt_national_code.Text);
            cmd.Parameters.AddWithValue("@datebirth", msk_date_birth.Text);
            cmd.Parameters.AddWithValue("@sex", selectsex());
            cmd.Parameters.AddWithValue("@education", cmb_education.Text);
            cmd.Parameters.AddWithValue("@tel", txt_tel.Text);
            cmd.Parameters.AddWithValue("@mob", txt_mob.Text);
            cmd.Parameters.AddWithValue("@email", txt_email.Text);
            cmd.Parameters.AddWithValue("@sponsorname", txt_sponsor_name.Text);
            cmd.Parameters.AddWithValue("@telsponsor", txt_tel_sponsor.Text);
            cmd.Parameters.AddWithValue("@mobsponsor", txt_mob_sponsor.Text);
            cmd.Parameters.AddWithValue("@startmember", msk_start_member.Text);
            cmd.Parameters.AddWithValue("@endmember", msk_end_member.Text);
            cmd.Parameters.AddWithValue("@debit", txt_debit.Text);
            cmd.Parameters.AddWithValue("@regdate", msk_regdate.Text);
            cmd.Parameters.AddWithValue("@addres", txt_address.Text);
            cmd.Parameters.AddWithValue("@images", GetByte(pictureBox));

            con.Open();
            cmd.ExecuteNonQuery();
            con.Close();
        }


        private void delete()
        {
            cmd = new SqlCommand("DeleteMembers", con);
            cmd.CommandType = CommandType.StoredProcedure;
            cmd.Parameters.AddWithValue("@code", dgv_member.CurrentRow.Cells[0].Value.ToString());
            con.Open();
            if (MessageBox.Show("آیا می خواهید این را حذف کنید", "حذف", MessageBoxButtons.YesNo) == DialogResult.Yes)
            {
                cmd.ExecuteNonQuery();
                MessageBox.Show("حذف شد");
            }
            con.Close();
        }

        private void btn_add_Click(object sender, EventArgs e)
        {
            if (pictureBox.Image == Properties.Resources.image_not_available)
            {
                MessageBox.Show("لطفا تصویری انتخاب کنید");
                return;
            }
            else
            {
                add();
                show();
                clear();
                txt_code.Focus();
            }
        }

        private void btn_edit_Click(object sender, EventArgs e)
        {
            edit();
            show();
            clear();
            txt_code.Focus();
        }

        private void btn_delete_Click(object sender, EventArgs e)
        {
            delete();
            show();
            clear();
            txt_code.Focus();
        }

        private void btn_clear_Click(object sender, EventArgs e)
        {
            clear();
            txt_code.Focus();
        }

        private void btn_show_Click(object sender, EventArgs e)
        {
            show();
            txt_code.Focus();
        }

        private void registermember_Load(object sender, EventArgs e)
        {
            show();
            InputLanguage.CurrentInputLanguage = InputLanguage.FromCulture(System.Globalization.CultureInfo.CreateSpecificCulture("fa-IR"));
        }

        private void btn_search_national_code_Click(object sender, EventArgs e)
        {
            search_national_code();
        }

        private void btn_search_one_Click(object sender, EventArgs e)
        {
            search_one();
        }

        private void btn_dates_Click(object sender, EventArgs e)
        {
            msk_onedate.Text = md.alldate();
        }

        private void btn_dates1_Click(object sender, EventArgs e)
        {
            msk_date1.Text = md.alldate();
        }

        private void btn_dates2_Click(object sender, EventArgs e)
        {
            msk_date2.Text = md.alldate();
        }

        private void btn_search_two_Click(object sender, EventArgs e)
        {
            search_two();
        }

        private void btn_search_member_number_Click(object sender, EventArgs e)
        {
            search_member_number();
        }

        private void btn_number_Click(object sender, EventArgs e)
        {
            txt_member_number.Text = "" + rnd.Next(0, 10) + rnd.Next(0, 10) + rnd.Next(0, 10) + rnd.Next(0, 10) + rnd.Next(0, 10) + rnd.Next(0, 10) + rnd.Next(0, 10) + rnd.Next(0, 10) + rnd.Next(0, 10) + rnd.Next(0, 10) + rnd.Next(0, 10) + rnd.Next(0, 10) + rnd.Next(0, 10);
        }

        private void btn_birth_Click(object sender, EventArgs e)
        {
            msk_date_birth.Text = md.alldate();
        }

        private void btn_date_start_Click(object sender, EventArgs e)
        {
            msk_start_member.Text = md.alldate();
            if (msk_start_member.Text.Length == 10)
            {
                DateTime dt = DateTime.Parse(msk_start_member.Text);
                msk_end_member.Text = (dt.Year + 1).ToString("0000") + "/" + dt.Month.ToString("00") + "/" + dt.Day.ToString("00");
            }
        }

        private void btn_date_end_Click(object sender, EventArgs e)
        {
            msk_end_member.Text = md.alldate();
        }

        private void btn_date4_Click(object sender, EventArgs e)
        {
            msk_regdate.Text = md.alldate();
        }

        private void btn_add_pic_Click(object sender, EventArgs e)
        {
            op.Multiselect = false;
            op.Filter = "Jpeg image(*.jpg)|*.jpg";
            if (op.ShowDialog() == DialogResult.OK)
                pictureBox.Image = Image.FromFile(op.FileName);
        }

        private void btn_del_pic_Click(object sender, EventArgs e)
        {
            pictureBox.Image = Properties.Resources.image_not_available;
        }

        private void dgv_member_MouseDoubleClick(object sender, MouseEventArgs e)
        {
            returns();
        }

        private void btn_filtering_ValueChanged(object sender, EventArgs e)
        {
            if (btn_filtering.Value == Enabled)
            {
                dgv_member.EnableFiltering = true;
                dgv_member.MouseDoubleClick -= dgv_member_MouseDoubleClick;
            }
            else
            {
                dgv_member.EnableFiltering = false;
            }
        }

        private void txt_debit_TextChanged(object sender, EventArgs e)
        {
            try
            {
                decimal price;
                price = decimal.Parse(txt_debit.Text, System.Globalization.NumberStyles.Currency);
                txt_debit.Text = price.ToString("#,#");
                txt_debit.SelectionStart = txt_debit.Text.Length;
            }

            catch (Exception)
            {

            }
        }

    }
}
