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;
using CommonLayer;


namespace Grocery
{
    public partial class Frmpardakht : Form
    {
        List<Sandogh> SandoghsList = new List<Sandogh>();
        List<Customer> CustomersList = new List<Customer>();
        public Frmpardakht()
        {
            InitializeComponent();
        }

        private void Bind()
        {
            try
            {
                SandoghsList = Sandogh.GetAllSandoghs();
                cmbsandogh.DataSource = SandoghsList;
                cmbsandogh.DisplayMember = "SandoghName";
                cmbsandogh.ValueMember = "Mojodi";
            }
            catch (Exception ex)
            {
                FarsiMessagbox.Show(ex.Message, "خطا", FMessageBoxButtons.Ok, FMessageBoxIcon.Error);
            }
        }

        private void GetCustomerData()
        {
            try
            {
                CustomersList = Customer.GetCustomer(Pardakht.cid);
                lblghablibedon.Text = CustomersList[0].bedehkar;
                lblghabliba.Text = (Convert.ToDecimal(CustomersList[0].bedehkar) + Pardakht.Mablagh).ToString();
                lblcid.Text = CustomersList[0].cid;
                lblname.Text = CustomersList[0].fname.Trim() + " " + CustomersList[0].lname.Trim();
            }
            catch (Exception ex)
            {
                FarsiMessagbox.Show(ex.Message, "خطا", FMessageBoxButtons.Ok, FMessageBoxIcon.Error);
            }
        }

        private void btnok_Click(object sender, EventArgs e)
        {
            errorProvider1.Clear();
            DisplaySum();
            FrmReport report = new FrmReport();
            if (Convert.ToDecimal(lbljamkol.Text) > Convert.ToDecimal(lblmablagh.Text))
            {
                FarsiMessagbox.Show("جمع کل از مبلغ قابل پرداخت بیشتر است", "پرداخت", FMessageBoxButtons.Ok, FMessageBoxIcon.Information);
                return;
            }
            if (Convert.ToDecimal(lbljamkol.Text) < Convert.ToDecimal(lblmablagh.Text))
            {
                if (Pardakht.Opt == op.Daryafti)
                {
                    if (FarsiMessagbox.Show("جمع کل از مبلغ قابل پرداخت کمتر است. آیا میخواهید مابقی به حساب این مشتری افزوده شود؟", "پرداخت", FMessageBoxButtons.YesNo, FMessageBoxIcon.Information) == DialogResult.No)
                        return;
                    else
                    {
                        Customer cs = new Customer();
                        cs.cid = lblcid.Text;
                        string ss = (Convert.ToDecimal(lblmablagh.Text) - Convert.ToDecimal(lbljamkol.Text)).ToString();
                        try
                        {
                            cs.AddToBedehkari(ss);
                            report.listView1.Items.Add("مبلغ " + ss + " به حساب " + lblname.Text + " اضافه شد", 0);
                        }
                        catch (Exception ex)
                        {
                            report.listView1.Items.Add("مبلغ " + ss + " به حساب " + lblname.Text + " اضافه نشد", 1);
                            report.listView1.Items.Add(ex.Message, 1);
                        }
                    }
                }
                else
                {
                    if (DialogResult.No == FarsiMessagbox.Show("جمع کل از مبلغ قابل پرداخت کمتر است.آیا عملیات را ادامه میدهید؟", "پرداخت", FMessageBoxButtons.YesNo, FMessageBoxIcon.Information))
                        return;
                }
            }
            if (Pardakht.Opt == op.Pardakhti && Convert.ToDecimal(lbljamkol.Text) > Convert.ToDecimal(lblmojodi.Text))
            {
                FarsiMessagbox.Show("موجودی صندوق کافی نمی باشد", "پرداخت", FMessageBoxButtons.Ok, FMessageBoxIcon.Information);
                return;
            }

            Sandogh s = new Sandogh();
            s.SandoghName = cmbsandogh.Text;
            if (Pardakht.Opt == op.Daryafti) s.Mojodi = (Convert.ToDecimal(lblmojodi.Text) + Convert.ToDecimal(txtnaghdi.Text)).ToString();
            else s.Mojodi = (Convert.ToDecimal(lblmojodi.Text) - Convert.ToDecimal(txtnaghdi.Text)).ToString();
            try
            {
                s.Update();
                report.listView1.Items.Add("موجودی صندوق تغییر یافت", 0);
            }
            catch (Exception ex)
            {
                report.listView1.Items.Add("موجودی صندوق تغییر نیافت", 1);
                report.listView1.Items.Add(ex.Message, 1);
            }
            lblmablagh.Text = (Convert.ToDecimal(lblmablagh.Text) - Convert.ToDecimal(txtnaghdi.Text)).ToString();
            txtnaghdi.Text = "0";
            Chek chk = new Chek();
            for (int i = 0; i < dataGridViewX1.RowCount; i++)
            {
                if (dataGridViewX1.Rows[i].Cells["serial"].Value == null || dataGridViewX1.Rows[i].Cells["serial"].Value.ToString().Trim() == string.Empty) { errorProvider1.SetError(dataGridViewX1, "در ردیف " + (i + 1).ToString() + " سریال چک را وارد نمایید"); return; }
                if (dataGridViewX1.Rows[i].Cells["date"].Value == null || FarsiDate.IsValidDate(dataGridViewX1.Rows[i].Cells["date"].Value.ToString()) == false) { errorProvider1.SetError(dataGridViewX1, "تاریخ وارد شده در ردیف " + (i + 1).ToString() + " صحیح نمی باشد"); return; }
                if (dataGridViewX1.Rows[i].Cells["bank"].Value == null) dataGridViewX1.Rows[i].Cells["bank"].Value = "";
                if (dataGridViewX1.Rows[i].Cells["shobe"].Value == null) dataGridViewX1.Rows[i].Cells["shobe"].Value = "";
                if (dataGridViewX1.Rows[i].Cells["mablagh"].Value == null) dataGridViewX1.Rows[i].Cells["mablagh"].Value = "0";
                chk.Serial = dataGridViewX1.Rows[i].Cells["serial"].Value.ToString();
                chk.Bank = dataGridViewX1.Rows[i].Cells["bank"].Value.ToString();
                chk.Shobe = dataGridViewX1.Rows[i].Cells["shobe"].Value.ToString();
                chk.Date = dataGridViewX1.Rows[i].Cells["date"].Value.ToString();
                chk.Mablagh = dataGridViewX1.Rows[i].Cells["mablagh"].Value.ToString();
                chk.Daryafti = (int)Pardakht.Opt;
                chk.PasShode = 0;
                try
                {
                    chk.Insert();
                    lblmablagh.Text = (Convert.ToDecimal(lblmablagh.Text) - Convert.ToDecimal(chk.Mablagh)).ToString();
                    report.listView1.Items.Add("چک با شماره سریال " + chk.Serial + " ثبت شد", 0);
                    //dataGridViewX1.Rows.RemoveAt(i);
                    //i--;
                }
                catch (Exception ex)
                {
                    report.listView1.Items.Add("چک با شماره سریال " + chk.Serial + " ثبت نشد", 1);
                    report.listView1.Items.Add(ex.Message, 1);
                }
            }
            int idx = cmbsandogh.SelectedIndex;
            Bind();
            cmbsandogh.SelectedIndex = idx;
            if (Pardakht.Opt == op.Daryafti) GetCustomerData();
            DisplaySum();
            report.Show(this);
            txtnaghdi.Enabled = dataGridViewX1.Enabled = btnaddrow.Enabled = btndelrow.Enabled = btnnewsandogh.Enabled = btnok.Enabled = false;
        }

        private void Frmpardakht_Load(object sender, EventArgs e)
        {
            try
            {
                Bind();
                lblmablagh.Text = Pardakht.Mablagh.ToString();
                cmbsandogh_SelectedIndexChanged(null, null);
                if (Pardakht.Opt == op.Daryafti && Pardakht.cid != 0) GetCustomerData();
                else { groupPanel2.Visible = false; this.Height -= groupPanel2.Height; }
            }
            catch (Exception ex)
            {
                FarsiMessagbox.Show(ex.Message, "خطا", FMessageBoxButtons.Ok, FMessageBoxIcon.Error);
            }

        }

        private void cmbsandogh_SelectedIndexChanged(object sender, EventArgs e)
        {
            lblmojodi.Text = cmbsandogh.SelectedValue.ToString();
        }

        private void btnaddrow_Click(object sender, EventArgs e)
        {
            dataGridViewX1.Rows.Add();
            dataGridViewX1.Focus();
        }

        private void btndelrow_Click(object sender, EventArgs e)
        {
            try
            {
                dataGridViewX1.Rows.RemoveAt(dataGridViewX1.CurrentRow.Index);
                DisplaySum();
            }
            catch (Exception ex)
            {
                FarsiMessagbox.Show(ex.Message, "خطا", FMessageBoxButtons.Ok, FMessageBoxIcon.Error);
            }
        }

        private void DisplaySum()
        {
            try
            {
                decimal cheks = 0;
                if (txtnaghdi.Text.Trim() == string.Empty) txtnaghdi.Text = "0";
                for (int i = 0; i < dataGridViewX1.Rows.Count; i++) cheks += Convert.ToDecimal(dataGridViewX1.Rows[i].Cells["mablagh"].Value);
                lbljamchek.Text = cheks.ToString();
                lbljamkol.Text = (Convert.ToDecimal(txtnaghdi.Text) + cheks).ToString();
            }
            catch (Exception ex)
            {
                FarsiMessagbox.Show(ex.Message, "خطا", FMessageBoxButtons.Ok, FMessageBoxIcon.Error);
            }
        }

        private void txtnaghdi_TextChanged(object sender, EventArgs e)
        {
            DisplaySum();
        }

        private void dataGridViewX1_CellEndEdit(object sender, DataGridViewCellEventArgs e)
        {
            try
            {
                if (dataGridViewX1.Rows[e.RowIndex].Cells["mablagh"].Value != null && dataGridViewX1.Rows[e.RowIndex].Cells["mablagh"].Value.ToString() != "")
                {
                    foreach (char ch in dataGridViewX1.Rows[e.RowIndex].Cells["mablagh"].Value.ToString().ToCharArray())
                    {
                        if (char.IsDigit(ch) == false && ch != '.') dataGridViewX1.Rows[e.RowIndex].Cells["mablagh"].Value = dataGridViewX1.Rows[e.RowIndex].Cells["mablagh"].Value.ToString().Replace(ch.ToString(), "");
                    }
                    DisplaySum();
                }
            }
            catch (Exception ex)
            {
                FarsiMessagbox.Show(ex.Message, "خطا", FMessageBoxButtons.Ok, FMessageBoxIcon.Error);
            }
        }

        private void btnnewsandogh_Click(object sender, EventArgs e)
        {
            try
            {
                FrmSandogh sandogh = new FrmSandogh(true);
                sandogh.ShowDialog();
                SandoghsList = Sandogh.GetAllSandoghs();
                cmbsandogh.DataSource = SandoghsList;
                cmbsandogh.DisplayMember = "SandoghName";
                cmbsandogh.ValueMember = "Mojodi";
                cmbsandogh.SelectedIndex = cmbsandogh.Items.Count - 1;
            }
            catch (Exception ex)
            {
                FarsiMessagbox.Show(ex.Message, "خطا", FMessageBoxButtons.Ok, FMessageBoxIcon.Error);
            }
        }

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

        private void Frmpardakht_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Insert)
                btnaddrow.PerformClick();
            if (e.KeyCode == Keys.F8)
                btnok.PerformClick();
            if (e.KeyCode == Keys.Escape)
                this.Close();
            if (e.KeyCode == Keys.Enter)
                SendKeys.Send("{Tab}");
        }
    }
}
