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 cards : Form
    {
        static SqlConnection con = new SqlConnection(Connection.con);
        public cards()
        {
            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();


         byte[] picture = null;
         public void GetMemberName(string num, string name, string code, byte[] pic)
        {
            txt_member_number.Text = num;
            txt_name_family.Text = name;
            txt_national_code.Text = code;
            picture = pic;
            ms = new MemoryStream(pic);
            pictureBox.Image = Image.FromStream(ms);
        }

         private void clear()
         {
             txt_code.Clear();
             txt_member_number.Clear();
             txt_name_family.Clear();
             txt_national_code.Clear();
             msk_issue_date.Text = "";
             msk_expiration_date.Text = "";
             msk_regdate.Text = "";
             pictureBox.Image = Properties.Resources.image_not_available;
         }

         private void returns()
         {
             txt_code.Text = dgv_cards.CurrentRow.Cells[1].Value.ToString();
             txt_member_number.Text = dgv_cards.CurrentRow.Cells[2].Value.ToString();
             txt_name_family.Text = dgv_cards.CurrentRow.Cells[3].Value.ToString();
             txt_national_code.Text = dgv_cards.CurrentRow.Cells[4].Value.ToString();
             msk_issue_date.Text = dgv_cards.CurrentRow.Cells[5].Value.ToString();
             msk_expiration_date.Text = dgv_cards.CurrentRow.Cells[6].Value.ToString();
             msk_regdate.Text = dgv_cards.CurrentRow.Cells[7].Value.ToString();
             var data = (byte[])(dgv_cards.CurrentRow.Cells[8].Value);
             var stream = new MemoryStream(data);
             pictureBox.Image = Image.FromStream(stream);
             lbl_id.Text = dgv_cards.CurrentRow.Cells[9].Value.ToString();
         }

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

         private void add()
         {
             cmd = new SqlCommand("AddCards", 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("@nationalcode", txt_national_code.Text);
             cmd.Parameters.AddWithValue("@issuedate", msk_issue_date.Text);
             cmd.Parameters.AddWithValue("@expirationdate", msk_expiration_date.Text);
             cmd.Parameters.AddWithValue("@regdate", msk_regdate.Text);
             cmd.Parameters.AddWithValue("@images", picture);

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

         private void edit()
         {
             cmd = new SqlCommand("EditCards", 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("@nationalcode", txt_national_code.Text);
             cmd.Parameters.AddWithValue("@issuedate", msk_issue_date.Text);
             cmd.Parameters.AddWithValue("@expirationdate", msk_expiration_date.Text);
             cmd.Parameters.AddWithValue("@regdate", msk_regdate.Text);
             cmd.Parameters.AddWithValue("@images", picture);

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

         private void delete()
         {
             string id = string.Empty;
             for (int i = 0; i <= dgv_cards.RowCount - 1; i++)
             {
                 if (Convert.ToBoolean(dgv_cards.Rows[i].Cells["Col1"].Value) == true)
                     id = id + dgv_cards.Rows[i].Cells[9].Value + ",";
             }
             if (id.Length > 0)
                 id = id.Remove(id.Length - 1, 1);

             SqlCommand cmd = new SqlCommand("DeleteCards", con);
             cmd.CommandType = CommandType.StoredProcedure;
             cmd.Parameters.AddWithValue("@id", id);
             con.Open();
             if (MessageBox.Show("آیا می خواهید این را حذف کنید", "حذف", MessageBoxButtons.YesNo) == DialogResult.Yes)
             {
                 cmd.ExecuteNonQuery();
                 MessageBox.Show("حذف شد");
             }
             con.Close();
         }


         public void print()
         {
             DataTable dt = new DataTable();
             dt.Columns.Add(new DataColumn("membernumber", typeof(string)));
             dt.Columns.Add(new DataColumn("namefamily", typeof(string)));
             dt.Columns.Add(new DataColumn("nationalcode", typeof(string)));
             dt.Columns.Add(new DataColumn("issuedate", typeof(string)));
             dt.Columns.Add(new DataColumn("expirationdate", typeof(string)));
             dt.Columns.Add(new DataColumn("images", typeof(byte[])));


             int dtRow = 0;
             for (int i = 0; i <= dgv_cards.RowCount - 1; i++)
             {
                 if (Convert.ToBoolean(dgv_cards.Rows[i].Cells["col1"].Value) == true)
                 {
                     dt.Rows.Add();
                     dt.Rows[dtRow][0] = dgv_cards.Rows[i].Cells[2].Value;
                     dt.Rows[dtRow][1] = dgv_cards.Rows[i].Cells[3].Value;
                     dt.Rows[dtRow][2] = dgv_cards.Rows[i].Cells[4].Value;
                     dt.Rows[dtRow][3] = dgv_cards.Rows[i].Cells[5].Value;
                     dt.Rows[dtRow][4] = dgv_cards.Rows[i].Cells[6].Value;
                     dt.Rows[dtRow][5] = dgv_cards.Rows[i].Cells[8].Value;
                     dtRow += 1;
                 }
             }

             if (dt.Rows.Count > 0)
             {
                 StiReport stiReport = new StiReport();
                 stiReport.Load(Application.StartupPath + "\\selected-cards.mrt");
                 stiReport.RegBusinessObject("Cards", dt);
                 stiReport.Show();
             }
         }

         private void btn_add_Click(object sender, EventArgs e)
         {
             if (dgv_cards.Rows.Count == 3)
             {
                 show();
                 clear();
                 txt_code.Focus();
                 MessageBox.Show("شما از نسخه دموی برنامه استفاده می کنید");
             }
             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 cards_Load(object sender, EventArgs e)
         {
             show();
             InputLanguage.CurrentInputLanguage = InputLanguage.FromCulture(System.Globalization.CultureInfo.CreateSpecificCulture("fa-IR"));
         }

         private void btn_number_Click(object sender, EventArgs e)
         {
             memberlists mls = new memberlists();
             mls.ShowDialog();
         }

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

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

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

         private void btn_print_Click(object sender, EventArgs e)
         {
             print();
         }


    }
}
