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.Data.SqlClient;
using System.Xml;
namespace Stone
{
    public partial class FrmSearchFactors : Form
    {

       
        string strlength = "";
     
        public string StrCode = "";
        public string StrPrice = "";
        public string StrMod = "";
        public FrmSearchFactors()
        {
            InitializeComponent();
        }

        private void btnSearch_Click(object sender, EventArgs e)
        {

            StrCode = "";
            StrPrice = "";
            StrMod = "";

            #region Collection Information For Search

            #region CodeFactor

            if (checkBoxCode.Checked == true)
            {
                if (radioButtonBigThan.Checked == true && txtCode1.TextLength > 0)
                {
                    StrCode = StrCode + "IdFactorProperties>" + txtCode1.Text; 
                }

                if (radioButtonLowThan.Checked == true && txtCode1.TextLength > 0)
                {
                    StrCode = StrCode + "IdFactorProperties<" + txtCode1.Text; 
                }

                if (radioButtonEqualThan.Checked == true && txtCode1.TextLength > 0)
                {
                    StrCode = StrCode + "IdFactorProperties=" + txtCode1.Text; 
                }

                if (radioButtonBetween.Checked == true && txtCode1.TextLength > 0 && txtCode2.TextLength > 0)
                {
                    StrCode = StrCode + "IdFactorProperties>" + txtCode1.Text + " and IdFactorProperties<" + txtCode2.Text;
                }

                if (StrCode == "") {MessageBox.Show("لطفاً موارد مربوط به کد را به درستی وارد کنید", "زمرد", MessageBoxButtons.OK, MessageBoxIcon.Information); return;}
            }

            #endregion

            #region Mod

            if (CheckBoxMod.Checked == true)
            {
                if (radioButtonWidthBigThan.Checked == true && txtMod1.TextLength > 0)
                {
                   StrMod = StrMod + "Mod>" + txtMod1.Text; 
                }

                if (radioButtonWidthLowThan.Checked == true && txtMod1.TextLength > 0)
                {
                    StrMod = StrMod + "Mod<" + txtMod1.Text;
                }

                if (radioButtonWidthEqual.Checked == true && txtMod1.TextLength > 0)
                {
                    StrMod = StrMod + "Mod=" + txtMod1.Text; 
                }

                if (radioButtonWidthBetween.Checked == true && txtMod1.TextLength > 0 && txtMod2.TextLength > 0)
                {
                     StrMod = StrMod + "Mod>" + txtMod1.Text + " and Mod<" + txtMod2.Text; 
                }
                if (StrMod == "") {MessageBox.Show("لطفاً موارد مربوط به باقی مانده را به درستی وارد کنید", "زمرد", MessageBoxButtons.OK, MessageBoxIcon.Information); return;}

            }

#endregion
         
            #region Price
            if (checkBoxPrice.Checked == true)
            {
                if (radioButtonHeightBigThan.Checked == true && txtPrice1.TextLength > 0)
                {
                   StrPrice = StrPrice + "PriceFactor>" + txtPrice1.Text; 
                }

                if (radioButtonHeightLowThan.Checked == true && txtPrice1.TextLength > 0)
                {
                    StrPrice = StrPrice + "PriceFactor<" + txtPrice1.Text; 
                }

                if (radioButtonHeightEqual.Checked == true && txtPrice1.TextLength > 0)
                {
                  StrPrice = StrPrice + "PriceFactor=" + txtPrice1.Text;
                }

                if (radioButtonHeightBetween.Checked == true && txtPrice1.TextLength > 0 && txtPrice2.TextLength > 0)
                {
                    StrPrice = StrPrice + "PriceFactor>" + txtPrice1.Text + " and PriceFactor<" + txtPrice2.Text;  
                }
                if (StrPrice == "") { MessageBox.Show("لطفاً موارد مربوط به قیمت را به درستی وارد کنید", "زمرد", MessageBoxButtons.OK, MessageBoxIcon.Information); return; }
            }

            #endregion

            #region Name - Tel - Date
            if (checkBoxName.Checked)
            {
            if (txtName.Text == "")
            {
                MessageBox.Show("لطفاً موارد مربوط به نام خریدار را به درستی وارد کنید", "زمرد", MessageBoxButtons.OK, MessageBoxIcon.Information);
                
                return; 
            }
            }

            if (checkBoxDate.Checked)
            {
                if (txtDate.Text == "")
                {
                    MessageBox.Show("لطفاً موارد مربوط به تاریخ را به درستی وارد کنید", "زمرد", MessageBoxButtons.OK, MessageBoxIcon.Information);

                    return;
                }
            }

            if (checkBoxTel.Checked)
            {
                if (txtTel.Text == "")
                {
                    MessageBox.Show("لطفاً موارد مربوط به تلفن را به درستی وارد کنید", "زمرد", MessageBoxButtons.OK, MessageBoxIcon.Information);

                    return;
                }
            }
            #endregion

            if (!checkBoxName.Checked && !checkBoxPrice.Checked && !CheckBoxMod.Checked && !checkBoxTel.Checked && !checkBoxDate.Checked && !checkBoxCode.Checked)
            { MessageBox.Show("لطفاً یکی موارد را انتخاب کنید", "زمرد", MessageBoxButtons.OK, MessageBoxIcon.Information); return; }


            #endregion
            DSForOnlyOneFactor.Tables["TDetailFactor"].Clear();
            DSForPrint.Tables["TDetailFactorManagment"].Clear();
            DSForPrint.Tables["TPropertiesFactor"].Clear();
            DSHelp.Tables["TPropertiesFactor"].Clear();
            BGW.RunWorkerAsync();
          
           
        }

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

        private void checkBox1_CheckedChanged(object sender, EventArgs e)
        {
            if (checkBoxCode.Checked == true)
            {
                groupBoxCode.Enabled = true;
               
            }
            else
            {

                groupBoxCode.Enabled = false;
                radioButtonBetween.Checked = false;
                radioButtonBigThan.Checked = false;
                radioButtonEqualThan.Checked = false;
                radioButtonLowThan.Checked = false;
                txtCode1.Text = "";
                txtCode2.Text = "";


            }
        }

        private void checkBoxKind_CheckedChanged(object sender, EventArgs e)
        {
            if (checkBoxName.Checked == true)
            {
                groupBoxKind.Enabled = true;
            }
            else
            {
                groupBoxKind.Enabled = false;
                txtName.Text = "";

            }
        }

        private void checkBoxCount_CheckedChanged(object sender, EventArgs e)
        {
            if (checkBoxTel.Checked == true)
            {
                groupBoxCount.Enabled = true;

            }
            else {
                groupBoxCount.Enabled = false;
                txtTel.Text = "";
            }
          
        }

        private void radioButtonBetween_CheckedChanged(object sender, EventArgs e)
        {
            if (radioButtonBetween.Checked == true)
            {
                txtCode2.Enabled = true;
            }
            else
            {
                txtCode2.Enabled = false;
                txtCode2.Text = "";
            }
        }

        private void radioButtonCountBetween_CheckedChanged(object sender, EventArgs e)
        {
            
        }

        private void checkBoxWidthHeight_CheckedChanged(object sender, EventArgs e)
        {
            if (checkBoxPrice.Checked == true)
            {
                groupBoxWidthHeight.Enabled = true;
            }
            else
            {
                groupBoxWidthHeight.Enabled = false;

                radioButtonHeightBetween.Checked = false;
                radioButtonHeightBigThan.Checked = false;
                radioButtonHeightEqual.Checked = false;
                radioButtonHeightLowThan.Checked = false;
                txtPrice1.Text = "";
                txtPrice2.Text = "";


                radioButtonWidthBetween.Checked = false;
                radioButtonWidthBigThan.Checked = false;
                radioButtonWidthEqual.Checked = false;
                radioButtonWidthLowThan.Checked = false;
                txtMod1.Text = "";
                txtMod2.Text = "";




            }
        }

        private void radioButtonWidthBetween_CheckedChanged(object sender, EventArgs e)
        {
            if (radioButtonWidthBetween.Checked == true)
            {
                txtMod2.Enabled = true;
            }
            else
            {
                txtMod2.Enabled = false;
                txtMod2.Text = "";
            }
        }

        private void checkBoxMeasure_CheckedChanged(object sender, EventArgs e)
        {
            if (checkBoxDate.Checked == true)
            {
                groupBoxMeasure.Enabled = true;
            }
            else
            {
                groupBoxMeasure.Enabled = false;
                txtDate.Text = "";

            }
        }

        private void checkBoxDate_CheckedChanged(object sender, EventArgs e)
        {

        }

        private void radioButtonHeightBetween_CheckedChanged(object sender, EventArgs e)
        {
            if (radioButtonHeightBetween.Checked == true)
            {
                txtPrice2.Enabled = true;
            }
            else
            {
                txtPrice2.Enabled = false;
                txtPrice2.Text = "";
            }
        }

        private void radioButtonPriceBetween_CheckedChanged(object sender, EventArgs e)
        {

        }

        private void checkBoxPrice_CheckedChanged(object sender, EventArgs e)
        {

        }

        private void groupBoxWidthHeight_Enter(object sender, EventArgs e)
        {

        }

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

                BtnEdit.Enabled = true;
                BtnDelCurrent.Enabled = true;

                PrePrintOne.Enabled = true;
             
                PrePrintAll.Enabled = true;
            }
            else
            {

                BtnEdit.Enabled = false;
                BtnDelCurrent.Enabled = false;

                PrePrintOne.Enabled = false;
          
                PrePrintAll.Enabled = false;
            }
        }

        private void BtnEdit_Click(object sender, EventArgs e)
        {
            EditPropertiesFactor EditWindow = new EditPropertiesFactor(GridViewProperties.CurrentRow);
            EditWindow.StrIndex = "Edit";
            EditWindow.ShowDialog();
        }

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

                foreach (DataGridViewRow r in GridViewProperties.SelectedRows)
                {

                    DataRow[] DrArray = Program.FC.DSFactor.Tables["TDetailFactorManagment"].Select("IdFactor = " + GridViewProperties.CurrentRow.Cells[0].Value.ToString());
                    int Length = DrArray.Length;
                    while (Length != 0)
                    {
                        int count = Program.FC.DSFactor.Tables["TDetailFactorManagment"].Rows.Count - 1;

                        for (int i = 0; i <= count; i++)
                        {
                            if (r.Cells[0].Value.ToString() == Program.FC.DSFactor.Tables["TDetailFactorManagment"].Rows[i][0].ToString())
                            {
                                Program.FC.DSFactor.Tables["TDetailFactorManagment"].Rows[i].Delete();
                                Program.FC.DSFactor.Tables["TDetailFactorManagment"].Rows[i].AcceptChanges();
                                break;
                            }
                        }
                        Length--;
                    }

                    Program.FC.DSFactor.Tables["TPropertiesFactor"].Rows[FindIndexRow(r)].Delete();
                    GridViewProperties.Rows.Remove(r);
                    Program.MGF.DS.Clear();
                }
                Program.FC.DSFactor.Tables["TPropertiesFactor"].AcceptChanges();
                Program.FC.DSFactor.Tables["TPropertiesFactor"].WriteXml(Program.FC.XmlDataFileNamePropertiesFactor);

                Program.FC.DSFactor.Tables["TDetailFactorManagment"].AcceptChanges();
                Program.FC.DSFactor.Tables["TDetailFactorManagment"].WriteXml(Program.FC.XmlDataFileNameDetailFactor);
                DSForOnlyOneFactor.Tables["TDetailFactor"].Clear();
            
            }




        }

        private int FindIndexRow(DataGridViewRow r)
        {
            for (int i=0; i <=   Program.FC.DSFactor.Tables["TPropertiesFactor"].Rows.Count - 1;i++)
            if (r.Cells[0].Value.ToString() == Program.FC.DSFactor.Tables["TPropertiesFactor"].Rows[i][0].ToString())
            {

                return i;
            }
            return -1;
        }

        private void BtnDelAllProducts_Click(object sender, EventArgs e)
        {


        }

        private void BtnFactorShow_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 BtnAddToFactor_Click(object sender, EventArgs e)
        {
            DialogCount DC = new DialogCount(GridViewProperties.CurrentRow);
            DC.ShowDialog();
        }

        private void PrePrint_Click(object sender, EventArgs e)
        {
            if (DSForOnlyOneFactor.Tables["TDetailFactor"].Rows.Count == 0)
            {
                MessageBox.Show("لطفاً سطری را انتخاب کنید", "زمرد", MessageBoxButtons.OK, MessageBoxIcon.Information);
                return;
            }
            CopyInTPropertisHelp();
            
            DSForOnlyOneFactorPrint.Tables.Clear();
            DSForOnlyOneFactorPrint.Tables.Add(Program.FC.DSFactor.Tables["PropertiesHelp"].Copy());
            DSForOnlyOneFactorPrint.Tables.Add(DSForOnlyOneFactor.Tables["TDetailFactor"].Copy());
            DSForOnlyOneFactorPrint.Tables.Add(Program.FC.DSFactor.Tables["Information"].Copy());

            stiReport1.Load(Application.StartupPath + "\\FactorPrint.mrt");
            stiReport1.RegData(DSForOnlyOneFactorPrint);
            stiReport1.Show();
        }


        private void CopyInTPropertisHelp()
        {

            Program.FC.DSFactor.Tables["PropertiesHelp"].Clear();
            DataRow dr = Program.FC.DSFactor.Tables["PropertiesHelp"].NewRow();
            dr["IdFactorProperties"] = GridViewProperties.CurrentRow.Cells[0].Value.ToString();
            dr["Name"] = GridViewProperties.CurrentRow.Cells[1].Value.ToString();
            dr["Tel"] = GridViewProperties.CurrentRow.Cells[2].Value.ToString();
            dr["Address"] = GridViewProperties.CurrentRow.Cells[7].Value.ToString();
            dr["PriceFactor"] = GridViewProperties.CurrentRow.Cells[3].Value.ToString();
            dr["Mod"] = GridViewProperties.CurrentRow.Cells[4].Value.ToString();
            dr["Date"] = GridViewProperties.CurrentRow.Cells[6].Value.ToString();
            dr["StringMoney"] = GridViewProperties.CurrentRow.Cells[5].Value.ToString();
            Program.FC.DSFactor.Tables["PropertiesHelp"].Rows.Add(dr);

        }

        private void txtCode1_KeyPress(object sender, KeyPressEventArgs e)
        {
            e.Handled = (char.IsNumber(e.KeyChar) || char.IsControl(e.KeyChar) || char.IsPunctuation(e.KeyChar)) ? false : true;
        }

        private void txtCode2_KeyPress(object sender, KeyPressEventArgs e)
        {
            e.Handled = (char.IsNumber(e.KeyChar) || char.IsControl(e.KeyChar) || char.IsPunctuation(e.KeyChar)) ? false : true;
        }

        private void txtCount1_KeyPress(object sender, KeyPressEventArgs e)
        {
            e.Handled = (char.IsNumber(e.KeyChar) || char.IsControl(e.KeyChar)) ? false : true;
        }

        private void txtCount2_KeyPress(object sender, KeyPressEventArgs e)
        {
            e.Handled = (char.IsNumber(e.KeyChar) || char.IsControl(e.KeyChar)) ? false : true;
        }

        private void txtHeight1_KeyPress(object sender, KeyPressEventArgs e)
        {
            e.Handled = (char.IsNumber(e.KeyChar) || char.IsControl(e.KeyChar) || char.IsPunctuation(e.KeyChar)) ? false : true;
        }

        private void txtHeight2_KeyPress(object sender, KeyPressEventArgs e)
        {
            e.Handled = (char.IsNumber(e.KeyChar) || char.IsControl(e.KeyChar) || char.IsPunctuation(e.KeyChar)) ? false : true;
        }

        private void txtWidth1_KeyPress(object sender, KeyPressEventArgs e)
        {
            e.Handled = (char.IsNumber(e.KeyChar) || char.IsControl(e.KeyChar) || char.IsPunctuation(e.KeyChar)) ? false : true;
        }

        private void txtWidth2_KeyPress(object sender, KeyPressEventArgs e)
        {
            e.Handled = (char.IsNumber(e.KeyChar) || char.IsControl(e.KeyChar) || char.IsPunctuation(e.KeyChar)) ? false : true;
        }

        private void txtPrice1_KeyPress(object sender, KeyPressEventArgs e)
        {
            e.Handled = (char.IsNumber(e.KeyChar) || char.IsControl(e.KeyChar)) ? false : true;
        }

        private void txtPrice2_KeyPress(object sender, KeyPressEventArgs e)
        {
            e.Handled = (char.IsNumber(e.KeyChar) || char.IsControl(e.KeyChar)) ? false : true;
        }



        private void BGW_ProgressChanged(object sender, ProgressChangedEventArgs e)
        {


        }

        private void BGW_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
        {
             GridViewProperties.DataSource = DSForPrint.Tables["TPropertiesFactor"];
         if (GridViewProperties.RowCount != 0)
         {
            GridViewProperties.Sort(GridViewProperties.Columns[0], ListSortDirection.Ascending);
            GridViewProperties.Columns[0].HeaderText = "کــد";
            GridViewProperties.Columns[0].AutoSizeMode = DataGridViewAutoSizeColumnMode.DisplayedCells;
            GridViewProperties.Columns[0].DefaultCellStyle.ForeColor = Color.Red;
            GridViewProperties.Columns[1].HeaderText = "نام و نام خــانوادگی";
            GridViewProperties.Columns[1].AutoSizeMode = DataGridViewAutoSizeColumnMode.DisplayedCells;
            GridViewProperties.Columns[2].HeaderText = "تـلفن";
            GridViewProperties.Columns[2].AutoSizeMode = DataGridViewAutoSizeColumnMode.DisplayedCells;
            GridViewProperties.Columns[3].HeaderText = "قیـمت کـل";
            GridViewProperties.Columns[3].AutoSizeMode = DataGridViewAutoSizeColumnMode.DisplayedCells;
            GridViewProperties.Columns[4].HeaderText = "باقی مانده";
            GridViewProperties.Columns[4].AutoSizeMode = DataGridViewAutoSizeColumnMode.DisplayedCells;
            GridViewProperties.Columns[5].HeaderText = "قـیمت به حـروف";
            GridViewProperties.Columns[5].AutoSizeMode = DataGridViewAutoSizeColumnMode.DisplayedCells;
            GridViewProperties.Columns[5].Visible = false;
            GridViewProperties.Columns[6].HeaderText = "تـاریخ";
            GridViewProperties.Columns[6].AutoSizeMode = DataGridViewAutoSizeColumnMode.DisplayedCells;
            GridViewProperties.Columns[7].HeaderText = "آدرس";
            GridViewProperties.Columns[7].AutoSizeMode = DataGridViewAutoSizeColumnMode.Fill;
            GridViewProperties.Columns[8].Visible = false;}
            MessageBox.Show("جستجو به اتمام رسید", "زمرد", MessageBoxButtons.OK, MessageBoxIcon.Information);
        }

        private void BGW_DoWork(object sender, DoWorkEventArgs e)
        {

            int counter = 0;

            #region Search By Name
            if (checkBoxName.Checked)
            {
                if (counter == 0)
                {
                    XmlDocument xmlDoc = new XmlDocument();
                    XmlNode rootNode;
                    xmlDoc.Load(Program.FC.XmlDataFileNamePropertiesFactor);
                    rootNode = xmlDoc.DocumentElement;
                    XmlNodeList PropertiesNodes = xmlDoc.GetElementsByTagName("TPropertiesFactor");

                    foreach (XmlNode node in PropertiesNodes)
                    {

                        long IdFactorProperties = Int64.Parse(node["IdFactorProperties"].InnerText);
                        string Name = node["Name"].InnerText;
                        long Tel = Int64.Parse(node["Tel"].InnerText);
                        string Address = node["Address"].InnerText;
                        long PriceFactor = Int64.Parse(node["PriceFactor"].InnerText);
                        long Mod = Int64.Parse(node["Mod"].InnerText);
                        string Date = node["Date"].InnerText;
                        string StringMoney = node["StringMoney"].InnerText;

                        if (OperateOnStringName(Name))
                        {

                            DataRow dr = DSHelp.Tables["TPropertiesFactor"].NewRow();
                            dr["IdFactorProperties"] = IdFactorProperties.ToString();
                            dr["Name"] = Name;
                            dr["Tel"] = Tel.ToString();
                            dr["Address"] = Address;
                            dr["PriceFactor"] = PriceFactor.ToString();
                            dr["Mod"] = Mod.ToString();
                            dr["Date"] = Date;
                            dr["StringMoney"] = StringMoney;
                            DSHelp.Tables["TPropertiesFactor"].Rows.Add(dr);
                            DSHelp.Tables["TPropertiesFactor"].AcceptChanges();

                        }

                    }
                }

                else
                {

                    DSHelp.Tables["TPropertiesFactor"].Clear();
                    for (int lng = 0; lng <= DSForPrint.Tables["TPropertiesFactor"].Rows.Count - 1; lng++)
                    {

                        if (OperateOnStringName(DSForPrint.Tables["TPropertiesFactor"].Rows[lng][1].ToString()))
                        {
                            DataRow dr = DSHelp.Tables["TPropertiesFactor"].NewRow();
                            dr["IdFactorProperties"] = DSForPrint.Tables["TPropertiesFactor"].Rows[lng][0].ToString();
                            dr["Name"] = DSForPrint.Tables["TPropertiesFactor"].Rows[lng][1].ToString();
                            dr["Tel"] = DSForPrint.Tables["TPropertiesFactor"].Rows[lng][2].ToString();
                            dr["Address"] = DSForPrint.Tables["TPropertiesFactor"].Rows[lng][7].ToString();
                            dr["PriceFactor"] = DSForPrint.Tables["TPropertiesFactor"].Rows[lng][3].ToString();
                            dr["Mod"] = DSForPrint.Tables["TPropertiesFactor"].Rows[lng][4].ToString();
                            dr["Date"] = DSForPrint.Tables["TPropertiesFactor"].Rows[lng][6].ToString();
                            dr["StringMoney"] = DSForPrint.Tables["TPropertiesFactor"].Rows[lng][5].ToString();
                            DSHelp.Tables["TPropertiesFactor"].Rows.Add(dr);
                            DSHelp.Tables["TPropertiesFactor"].AcceptChanges();


                        }
                    }
                  
                }

                DSForPrint.Relations.Clear();
                DSForPrint.Tables["TDetailFactorManagment"].Constraints.Clear();
                DSForPrint.Tables.Remove("TPropertiesFactor");

                DSForPrint.Tables.Add(DSHelp.Tables["TPropertiesFactor"].Copy());
                CreateRelation();



                counter++;
            }

            #endregion

            #region Search By Tel
            if (checkBoxTel.Checked)
            {
                if (counter == 0)
                {
                    XmlDocument xmlDoc = new XmlDocument();
                    XmlNode rootNode;
                    xmlDoc.Load(Program.FC.XmlDataFileNamePropertiesFactor);
                    rootNode = xmlDoc.DocumentElement;
                    XmlNodeList PropertiesNodes = xmlDoc.GetElementsByTagName("TPropertiesFactor");

                    foreach (XmlNode node in PropertiesNodes)
                    {

                        long IdFactorProperties = Int64.Parse(node["IdFactorProperties"].InnerText);
                        string Name = node["Name"].InnerText;
                        long Tel = Int64.Parse(node["Tel"].InnerText);
                        string Address = node["Address"].InnerText;
                        long PriceFactor = Int64.Parse(node["PriceFactor"].InnerText);
                        long Mod = Int64.Parse(node["Mod"].InnerText);
                        string Date = node["Date"].InnerText;
                        string StringMoney = node["StringMoney"].InnerText;

                        if (OperateOnStringTel(Tel.ToString()))
                        {

                            DataRow dr = DSHelp.Tables["TPropertiesFactor"].NewRow();
                            dr["IdFactorProperties"] = IdFactorProperties.ToString();
                            dr["Name"] = Name;
                            dr["Tel"] = Tel.ToString();
                            dr["Address"] = Address;
                            dr["PriceFactor"] = PriceFactor.ToString();
                            dr["Mod"] = Mod.ToString();
                            dr["Date"] = Date;
                            dr["StringMoney"] = StringMoney;
                            DSHelp.Tables["TPropertiesFactor"].Rows.Add(dr);
                            DSHelp.Tables["TPropertiesFactor"].AcceptChanges();

                        }

                    }
                }

                else
                {

                    DSHelp.Tables["TPropertiesFactor"].Clear();
                    for (int lng = 0; lng <= DSForPrint.Tables["TPropertiesFactor"].Rows.Count - 1; lng++)
                    {

                        if (OperateOnStringTel(DSForPrint.Tables["TPropertiesFactor"].Rows[lng][2].ToString()))
                        {
                            DataRow dr = DSHelp.Tables["TPropertiesFactor"].NewRow();
                            dr["IdFactorProperties"] = DSForPrint.Tables["TPropertiesFactor"].Rows[lng][0].ToString();
                            dr["Name"] = DSForPrint.Tables["TPropertiesFactor"].Rows[lng][1].ToString();
                            dr["Tel"] = DSForPrint.Tables["TPropertiesFactor"].Rows[lng][2].ToString();
                            dr["Address"] = DSForPrint.Tables["TPropertiesFactor"].Rows[lng][7].ToString();
                            dr["PriceFactor"] = DSForPrint.Tables["TPropertiesFactor"].Rows[lng][3].ToString();
                            dr["Mod"] = DSForPrint.Tables["TPropertiesFactor"].Rows[lng][4].ToString();
                            dr["Date"] = DSForPrint.Tables["TPropertiesFactor"].Rows[lng][6].ToString();
                            dr["StringMoney"] = DSForPrint.Tables["TPropertiesFactor"].Rows[lng][5].ToString();
                            DSHelp.Tables["TPropertiesFactor"].Rows.Add(dr);
                            DSHelp.Tables["TPropertiesFactor"].AcceptChanges();


                        }
                    }

                   

                }


                DSForPrint.Relations.Clear();
                DSForPrint.Tables["TDetailFactorManagment"].Constraints.Clear();
                DSForPrint.Tables.Remove("TPropertiesFactor");

                DSForPrint.Tables.Add(DSHelp.Tables["TPropertiesFactor"].Copy());
                CreateRelation();



                counter++;
            }

            #endregion

            #region Search By Date
            if (checkBoxDate.Checked)
            {
                if (counter == 0)
                {
                    XmlDocument xmlDoc = new XmlDocument();
                    XmlNode rootNode;
                    xmlDoc.Load(Program.FC.XmlDataFileNamePropertiesFactor);
                    rootNode = xmlDoc.DocumentElement;
                    XmlNodeList PropertiesNodes = xmlDoc.GetElementsByTagName("TPropertiesFactor");

                    foreach (XmlNode node in PropertiesNodes)
                    {

                        long IdFactorProperties = Int64.Parse(node["IdFactorProperties"].InnerText);
                        string Name = node["Name"].InnerText;
                        long Tel = Int64.Parse(node["Tel"].InnerText);
                        string Address = node["Address"].InnerText;
                        long PriceFactor = Int64.Parse(node["PriceFactor"].InnerText);
                        long Mod = Int64.Parse(node["Mod"].InnerText);
                        string Date = node["Date"].InnerText;
                        string StringMoney = node["StringMoney"].InnerText;

                        if (OperateOnStringDate(Date))
                        {

                            DataRow dr = DSHelp.Tables["TPropertiesFactor"].NewRow();
                            dr["IdFactorProperties"] = IdFactorProperties.ToString();
                            dr["Name"] = Name;
                            dr["Tel"] = Tel.ToString();
                            dr["Address"] = Address;
                            dr["PriceFactor"] = PriceFactor.ToString();
                            dr["Mod"] = Mod.ToString();
                            dr["Date"] = Date;
                            dr["StringMoney"] = StringMoney;
                            DSHelp.Tables["TPropertiesFactor"].Rows.Add(dr);
                            DSHelp.Tables["TPropertiesFactor"].AcceptChanges();

                        }

                    }
                }

                else
                {

                    DSHelp.Tables["TPropertiesFactor"].Clear();
                    for (int lng = 0; lng <= DSForPrint.Tables["TPropertiesFactor"].Rows.Count - 1; lng++)
                    {

                        if (OperateOnStringTel(DSForPrint.Tables["TPropertiesFactor"].Rows[lng][6].ToString()))
                        {
                            DataRow dr = DSHelp.Tables["TPropertiesFactor"].NewRow();
                            dr["IdFactorProperties"] = DSForPrint.Tables["TPropertiesFactor"].Rows[lng][0].ToString();
                            dr["Name"] = DSForPrint.Tables["TPropertiesFactor"].Rows[lng][1].ToString();
                            dr["Tel"] = DSForPrint.Tables["TPropertiesFactor"].Rows[lng][2].ToString();
                            dr["Address"] = DSForPrint.Tables["TPropertiesFactor"].Rows[lng][7].ToString();
                            dr["PriceFactor"] = DSForPrint.Tables["TPropertiesFactor"].Rows[lng][3].ToString();
                            dr["Mod"] = DSForPrint.Tables["TPropertiesFactor"].Rows[lng][4].ToString();
                            dr["Date"] = DSForPrint.Tables["TPropertiesFactor"].Rows[lng][6].ToString();
                            dr["StringMoney"] = DSForPrint.Tables["TPropertiesFactor"].Rows[lng][5].ToString();
                            DSHelp.Tables["TPropertiesFactor"].Rows.Add(dr);
                            DSHelp.Tables["TPropertiesFactor"].AcceptChanges();


                        }
                    }

                   
                }


                DSForPrint.Relations.Clear();
                DSForPrint.Tables["TDetailFactorManagment"].Constraints.Clear();
                DSForPrint.Tables.Remove("TPropertiesFactor");

                DSForPrint.Tables.Add(DSHelp.Tables["TPropertiesFactor"].Copy());
                CreateRelation();
                counter++;
            }

            #endregion

            #region Search By CodeFactor
            if (checkBoxCode.Checked)
            {
                if (counter == 0)
                {
                    DataRow[] dr = Program.FC.DSFactor.Tables["TPropertiesFactor"].Select(StrCode);
                    for (int i = 0; i <= dr.Length - 1; i++)
                        DSHelp.Tables["TPropertiesFactor"].ImportRow(dr[i]);
                }

                else
                {


                    DSHelp.Tables["TPropertiesFactor"].Clear();
                    DataRow[] dr = DSForPrint.Tables["TPropertiesFactor"].Select(StrCode);
                    for (int i = 0; i <= dr.Length - 1; i++)
                        DSHelp.Tables["TPropertiesFactor"].ImportRow(dr[i]);


                    DSHelp.Tables["TPropertiesFactor"].AcceptChanges();



                }

                DSForPrint.Relations.Clear();
                DSForPrint.Tables["TDetailFactorManagment"].Constraints.Clear();
                DSForPrint.Tables.Remove("TPropertiesFactor");

                DSForPrint.Tables.Add(DSHelp.Tables["TPropertiesFactor"].Copy());
                CreateRelation();
                counter++;
            }
          
            #endregion

            #region Search By Price
            if (checkBoxPrice.Checked)
            {
                
                if (counter == 0)
                {
                    DataRow[] dr = Program.FC.DSFactor.Tables["TPropertiesFactor"].Select(StrPrice);
                    for (int i = 0; i <= dr.Length - 1; i++)
                        DSHelp.Tables["TPropertiesFactor"].ImportRow(dr[i]);
                }

                else
                {


                    DSHelp.Tables["TPropertiesFactor"].Clear();
                    DataRow[] dr = DSForPrint.Tables["TPropertiesFactor"].Select(StrPrice);
                    for (int i = 0; i <= dr.Length - 1; i++)
                        DSHelp.Tables["TPropertiesFactor"].ImportRow(dr[i]);


                    DSHelp.Tables["TPropertiesFactor"].AcceptChanges();



                }

                DSForPrint.Relations.Clear();
                DSForPrint.Tables["TDetailFactorManagment"].Constraints.Clear();
                DSForPrint.Tables.Remove("TPropertiesFactor");

                DSForPrint.Tables.Add(DSHelp.Tables["TPropertiesFactor"].Copy());
                CreateRelation();
                counter++;
              
               
            }
            
            #endregion


            #region Search By Mod
            if (CheckBoxMod.Checked)
            {
                if (counter == 0)
                {
                    DataRow[] dr = Program.FC.DSFactor.Tables["TPropertiesFactor"].Select(StrMod);
                    for (int i = 0; i <= dr.Length - 1; i++)
                        DSHelp.Tables["TPropertiesFactor"].ImportRow(dr[i]);
                }

                else
                {

                    DSHelp.Tables["TPropertiesFactor"].Clear();
                    DataRow[] dr = DSForPrint.Tables["TPropertiesFactor"].Select(StrMod);
                    for (int i = 0; i <= dr.Length - 1; i++)
                        DSHelp.Tables["TPropertiesFactor"].ImportRow(dr[i]);


                    DSHelp.Tables["TPropertiesFactor"].AcceptChanges();
                }

                DSForPrint.Relations.Clear();
                DSForPrint.Tables["TDetailFactorManagment"].Constraints.Clear();
                DSForPrint.Tables.Remove("TPropertiesFactor");

                DSForPrint.Tables.Add(DSHelp.Tables["TPropertiesFactor"].Copy());
                CreateRelation();
                counter++;


            }
            #endregion
        }


        private void FrmSearchProducts_Load(object sender, EventArgs e)
        {


        }

        private bool OperateOnStringName(string name)
        {
            String str = name;
            int i = str.IndexOf(txtName.Text);
            if (txtName.Text == "")
                return false;

            if (i > -1)
            {
                return true;
            }
            else
            {
                return false;
            }
        }

        private bool OperateOnStringDate(string date)
        {
            String str = date;
            int i = str.IndexOf(txtDate.Text);
            if (txtDate.Text == "")
                return false;

            if (i > -1)
            {
                return true;
            }
            else
            {
                return false;
            }
        }

        private bool OperateOnStringTel(string tel)
        {
            String str = tel;
            int i = str.IndexOf(txtTel.Text);

            if (txtTel.Text == "")
                return false;

            if (i > -1)
            {
                return true;
            }
            else 
            {
                return false;
            }
        }

        private void PrePrintAll_Click(object sender, EventArgs e)
        {
            for (int i = 0; i <= DSForPrint.Tables["TPropertiesFactor"].Rows.Count - 1; i++)
            {

                DataRow[] DrArray = Program.FC.DSFactor.Tables["TDetailFactorManagment"].Select("IdFactor = " + DSForPrint.Tables["TPropertiesFactor"].Rows[i][0].ToString());

                for (int j = 0; j <= DrArray.Length - 1; j++)
                    DSForPrint.Tables["TDetailFactorManagment"].ImportRow(DrArray[j]);
            }
            DSForPrint.Tables["Information"].Clear();
            DSForPrint.Tables["Information"].ImportRow(Program.FC.DSFactor.Tables["Information"].Rows[0]); ;
            stiReport2.Load(Application.StartupPath + "\\SaveFactors.mrt");
            stiReport2.RegData(DSForPrint);
            stiReport2.Show();

        }


     



        private void CheckBoxMod_CheckedChanged(object sender, EventArgs e)
        {
            if (CheckBoxMod.Checked == true)
            {
                groupBoxMod.Enabled = true;
            }
            else
            {
                groupBoxMod.Enabled = false;
                radioButtonWidthBetween.Checked = false;
                radioButtonWidthBigThan.Checked = false;
                radioButtonWidthEqual.Checked = false;
                radioButtonWidthLowThan.Checked = false;
                txtMod1.Text = "";
                txtMod2.Text = "";
            }
        }



        private void CreateRelation()
        {


            DataColumn parentColumn =
               DSForPrint.Tables["TPropertiesFactor"].Columns["IdFactorProperties"];
            DataColumn childColumn =
                DSForPrint.Tables["TDetailFactorManagment"].Columns["IdFactor"];


            DataRelation relCustOrder;
            relCustOrder = new DataRelation("Relation_Pro_Det",
                parentColumn, childColumn);


            DSForPrint.Relations.Add(relCustOrder);
        }

        private void GridViewProperties_Click(object sender, EventArgs e)
        {
            if (GridViewProperties.RowCount != 0)
            {
                DSForOnlyOneFactor.Tables["TDetailFactor"].Clear();
                DataRow[] DrArray = Program.FC.DSFactor.Tables["TDetailFactorManagment"].Select("IdFactor = " + GridViewProperties.CurrentRow.Cells[0].Value.ToString());

                for (int i = 0; i <= DrArray.Length - 1; i++)
                    DSForOnlyOneFactor.Tables["TDetailFactor"].ImportRow(DrArray[i]);



                GridViewDetail.DataSource = DSForOnlyOneFactor.Tables["TDetailFactor"];


                GridViewDetail.Columns[0].Visible = false;

                GridViewDetail.Columns[1].HeaderText = "کــد کالا";

                GridViewDetail.Columns[2].HeaderText = "نوع سنـگ";
                GridViewDetail.Columns[3].HeaderText = "تعـداد";
                GridViewDetail.Columns[4].HeaderText = "طـول";
                GridViewDetail.Columns[5].HeaderText = "عـرض";
                GridViewDetail.Columns[6].HeaderText = "معـیار";
                GridViewDetail.Columns[7].HeaderText = "بـهای واحـد";
                GridViewDetail.Columns[8].HeaderText = "جـمع کـل";
                GridViewDetail.Columns[9].HeaderText = "تــاریخ";

               


              
            }
        }

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

                Edit.Enabled = true;
                Del.Enabled = true;

                Add.Enabled = true;
             
            }
            else
            {

                Edit.Enabled = false;
                Del.Enabled = false;

                Add.Enabled = false;
               
            }
        }

        private void Add_Click(object sender, EventArgs e)
        {

            if (GridViewProperties.CurrentRow == null)
            {
                MessageBox.Show("لطفاً سطری از جدول بالا ارا انتخاب کنید", "زمرد", MessageBoxButtons.OK, MessageBoxIcon.Information);
                return;
            }
            Add_EditFactor Add = new Add_EditFactor();
            Add.StrFrm = "SF";
            Add.StrIndex = "Add";
            Add.ShowDialog();
        }

        private void Edit_Click(object sender, EventArgs e)
        {
            Add_EditFactor Add = new Add_EditFactor();
            Add.StrFrm = "SF";
            Add.StrIndex = "Edit";
            Add.ShowDialog();
        }

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

                foreach (DataGridViewRow r in GridViewDetail.SelectedRows)
                {
                    DataRow[] DrArray = Program.FC.DSFactor.Tables["TDetailFactorManagment"].Select("IdFactor = " + GridViewProperties.CurrentRow.Cells[0].Value.ToString() + " and " + "Id = " + r.Cells[1].Value.ToString());
                    int Length = DrArray.Length;
                    while (Length != 0)
                    {
                        int count = Program.FC.DSFactor.Tables["TDetailFactorManagment"].Rows.Count - 1;

                        for (int i = 0; i <= count; i++)
                        {
                            if (r.Cells[0].Value.ToString() == Program.FC.DSFactor.Tables["TDetailFactorManagment"].Rows[i][0].ToString() &&

                                r.Cells[1].Value.ToString() == Program.FC.DSFactor.Tables["TDetailFactorManagment"].Rows[i][1].ToString()

                                )
                            {
                                Program.FC.DSFactor.Tables["TDetailFactorManagment"].Rows[i].Delete();
                                Program.FC.DSFactor.Tables["TDetailFactorManagment"].AcceptChanges();
                                break;
                            }
                        }
                        Length--;
                    }
                    GridViewDetail.Rows.Remove(r);
                }

                Program.FC.DSFactor.Tables["TDetailFactorManagment"].AcceptChanges();
                Program.FC.DSFactor.Tables["TDetailFactorManagment"].WriteXml(Program.FC.XmlDataFileNameDetailFactor);
                long lng = 0;
                for (int i = 0; i <= Program.SF.GridViewDetail.RowCount - 1; i++)
                {

                    lng = lng + Int64.Parse(Program.SF.GridViewDetail.Rows[i].Cells[8].Value.ToString());

                }
                Program.FC.DSFactor.Tables["TPropertiesFactor"].Rows[FindIndexRow(Program.SF.GridViewProperties.CurrentRow)][3] = lng.ToString();
                Program.FC.DSFactor.Tables["TPropertiesFactor"].Rows[FindIndexRow(Program.SF.GridViewProperties.CurrentRow)][5] = PDT.PersianDate.c15d(lng.ToString()) + " ریــال ";
                Program.FC.DSFactor.Tables["TPropertiesFactor"].AcceptChanges();
                Program.FC.DSFactor.Tables["TPropertiesFactor"].WriteXml(Program.FC.XmlDataFileNamePropertiesFactor);



                Program.SF.DSForPrint.Tables["TPropertiesFactor"].Rows[Program.SF.GridViewProperties.CurrentRow.Index][3] = lng.ToString();
                Program.SF.DSForPrint.Tables["TPropertiesFactor"].Rows[Program.SF.GridViewProperties.CurrentRow.Index][5] = PDT.PersianDate.c15d(lng.ToString()) + " ریــال ";
                Program.SF.DSForPrint.Tables["TPropertiesFactor"].AcceptChanges();

            }
        }

        private void radioButtonWidthBetween_CheckedChanged_1(object sender, EventArgs e)
        {
            if (radioButtonWidthBetween.Checked == true)
            {
                txtMod2.Enabled = true;
            }
            else
            {
                txtMod2.Enabled = false;
                txtMod2.Text = "";
            }
        }

        private void GridViewProperties_CellContentClick(object sender, DataGridViewCellEventArgs e)
        {

        }

        private void GridViewDetail_CellContentClick(object sender, DataGridViewCellEventArgs e)
        {

        }

        private void toolStripButton1_Click(object sender, EventArgs e)
        {
            Account Ac = new Account();
            Ac.ShowDialog();
        }

    }


}
