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.Globalization;

namespace Entity_FrameWork
{
    public partial class Comment : Form
    {
        public Comment()
        {
            InitializeComponent();
            this.MinimumSize = new System.Drawing.Size(727, 520);
        }
        Entity_FrameWorkEntities5 MyModel = new Entity_FrameWorkEntities5();
        private void ribbonClientPanel1_Click(object sender, EventArgs e)
        {

        }

        private void button_Insert_Click(object sender, EventArgs e)
        {
            using (Entity_FrameWorkEntities5 MyModel = new Entity_FrameWorkEntities5())
            {
                Table_Comment Comment = new Table_Comment()
                {
                     DateComment=textBox_Date.Text,
                    Comment=richTextBox_Comment.Text,

                   
                };
                MyModel.AddToTable_Comment(Comment);
                MyModel.SaveChanges();
                FMessegeBox.FarsiMessegeBox.Show("ثبت شد");
                dataGridViewComment.DataSource = MyModel.Table_Comment.ToList();

            }
        }

        private void button_Delete_Click(object sender, EventArgs e)
        {
            int rowData = Convert.ToInt32(dataGridViewComment.CurrentRow.Cells[0].Value.ToString());
            Table_Comment comment = new Table_Comment();
            comment=MyModel.Table_Comment.First(row=>row.id==rowData);
            MyModel.DeleteObject(comment);
            MyModel.SaveChanges();
            FMessegeBox.FarsiMessegeBox.Show("حذف شد");
            dataGridViewComment.DataSource = MyModel.Table_Comment.ToList();
        }

        private void button_Update_Click(object sender, EventArgs e)
        {
            int rowData = Convert.ToInt32(dataGridViewComment.CurrentRow.Cells[0].Value.ToString());
            Table_Comment comment = new Table_Comment();
            comment = MyModel.Table_Comment.First(row => row.id == rowData);
            comment.DateComment = textBox_Date.Text;
            comment.Comment = richTextBox_Comment.Text;
            
            MyModel.SaveChanges();
            FMessegeBox.FarsiMessegeBox.Show("ويرايش شد");

            dataGridViewComment.DataSource = MyModel.Table_Comment.ToList();

        }

        private void Comment_Load(object sender, EventArgs e)
        {
            
           // System.Globalization.PersianCalendar pc = new System.Globalization.PersianCalendar();
            // string Date = pc.GetYear(DateTime.Now) + "/" + "0"+pc.GetMonth(DateTime.Now) + "/" + pc.GetDayOfMonth(DateTime.Now);
            // textBoxX1.Text = Date;
            dataGridViewComment.DataSource = MyModel.Sp_SelectComment().ToList();
          


            dataGridViewComment.Columns[0].HeaderText = "كدسيستمي";
            dataGridViewComment.Columns[1].HeaderText = "تاريخ پيام";
            dataGridViewComment.Columns[2].HeaderText = "توضيحات";
           textBox_Date.Text= myfunction.convertDate(DateTime.Now.ToShortDateString(),myfunction.dataConvertMode.toPersian);
        }

        private void dataGridViewComment_MouseDoubleClick(object sender, MouseEventArgs e)
        {
           textBox_Date.Text= dataGridViewComment.CurrentRow.Cells[1].Value.ToString();
            richTextBox_Comment.Text=dataGridViewComment.CurrentRow.Cells[2].Value.ToString();
        }

        private void buttonPintComment_Click(object sender, EventArgs e)
        {
            PrintComment.Load(Application.StartupPath + @"\ReportPrintComment.mrt");
            tableCommentBindingSource.DataSource = MyModel.Table_Comment.ToList();
            PrintComment.RegReportDataSources();
            PrintComment.Show();
        }

        private void dataGridViewComment_UserDeletingRow(object sender, DataGridViewRowCancelEventArgs e)
        {
            FMessegeBox.FarsiMessegeBox.Show("حذف");
        }
    }
}
