using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Data.SqlClient;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace personal_info
{
    public partial class addinfo : Form
    {
        public addinfo(DataTable dataTable,int rowIndex,string operation)
        {
            InitializeComponent();
            dt = dataTable;
            _rowIndex = rowIndex;
            _operation = operation;
            if(operation=="ویرایش")
            {
                txtCode.Text = dt.Rows[rowIndex][0].ToString();
                txtFirsName.Text = dt.Rows[rowIndex][1].ToString();
                txtLastName.Text = dt.Rows[rowIndex][2].ToString();
                txtMobile.Text = dt.Rows[rowIndex][4].ToString();
                txtPhone.Text = dt.Rows[rowIndex][3].ToString();
                mskRegdate.Text= dt.Rows[rowIndex][5].ToString();
                txtCode.Enabled=false;
            }
        }

        int _rowIndex;
            string _operation;
        DataTable dt = new DataTable();
        SqlConnection con = new SqlConnection("Data Source=.;Initial Catalog=DBTest;Integrated Security=True");
        private void btn_edit_Click(object sender, EventArgs e)
        {
            if (_operation == "جدید")
            {
                SqlCommand cmd = new SqlCommand();
                cmd.CommandText = "insert into tblPerson  (CodeMelli,FirstName,LastName,Phone,Mobile,DateOfSabt) values (@CodeMelli,@FirstName,@LastName,@Phone,@Mobile,@DateOfSabt)";
                cmd.Connection = con;
                cmd.Parameters.Add("@CodeMelli", SqlDbType.NVarChar).Value = txtCode.Text; ;
                cmd.Parameters.Add("@FirstName", SqlDbType.NVarChar).Value = txtFirsName.Text;
                cmd.Parameters.Add("@LastName", SqlDbType.NVarChar).Value = txtLastName.Text;
                cmd.Parameters.Add("@Phone", SqlDbType.NVarChar).Value = txtPhone.Text;
                cmd.Parameters.Add("@Mobile", SqlDbType.NVarChar).Value = txtMobile.Text;
                cmd.Parameters.Add("@DateOfSabt", SqlDbType.NVarChar).Value = mskRegdate.Text;
                con.Open();
                cmd.ExecuteNonQuery();
                con.Close();
                dt.Rows.Add(txtCode.Text, txtFirsName.Text, txtLastName.Text, txtPhone.Text, txtMobile.Text, mskRegdate.Text);

            }
            else
            {
                SqlCommand cmd = new SqlCommand();
                cmd.CommandText = "update tblPerson set FirstName=@FirstName,LastName=@LastName,Phone=@Phone,Mobile=@Mobile,DateOfSabt=@DateOfSabt where CodeMelli=@CodeMelli";
                cmd.Connection = con;
                cmd.Parameters.Add("@CodeMelli", SqlDbType.NVarChar).Value = txtCode.Text; ;
                cmd.Parameters.Add("@FirstName", SqlDbType.NVarChar).Value = txtFirsName.Text;
                cmd.Parameters.Add("@LastName", SqlDbType.NVarChar).Value = txtLastName.Text;
                cmd.Parameters.Add("@Phone", SqlDbType.NVarChar).Value = txtPhone.Text;
                cmd.Parameters.Add("@Mobile", SqlDbType.NVarChar).Value = txtMobile.Text;
                cmd.Parameters.Add("@DateOfSabt", SqlDbType.NVarChar).Value = mskRegdate.Text;
                con.Open();
                cmd.ExecuteNonQuery();
                con.Close();

                dt.Rows[_rowIndex][0] = txtCode.Text;
                dt.Rows[_rowIndex][1] = txtFirsName.Text;
                dt.Rows[_rowIndex][2] = txtLastName.Text;
                dt.Rows[_rowIndex][3] =  txtPhone.Text;
                dt.Rows[_rowIndex][4] = txtMobile.Text;
                dt.Rows[_rowIndex][5] = mskRegdate.Text;
                this.Close();
            }
        }

        private void addinfo_Load(object sender, EventArgs e)
        {

        }

        private void btnCancel_Click(object sender, EventArgs e)
        {
            this.Close();
        }
    }
}
