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

        SqlDataAdapter adp = new SqlDataAdapter();
        DataTable dt = new DataTable();
        DataSet ds = new DataSet();
        MyDate md = new MyDate();

        //---------------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_price()
        {
            SqlDataAdapter adp = new SqlDataAdapter("select * from book where price='" +  txt_search_price.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 ----------------------------------------//

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

        private void clear()
        {
            txt_search_copies_available.Clear();
            msk_onedate.Text = "";
            msk_date1.Text = "";
            msk_date2.Text = "";
            txt_serach_barcode.Clear();
        }

        private void booklist_Load(object sender, EventArgs e)
        {
            show();
            clear();
        }

        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 btn_filtering_ValueChanged(object sender, EventArgs e)
        {
            if (btn_filtering.Value == Enabled)
            {
                dgv_books.EnableFiltering = true;
            }
            else
            {
                dgv_books.EnableFiltering = false;
            }
        }

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

        private void brn_show_Click(object sender, EventArgs e)
        {
            show();
            clear();
        }

        private void btn_search_price_Click(object sender, EventArgs e)
        {
            search_price();
        }

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

            catch (Exception)
            {

            }
        }
    }
}
