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 frmMovie : Form
    {
        public frmMovie()
        {
            InitializeComponent();
        }

        private void frmMovie_Load(object sender, EventArgs e)
        {
            Udf_FillCombo();
            Udf_FillGrid();
        }
        private void Udf_FillCombo()
        {
            Lsc_VideoClubDataContext db = new Lsc_VideoClubDataContext();
            var Query = from Row in db.tblTypeProducts 
                        select Row;
            Cmbx_TypeProduct.DataSource = Query;
            Cmbx_TypeProduct.ValueMember = "TypeId";
            Cmbx_TypeProduct.DisplayMember ="Type" ;
 
           
            var Query1 = from Row in db.TypeMovies 
                        select Row;

            Cmbx_TypeFilm.DataSource = Query1;
            Cmbx_TypeFilm.ValueMember = "TypeId";
            Cmbx_TypeFilm.DisplayMember = "TypeFilm";
        }
        private void Udf_FillGrid()
        {
            Lsc_VideoClubDataContext db = new Lsc_VideoClubDataContext();
            var Query = from Row in db.xtblMovies 
                        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[6].HeaderText = "کارگردان";
            Dg_Customer.Columns[7].HeaderText = "نویسنده";
            Dg_Customer.Columns[8].HeaderText = "سال تولید";
            Dg_Customer.Columns[9].HeaderText = "مدت زمان";
            Dg_Customer.Columns[4].Visible  = false ;
            Dg_Customer.Columns[5].Visible  = false ;


        }

        private void Btn_Insert_Click(object sender, EventArgs e)
        {
            try
            {
                if (Txt_Kargardan.Text != "" && Txt_DateProduce.Text != "" && Txt_Time.Text != "" && Txt_Name.Text != "" && Txt_Writer.Text != "")
                {
                    Lsc_VideoClubDataContext db = new Lsc_VideoClubDataContext();
                    tblMovie add = new tblMovie();
                    add.Kargardan = Txt_Kargardan.Text;
                    add.SaleTolid = Txt_DateProduce.Text;
                    add.Time = Txt_Time.Text;
                    add.Title = Txt_Name.Text;
                    add.TypeFilmId = int.Parse(Cmbx_TypeFilm.SelectedValue.ToString());
                    add.TypeProductId = int.Parse(Cmbx_TypeProduct.SelectedValue.ToString());
                    add.writer = Txt_Writer.Text;
                    add.AmanatBit = false;
                    db.tblMovies.InsertOnSubmit(add);
                    db.SubmitChanges();
                    MessageBox.Show("عملیات درج انجام شد!");
                    Udf_FillGrid();
                    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)
        {
            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_Name.Text = Dg_Customer.Rows[int.Parse(Dg_Customer.SelectedCells[i].RowIndex.ToString())].Cells[1].Value.ToString();
                        Cmbx_TypeFilm.SelectedValue   =int.Parse( Dg_Customer.Rows[int.Parse(Dg_Customer.SelectedCells[i].RowIndex.ToString())].Cells[4].Value.ToString());
                        Cmbx_TypeProduct.SelectedValue   =int.Parse( Dg_Customer.Rows[int.Parse(Dg_Customer.SelectedCells[i].RowIndex.ToString())].Cells[5].Value.ToString());
                        Cmbx_TypeFilm.DisplayMember  = Dg_Customer.Rows[int.Parse(Dg_Customer.SelectedCells[i].RowIndex.ToString())].Cells[2].Value.ToString();
                        Cmbx_TypeProduct.DisplayMember  = Dg_Customer.Rows[int.Parse(Dg_Customer.SelectedCells[i].RowIndex.ToString())].Cells[3].Value.ToString();
                        Txt_Kargardan.Text = Dg_Customer.Rows[int.Parse(Dg_Customer.SelectedCells[i].RowIndex.ToString())].Cells[6].Value.ToString();
                        Txt_Writer.Text = Dg_Customer.Rows[int.Parse(Dg_Customer.SelectedCells[i].RowIndex.ToString())].Cells[7].Value.ToString();
                        Txt_DateProduce .Text = Dg_Customer.Rows[int.Parse(Dg_Customer.SelectedCells[i].RowIndex.ToString())].Cells[8].Value.ToString();
                        Txt_Time.Text = Dg_Customer.Rows[int.Parse(Dg_Customer.SelectedCells[i].RowIndex.ToString())].Cells[9].Value.ToString();

                    }
                }
            }
        }

        private void Btn_Delete_Click(object sender, EventArgs e)
        {
            try
            {
                Lsc_VideoClubDataContext db = new Lsc_VideoClubDataContext();
                tblMovie DelTbl = db.tblMovies .SingleOrDefault(n => n.FilmId  == Decimal.Parse(Dg_Customer.CurrentRow.Cells[0].Value.ToString()));
                DialogResult result = MessageBox.Show("آیا شما می خواهیدفیلم باشماره" + DelTbl.FilmId  + "راحذف نمایید", "اخطار", MessageBoxButtons.OKCancel, MessageBoxIcon.Warning, MessageBoxDefaultButton.Button1);
                if (result == DialogResult.OK)
                {
                    db.tblMovies .DeleteOnSubmit(DelTbl);
                    db.SubmitChanges();
                    MessageBox.Show("عملیات  حذف انجام شد!");
                }
                Udf_FillGrid();
            }
            catch
            {
                MessageBox.Show("اشکالی در حذف رکورد پیش آمده است");
            }
        }

        private void Btn_Update_Click(object sender, EventArgs e)
        {
            try
            {
                Lsc_VideoClubDataContext db = new Lsc_VideoClubDataContext();
                tblMovie UpdateTbl = db.tblMovies .Single(n => n.FilmId  == int.Parse(Dg_Customer.CurrentRow.Cells[0].Value.ToString()));
                UpdateTbl.Kargardan  = Txt_Kargardan.Text;
                UpdateTbl.SaleTolid = Txt_DateProduce.Text;
                UpdateTbl.Time  = Txt_Time.Text;
                UpdateTbl.Title  = Txt_Name.Text;
                UpdateTbl.TypeFilmId =int.Parse( Cmbx_TypeFilm.SelectedValue .ToString());
                UpdateTbl.TypeProductId = int.Parse(Cmbx_TypeProduct.SelectedValue.ToString()); 
                UpdateTbl.writer  = Txt_Writer.Text;
               
                
                db.SubmitChanges();
                MessageBox.Show("رکورد مورد نظر ویرایش شد");
                Udf_FillGrid();

            }
            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;
            }
        }
    }
}
