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;


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

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

        public void GetCategoryName(string name)
        {
            txt_category.Text = name;
        }

        private void clear()
        {
            txt_search_copies_available.Clear();
            msk_onedate.Text = "";
            msk_date1.Text = "";
            msk_date2.Text = "";
            txt_serach_barcode.Clear();
            cmb_search.Text = "";
            txt_code.Clear();
            txt_title.Clear();
            txt_category.Clear();
            txt_author.Clear();
            txt_translator.Clear();
            txt_publisher.Clear();
            msk_publication_date.Text = "";
            txt_published.Clear();
            txt_barcode.Clear();
            txt_isbn.Clear();
            txt_national.Clear();
            txt_congress.Clear();
            txt_dewey.Clear();
            cmb_printing_type.Text = "";
            txt_cover.Clear();
            txt_copies_available.Clear();
            msk_year.Text = "";
            txt_circulation.Clear();
            txt_price.Clear();
            msk_regdate.Text = "";
        }


        private void returns()
        {
            bookEditId.Text = dgv_books.CurrentRow.Cells[20].Value.ToString();
            txt_code.Text = dgv_books.CurrentRow.Cells[0].Value.ToString();
            txt_title.Text = dgv_books.CurrentRow.Cells[1].Value.ToString();
            txt_category.Text = dgv_books.CurrentRow.Cells[2].Value.ToString();
            txt_author.Text = dgv_books.CurrentRow.Cells[3].Value.ToString();
            txt_translator.Text = dgv_books.CurrentRow.Cells[4].Value.ToString();
            txt_publisher.Text = dgv_books.CurrentRow.Cells[5].Value.ToString();
            msk_publication_date.Text = dgv_books.CurrentRow.Cells[6].Value.ToString();
            txt_published.Text = dgv_books.CurrentRow.Cells[7].Value.ToString();
            txt_barcode.Text = dgv_books.CurrentRow.Cells[8].Value.ToString();
            txt_isbn.Text = dgv_books.CurrentRow.Cells[9].Value.ToString();
            txt_national.Text = dgv_books.CurrentRow.Cells[10].Value.ToString();
            txt_congress.Text = dgv_books.CurrentRow.Cells[11].Value.ToString();
            txt_dewey.Text = dgv_books.CurrentRow.Cells[12].Value.ToString();
            cmb_printing_type.Text = dgv_books.CurrentRow.Cells[13].Value.ToString();
            txt_cover.Text = dgv_books.CurrentRow.Cells[14].Value.ToString();
            txt_copies_available.Text = dgv_books.CurrentRow.Cells[15].Value.ToString();
            msk_year.Text = dgv_books.CurrentRow.Cells[16].Value.ToString();
            txt_circulation.Text = dgv_books.CurrentRow.Cells[17].Value.ToString();
            txt_price.Text = dgv_books.CurrentRow.Cells[18].Value.ToString();
            msk_regdate.Text = dgv_books.CurrentRow.Cells[19].Value.ToString();
        }

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

        //--------------begin search in combobox ----------------------------------------//
        public void search_code()
        {
            SqlDataAdapter adp = new SqlDataAdapter("select * from book where code='" + txt_search.Text + "'", con);
            adp.Fill(ds, "t1");
            dgv_books.DataSource = ds;
            dgv_books.DataMember = "t1";
        }

        public void search_title()
        {
            SqlDataAdapter adp = new SqlDataAdapter("select * from book where title='" + txt_search.Text + "'", con);
            adp.Fill(ds, "t1");
            dgv_books.DataSource = ds;
            dgv_books.DataMember = "t1";
        }

        public void search_category()
        {
            SqlDataAdapter adp = new SqlDataAdapter("select * from book where category='" + txt_search.Text + "'", con);
            adp.Fill(ds, "t1");
            dgv_books.DataSource = ds;
            dgv_books.DataMember = "t1";
        }

        public void search_author()
        {
            SqlDataAdapter adp = new SqlDataAdapter("select * from book where author='" + txt_search.Text + "'", con);
            adp.Fill(ds, "t1");
            dgv_books.DataSource = ds;
            dgv_books.DataMember = "t1";
        }

        public void search_translator()
        {
            SqlDataAdapter adp = new SqlDataAdapter("select * from book where translator='" + txt_search.Text + "'", con);
            adp.Fill(ds, "t1");
            dgv_books.DataSource = ds;
            dgv_books.DataMember = "t1";
        }

        public void search_publisher()
        {
            SqlDataAdapter adp = new SqlDataAdapter("select * from book where publisher='" + txt_search.Text + "'", con);
            adp.Fill(ds, "t1");
            dgv_books.DataSource = ds;
            dgv_books.DataMember = "t1";
        }

        public void search_publicationdate()
        {
            SqlDataAdapter adp = new SqlDataAdapter("select * from book where publicationdate='" + txt_search.Text + "'", con);
            adp.Fill(ds, "t1");
            dgv_books.DataSource = ds;
            dgv_books.DataMember = "t1";
        }

        public void search_published()
        {
            SqlDataAdapter adp = new SqlDataAdapter("select * from book where published='" + txt_search.Text + "'", con);
            adp.Fill(ds, "t1");
            dgv_books.DataSource = ds;
            dgv_books.DataMember = "t1";
        }

        public void search_barcode()
        {
            SqlDataAdapter adp = new SqlDataAdapter("select * from book where barcode='" + txt_search.Text + "'", con);
            adp.Fill(ds, "t1");
            dgv_books.DataSource = ds;
            dgv_books.DataMember = "t1";
        }

        public void search_isbn()
        {
            SqlDataAdapter adp = new SqlDataAdapter("select * from book where isbn='" + txt_search.Text + "'", con);
            adp.Fill(ds, "t1");
            dgv_books.DataSource = ds;
            dgv_books.DataMember = "t1";
        }

        public void search_nationals()
        {
            SqlDataAdapter adp = new SqlDataAdapter("select * from book where nationals='" + txt_search.Text + "'", con);
            adp.Fill(ds, "t1");
            dgv_books.DataSource = ds;
            dgv_books.DataMember = "t1";
        }

        public void search_congress()
        {
            SqlDataAdapter adp = new SqlDataAdapter("select * from book where congress='" + txt_search.Text + "'", con);
            adp.Fill(ds, "t1");
            dgv_books.DataSource = ds;
            dgv_books.DataMember = "t1";
        }

        public void search_dewey()
        {
            SqlDataAdapter adp = new SqlDataAdapter("select * from book where dewey='" + txt_search.Text + "'", con);
            adp.Fill(ds, "t1");
            dgv_books.DataSource = ds;
            dgv_books.DataMember = "t1";
        }


        public void search_printingtype()
        {
            SqlDataAdapter adp = new SqlDataAdapter("select * from book where printingtype='" + txt_search.Text + "'", con);
            adp.Fill(ds, "t1");
            dgv_books.DataSource = ds;
            dgv_books.DataMember = "t1";
        }

        public void search_cover()
        {
            SqlDataAdapter adp = new SqlDataAdapter("select * from book where cover='" + txt_search.Text + "'", con);
            adp.Fill(ds, "t1");
            dgv_books.DataSource = ds;
            dgv_books.DataMember = "t1";
        }

        public void search_copiesavailable()
        {
            SqlDataAdapter adp = new SqlDataAdapter("select * from book where copiesavailable='" + txt_search.Text + "'", con);
            adp.Fill(ds, "t1");
            dgv_books.DataSource = ds;
            dgv_books.DataMember = "t1";
        }

        public void search_years()
        {
            SqlDataAdapter adp = new SqlDataAdapter("select * from book where years='" + txt_search.Text + "'", con);
            adp.Fill(ds, "t1");
            dgv_books.DataSource = ds;
            dgv_books.DataMember = "t1";
        }

        public void search_circulation()
        {
            SqlDataAdapter adp = new SqlDataAdapter("select * from book where circulation='" + txt_search.Text + "'", con);
            adp.Fill(ds, "t1");
            dgv_books.DataSource = ds;
            dgv_books.DataMember = "t1";
        }

        public void search_price()
        {
            SqlDataAdapter adp = new SqlDataAdapter("select * from book where price='" + txt_search.Text + "'", con);
            adp.Fill(ds, "t1");
            dgv_books.DataSource = ds;
            dgv_books.DataMember = "t1";
        }

        public void search_regdate()
        {
            SqlDataAdapter adp = new SqlDataAdapter("select * from book where regdate='" + txt_search.Text + "'", con);
            adp.Fill(ds, "t1");
            dgv_books.DataSource = ds;
            dgv_books.DataMember = "t1";
        }

        //--------------end search in combobox ----------------------------------------//


        //---------------spaical searches -------------------------------------------//

        public void search_available()
        {
            SqlDataAdapter adp = new SqlDataAdapter("select * from book where copiesavailable='" + txt_search_copies_available.Text + "'", con);
            adp.Fill(ds, "t1");
            dgv_books.DataSource = ds;
            dgv_books.DataMember = "t1";
        }

        public void search_one()
        {
            SqlDataAdapter adp = new SqlDataAdapter("select * from book where regdate='" + msk_onedate.Text + "'", con);
            adp.Fill(ds, "t1");
            dgv_books.DataSource = ds;
            dgv_books.DataMember = "t1";
        }

        public void search_two()
        {
            SqlDataAdapter adp = new SqlDataAdapter("select * from book where regdate between '" + msk_date1.Text + "' and '" + msk_date2.Text + "'", con);
            adp.Fill(ds, "t1");
            dgv_books.DataSource = ds;
            dgv_books.DataMember = "t1";
        }

        public void search_barcode_sp()
        {
            SqlDataAdapter adp = new SqlDataAdapter("select * from book where barcode='" + txt_serach_barcode.Text + "'", con);
            adp.Fill(ds, "t1");
            dgv_books.DataSource = ds;
            dgv_books.DataMember = "t1";
        }


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

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

            cmd.Parameters.AddWithValue("@code", txt_code.Text);
            cmd.Parameters.AddWithValue("@title", txt_title.Text);
            cmd.Parameters.AddWithValue("@category", txt_category.Text);
            cmd.Parameters.AddWithValue("@author", txt_author.Text);
            cmd.Parameters.AddWithValue("@translator", txt_translator.Text);
            cmd.Parameters.AddWithValue("@publisher", txt_publisher.Text);
            cmd.Parameters.AddWithValue("@publicationdate", msk_publication_date.Text);
            cmd.Parameters.AddWithValue("@published", txt_published.Text);
            cmd.Parameters.AddWithValue("@barcode", txt_barcode.Text);
            cmd.Parameters.AddWithValue("@isbn", txt_isbn.Text);
            cmd.Parameters.AddWithValue("@nationals", txt_national.Text);
            cmd.Parameters.AddWithValue("@congress", txt_congress.Text);
            cmd.Parameters.AddWithValue("@dewey", txt_dewey.Text);
            cmd.Parameters.AddWithValue("@printingtype", cmb_printing_type.Text);
            cmd.Parameters.AddWithValue("@cover", txt_cover.Text);
            cmd.Parameters.AddWithValue("@copiesavailable", txt_copies_available.Text);
            cmd.Parameters.AddWithValue("@years", msk_year.Text);
            cmd.Parameters.AddWithValue("@circulation", txt_circulation.Text);
            cmd.Parameters.AddWithValue("@price", txt_price.Text);
            cmd.Parameters.AddWithValue("@regdate", msk_regdate.Text);

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

        private void edit()
        {
            cmd = new SqlCommand("EditBook", con);
            cmd.CommandType = CommandType.StoredProcedure;
            cmd.Parameters.AddWithValue("@id", bookEditId.Text);
            cmd.Parameters.AddWithValue("@code", txt_code.Text);
            cmd.Parameters.AddWithValue("@title", txt_title.Text);
            cmd.Parameters.AddWithValue("@category", txt_category.Text);
            cmd.Parameters.AddWithValue("@author", txt_author.Text);
            cmd.Parameters.AddWithValue("@translator", txt_translator.Text);
            cmd.Parameters.AddWithValue("@publisher", txt_publisher.Text);
            cmd.Parameters.AddWithValue("@publicationdate", msk_publication_date.Text);
            cmd.Parameters.AddWithValue("@published", txt_published.Text);
            cmd.Parameters.AddWithValue("@barcode", txt_barcode.Text);
            cmd.Parameters.AddWithValue("@isbn", txt_isbn.Text);
            cmd.Parameters.AddWithValue("@nationals", txt_national.Text);
            cmd.Parameters.AddWithValue("@congress", txt_congress.Text);
            cmd.Parameters.AddWithValue("@dewey", txt_dewey.Text);
            cmd.Parameters.AddWithValue("@printingtype", cmb_printing_type.Text);
            cmd.Parameters.AddWithValue("@cover", txt_cover.Text);
            cmd.Parameters.AddWithValue("@copiesavailable", txt_copies_available.Text);
            cmd.Parameters.AddWithValue("@years", msk_year.Text);
            cmd.Parameters.AddWithValue("@circulation", txt_circulation.Text);
            cmd.Parameters.AddWithValue("@price", txt_price.Text);
            cmd.Parameters.AddWithValue("@regdate", msk_regdate.Text);

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

        private void delete()
        {
            cmd = new SqlCommand("DeleteBook", con);
            cmd.CommandType = CommandType.StoredProcedure;
            cmd.Parameters.AddWithValue("@code", dgv_books.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)
        {
            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();
            show();
            txt_code.Focus();
        }

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

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

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

        private void btn_tolidbarcode_Click(object sender, EventArgs e)
        {
            txt_barcode.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_year_Click(object sender, EventArgs e)
        {
            msk_year.Text = pc.GetYear(DateTime.Now).ToString("0000");
        }

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

        private void btn_search_copies_available_Click(object sender, EventArgs e)
        {
            search_available();
        }

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

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

        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_barcode_Click(object sender, EventArgs e)
        {
            search_barcode_sp();
        }

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

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

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

            catch (Exception)
            {

            }
        }

        private void txt_price_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (!(char.IsDigit(e.KeyChar) || char.IsControl(e.KeyChar)))
            {
                e.Handled = true;
            }
        }

        private void txt_circulation_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (!(char.IsDigit(e.KeyChar) || char.IsControl(e.KeyChar)))
            {
                e.Handled = true;
            }
        }

        private void txt_copies_available_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (!(char.IsDigit(e.KeyChar) || char.IsControl(e.KeyChar)))
            {
                e.Handled = true;
            }
        }

        private void txt_cover_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (!(char.IsDigit(e.KeyChar) || char.IsControl(e.KeyChar)))
            {
                e.Handled = true;
            }
        }

        private void btn_search_Click(object sender, EventArgs e)
        {
            if (cmb_search.Text == "")
            {
                MessageBox.Show("لطفاً یکی از آیتم های مورد نظر را انتخاب کنید");
            }
            else
            {
                if (cmb_search.Text == "کد")
                {
                    search_code();
                }
                if (cmb_search.Text == "عنوان")
                {
                    search_title();
                }
                if (cmb_search.Text == "دسته بندی")
                {
                    search_code();
                }
                if (cmb_search.Text == "نویسنده")
                {
                    search_author();
                }
                if (cmb_search.Text == "مترجم")
                {
                    search_translator();
                }
                if (cmb_search.Text == "ناشر")
                {
                    search_publisher();
                }
                if (cmb_search.Text == "تاریخ نشر")
                {
                    search_publicationdate();
                }
                if (cmb_search.Text == "نوبت چاپ")
                {
                    search_published();
                }
                if (cmb_search.Text == "بارکد")
                {
                    search_barcode();
                }
                if (cmb_search.Text == "شابک")
                {
                    search_isbn();
                }
                if (cmb_search.Text == "شماره ملی")
                {
                    search_nationals();
                }
                if (cmb_search.Text == "کد رده بندی کنگره")
                {
                    search_congress();
                }
                if (cmb_search.Text == "کد رده بندی دیویی")
                {
                    search_dewey();
                }
                if (cmb_search.Text == "نوع چاپ")
                {
                    search_printingtype();
                }
                if (cmb_search.Text == "تعداد جلد")
                {
                    search_cover();
                }
                if (cmb_search.Text == "تعداد نسخه موجود")
                {
                    search_copiesavailable();
                }
                if (cmb_search.Text == "سال انتشار")
                {
                    search_years();
                }
                if (cmb_search.Text == "تیراژ")
                {
                    search_code();
                }
                if (cmb_search.Text == "قیمت")
                {
                    search_price();
                }
                if (cmb_search.Text == "تاریخ ثبت")
                {
                    search_regdate();
                }
            }
        }

        private void btn_category_Click(object sender, EventArgs e)
        {
            listcategory lc = new listcategory();
            lc.ShowDialog();
        }

        private void txt_barcode_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (!(char.IsDigit(e.KeyChar) || char.IsControl(e.KeyChar)))
            {
                e.Handled = true;
            }
        }
 

    }
}
