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;
using System.IO;
using BehComponents;
namespace Stone
{
    public partial class ADD_EditProducts : Form
    {
        string XmlDataFileName = System.Environment.GetFolderPath(System.Environment.SpecialFolder.ApplicationData) + "\\ParsZSDB" + "\\Products.xml";
        string XmlSchemaFileName = System.Environment.GetFolderPath(System.Environment.SpecialFolder.ApplicationData) + "\\ParsZSDB" + "\\Products.xsd";

        DataGridViewRow GridRow = null;
        DataRow Dr;
        public ADD_EditProducts()
        {
            InitializeComponent();
         
        }

        public ADD_EditProducts(DataGridViewRow GridRowEdit)
        {
            InitializeComponent();
            GridRow = GridRowEdit;

        }

        private void button3_Click(object sender, EventArgs e)
        {
            this.Close();
            this.StrIndex = "";
        }

        private void ADD_EditProducts_Load(object sender, EventArgs e)
        {
            if (GridRow != null)
            {
                txtid.Text = GridRow.Cells[0].Value.ToString();
                txtkind.Text = GridRow.Cells[1].Value.ToString();
                txtcount.Text = GridRow.Cells[2].Value.ToString();
                txtlength.Text = GridRow.Cells[3].Value.ToString();
                txtwidth.Text = GridRow.Cells[4].Value.ToString();
                txtmeasure.Text = GridRow.Cells[5].Value.ToString();
                txtprice.Text = GridRow.Cells[6].Value.ToString();

                dateper.Text = GridRow.Cells[7].Value.ToString();
                return;
            }
           
            if (this.StrIndex == "Edit")
            {
                    txtid.Text = Program.MP.GridViewProduct.CurrentRow.Cells[0].Value.ToString();
                    txtkind.Text = Program.MP.GridViewProduct.CurrentRow.Cells[1].Value.ToString();
                    txtcount.Text = Program.MP.GridViewProduct.CurrentRow.Cells[2].Value.ToString();
                    txtlength.Text = Program.MP.GridViewProduct.CurrentRow.Cells[3].Value.ToString();
                    txtwidth.Text = Program.MP.GridViewProduct.CurrentRow.Cells[4].Value.ToString();
                    txtmeasure.Text = Program.MP.GridViewProduct.CurrentRow.Cells[5].Value.ToString();
                    txtprice.Text = Program.MP.GridViewProduct.CurrentRow.Cells[6].Value.ToString();
                  
                    dateper.Text = Program.MP.GridViewProduct.CurrentRow.Cells[7].Value.ToString();
            }

            if (this.StrIndex == "Add")
            {
                Dr = Program.MP.DS.Tables["Products"].NewRow();
                txtid.Text = Dr["Id"].ToString();
            }
        }
        private void btnreg_Click(object sender, EventArgs e)
        {
            if (txtkind.Text == "" && txtcount.Text == "0" && txtlength.Text == "0" && txtwidth.Text == "0" && txtmeasure.Text == "" && txtprice.Text == "0")
            {
                MessageBox.Show("حداقل یکی از کادرها را پر کنید", "زمرد", MessageBoxButtons.OK, MessageBoxIcon.Information);
                return;
            }

            if (GridRow != null)
            {

                DataRow[] DrArray = Program.MP.DS.Tables["Products"].Select("Id = " + txtid.Text);
              DrArray[0]["KindStone"] = txtkind.Text;
                   DrArray[0]["Count"] = Int32.Parse(txtcount.Text);
                   DrArray[0]["Length"] = Double.Parse(txtlength.Text);
                   DrArray[0]["Width"] = Double.Parse(txtwidth.Text);
                   DrArray[0]["Measure"] = txtmeasure.Text;
                   DrArray[0]["Price"] = Int64.Parse(txtprice.Text);
                   DrArray[0]["Date"] = dateper.Text;
              
                   Program.MP.DS.Tables["Products"].AcceptChanges();
                   Program.MP.DS.Tables["Products"].WriteXml(XmlDataFileName);


                DrArray = Program.MP.DS.Tables["SearchProducts"].Select("Id = " + txtid.Text);
                   DrArray[0]["KindStone"] = txtkind.Text;
                   DrArray[0]["Count"] = Int32.Parse(txtcount.Text);
                   DrArray[0]["Length"] = Double.Parse(txtlength.Text);
                   DrArray[0]["Width"] = Double.Parse(txtwidth.Text);
                   DrArray[0]["Measure"] = txtmeasure.Text;
                   DrArray[0]["Price"] = Int64.Parse(txtprice.Text);
                   DrArray[0]["Date"] = dateper.Text;
              
                   Program.MP.DS.Tables["SearchProducts"].AcceptChanges();
                



                   MessageBox.Show("داده ها با موفقیت ثبت شد", "", MessageBoxButtons.OK, MessageBoxIcon.Information);
                   Program.MP.LblAllProducts.Text = Program.MP.BS.Count.ToString() + " عدد ";
            
                
                
                return;
            }






            if (this.StrIndex == "Add")
            {
                Dr["KindStone"] = txtkind.Text;
                Dr["Count"] = Int32.Parse(txtcount.Text);
                Dr["Length"] = Double.Parse(txtlength.Text);
                Dr["Width"] = Double.Parse(txtwidth.Text);
                Dr["Measure"] = txtmeasure.Text;
                Dr["Price"] = Int64.Parse(txtprice.Text);
                Dr["Date"] = dateper.Text;
           
              
                Program.MP.DS.Tables["Products"].Rows.Add(Dr);
                Program.MP.DS.Tables["Products"].WriteXml(XmlDataFileName);

                MessageBox.Show("داده ها با موفقیت ثبت شد", "زمرد", MessageBoxButtons.OK, MessageBoxIcon.Information);
                Program.MP.LblAllProducts.Text = Program.MP.BS.Count.ToString() + " عدد ";


                Dr = Program.MP.DS.Tables["Products"].NewRow();
                txtid.Text = Dr["Id"].ToString();
                txtkind.Text = "";
                txtcount.Text = "0";
                txtlength.Text = "0";
                txtwidth.Text = "0";
                txtmeasure.Text = "";
                txtprice.Text = "0";
                dateper.Text = "";


            }


            if (this.StrIndex == "Edit")
            {
             DataRow[] DrArray = Program.MP.DS.Tables["Products"].Select("Id = " + txtid.Text);
              DrArray[0]["KindStone"] = txtkind.Text;
                   DrArray[0]["Count"] = Int32.Parse(txtcount.Text);
                   DrArray[0]["Length"] = Double.Parse(txtlength.Text);
                   DrArray[0]["Width"] = Double.Parse(txtwidth.Text);
                   DrArray[0]["Measure"] = txtmeasure.Text;
                   DrArray[0]["Price"] = Int64.Parse(txtprice.Text);
                   DrArray[0]["Date"] = dateper.Text;
              
                   Program.MP.DS.Tables["Products"].AcceptChanges();
                   Program.MP.DS.Tables["Products"].WriteXml(XmlDataFileName);


                   MessageBox.Show("داده ها با موفقیت ثبت شد", "", MessageBoxButtons.OK, MessageBoxIcon.Information);
                   Program.MP.LblAllProducts.Text = Program.MP.BS.Count.ToString() + " عدد ";
            }

        }

        private void button1_Click(object sender, EventArgs e)
        {
            txtkind.Text = "";
            txtcount.Text = "0";
            txtlength.Text = "0";
            txtwidth.Text = "0";
            txtmeasure.Text = "";
            txtprice.Text = "0";
            dateper.Text = "";
        }

        private void textBox1_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (!(char.IsDigit(e.KeyChar) || char.IsControl(e.KeyChar) || (e.KeyChar == (char)46)))
            {
                e.Handled = true;
            }

        }

        private void txtwidth_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (!(char.IsDigit(e.KeyChar) || char.IsControl(e.KeyChar) || (e.KeyChar == (char)46)))
            {
                e.Handled = true;
            }
        }

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

        private void txtlength_TextChanged(object sender, EventArgs e)
        {
            if (txtlength.Text == "") txtlength.Text = "0";
            try
            {
                txtmeasure.Text = (double.Parse(txtlength.Text) * double.Parse(txtwidth.Text)).ToString();
            }
            catch (Exception ex)
            {}
        }

    


        private void txtwidth_TextChanged(object sender, EventArgs e)
        {
            if (txtwidth.Text == "") txtwidth.Text = "0";
            try
            {
                txtmeasure.Text = (double.Parse(txtlength.Text) * double.Parse(txtwidth.Text)).ToString();
            }
            catch (Exception ex)
            { }
        }

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

        private void dateper_MaskInputRejected(object sender, MaskInputRejectedEventArgs e)
        {

        }

        private void txtmeasure_TextChanged(object sender, EventArgs e)
        {
            string strmeasure = txtmeasure.Text;


            string[] split = strmeasure.Split(new Char[] { '.' });

            if (split.Length == 1) return;

            if (Int32.Parse(split[1]) < 100)
            {
                return;
            }

            if (Int32.Parse(split[1]) < 1000)
            {
                split[1] = (Int32.Parse(split[1]) / 10).ToString();
                txtmeasure.Text = split[0] + "." +split[1];
                return;
            }


            if (Int32.Parse(split[1]) < 10000)
            {
                split[1] = (Int32.Parse(split[1]) / 10).ToString();
                split[1] = (Int32.Parse(split[1]) / 10).ToString();
                txtmeasure.Text = split[0] + "." + split[1];
                return;
            }  

        }

     

    
    }
}
