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 FrmChek : Form
    {
        public enum Mod { NewRecord = 0, UpdateRecode = 1 };
        private Mod mod;
        List<Chek> ChekList = new List<Chek>();
        DataView objview;
        private void Bind()
        {
            try
            {
                ChekList = Chek.GetAllCheks();
                DataTable tbl = new DataTable("f");
                tbl.Columns.Add("row");
                tbl.Columns.Add("serial");
                tbl.Columns.Add("bank");
                tbl.Columns.Add("shobe");
                tbl.Columns.Add("date");
                tbl.Columns.Add("mablagh");
                tbl.Columns.Add("vaziat");
                tbl.Columns.Add("daryafti");

                object[] chk = new object[8];
                for (int i = 0; i < ChekList.Count; i++)
                {
                    chk[0] = i + 1;
                    chk[1] = ChekList[i].Serial;
                    chk[2] = ChekList[i].Bank;
                    chk[3] = ChekList[i].Shobe;
                    chk[4] = ChekList[i].Date;
                    chk[5] = ChekList[i].Mablagh;
                    switch (ChekList[i].PasShode)
                    {
                        case -1:
                            chk[6] = ""; break;
                        case 0:
                            chk[6] = "پاس نشده"; break;
                        case 1:
                            chk[6] = "پاس شده"; break;
                        case 2:
                            chk[6] = "برگشت خورده"; break;
                    }
                    chk[7] = ChekList[i].Daryafti;
                    tbl.Rows.Add(chk);
                }
                objview = new DataView(tbl);
                dataGridViewX1.DataSource = objview;
                dataGridViewX1.Columns["row"].HeaderText = "ردیف";
                dataGridViewX1.Columns["row"].Width = 50;
                dataGridViewX1.Columns["bank"].HeaderText = "بانک";
                dataGridViewX1.Columns["shobe"].HeaderText = "شعبه";
                dataGridViewX1.Columns["date"].HeaderText = "تاریخ";
                dataGridViewX1.Columns["mablagh"].HeaderText = "مبلغ";
                dataGridViewX1.Columns["vaziat"].HeaderText = "وضعیت";
                dataGridViewX1.Columns["daryafti"].Visible = false;
                cmbgroups_SelectedIndexChanged(null, null);
            }
            catch (Exception ex)
            {
                FarsiMessagbox.Show(ex.Message, "خطا", FMessageBoxButtons.Ok, FMessageBoxIcon.Error);
            }
        }

        public FrmChek()
        {
            InitializeComponent();
        }

        private void FrmChek_Load(object sender, EventArgs e)
        {
            Bind();
            cmbgroups.SelectedIndex = 0;
            mod = Mod.UpdateRecode;
            EnableControls();
        }



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

        private void Btn_New_Click(object sender, EventArgs e)
        {
            mod = Mod.NewRecord;
            EnableControls();
            txtserial.Text = txtmablagh.Text = txtdate.Text = txtshobe.Text = string.Empty;
            cmbbank.SelectedIndex = 0;
            cmbvaziat.SelectedIndex = 0;
            txtserial.Focus();
        }

        private void Btn_Update_Click(object sender, EventArgs e)
        {
            try
            {
                errorProvider1.Clear();
                if (txtserial.Text.Trim().Length == 0)
                {
                    errorProvider1.SetError(txtserial, "لطفا این فیلد را پر کنید");
                    return;
                }
                if (txtmablagh.Text.Trim().Length == 0)
                {
                    errorProvider1.SetError(txtmablagh, "لطفا این فیلد را پر کنید");
                    return;
                }
                if (txtdate.Text.Trim().Length == 0)
                {
                    errorProvider1.SetError(txtdate, "لطفا این فیلد را پر کنید");
                    return;
                }

                Chek chk = new Chek(txtserial.Text, cmbbank.Text, txtshobe.Text, txtdate.Text, txtmablagh.Text, Convert.ToInt16(cmbgroups.SelectedIndex), Convert.ToInt16(cmbvaziat.SelectedIndex));
                chk.Insert();
                FarsiMessagbox.Show("ذخیره شد", "مشخصات کالا", FMessageBoxButtons.Ok, FMessageBoxIcon.Information);
                Bind();
                EnableControls();
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }

        private void Btn_Save_Click(object sender, EventArgs e)
        {
            try
            {
                errorProvider1.Clear();
                if (txtserial.Text.Trim().Length == 0)
                {
                    errorProvider1.SetError(txtserial, "لطفا این فیلد را پر کنید");
                    return;
                }
                if (txtmablagh.Text.Trim().Length == 0)
                {
                    errorProvider1.SetError(txtmablagh, "لطفا این فیلد را پر کنید");
                    return;
                }
                if (txtdate.Text.Trim().Length == 0)
                {
                    errorProvider1.SetError(txtdate, "لطفا این فیلد را پر کنید");
                    return;
                }

                Chek chk = new Chek(txtserial.Text, cmbbank.Text, txtshobe.Text, txtdate.Text, txtmablagh.Text, cmbgroups.SelectedIndex, cmbvaziat.SelectedIndex);
                chk.Insert();
                FarsiMessagbox.Show("ذخیره شد", "مشخصات کالا", FMessageBoxButtons.Ok, FMessageBoxIcon.Information);
                Bind();
                mod = Mod.UpdateRecode;
                EnableControls();
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }

        private void Btn_ExitAction_Click(object sender, EventArgs e)
        {
            errorProvider1.Clear();
            mod = Mod.UpdateRecode;
            EnableControls();
        }

        private void FrmChek_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}");
        }

        private void Btn_Delete_Click(object sender, EventArgs e)
        {

            try
            {
                if (FarsiMessagbox.Show("آیا مطمئن هستید؟", "", FMessageBoxButtons.YesNo, FMessageBoxIcon.Information) == DialogResult.Yes)
                {
                    Chek chk = new Chek();
                    chk.Serial = txtserial.Text;
                    chk.Delete();
                    txtserial.Text = txtmablagh.Text = txtdate.Text = txtshobe.Text = string.Empty;
                    Bind();
                    EnableControls();
                    FarsiMessagbox.Show("حذف شد", "مشخصات کالا", FMessageBoxButtons.Ok, FMessageBoxIcon.Information);
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }

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

        private void dataGridViewX1_CellClick(object sender, DataGridViewCellEventArgs e)
        {
            try
            {
                if (mod == Mod.NewRecord) return;
                txtdate.Text = dataGridViewX1.Rows[e.RowIndex].Cells["date"].Value.ToString();
                txtmablagh.Text = dataGridViewX1.Rows[e.RowIndex].Cells["mablagh"].Value.ToString();
                txtserial.Text = dataGridViewX1.Rows[e.RowIndex].Cells["serial"].Value.ToString();
                txtshobe.Text = dataGridViewX1.Rows[e.RowIndex].Cells["shobe"].Value.ToString();
                cmbbank.Text = dataGridViewX1.Rows[e.RowIndex].Cells["bank"].Value.ToString();
                cmbvaziat.Text = dataGridViewX1.Rows[e.RowIndex].Cells["vaziat"].Value.ToString();
            }
            catch (Exception ex)
            {
                FarsiMessagbox.Show(ex.Message, "خطا", FMessageBoxButtons.Ok, FMessageBoxIcon.Error);
            }
        }

        private void cmbgroups_SelectedIndexChanged(object sender, EventArgs e)
        {
            try
            {
                objview.RowFilter = "daryafti=" + cmbgroups.SelectedIndex;
                for (int i = 0; i < dataGridViewX1.RowCount; i++)
                {
                    dataGridViewX1.Rows[i].Cells["row"].Value = i + 1;
                }
            }
            catch { }
        }

    }
}
