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 Entity_FrameWork
{
    public partial class DataBuy : Form
    {
        int CodBuy;
        public DataBuy(int _CodBuy)
        {
            InitializeComponent();
            CodBuy = _CodBuy;
            this.MinimumSize = new System.Drawing.Size(762, 440);
        }
        Entity_FrameWorkEntities5 MyModel = new Entity_FrameWorkEntities5();
        private void DataBuy_Load(object sender, EventArgs e)
        {
            dataGridViewDataBuy.DataSource = MyModel.Sp_SelectListKharid(CodBuy).ToList();
            dataGridViewDataBuy.Columns[0].HeaderText = "كد خريد";
            dataGridViewDataBuy.Columns[1].HeaderText = "كد كالا";
            dataGridViewDataBuy.Columns[2].HeaderText = "نام كالا";
            dataGridViewDataBuy.Columns[3].HeaderText = "مدل";
            dataGridViewDataBuy.Columns[4].HeaderText = "رنگ";
            dataGridViewDataBuy.Columns[5].HeaderText = "تعداد";
            dataGridViewDataBuy.Columns[6].HeaderText = "قيمت";
        }

        private void dataGridViewDataBuy_RowHeaderMouseClick(object sender, DataGridViewCellMouseEventArgs e)
        {
            textBoxCodKala.Text = dataGridViewDataBuy.CurrentRow.Cells[1].Value.ToString();
            textBoxNameKala.Text = dataGridViewDataBuy.CurrentRow.Cells[2].Value.ToString();
            textBoxModel.Text = dataGridViewDataBuy.CurrentRow.Cells[3].Value.ToString();
            textBoxColor.Text = dataGridViewDataBuy.CurrentRow.Cells[4].Value.ToString();
            textBoxTedad.Text = dataGridViewDataBuy.CurrentRow.Cells[5].Value.ToString();
            textBoxMoney.Text = dataGridViewDataBuy.CurrentRow.Cells[6].Value.ToString();
        }

        private void button_Insert_Click(object sender, EventArgs e)
        {
            MyModel.Sp_InsertDataBuy(CodBuy,Convert.ToInt32(textBoxCodKala.Text),textBoxNameKala.Text,textBoxModel.Text,textBoxColor.Text,textBoxTedad.Text,Convert.ToInt32(textBoxMoney.Text));
            FMessegeBox.FarsiMessegeBox.Show("ثبت شد");
            dataGridViewDataBuy.DataSource = MyModel.Sp_SelectListKharid(CodBuy).ToList();

        }
        int rowindex;
        private void button_update_Click(object sender, EventArgs e)
        {
            rowindex = dataGridViewDataBuy.CurrentRow.Cells[1].RowIndex;
            MyModel.Sp_UpdateDateBuy(Convert.ToInt32(dataGridViewDataBuy.CurrentRow.Cells[1].Value.ToString()),textBoxNameKala.Text, textBoxModel.Text, textBoxColor.Text, textBoxTedad.Text, Convert.ToInt32(textBoxMoney.Text));
            dataGridViewDataBuy.DataSource = MyModel.Sp_SelectListKharid(CodBuy).ToList();
            dataGridViewDataBuy.ClearSelection();
            dataGridViewDataBuy.Rows[rowindex].Selected = true;
            FMessegeBox.FarsiMessegeBox.Show("ويرايش شد");
           

        }

        private void button_Delete_Click(object sender, EventArgs e)
        {
            MyModel.Sp_DeleteDateBuy(Convert.ToInt32(dataGridViewDataBuy.CurrentRow.Cells[1].Value.ToString()));
            FMessegeBox.FarsiMessegeBox.Show("حذف شد");
            dataGridViewDataBuy.DataSource = MyModel.Sp_SelectListKharid(CodBuy).ToList();
        }

        private void button_ListKharid_Click(object sender, EventArgs e)
        {
            stiReportListKharid.Load(Application.StartupPath + @"\ReportListKharid.mrt");
            spSelectListKharidResultBindingSource.DataSource = MyModel.Sp_SelectListKharid(CodBuy).ToList();
            stiReportListKharid.RegReportDataSources();
            stiReportListKharid.Show();
        }

        private void button_TransKala_Click(object sender, EventArgs e)
        {
            Kala kala = new Kala(Convert.ToInt32(dataGridViewDataBuy.CurrentRow.Cells[0].Value.ToString()));
            kala.Show();
        }

        private void textBoxSearch_TextChanged(object sender, EventArgs e)
        {
            dataGridViewDataBuy.DataSource=MyModel.Sp_SearchListKharid(textBoxSearch.Text,CodBuy);
        }

        private void buttonSearch_Click(object sender, EventArgs e)
        {
            dataGridViewDataBuy.DataSource = MyModel.Sp_SearchListKharid(textBoxSearch.Text, CodBuy);
        }
    }
}
