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

        SqlCommand cmd = new SqlCommand();
        SqlDataAdapter adp = new SqlDataAdapter();
        DataSet ds = new DataSet();
        DataTable dt = new DataTable();

        private void clear()
        {
            txt_code.Clear();
            txt_number_days.Clear();
            txt_amount_fine.Clear();
        }

        private void returns()
        {                                              
            txt_code.Text = dgv_fine.CurrentRow.Cells[0].Value.ToString();
            txt_number_days.Text = dgv_fine.CurrentRow.Cells[1].Value.ToString();
            txt_amount_fine.Text = dgv_fine.CurrentRow.Cells[2].Value.ToString();
            lbl_id.Text = dgv_fine.CurrentRow.Cells[3].Value.ToString();
        }

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

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

            cmd.Parameters.AddWithValue("@code", txt_code.Text);
            cmd.Parameters.AddWithValue("@numberdays", txt_number_days.Text);
            cmd.Parameters.AddWithValue("@amountfine", txt_amount_fine.Text);

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


        private void edit()
        {
            cmd = new SqlCommand("EditFineBooks", con);
            cmd.CommandType = CommandType.StoredProcedure;
            cmd.Parameters.AddWithValue("@id", lbl_id.Text);
            cmd.Parameters.AddWithValue("@code", txt_code.Text);
            cmd.Parameters.AddWithValue("@numberdays", txt_number_days.Text);
            cmd.Parameters.AddWithValue("@amountfine", txt_amount_fine.Text);

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

        private void delete()
        {
            cmd = new SqlCommand("DeleteFineBooks", con);
            cmd.CommandType = CommandType.StoredProcedure;
            cmd.Parameters.AddWithValue("@code", dgv_fine.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 (dgv_fine.Rows.Count == 3)
            {
                MessageBox.Show("شما از نسخه دموی برنامه استفاده می کنید");
                show();
                clear();
                txt_code.Focus();
            }
            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 finebooks_Load(object sender, EventArgs e)
        {
            show();
            InputLanguage.CurrentInputLanguage = InputLanguage.FromCulture(System.Globalization.CultureInfo.CreateSpecificCulture("fa-IR"));
        }

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

            catch (Exception)
            {

            }
        }

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

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

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

    }
}
