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

namespace Nano_System
{
    public partial class frmMaarjoii : Form
    {
        int KeyStatus1 = 0;
        MessageBoxOptions options = MessageBoxOptions.RightAlign;
        public frmMaarjoii()
        {
            InitializeComponent();
        }

        private void frmMaarjoii_Load(object sender, EventArgs e)
        {
            grdFill();
        }

        private void btnAddNew_Click(object sender, EventArgs e)
        {
            KeyStatus1 = 1;
            groupBox2.Visible = false;
            groupBox3.Visible = true;
            groupBox4.Enabled = false;
            dataGridView1.Enabled = false;
            this.tblMarjoiiBindingSource.AddNew();
            btnUpdate.Enabled = false;
        }

        private void btnSave_Click(object sender, EventArgs e)
        {
            if (tedadeMarjooiiTextBox.Text == "" || comboBox1.Text == "" || comboBox2.Text == "")
            {
                MessageBox.Show("انتخاب و ورود مقادیر تمامی فیلدها الزامی می باشد.");
                this.tblMarjoiiBindingSource.CancelEdit();
                groupBox2.Visible = true;
                groupBox3.Visible = false;
                groupBox4.Enabled = true;
                dataGridView1.Enabled = true;
                grdFill();
                btnUpdate.Enabled = false;
                return;
            }
            if (KeyStatus1 == 1)
            {
                tblMarjoiiTableAdapter.Insert(int.Parse(comboBox1.SelectedValue.ToString()), comboBox2.SelectedValue.ToString(), DateTime.Now, clsShamsi(DateTime.Now), int.Parse(tedadeMarjooiiTextBox.Text.Trim()));
                groupBox2.Visible = true;
                groupBox3.Visible = false;
                groupBox4.Enabled = true;
                dataGridView1.Enabled = true;
                dataGridView1.Refresh();
                grdFill();
                MessageBox.Show("رکورد مورد نظر ذخیره سازی گردید ", "تائید ثبت", MessageBoxButtons.OK, MessageBoxIcon.Warning, MessageBoxDefaultButton.Button1, options);
            }
            else if (KeyStatus1 == 2)
            {
                tblMarjoiiTableAdapter.UpdateQuery(int.Parse(comboBox1.SelectedValue.ToString()), comboBox2.SelectedValue.ToString(), DateTime.Now, clsShamsi(DateTime.Now), int.Parse(tedadeMarjooiiTextBox.Text.Trim()), Decimal.Parse(marjoiiIDTextBox.Text.Trim()));
                groupBox2.Visible = true;
                groupBox3.Visible = false;
                groupBox4.Enabled = true;
                dataGridView1.Enabled = true;
                dataGridView1.Refresh();
                grdFill();
                MessageBox.Show("رکورد مورد نظر بروزرسانی گردید ", "تائید ثبت", MessageBoxButtons.OK, MessageBoxIcon.Warning, MessageBoxDefaultButton.Button1, options);
            }
        }

        private void btnCancel_Click(object sender, EventArgs e)
        {
            this.tblMarjoiiBindingSource.CancelEdit();
            groupBox2.Visible = true;
            groupBox3.Visible = false;
            groupBox4.Enabled = true;
            dataGridView1.Enabled = true;
            grdFill();
            btnUpdate.Enabled = false;
        }

        private void grdFill()
        {
            this.goodsTableAdapter.Fill(this.nano_DataDataSet.Goods);
            // TODO: This line of code loads data into the 'nano_DataDataSet.FactForooshID' table. You can move, or remove it, as needed.
            this.factForooshIDTableAdapter.Fill(this.nano_DataDataSet.FactForooshID);
            // TODO: This line of code loads data into the 'nano_DataDataSet.tblMarjoii' table. You can move, or remove it, as needed.
            this.tblMarjoiiTableAdapter.Fill(this.nano_DataDataSet.tblMarjoii);
        }

        public String clsShamsi(DateTime MiladiDate)
        {
            String dtShamsi = String.Empty;
            PersianCalendar calFar1 = new PersianCalendar();
            dtShamsi = calFar1.GetYear(MiladiDate).ToString() + "/" + calFar1.GetMonth(MiladiDate).ToString("00") + "/" + calFar1.GetDayOfMonth(MiladiDate).ToString("00");
            return dtShamsi;
        }

        private void btnUpdate_Click(object sender, EventArgs e)
        {
            KeyStatus1 = 2;
            groupBox2.Visible = false;
            groupBox3.Visible = true;
            groupBox4.Enabled = false;
            dataGridView1.Enabled = false;
            btnUpdate.Enabled = false;
        }

        private void btnDelete_Click(object sender, EventArgs e)
        {
            DialogResult dr = MessageBox.Show("آیا از حذف رکورد اطمینان دارید", "اخطار حذف", MessageBoxButtons.OKCancel, MessageBoxIcon.Warning);
            if (dr == DialogResult.OK)
            {
                if (marjoiiIDTextBox.Text == "")
                {
                    MessageBox.Show("ركوردي براي حذف انتخاب نشده است");
                    return;
                }
                tblMarjoiiTableAdapter.DeleteQuery(Decimal.Parse(marjoiiIDTextBox.Text.Trim()));
                grdFill();
                MessageBox.Show("رکورد مورد نظر حذف گردید");
            }
        }

        private void btnRefresh_Click(object sender, EventArgs e)
        {
            grdFill();
        }

        private void btmNext_Click(object sender, EventArgs e)
        {
            if (this.tblMarjoiiBindingSource.Position + 1 < this.tblMarjoiiBindingSource.Count)
            {
                this.tblMarjoiiBindingSource.MoveNext();
            }
        }

        private void btnLast_Click(object sender, EventArgs e)
        {
            this.tblMarjoiiBindingSource.MoveLast();
        }

        private void btnPrevoiuse_Click(object sender, EventArgs e)
        {
            this.tblMarjoiiBindingSource.MovePrevious();
        }

        private void btnFirst_Click(object sender, EventArgs e)
        {
            this.tblMarjoiiBindingSource.MoveFirst();
        }
    }
}