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


namespace Grocery
{
    public partial class FrmAnbar : Form
    {
        public enum Mod { NewRecord = 0, UpdateRecode = 1 };
        private Mod mod;
        List<Anbar> AnbarList = new List<Anbar>();
        public static bool kalachanged = false;
        public static string selectedkalaid = "";
        string acode = "";
        public FrmAnbar()
        {
            InitializeComponent();
        }

        private void Bind()
        {
            try
            {
                AnbarList = Anbar.GetAllKalas();
                dataGridViewX1.DataSource = AnbarList;
                dataGridViewX1.Columns["acode"].Visible = false;
                dataGridViewX1.Columns["kalaid"].HeaderText = "کد کالا";
                dataGridViewX1.Columns["kalaname"].HeaderText = "نام کالا";
                dataGridViewX1.Columns["mojodi"].HeaderText = "موجودی";
                dataGridViewX1.Columns["kharid"].HeaderText = "قیمت خرید";
            }
            catch (Exception ex)
            {
                FarsiMessagbox.Show(ex.Message, "خطا", FMessageBoxButtons.Ok, FMessageBoxIcon.Error);
            }
        }
        private void Anbar_Load(object sender, EventArgs e)
        {
            try
            {
                Bind();
                mod = Mod.UpdateRecode;
                EnableControls();
            }
            catch (Exception ex)
            {
                FarsiMessagbox.Show(ex.Message, "خطا", FMessageBoxButtons.Ok, FMessageBoxIcon.Error);
            }
        }

        private void EnableControls()
        {
            switch (mod)
            {
                case Mod.NewRecord:
                    Btn_Delete.Enabled = false;
                    Btn_New.Enabled = false;
                    Btn_Save.Enabled = true;
                    Btn_Update.Enabled = false;
                    txtkalaid.ReadOnly = false;
                    break;
                case Mod.UpdateRecode:
                    Btn_Delete.Enabled = true;
                    Btn_New.Enabled = true;
                    Btn_Save.Enabled = false;
                    Btn_Update.Enabled = true;
                    txtkalaid.ReadOnly = true;
                    break;
            }
        }

        private void btndeleteZero_Click(object sender, EventArgs e)
        {
            try
            {
                Anbar.DeleteZeroMojodi();
                Bind();
            }
            catch (Exception ex)
            {
                FarsiMessagbox.Show(ex.Message, "خطا", FMessageBoxButtons.Ok, FMessageBoxIcon.Error);
            }
        }
        private void Btn_New_Click(object sender, EventArgs e)
        {
            try
            {
                mod = Mod.NewRecord;
                EnableControls();
                txtkalaid.Text = txtkalaname.Text = txtkharid.Text = txtmojodi.Text = string.Empty;
                txtkalaid.Focus();
            }
            catch (Exception ex)
            {
                FarsiMessagbox.Show(ex.Message, "خطا", FMessageBoxButtons.Ok, FMessageBoxIcon.Error);
            }
        }

        private void Btn_Update_Click(object sender, EventArgs e)
        {
            try
            {
                if (acode == "") { FarsiMessagbox.Show("شما باید یک کالا را انتخاب نمایید", "انبار", FMessageBoxButtons.Ok, FMessageBoxIcon.Information); return; }
                errorProvider1.Clear();
                if (txtkalaname.Text.Trim().Length == 0)
                {
                    errorProvider1.SetError(txtkalaid, "لطفا کالا را انتخاب کنید");
                    return;
                }
                if (txtkharid.Text.Trim().Length == 0)
                {
                    errorProvider1.SetError(txtkharid, "لطفا این فیلد را پر کنید");
                    return;
                }
                if (txtmojodi.Text.Trim().Length == 0)
                {
                    errorProvider1.SetError(txtmojodi, "لطفا این فیلد را پر کنید");
                    return;
                }
                int row = dataGridViewX1.CurrentCell.RowIndex;
                Anbar a = new Anbar();
                a.acode = acode;
                a.kalaid = txtkalaid.Text;
                a.kharid = txtkharid.Text;
                a.Mojodi = txtmojodi.Text;
                a.Update();
                FarsiMessagbox.Show("ذخیره شد", "انبار", FMessageBoxButtons.Ok, FMessageBoxIcon.Information);
                Bind();
                EnableControls();
                dataGridViewX1_CellClick(null, new DataGridViewCellEventArgs(0, row));
            }
            catch (Exception ex)
            {
                FarsiMessagbox.Show(ex.Message, "خطا", FMessageBoxButtons.Ok, FMessageBoxIcon.Error);
            }
        }

        private void Btn_Save_Click(object sender, EventArgs e)
        {
            try
            {
                errorProvider1.Clear();
                if (txtkalaname.Text.Trim().Length == 0)
                {
                    errorProvider1.SetError(txtkalaid, "لطفا کالا را انتخاب کنید");
                    return;
                }
                if (txtkharid.Text.Trim().Length == 0)
                {
                    errorProvider1.SetError(txtkharid, "لطفا این فیلد را پر کنید");
                    return;
                }
                if (txtmojodi.Text.Trim().Length == 0)
                {
                    errorProvider1.SetError(txtmojodi, "لطفا این فیلد را پر کنید");
                    return;
                }

                Anbar a = new Anbar();
                a.kalaid = txtkalaid.Text;
                a.kharid = txtkharid.Text;
                a.Mojodi = txtmojodi.Text;
                a.Insert();
                FarsiMessagbox.Show("ذخیره شد", "انبار", FMessageBoxButtons.Ok, FMessageBoxIcon.Information);
                Bind();
                mod = Mod.UpdateRecode;
                txtkalaid.Text = txtkalaname.Text = txtkharid.Text = txtmojodi.Text = string.Empty;
                EnableControls();
            }
            catch (Exception ex)
            {
                FarsiMessagbox.Show(ex.Message, "خطا", FMessageBoxButtons.Ok, FMessageBoxIcon.Error);
            }
        }

        private void Btn_ExitAction_Click(object sender, EventArgs e)
        {
            try
            {
                errorProvider1.Clear();
                mod = Mod.UpdateRecode;
                EnableControls();
                dataGridViewX1_CellClick(null, new DataGridViewCellEventArgs(dataGridViewX1.CurrentCell.ColumnIndex, dataGridViewX1.CurrentCell.RowIndex));
            }
            catch (Exception ex)
            {
                FarsiMessagbox.Show(ex.Message, "خطا", FMessageBoxButtons.Ok, FMessageBoxIcon.Error);
            }
        }
        private void Btn_Delete_Click(object sender, EventArgs e)
        {
            try
            {
                if (FarsiMessagbox.Show("آیا مطمئن هستید؟", "", FMessageBoxButtons.YesNo, FMessageBoxIcon.Information) == DialogResult.Yes)
                {
                    Anbar a = new Anbar();
                    a.acode = acode;
                    a.Delete();
                    txtkalaid.Text = txtkalaname.Text = txtkharid.Text = txtmojodi.Text = string.Empty;
                    Bind();
                    EnableControls();
                    FarsiMessagbox.Show("حذف شد", "انبار", FMessageBoxButtons.Ok, FMessageBoxIcon.Information);
                }
            }
            catch (Exception ex)
            {
                FarsiMessagbox.Show(ex.Message, "خطا", FMessageBoxButtons.Ok, FMessageBoxIcon.Error);
            }
        }

        private void dataGridViewX1_CellClick(object sender, DataGridViewCellEventArgs e)
        {
            try
            {
                if (mod == Mod.NewRecord) return;
                txtkalaid.Text = dataGridViewX1.Rows[e.RowIndex].Cells["kalaid"].Value.ToString();
                txtkalaname.Text = dataGridViewX1.Rows[e.RowIndex].Cells["kalaname"].Value.ToString();
                txtkharid.Text = dataGridViewX1.Rows[e.RowIndex].Cells["kharid"].Value.ToString();
                txtmojodi.Text = dataGridViewX1.Rows[e.RowIndex].Cells["mojodi"].Value.ToString();
                acode = dataGridViewX1.Rows[e.RowIndex].Cells["acode"].Value.ToString();
                lblmojodi.Text= Anbar.GetMojodiKala(dataGridViewX1.Rows[e.RowIndex].Cells["kalaid"].Value.ToString()).ToString();
            }
            catch (Exception ex)
            {
                FarsiMessagbox.Show(ex.Message, "خطا", FMessageBoxButtons.Ok, FMessageBoxIcon.Error);
            }
        }

        private void txtkalaid_TextChanged(object sender, EventArgs e)
        {
            foreach (char ch in txtkalaid.Text.ToCharArray())
            {
                if (char.IsDigit(ch) == false && ch != '.') txtkalaid.Text = txtkalaid.Text.Replace(ch.ToString(), "");
            }
        }

        private void txtkharid_TextChanged(object sender, EventArgs e)
        {
            foreach (char ch in txtkharid.Text.ToCharArray())
            {
                if (char.IsDigit(ch) == false && ch != '.') txtkharid.Text = txtkharid.Text.Replace(ch.ToString(), "");
            }
        }

        private void txtmojodi_TextChanged(object sender, EventArgs e)
        {
            foreach (char ch in txtmojodi.Text.ToCharArray())
            {
                if (char.IsDigit(ch) == false && ch != '.') txtmojodi.Text = txtmojodi.Text.Replace(ch.ToString(), "");
            }
        }

        private void txtkalaid_KeyDown(object sender, KeyEventArgs e)
        {
            try
            {
                List<Kala> KalasList = new List<Kala>();
                if (e.KeyData == Keys.Enter)
                {
                    if (txtkalaid.Text.Trim() != string.Empty)
                    {
                        KalasList = Kala.GetKala(Convert.ToInt64(txtkalaid.Text));
                        if (KalasList.Count > 0)
                        {
                            txtkalaname.Text = KalasList[0].kalaname;
                        }
                        else FarsiMessagbox.Show("پبدا نشد");
                    }
                    else
                    {
                        try
                        {
                            FrmListKala list = new FrmListKala();
                            list.ShowDialog();
                            if (selectedkalaid != "")
                            {
                                txtkalaid.Text = selectedkalaid;
                                txtkalaid_KeyDown(null, new KeyEventArgs(Keys.Enter));
                            }
                        }
                        catch (Exception ex)
                        {
                            FarsiMessagbox.Show(ex.Message, "خطا", FMessageBoxButtons.Ok, FMessageBoxIcon.Error);
                        }
                    }
                }

            }
            catch (Exception ex)
            {
                FarsiMessagbox.Show(ex.Message, "خطا", FMessageBoxButtons.Ok, FMessageBoxIcon.Error);
            }
        }

        private void FrmAnbar_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Insert)
                Btn_New.PerformClick();
            if (e.KeyCode == Keys.F1)
                Btn_ExitAction.PerformClick();
            if (e.KeyCode == Keys.F2)
                Btn_Update.PerformClick();
            if (e.KeyCode == Keys.F8)
                Btn_Save.PerformClick();
            if (e.KeyCode == Keys.F6)
                Btn_Delete.PerformClick();
            if (e.KeyCode == Keys.Escape)
                this.Close();
            if (e.KeyCode == Keys.Enter)
                SendKeys.Send("{Tab}");
        }
    }
}
