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 Stone.Class;
namespace Stone
{
    public partial class ManagmentProducts : Form
    {
        Waiting wt = new Waiting();

        string XmlDataFileName = System.Environment.GetFolderPath(System.Environment.SpecialFolder.ApplicationData) + "\\ParsZSDB" + "\\Products.xml";
        string XmlSchemaFileName = System.Environment.GetFolderPath(System.Environment.SpecialFolder.ApplicationData) + "\\ParsZSDB" + "\\Products.xsd";
        public DataRow Dr;
        public ManagmentProducts()
        {
            InitializeComponent();
        }

        private void ManagmentProducts_Load(object sender, EventArgs e)
        {
           
            try
            {

              
           
                LblAllProducts.Text = BS.Count.ToString() + " عدد ";
                if (GridViewProduct.RowCount != 0)
                {

                    if (GridViewProduct.CurrentRow != null)
                    LblCurrentProduct.Text = GridViewProduct.CurrentRow.Cells[0].Value.ToString() + " - " + GridViewProduct.CurrentRow.Cells[1].Value.ToString();
                }

                if (GridViewProduct.RowCount != 0)
                {

                    BtnEdit.Enabled = true;
                    BtnDelCurrent.Enabled = true;
                    BtnSearch.Enabled = true;
                    BtnAddToFactor.Enabled = true;
                    PrePrint.Enabled = true;
                }
                else
                {
                    BtnEdit.Enabled = false;
                    BtnDelCurrent.Enabled = false;
                    BtnSearch.Enabled = false;
                    BtnAddToFactor.Enabled = false;
                    PrePrint.Enabled = false;
                }
                if (GridViewProduct.RowCount != 0)
                GridViewProduct.Sort(GridViewProduct.Columns[0], ListSortDirection.Ascending);
            }

            catch (Exception ex)
            {
            
            MessageBox.Show(ex.Message + "           "  + ex.TargetSite + "          " );
            
            }
        }

      

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


        private void toolStripButton7_Click(object sender, EventArgs e)
        {
            ADD_EditProducts AddEdit = new ADD_EditProducts();
            AddEdit.StrIndex = "Add";
            AddEdit.ShowDialog();
        }



      

        private void BtnEdit_Click(object sender, EventArgs e)
        {
            ADD_EditProducts AddEdit = new ADD_EditProducts();
            AddEdit.StrIndex = "Edit";
            AddEdit.ShowDialog();
        }

        private void GridViewProduct_SelectionChanged(object sender, EventArgs e)
        {
            if (GridViewProduct.RowCount != 0)
            {
                LblCurrentProduct.Text = GridViewProduct.CurrentRow.Cells[0].Value.ToString() + " - " + GridViewProduct.CurrentRow.Cells[1].Value.ToString();
                LblAllProducts.Text = BS.Count.ToString() + " عدد ";
            }

            if (GridViewProduct.RowCount != 0)
            {

                BtnEdit.Enabled = true;
                BtnDelCurrent.Enabled = true;
                BtnSearch.Enabled = true;
                BtnAddToFactor.Enabled = true;
                PrePrint.Enabled = true;   
            }
            else
            {
                BtnEdit.Enabled = false;
                BtnDelCurrent.Enabled = false;
                BtnSearch.Enabled = false;
                BtnAddToFactor.Enabled = false;
                PrePrint.Enabled = false;
            }
         
        }


        private void BtnDelCurrent_Click(object sender, EventArgs e)
        {

            if (MessageBox.Show("آیا شما مطمئن هستید؟", "زمرد", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes)
            {
               
            wt.CreateInForm(typeof(ManagmentProducts), this, true, false, "DoWorkInForm");
               
            LblAllProducts.Text = BS.Count.ToString() + " عدد ";
            Program.MP.GridViewProduct.Refresh();
            }
          

        }

        public void DoWorkInForm()
        {
            

                foreach (DataGridViewRow r in GridViewProduct.SelectedRows)
                    GridViewProduct.Rows.Remove(r);
                DS.Tables["Products"].AcceptChanges();
                DS.Tables["Products"].WriteXml(XmlDataFileName);
            

        }

        private void BtnALLProduct_Click_1(object sender, EventArgs e)
        {
            GridViewProduct.DataSource = DS.Tables["Products"];
        }

        private void BtnSearch_Click_1(object sender, EventArgs e)
        {
            FrmSearchProducts FSM = new FrmSearchProducts();
            FSM.ShowDialog();
        }

        private void BtnAddToFactor_Click(object sender, EventArgs e)
        {
            DialogCount Dc = new DialogCount();
            Dc.ShowDialog();
        }

        private void PrePrint_Click(object sender, EventArgs e)
        {
            Stimulsoft.Report.StiReport report = new Stimulsoft.Report.StiReport();
            report.Load(Application.StartupPath + "\\Products.mrt");
            report.RegData(DS);
            report.Show();

         
        }

        private void toolStripButton1_Click(object sender, EventArgs e)
        {
            if (Program.F == null)
            {
                Program.F = new Factor();
                Program.F.Show();
                return;
            }

            if (Program.F.IsDisposed)
            {
                Program.F = new Factor();
                Program.F.Show();
                return;
            }

            Program.F.Show();


        }

        private void MnuAdd_Click(object sender, EventArgs e)
        {
            toolStripButton7_Click(sender, e);
        }

        private void MnuEdit_Click(object sender, EventArgs e)
        {
            BtnEdit_Click(sender, e);
        }

        private void MnuDel_Click(object sender, EventArgs e)
        {
            BtnDelCurrent_Click(sender,  e);
        }

        private void MnuAddToFactor_Click(object sender, EventArgs e)
        {
             BtnAddToFactor_Click(sender,  e);
        }

    
    }
}
