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;

namespace Library
{    
    public partial class FormBorrow : Form
    {
        DataAccess da = new DataAccess();
        public FormBorrow()
        {
            InitializeComponent();
        }

        private void button4_Click(object sender, EventArgs e)
        {
            FormMain fe = new FormMain();
            this.Hide();
            fe.Show();
        }

        private void button2_Click(object sender, EventArgs e)
        {
            groupBox1.Enabled = true;
            da.Connect();
            string sql = "select * from category";
            DataTable dt = new DataTable();
            dt = da.Select(sql);
            da.DisConnect();
            cbcategory.DisplayMember = "catname";
            cbcategory.ValueMember = "catid";
            cbcategory.DataSource = dt;
        }

        private void textBox1_MouseDown(object sender, MouseEventArgs e)
        {
            txtsearch.Text = "";
        }

        private void button1_Click(object sender, EventArgs e)
        {
            if (txtsearch.Text == "کد کاربر ...")
                return;
            if (txtsearch.Text == "")
            {
                linkback.Enabled = false;
                linkget.Enabled = false;
                MessageBox.Show("فیلد جستجو را پر نمائید");
                return;
            }
            da.Connect();
            string sql = "select * from member where muser=N'{0}'";
            sql = string.Format(sql,txtsearch.Text);
            DataTable dt = new DataTable();
            dt = da.Select(sql);            
            if (dt.Rows.Count == 0)
            {
                linkback.Enabled = false;
                linkget.Enabled = false;
                MessageBox.Show("چنین کاربری وجود ندارد");
                da.DisConnect();
                return;
            }
            else
            {
                if (dt.Rows[0]["mpic"].ToString() != "")
                    pictureBox1.Load(dt.Rows[0]["mpic"].ToString());
                sql = "select * from borrow where muser_fk=N'{0}'";
                sql = string.Format(sql,txtsearch.Text);
                dt = da.Select(sql);                                
                dataGridView1.DataSource = dt;
                linkback.Enabled = true;
                linkget.Enabled = true;
            }
            da.DisConnect();
        }

        private void FormBorrow_Load(object sender, EventArgs e)
        {
            dataGridView1.AutoGenerateColumns = false;
            da.Connect();
            DataTable dt = new DataTable();
            dt = da.Select("select * from borrow");
            da.DisConnect();
            dataGridView1.DataSource = dt;
        }

        private void cbcategory_SelectedIndexChanged(object sender, EventArgs e)
        {
            da.Connect();
            string sql = "select * from book where catid_fk={0}";
            sql = string.Format(sql,cbcategory.SelectedValue);
            DataTable dt = new DataTable();
            dt = da.Select(sql);
            if (dt.Rows.Count == 0)
            {
                cbbook.Text = "";
            }
            cbbook.DisplayMember = "bname";
            cbbook.ValueMember = "bid";
            cbbook.DataSource = dt;
            da.DisConnect();
        }

        private void button5_Click(object sender, EventArgs e)
        {
            if (txtsearch.Text == "")
            {
                MessageBox.Show("لطفا فیلد جستجو را پر نمائید");
                return;
            }
            da.Connect();
            string sql = "insert into borrow (muser_fk,bid_fk,gettime,backtime,state)";
            sql += "values(N'{0}',{1},N'{2}',N'{3}',{4})";
            sql = string.Format(sql,txtsearch.Text,cbbook.SelectedValue,txtgettime.Text,txtbacktime.Text,0);
            da.DoCommand(sql);
            da.DisConnect();
            SearchBorrow(txtsearch.Text);
        }
        public void SearchBorrow(string id)
        {
            da.Connect();
            string sql = "select * from borrow where muser_fk = N'{0}'";
            sql = string.Format(sql,id);
            DataTable dt = new DataTable();
            dt = da.Select(sql);
            dataGridView1.DataSource = dt;
            da.DisConnect();
        }

        private void button3_Click(object sender, EventArgs e)
        {
            if (txtsearch.Text == "")
            {
                MessageBox.Show("لطفا فیلد جستجو را پر نمائید");
                return;
            }
            DialogResult dr = MessageBox.Show("آیا مطمئن به حذف رکورد جاری می باشید ؟","",MessageBoxButtons.YesNo,MessageBoxIcon.Question);
            if (dr == DialogResult.Yes)
            {
                da.Connect();
                string index = dataGridView1.CurrentRow.Cells["id"].Value.ToString();
                string sql = "delete from borrow where brid={0}";
                sql = string.Format(sql, index);
                da.DoCommand(sql);
                da.DisConnect();
                SearchBorrow(txtsearch.Text);
            }
        }

        private void FormBorrow_DoubleClick(object sender, EventArgs e)
        {

        }


        private void dataGridView1_DoubleClick(object sender, EventArgs e)
        {
            if (txtsearch.Text == "")
            {
                MessageBox.Show("لطفا فیلد جستجو را پر نمائید");
                return;
            }
            DialogResult dr = MessageBox.Show("آیا مطمئن به برگشت کتاب می باشید ؟","",MessageBoxButtons.YesNo,MessageBoxIcon.Question);
            if (dr == DialogResult.Yes)
            {
                da.Connect();
                string index = dataGridView1.CurrentRow.Cells["id"].Value.ToString();
                string sql = "select * from borrow where brid={0}";
                sql = string.Format(sql,index);
                DataTable dt = new DataTable();
                dt = da.Select(sql);
                if (dt.Rows[0]["state"].ToString() == "False")
                {
                    sql = "update borrow set state={0} where brid={1}";
                    sql = string.Format(sql, 1, index);
                    da.DoCommand(sql);
                    da.DisConnect();
                    SearchBorrow(txtsearch.Text);
                }
                da.DisConnect();
            }
        }

        private void linkback_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
        {
            if (txtsearch.Text == "")
            {
                linkback.Enabled = false;
                linkget.Enabled = false;
                MessageBox.Show("فیلد جستجو را پر نمائید");
                return;
            }
            da.Connect();
            string index = dataGridView1.CurrentRow.Cells["muser"].Value.ToString();
            string sql = "select * from borrow where muser_fk={0} and state={1}";
            sql = string.Format(sql, index, 1);
            DataTable dt = new DataTable();
            dt = da.Select(sql);
            dataGridView1.DataSource=dt;
            da.DisConnect();
        }

        private void linkget_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
        {
            if (txtsearch.Text == "")
            {
                linkback.Enabled = false;
                linkget.Enabled = false;
                MessageBox.Show("فیلد جستجو را پر نمائید");
                return;
            }
            da.Connect();
            string index = dataGridView1.CurrentRow.Cells["muser"].Value.ToString();
            string sql = "select * from borrow where muser_fk={0} and state={1}";
            sql = string.Format(sql, index, 0);
            DataTable dt = new DataTable();
            dt = da.Select(sql);
            dataGridView1.DataSource=dt;
            da.DisConnect();
        }
    }
}
