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

        

        public void GetBookBorrowName(string name, string num, string namefamily, string sdate, string edate, string loanperiod)
        {
            txt_bookname.Text = name;
            txt_member_number.Text = num;
            txt_namefamily.Text = namefamily;
            txt_startdate.Text = sdate;
            txt_enddate.Text = edate;
            txt_loanperiod.Text = loanperiod;
        }

        private void clear()
        {
            txt_code.Clear();
            txt_bookname.Clear();
            txt_member_number.Clear();
            txt_namefamily.Clear();
            txt_startdate.Clear();
            txt_enddate.Clear();
            txt_loanperiod.Clear();
            txt_daysfine.Clear();
            txt_amountfine.Clear();
            msk_regdate.Text = "";
        }

        private void returns()
        {
           txt_code.Text = dgv_backbook.CurrentRow.Cells[0].Value.ToString();
           txt_bookname.Text = dgv_backbook.CurrentRow.Cells[1].Value.ToString();
           txt_member_number.Text = dgv_backbook.CurrentRow.Cells[2].Value.ToString();
           txt_namefamily.Text = dgv_backbook.CurrentRow.Cells[3].Value.ToString();
           txt_startdate.Text = dgv_backbook.CurrentRow.Cells[4].Value.ToString();
           txt_enddate.Text = dgv_backbook.CurrentRow.Cells[5].Value.ToString();
           txt_loanperiod.Text = dgv_backbook.CurrentRow.Cells[6].Value.ToString();
           txt_daysfine.Text = dgv_backbook.CurrentRow.Cells[7].Value.ToString();
           txt_amountfine.Text = dgv_backbook.CurrentRow.Cells[8].Value.ToString();
           msk_regdate.Text = dgv_backbook.CurrentRow.Cells[9].Value.ToString();
           lbl_id.Text = dgv_backbook.CurrentRow.Cells[10].Value.ToString();
        }

        private void getdebit()
        {
            con.Open();
            SqlDataReader myReader1 = null;
            SqlCommand myCommand2 = new SqlCommand("select * from members where membernumber='" + txt_member_number.Text + "'", con);
            myReader1 = myCommand2.ExecuteReader();
            while (myReader1.Read())
            {
                lbl_debit.Text = (myReader1["debit"].ToString());
            }
            myReader1.Close();
            con.Close();
        }

        private void adddebit()
        {
            if (txt_amountfine.Text == "0")
            {
                
            }

            else
            {
                try
                {

                    decimal debit;
                    decimal amountfine;
                    debit = Convert.ToDecimal(lbl_debit.Text);
                    amountfine = Convert.ToDecimal(txt_amountfine.Text);
                    decimal b = debit + amountfine;
                    //------------------------------------------------------------------------------------------------//
                    string updatedebit = "update members set debit='" + b + "' where membernumber='" + txt_member_number.Text + "'";
                    SqlCommand com = new SqlCommand(updatedebit, con);
                    con.Open();
                    com.ExecuteNonQuery();
                    con.Close();
                    MessageBox.Show("بدهی مورد نظر با موفقیت به بدهی عضواضافه شد.");
                    show();
                }

                catch (Exception)
                {
                    MessageBox.Show("خطا در بروزسانی بدهی عضو");
                }

            }
           

        }

        private void Getfine()
        {
            try
            {
                DateTime enddate = DateTime.Parse(txt_enddate.Text);
                DateTime datenow = DateTime.Parse(lbl_datenow.Text);
                DateTime end = pc.ToDateTime(enddate.Year, enddate.Month, enddate.Day, enddate.Hour, enddate.Minute, enddate.Second, enddate.Millisecond);
                DateTime now = pc.ToDateTime(datenow.Year, datenow.Month, datenow.Day, datenow.Hour, datenow.Minute, datenow.Second, datenow.Millisecond);

                if (now > end)
                {
                    try
                    {
                        DateTime dt = DateTime.Parse(lbl_datenow.Text);
                        DateTime dt2 = DateTime.Parse(txt_enddate.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 = date1.Subtract(date2);
                        txt_daysfine.Text = ts.Days.ToString();
                    }
                    catch (Exception)
                    {

                    }
                    con.Open();
                    SqlDataReader myReader = null;
                    SqlCommand myCommand = new SqlCommand("select * from finebooks where numberdays='" + txt_daysfine.Text + "'", con);
                    myReader = myCommand.ExecuteReader();
                    while (myReader.Read())
                    {
                        txt_amountfine.Text = (myReader["amountfine"].ToString());
                    }
                    myReader.Close();
                    con.Close();
                }
                else if (end < now)
                {
                    txt_daysfine.Text = "0";
                    txt_amountfine.Text = "0";
                }
                else
                {
                    txt_daysfine.Text = "0";
                    txt_amountfine.Text = "0";
                }
            }

            catch (Exception)
            {
 
            }
        }


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

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

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


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


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

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

            cmd.Parameters.AddWithValue("@code", txt_code.Text);
            cmd.Parameters.AddWithValue("@bookname", txt_bookname.Text);
            cmd.Parameters.AddWithValue("@membernumber", txt_member_number.Text);
            cmd.Parameters.AddWithValue("@namefamily", txt_namefamily.Text);
            cmd.Parameters.AddWithValue("@startdate", txt_startdate.Text);
            cmd.Parameters.AddWithValue("@enddate", txt_enddate.Text);
            cmd.Parameters.AddWithValue("@loanperiod", txt_loanperiod.Text);
            cmd.Parameters.AddWithValue("@daysfine", txt_daysfine.Text);
            cmd.Parameters.AddWithValue("@amountfine", txt_amountfine.Text);
            cmd.Parameters.AddWithValue("@regdate", msk_regdate.Text);

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

        private void edit()
        {
            cmd = new SqlCommand("EditBackBook", 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("@membernumber", txt_member_number.Text);
            cmd.Parameters.AddWithValue("@namefamily", txt_namefamily.Text);
            cmd.Parameters.AddWithValue("@startdate", txt_startdate.Text);
            cmd.Parameters.AddWithValue("@enddate", txt_enddate.Text);
            cmd.Parameters.AddWithValue("@loanperiod", txt_loanperiod.Text);
            cmd.Parameters.AddWithValue("@daysfine", txt_daysfine.Text);
            cmd.Parameters.AddWithValue("@amountfine", txt_amountfine.Text);
            cmd.Parameters.AddWithValue("@regdate", msk_regdate.Text);

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

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


        private void btn_namefamily_Click(object sender, EventArgs e)
        {
            borrowbookslist bbl = new borrowbookslist();
            bbl.ShowDialog();
            Getfine();
            getdebit();
        }

        private void txt_daysfine_TextChanged(object sender, EventArgs e)
        {
            Getfine();
        }

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

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

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

        private void btn_delete_Click(object sender, EventArgs e)
        {
            delete();
            show();
            clear();
            count();
            Getfine();
            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 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 backbook_Load(object sender, EventArgs e)
        {
            lbl_datenow.Text = md.alldate();
            count();
            show();
            InputLanguage.CurrentInputLanguage = InputLanguage.FromCulture(System.Globalization.CultureInfo.CreateSpecificCulture("fa-IR"));
        }

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

      
    }
}
