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 borrowbooks : Form
    {
        static SqlConnection con = new SqlConnection(Connection.con);
        public borrowbooks()
        {
            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 GetBookName(string name, string cat)
        {
            txt_bookname.Text = name;
            txt_category.Text = cat;
        }

        public void GetMemberName(string num, string name)
        {
            txt_member_number.Text = num;
            txt_namefamily.Text = name;
        }

        private void clear()
        {
            txt_search.Clear();
            txt_code.Clear();
            txt_bookname.Clear();
            txt_category.Clear();
            txt_member_number.Clear();
            txt_namefamily.Clear();
            msk_start_date.Text = "";
            msk_end_date.Text = "";
            txt_loanperiod.Clear();
            msk_regdate.Text = "";
        }

        private void menha()
        {
            try
            {
                DateTime dt = DateTime.Parse(msk_start_date.Text);
                DateTime dt2 = DateTime.Parse(msk_end_date.Text);
                DateTime date1 = pc.ToDateTime(dt.Year, dt.Month, dt.Day, dt.Hour, dt.Minute, dt.Second, dt.Millisecond);
                DateTime date2 = pc.ToDateTime(dt2.Year, dt2.Month, dt2.Day, dt2.Hour, dt2.Minute, dt2.Second, dt2.Millisecond);
                TimeSpan ts = date2.Subtract(date1);
                txt_loanperiod.Text = ts.Days.ToString();
            }
            catch(Exception)
            {

            }
        }

         private void count()
        {
            SqlCommand com = new SqlCommand("select count (*) from borrowbooks", con);
            con.Open();
            lbl_count_book.Text = Convert.ToString((int)com.ExecuteScalar()) + "";
            con.Close();
            //--------------------------------------------------------------------------------------------
            SqlCommand com2 = new SqlCommand("select count (*) from borrowbooks", con);
            con.Open();
            lbl_count_members.Text = Convert.ToString((int)com2.ExecuteScalar()) + "";
            con.Close();
            //--------------------------------------------------------------------------------------------
        }

         private void lastrecord()
         {
             
             con.Open();
             SqlDataReader myReader = null;
             SqlCommand myCommand = new SqlCommand("select top 1 * from borrowbooks order by code desc", con);
             myReader = myCommand.ExecuteReader();
             while (myReader.Read())
             {
                 lbl_lastest_book.Text = (myReader["bookname"].ToString());
                 lbl_lastest_member.Text = (myReader["namefamily"].ToString());
             }
             myReader.Close();
             con.Close();
         }


         private void returns()
         {
             lbl_id.Text = dgv_borrowbooks.CurrentRow.Cells[10].Value.ToString();
             txt_code.Text = dgv_borrowbooks.CurrentRow.Cells[1].Value.ToString();
             txt_bookname.Text = dgv_borrowbooks.CurrentRow.Cells[2].Value.ToString();
             txt_category.Text = dgv_borrowbooks.CurrentRow.Cells[3].Value.ToString();
             txt_member_number.Text = dgv_borrowbooks.CurrentRow.Cells[4].Value.ToString();
             txt_namefamily.Text = dgv_borrowbooks.CurrentRow.Cells[5].Value.ToString();
             msk_start_date.Text = dgv_borrowbooks.CurrentRow.Cells[6].Value.ToString();
             msk_end_date.Text = dgv_borrowbooks.CurrentRow.Cells[7].Value.ToString();
             txt_loanperiod.Text = dgv_borrowbooks.CurrentRow.Cells[8].Value.ToString();
             msk_regdate.Text = dgv_borrowbooks.CurrentRow.Cells[9].Value.ToString();
         }


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


        //------------------------------------------//
        public void search_num()
        {
            SqlDataAdapter adp = new SqlDataAdapter("select * from borrowbooks where membernumber='" + txt_search.Text + "'",con);
            adp.Fill(ds, "t1");
            dgv_borrowbooks.DataSource = ds;
            dgv_borrowbooks.DataMember = "t1";
        }


        public void search_namefamily()
        {
            SqlDataAdapter adp = new SqlDataAdapter("select * from borrowbooks where namefamily='" + txt_search.Text + "'",con);
            adp.Fill(ds, "t1");
            dgv_borrowbooks.DataSource = ds;
            dgv_borrowbooks.DataMember = "t1";
        }


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

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

            cmd.Parameters.AddWithValue("@code", txt_code.Text);
            cmd.Parameters.AddWithValue("@bookname", txt_bookname.Text);
            cmd.Parameters.AddWithValue("@category", txt_category.Text);
            cmd.Parameters.AddWithValue("@membernumber", txt_member_number.Text);
            cmd.Parameters.AddWithValue("@namefamily", txt_namefamily.Text);
            cmd.Parameters.AddWithValue("@startdate", msk_start_date.Text);
            cmd.Parameters.AddWithValue("@enddate", msk_end_date.Text);
            cmd.Parameters.AddWithValue("@loanperiod", txt_loanperiod.Text);
            cmd.Parameters.AddWithValue("@regdate", msk_regdate.Text);

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

        private void edit()
        {
            cmd = new SqlCommand("EditBorrowBooks", con);
            cmd.CommandType = CommandType.StoredProcedure;
            cmd.Parameters.AddWithValue("@id", lbl_id.Text);
            cmd.Parameters.AddWithValue("@code", txt_code.Text);
            cmd.Parameters.AddWithValue("@bookname", txt_bookname.Text);
            cmd.Parameters.AddWithValue("@category", txt_category.Text);
            cmd.Parameters.AddWithValue("@membernumber", txt_member_number.Text);
            cmd.Parameters.AddWithValue("@namefamily", txt_namefamily.Text);
            cmd.Parameters.AddWithValue("@startdate", msk_start_date.Text);
            cmd.Parameters.AddWithValue("@enddate", msk_end_date.Text);
            cmd.Parameters.AddWithValue("@loanperiod", txt_loanperiod.Text);
            cmd.Parameters.AddWithValue("@regdate", msk_regdate.Text);

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

        public void print()
        {
            DataTable dt = new DataTable();
            dt.Columns.Add(new DataColumn("code", typeof(string)));
            dt.Columns.Add(new DataColumn("bookname", typeof(string)));
            dt.Columns.Add(new DataColumn("category", typeof(string)));
            dt.Columns.Add(new DataColumn("membernumber", typeof(string)));
            dt.Columns.Add(new DataColumn("namefamily", typeof(string)));
            dt.Columns.Add(new DataColumn("startdate", typeof(string)));
            dt.Columns.Add(new DataColumn("enddate", typeof(string)));
            dt.Columns.Add(new DataColumn("loanperiod", typeof(string)));
            dt.Columns.Add(new DataColumn("regdate", typeof(string)));


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

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


        private void btn_add_Click(object sender, EventArgs e)
        {
            if (dgv_borrowbooks.Rows.Count == 3)
            {
                MessageBox.Show("شما از نسخه دموی برنامه استفاده می کنید");
                clear();
                count();
                lastrecord();
                txt_code.Focus();
            }
            else
            {
                add();
                show();
                clear();
                count();
                lastrecord();
                txt_code.Focus();
            }
        }

        private void btn_edit_Click(object sender, EventArgs e)
        {
            edit();
            show();
            clear();
            count();
            lastrecord();
            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_code.Focus();
        }

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

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

        private void btn_namefamily_Click(object sender, EventArgs e)
        {
            memberlist ml = new memberlist();
            ml.ShowDialog();
        }

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

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

        private void msk_end_date_MaskInputRejected(object sender, MaskInputRejectedEventArgs e)
        {
            menha();
        }

        private void msk_start_date_MaskInputRejected(object sender, MaskInputRejectedEventArgs e)
        {
            menha();
        }

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

        private void msk_end_date_TextChanged(object sender, EventArgs e)
        {
            menha();
        }

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

        private void btn_search_Click(object sender, EventArgs e)
        {
            if (cmb_search.Text == "")
            {
                MessageBox.Show("لطفاً یکی از آیتم های مورد نظر را انتخاب کنید");
            }
            else
            {
                if (cmb_search.Text == "شماره عضویت")
                {
                    search_num();
                }
                if (cmb_search.Text == "نام و نام خانوادگی")
                {
                    search_namefamily();
                }
            }
        }

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

    




    }
}
