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

namespace Stone
{
    public partial class DialogCount : Form
    {


        DataGridViewRow GridRow = null;

        public DataRow Dr;
        public DialogCount()
        {
            InitializeComponent();
        }


        public DialogCount(DataGridViewRow GridRowEdit)
        {
            InitializeComponent();
            GridRow = GridRowEdit;
        }

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

        private void button1_Click(object sender, EventArgs e)
        {

            if (Int64.Parse(TxtCount.Text) == 0)
            {
                MessageBox.Show("عدد ورودی ، باید بالای صفر باشد", "زمرد", MessageBoxButtons.OK, MessageBoxIcon.Information);
                return;
            }


            if (GridRow != null)
            {
                DataRow[] DrArraySearch = Program.FC.DSFactor.Tables["TDetailFactor"].Select("Id = " + GridRow.Cells[0].Value.ToString());

                if (DrArraySearch.Length == 0)
                {
                    Dr = Program.FC.DSFactor.Tables["TDetailFactor"].NewRow();
                    Dr[0] = Int64.Parse(Program.FC.GenerateIDFactor());
                    Dr[1] = Int32.Parse(GridRow.Cells[0].Value.ToString());
                    Dr[2] = GridRow.Cells[1].Value.ToString();
                    Dr[3] = Int32.Parse(TxtCount.Text);
                    Dr[4] = Double.Parse(GridRow.Cells[3].Value.ToString());
                    Dr[5] = Double.Parse(GridRow.Cells[4].Value.ToString());
                    Dr[6] = GridRow.Cells[5].Value.ToString();
                    Dr[7] = Int64.Parse(GridRow.Cells[6].Value.ToString());
                    Dr[8] = Int64.Parse(GridRow.Cells[6].Value.ToString()) * Int64.Parse(TxtCount.Text);
                    Program.FC.DSFactor.Tables["TDetailFactor"].Rows.Add(Dr);
                    MessageBox.Show("محصول به فاکتور اضافه شد\n تعداد کالاهای داخل فاکتور " + Program.FC.DSFactor.Tables["TDetailFactor"].Rows.Count.ToString() + " عدد می باشد ", "زمرد", MessageBoxButtons.OK, MessageBoxIcon.Information);
                    this.Close();
                }
                else
                {

                    DrArraySearch[0]["Count"] = Int64.Parse(DrArraySearch[0]["Count"].ToString()) + Int64.Parse(TxtCount.Text);
                    DrArraySearch[0]["PriceAll"] = Int64.Parse(GridRow.Cells[6].Value.ToString()) * Int64.Parse(DrArraySearch[0]["Count"].ToString());
                    Program.FC.TDetailFactor.AcceptChanges();
                    MessageBox.Show("محصول به فاکتور اضافه شد\n تعداد کالاهای داخل فاکتور " + Program.FC.TDetailFactor.Rows.Count.ToString() + " عدد می باشد ", "زمرد", MessageBoxButtons.OK, MessageBoxIcon.Information);
                    this.Close();
                }
                return;
            }










            DataRow [] DrArray = Program.FC.DSFactor.Tables["TDetailFactor"].Select("Id = " + Program.MP.GridViewProduct.CurrentRow.Cells[0].Value.ToString());

            if (DrArray.Length == 0)
            {
                Dr = Program.FC.DSFactor.Tables["TDetailFactor"].NewRow();
                Dr[0] = Int64.Parse(Program.FC.GenerateIDFactor());
                Dr[1] = Int32.Parse(Program.MP.GridViewProduct.CurrentRow.Cells[0].Value.ToString());
                Dr[2] = Program.MP.GridViewProduct.CurrentRow.Cells[1].Value.ToString();
                Dr[3] = Int32.Parse(TxtCount.Text);
                Dr[4] = Double.Parse(Program.MP.GridViewProduct.CurrentRow.Cells[3].Value.ToString());
                Dr[5] = Double.Parse(Program.MP.GridViewProduct.CurrentRow.Cells[4].Value.ToString());
                Dr[6] = Program.MP.GridViewProduct.CurrentRow.Cells[5].Value.ToString();
                Dr[7] = Int64.Parse(Program.MP.GridViewProduct.CurrentRow.Cells[6].Value.ToString());
                Dr[8] = Int64.Parse(Program.MP.GridViewProduct.CurrentRow.Cells[6].Value.ToString()) * Int64.Parse(TxtCount.Text);
                Program.FC.DSFactor.Tables["TDetailFactor"].Rows.Add(Dr);
                MessageBox.Show("محصول به فاکتور اضافه شد\n تعداد کالاهای داخل فاکتور " + Program.FC.DSFactor.Tables["TDetailFactor"].Rows.Count.ToString() + " عدد می باشد ", "زمرد", MessageBoxButtons.OK, MessageBoxIcon.Information);
                this.Close();
            }
            else
            {

                DrArray[0]["Count"] = Int64.Parse(DrArray[0]["Count"].ToString()) + Int64.Parse(TxtCount.Text);
                DrArray[0]["PriceAll"] = Int64.Parse(Program.MP.GridViewProduct.CurrentRow.Cells[6].Value.ToString()) * Int64.Parse(DrArray[0]["Count"].ToString());
                Program.FC.TDetailFactor.AcceptChanges();
                MessageBox.Show("محصول به فاکتور اضافه شد\n تعداد کالاهای داخل فاکتور " + Program.FC.TDetailFactor.Rows.Count.ToString() + " عدد می باشد ", "زمرد", MessageBoxButtons.OK, MessageBoxIcon.Information);
                this.Close();
            }



        }

        private void DialogCount_Load(object sender, EventArgs e)
        {

        }

        private void TxtCount_TextChanged(object sender, EventArgs e)
        {
            if (TxtCount.Text == "") TxtCount.Text = "0";
        }
    }
}
