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 Account : Form
    {
        string StrCode;
        string StrSum;
        string StrSub;
        public Account(string strid , string strsum , string strsub)
        {
            InitializeComponent();
            StrCode = strid;
            StrSum = strsum;
            StrSub = strsub;
        }

        public Account()
        {
            InitializeComponent();
         
        }

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

        private void Account_Load(object sender, EventArgs e)
        {
            DataRow[] DrArray = Program.ACU.DSFactor.Tables["Account"].Select("IdFactor = " + StrCode);

            for (int i = 0; i <= DrArray.Length - 1; i++)
                DS.Tables[0].ImportRow(DrArray[i]);
            txtid.Text = StrCode;

            lblsum.Text = (Int64.Parse(StrSum) - Int64.Parse(StrSub)).ToString();



            for (int i = 0; i <= GridViewAccount.RowCount - 1; i++)
            {
                lblmod.Text = (Int64.Parse(lblmod.Text) + Int64.Parse(GridViewAccount.Rows[i].Cells[2].Value.ToString())).ToString();
            }

            lblmod.Text = (Int64.Parse(lblsum.Text) - Int64.Parse(lblmod.Text)).ToString();
        }

        private void button1_Click(object sender, EventArgs e)
        {


       


            DataRow Dr;
            string strid = Program.ACU.GenerateIDFactor();
            Dr = Program.ACU.DSFactor.Tables["Account"].NewRow();
            Dr["IdFactor"] = txtid.Text;
            Dr["Type"] = txtkind.Text;
            Dr["Price"] = txtprice.Text;
            Dr["Date"] = dateper.Text;
            Dr["Id"] = strid;
            Program.ACU.DSFactor.Tables["Account"].Rows.Add(Dr);
            Program.ACU.DSFactor.Tables["Account"].AcceptChanges();
            Program.ACU.DSFactor.Tables["Account"].WriteXml(Program.ACU.XmlDataFileName);

            ////////////////////////////////////////////////////


            Dr = DS.Tables[0].NewRow();

            Dr["IdFactor"] = txtid.Text;
            Dr["Type"] = txtkind.Text;
            Dr["Price"] = txtprice.Text;
            Dr["Date"] = dateper.Text;
            Dr["Id"] = strid;
            DS.Tables[0].Rows.Add(Dr);
            DS.Tables[0].AcceptChanges();


            lblmod.Text = "0";
            for (int i = 0; i <= GridViewAccount.RowCount - 1; i++)
            {
                lblmod.Text = (Int64.Parse(lblmod.Text) + Int64.Parse(GridViewAccount.Rows[i].Cells[2].Value.ToString())).ToString();
            }
            lblmod.Text = (Int64.Parse(lblsum.Text) - Int64.Parse(lblmod.Text)).ToString();

        }

        private void BtnEdit_Click(object sender, EventArgs e)
        {
            if (MessageBox.Show("آیا شما مطمئن هستید؟", "زمرد", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes)
            {
                DataRow[] DrArray = DS.Tables["Account"].Select("IdFactor = "

                       + txtid.Text + " and " + "Id = " + GridViewAccount.CurrentRow.Cells[0].Value.ToString());

                DrArray[0]["Type"] = txtkind.Text;
                DrArray[0]["Price"] = txtprice.Text;
                DrArray[0]["Date"] = dateper.Text;
                DS.AcceptChanges();


                ////////////////////////////////////////////////////////

                DrArray = Program.ACU.DSFactor.Tables["Account"].Select("IdFactor = "

                      + txtid.Text + " and " + "Id = " + GridViewAccount.CurrentRow.Cells[0].Value.ToString());

                DrArray[0]["Type"] = txtkind.Text;
                DrArray[0]["Price"] = txtprice.Text;
                DrArray[0]["Date"] = dateper.Text;
                Program.ACU.DSFactor.Tables["Account"].AcceptChanges();
                Program.ACU.DSFactor.Tables["Account"].WriteXml(Program.ACU.XmlDataFileName);

                lblmod.Text = "0";
                for (int i = 0; i <= GridViewAccount.RowCount - 1; i++)
                {
                    lblmod.Text = (Int64.Parse(lblmod.Text) + Int64.Parse(GridViewAccount.Rows[i].Cells[2].Value.ToString())).ToString();
                }
                lblmod.Text = (Int64.Parse(lblsum.Text) - Int64.Parse(lblmod.Text)).ToString();
            }

        }

        private void GridViewAccount_Click(object sender, EventArgs e)
        {
            


        }

        private void BtnDel_Click(object sender, EventArgs e)
        {

            if (MessageBox.Show("آیا شما مطمئن هستید؟", "زمرد", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes)
            {

                DataRow[] DrArray = Program.ACU.DSFactor.Tables["Account"].Select("IdFactor = "

                      + txtid.Text + " and " + "Id = " + lblid.Text);

                DrArray[0].Delete();

                Program.ACU.DSFactor.Tables["Account"].AcceptChanges();
                Program.ACU.DSFactor.Tables["Account"].WriteXml(Program.ACU.XmlDataFileName);

                /////////////////////////////////////////////////////////////////////////////////////////////////

                foreach (DataGridViewRow r in GridViewAccount.SelectedRows)
                    GridViewAccount.Rows.Remove(r);
                DS.Tables["Account"].AcceptChanges();

                if (GridViewAccount.RowCount == 0) lblmod.Text = "0";

                lblmod.Text = "0";
                for (int i = 0; i <= GridViewAccount.RowCount - 1; i++)
                {
                    lblmod.Text = (Int64.Parse(lblmod.Text) + Int64.Parse(GridViewAccount.Rows[i].Cells[2].Value.ToString())).ToString();
                }
                lblmod.Text = (Int64.Parse(lblsum.Text) - Int64.Parse(lblmod.Text)).ToString();
            }

        }

        private void GridViewAccount_SelectionChanged(object sender, EventArgs e)
        {
            if (GridViewAccount.RowCount != 0)
            {

                BtnEdit.Enabled = true;
                BtnDel.Enabled = true;

                txtkind.Text = GridViewAccount.CurrentRow.Cells[1].Value.ToString();
                txtprice.Text = GridViewAccount.CurrentRow.Cells[2].Value.ToString();
                dateper.Text = GridViewAccount.CurrentRow.Cells[3].Value.ToString();
                lblid.Text = GridViewAccount.CurrentRow.Cells[0].Value.ToString();
            }

            else
            {

                BtnEdit.Enabled = false;
                BtnDel.Enabled = false;
            }
        }

        private void button1_Click_1(object sender, EventArgs e)
        {
            txtkind.Text = "";
            txtprice.Text = "";
            dateper.Text = "";

        }
    }
}
