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 VideoCloop
{
    public partial class frmTahvil : Form
    {
        public frmTahvil()
        {
            InitializeComponent();
        }

        private void Udf_FillCombo()
        {
            Lsc_VideoClubDataContext db = new Lsc_VideoClubDataContext();
            var Query = from Row in db.tblCustomers 
                        select Row;
            Cmbx_Customer.DataSource = Query;
            Cmbx_Customer.ValueMember = "customerId";
            Cmbx_Customer.DisplayMember = "SName";

            var Query1 = from Row in db.tblMovies
                         where (Convert.ToBoolean(Row.AmanatBit.ToString()) == Convert.ToBoolean("false"))
                         select Row;

            Cmbx_Film.DataSource = Query1;
            Cmbx_Film.ValueMember = "FilmId";
            Cmbx_Film.DisplayMember = "Title";
        }
        private void Udf_FillGrid()
        {
            Lsc_VideoClubDataContext db = new Lsc_VideoClubDataContext();
            var Query = from Row in db.xtblTahvils 
                        select Row;
            Dg_Customer.DataSource = Query;
            Dg_Customer.Columns[0].HeaderText = "شماره تحویل";
            Dg_Customer.Columns[1].HeaderText = "تاریخ تحویل";
            Dg_Customer.Columns[2].HeaderText = "عنوان فیلم";
            Dg_Customer.Columns[3].HeaderText = "تحویل گیرنده";
            Dg_Customer.Columns[4].HeaderText = "تاریخ بازگشت";
            Dg_Customer.Columns[5].HeaderText = "مبلغ پرداختی";
            
            Dg_Customer.Columns[6].Visible = false;
            Dg_Customer.Columns[7].Visible = false;


        }

        private void frmTahvil_Load(object sender, EventArgs e)
        {
            Udf_FillCombo();
            Udf_FillGrid();
        }

        private void Btn_Insert_Click(object sender, EventArgs e)
        {
            try
            {
                if (Txt_DateTahvil.Text != "    /  /"   &&  Txt_TotalPrice.Text!="")
                {
                    Lsc_VideoClubDataContext db = new Lsc_VideoClubDataContext();
                    tblTahvil add = new tblTahvil();
                    add.DateBazgasht = Txt_Date_Bazgasht.Text;
                    add.DateTahvil = Txt_DateTahvil.Text;
                    add.FilmId = int.Parse(Cmbx_Film.SelectedValue.ToString());
                    add.PriceTotal = Txt_TotalPrice.Text;
                    add.tahvilgirandeId = int.Parse(Cmbx_Customer.SelectedValue.ToString());

                    db.tblTahvils.InsertOnSubmit(add);
                    tblMovie Update = db.tblMovies.SingleOrDefault(n => n.FilmId == int.Parse(Cmbx_Film.SelectedValue.ToString()));
                    Update.AmanatBit = true;
                    db.SubmitChanges();
                    MessageBox.Show("عملیات درج انجام شد!");
                    Udf_FillGrid();
                    Udf_FillCombo();
                    foreach (Control ctl in this.Controls)
                    {
                        if (ctl is TextBox) ctl.Enabled = true;
                        if (ctl is MaskedTextBox) ctl.Enabled = true;
                        if (ctl is Button) { ctl.Enabled = true; ctl.Visible = true; }
                        btnCancle.Visible = false;
                    }
                }
                else MessageBox.Show("لطفا اطلاعات راکامل وارد نمایید!");
            }
            catch (Exception ex)
            {
                MessageBox.Show("عملیات درج انجام نشد!");
            }
        }

        private void Dg_Customer_CellClick(object sender, DataGridViewCellEventArgs e)
        {
            try
            {
                Lsc_VideoClubDataContext db = new Lsc_VideoClubDataContext();

                Int32 selectedCellCount = Dg_Customer.GetCellCount(DataGridViewElementStates.Selected);

                if (selectedCellCount > 0)
                {
                    if (Dg_Customer.AreAllCellsSelected(true))
                    {
                        MessageBox.Show("لطفاسلول از سطر مورد نظر راانتخاب نماييد!", "Selected Cells");
                    }
                    else
                    {

                        for (int i = 0; i < selectedCellCount; i++)
                        {

                            Txt_Date_Bazgasht.Text = Dg_Customer.Rows[int.Parse(Dg_Customer.SelectedCells[i].RowIndex.ToString())].Cells[4].Value.ToString();
                            Cmbx_Customer.SelectedValue = int.Parse(Dg_Customer.Rows[int.Parse(Dg_Customer.SelectedCells[i].RowIndex.ToString())].Cells[7].Value.ToString());
                            Cmbx_Film.SelectedValue = int.Parse(Dg_Customer.Rows[int.Parse(Dg_Customer.SelectedCells[i].RowIndex.ToString())].Cells[6].Value.ToString());
                            Cmbx_Customer.DisplayMember = Dg_Customer.Rows[int.Parse(Dg_Customer.SelectedCells[i].RowIndex.ToString())].Cells[3].Value.ToString();
                            Cmbx_Film.DisplayMember = Dg_Customer.Rows[int.Parse(Dg_Customer.SelectedCells[i].RowIndex.ToString())].Cells[2].Value.ToString();
                            Txt_DateTahvil.Text = Dg_Customer.Rows[int.Parse(Dg_Customer.SelectedCells[i].RowIndex.ToString())].Cells[1].Value.ToString();
                            Txt_TotalPrice.Text = Dg_Customer.Rows[int.Parse(Dg_Customer.SelectedCells[i].RowIndex.ToString())].Cells[5].Value.ToString();

                        }
                    }
                }
            }
            catch
            { }
        }

        private void Btn_Delete_Click(object sender, EventArgs e)
        {
            try
            {
                Lsc_VideoClubDataContext db = new Lsc_VideoClubDataContext();
                tblTahvil  DelTbl = db.tblTahvils .SingleOrDefault(n => n.TahvilId  == Decimal.Parse(Dg_Customer.CurrentRow.Cells[0].Value.ToString()));

                tblMovie Upd = db.tblMovies.SingleOrDefault(n => n.FilmId == int.Parse(Dg_Customer.CurrentRow.Cells[6].Value.ToString()));
                Upd.AmanatBit = false;
                DialogResult result = MessageBox.Show("آیا شما می خواهیدتحویل باشماره" + DelTbl.TahvilId  + "راحذف نمایید", "اخطار", MessageBoxButtons.OKCancel, MessageBoxIcon.Warning, MessageBoxDefaultButton.Button1);
                if (result == DialogResult.OK)
                {
                    db.tblTahvils .DeleteOnSubmit(DelTbl);
                    db.SubmitChanges();
                    MessageBox.Show("عملیات  حذف انجام شد!");
                }
                Udf_FillGrid();
                Udf_FillCombo();
            }
            catch
            {
                MessageBox.Show("اشکالی در حذف رکورد پیش آمده است");
            }
        }

        private void Btn_Update_Click(object sender, EventArgs e)
        {
            try
            {
                Lsc_VideoClubDataContext db = new Lsc_VideoClubDataContext();
                tblTahvil  UpdateTbl = db.tblTahvils .SingleOrDefault (n => n.TahvilId  == int.Parse(Dg_Customer.CurrentRow.Cells[0].Value.ToString()));
                UpdateTbl.DateBazgasht  = Txt_Date_Bazgasht .Text;
                UpdateTbl.DateTahvil  = Txt_DateTahvil .Text;
                if (Cmbx_Film.SelectedValue != null)
                {
                    UpdateTbl.FilmId = int.Parse(Cmbx_Film.SelectedValue.ToString());
                }
                else
                {
                    UpdateTbl.FilmId = int.Parse(Dg_Customer.CurrentRow.Cells[6].Value.ToString());
                }
                UpdateTbl.PriceTotal  = Txt_TotalPrice .Text;
                UpdateTbl.tahvilgirandeId  = int.Parse(Cmbx_Customer .SelectedValue.ToString());
                if (Txt_Date_Bazgasht .Text != "    /  /")
                {
                    if (Cmbx_Film.SelectedValue != null)
                    {
                        tblMovie Upd = db.tblMovies.SingleOrDefault(n => n.FilmId == int.Parse(Cmbx_Film.SelectedValue.ToString()));
                        Upd.AmanatBit = false;
                    }
                    else
                    {
                        tblMovie Upd = db.tblMovies.SingleOrDefault(n => n.FilmId == int.Parse(Dg_Customer.CurrentRow.Cells[6].Value.ToString()));
                        Upd.AmanatBit = false;
                    }
                }
               
                db.SubmitChanges();
                MessageBox.Show("رکورد مورد نظر ویرایش شد");
                Udf_FillGrid();
                Udf_FillCombo();

            }
            catch (Exception ex)
            {
                MessageBox.Show("اشکالی در ویرایش رکورد پیش آمده است");
            }
        }

        private void btnAdd_Click(object sender, EventArgs e)
        {
            foreach (Control ctl in this.Controls)
            {
                if (ctl is TextBox) { ctl.Enabled = false; ctl.Text = ""; }
                if (ctl is Button) ctl.Enabled = false;
                if (ctl is MaskedTextBox) ctl.Enabled = false;
                if (ctl is ComboBox) ctl.Enabled = false;
            }
            DialogResult result = MessageBox.Show("آیا شما می خواهیدعمل درج راانجام دهید", "اخطار", MessageBoxButtons.OKCancel, MessageBoxIcon.Warning, MessageBoxDefaultButton.Button1);
            if (result == DialogResult.OK)
            {
                foreach (Control ctl in this.Controls)
                {
                    if (ctl is TextBox) ctl.Enabled = true;
                    if (ctl is MaskedTextBox) ctl.Enabled = true;
                    if (ctl is Button) ctl.Enabled = true;
                    if (ctl is ComboBox) ctl.Enabled = true;
                    btnCancle.Visible = true;
                    btnAdd.Visible = false;
                    Btn_Delete.Visible = false;
                    Btn_Update.Visible = false;
                }
            }
        }

        private void btnCancle_Click(object sender, EventArgs e)
        {
            foreach (Control ctl in this.Controls)
            {
                if (ctl is TextBox) ctl.Enabled = true;
                if (ctl is MaskedTextBox) ctl.Enabled = true;
                if (ctl is ComboBox) ctl.Enabled = true;
                if (ctl is Button) { ctl.Enabled = true; ctl.Visible = true; }
                btnCancle.Visible = false;
            }
        }
    }
}
