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 Stone.Class;
namespace Stone
{
    public partial class ManagmentFactors : Form
    {
        public ManagmentFactors()
        {
            InitializeComponent();
        }

        private void ManagmentFactors_Load(object sender, EventArgs e)
        {

                GridViewProperties.DataSource = Program.FC.DSFactor.Tables["TPropertiesFactor"];
                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[6].HeaderText = "تـاریخ";
                GridViewProperties.Columns[6].AutoSizeMode = DataGridViewAutoSizeColumnMode.DisplayedCells;
                GridViewProperties.Columns[7].HeaderText = "آدرس";
                GridViewProperties.Columns[7].AutoSizeMode = DataGridViewAutoSizeColumnMode.Fill;
                GridViewProperties.Columns[8].Visible = false;

          
                BS.DataSource = Program.FC.DSFactor;
                BS.DataMember = "TPropertiesFactor";


                if (GridViewProperties.RowCount != 0)
                {
                    GridViewProperties_Click(sender, e);
                    BSDetail.DataSource = DS;
                    BSDetail.DataMember = "TDetailFactor";
                    BSDetail.MoveFirst();
                    GridViewDetail.DataSource = BSDetail;
                }
         
            
        }

        private void BtnPrePrint_Click(object sender, EventArgs e)
        {
        
            DSForPrint.Relations.Clear();
            DSForPrint.Tables["TDetailFactorManagment"].Constraints.Clear();
            DSForPrint.Tables.Clear();       
            DSForPrint.Tables.Add(Program.FC.DSFactor.Tables["TDetailFactorManagment"].Copy());
            DSForPrint.Tables.Add(Program.FC.DSFactor.Tables["TPropertiesFactor"].Copy());
            DSForPrint.Tables.Add(Program.FC.DSFactor.Tables["Information"].Copy());
            CreateRelation();
            stiReport1.Load(Application.StartupPath + "\\SaveFactors.mrt");
            stiReport1.RegData(DSForPrint);
            stiReport1.Show();
        }



        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_SelectionChanged(object sender, EventArgs e)
        {
            if (GridViewProperties.RowCount != 0)
            {
                LblCurrentFactor.Text = GridViewProperties.CurrentRow.Cells[0].Value.ToString() + " - " + GridViewProperties.CurrentRow.Cells[1].Value.ToString();
                LblSum.Text = BS.Count.ToString() + " عدد ";

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

                toolStripButton1.Enabled = true;
                BtnSearch.Enabled = true;
                BtnPrePrint.Enabled = true;
                BtnAccount.Enabled = true;
            }

            else
            {

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

                toolStripButton1.Enabled = false;
                BtnSearch.Enabled = false;
                BtnPrePrint.Enabled = false;
                BtnAccount.Enabled = false;
            }


        }

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

         
        }

        private void BtnAdd_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 BtnDel_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"].AcceptChanges();
                            break;
                        }
                    }
                    Length--;
                    }
                    GridViewProperties.Rows.Remove(r);
                }
                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);
                DS.Tables["TDetailFactor"].Clear();
                BS.MoveFirst();
            }
        }

        private void GridViewProperties_Click(object sender, EventArgs e)
        {
            if (GridViewProperties.RowCount != 0)
            {
                DS.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++)
                    DS.Tables[0].ImportRow(DrArray[i]);

                GridViewDetail.DataSource = DS.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 = "تــاریخ";
                GridViewDetail.Columns[2].AutoSizeMode = DataGridViewAutoSizeColumnMode.Fill;
            }

        }

        private void BtnEdit_Click(object sender, EventArgs e)
        {
            EditPropertiesFactor edit = new EditPropertiesFactor();
            edit.ShowDialog();
        }



        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 toolStripButton1_Click(object sender, EventArgs e)
        {

                CopyInTPropertisHelp();
                DSForOnlyOneFactor.Tables.Clear();
                DSForOnlyOneFactor.Tables.Add(Program.FC.DSFactor.Tables["PropertiesHelp"].Copy());
                DSForOnlyOneFactor.Tables.Add(DS.Tables["TDetailFactor"].Copy());
                DSForOnlyOneFactor.Tables.Add(Program.FC.DSFactor.Tables["Information"].Copy());
                DSForOnlyOneFactor.Tables.Add(ReturnAccountuser(GridViewProperties.CurrentRow));
                stiReport2.Load(Application.StartupPath + "\\FactorPrint.mrt");
                stiReport2.RegData(DSForOnlyOneFactor);
                stiReport2.Show();
           
        }

        private DataTable ReturnAccountuser(DataGridViewRow Row)
        {
            DataTable Account1 = new DataTable("Account");
            DataColumn IdFactor = new DataColumn("IdFactor", System.Type.GetType("System.String"));
            DataColumn Type = new DataColumn("Type", System.Type.GetType("System.String"));
            DataColumn Price = new DataColumn("Price", System.Type.GetType("System.String"));
            DataColumn Date = new DataColumn("Date", System.Type.GetType("System.String"));
            DataColumn Id = new DataColumn("Id", System.Type.GetType("System.Int32"));
            DataColumn Max = new DataColumn("Max", System.Type.GetType("System.String"), "Max(Id)");

            Account1.Columns.Add(IdFactor);
            Account1.Columns.Add(Type);
            Account1.Columns.Add(Price);
            Account1.Columns.Add(Date);
            Account1.Columns.Add(Id);
            Account1.Columns.Add(Max);

            DataRow[] DrArray = Program.ACU.DSFactor.Tables["Account"].Select("IdFactor = " + Row.Cells[0].Value.ToString());
            DataRow Dr;

            for (int i = 0; i <= DrArray.Length - 1; i++)
            {
                Dr = null;
                Dr = Account1.NewRow();
                Dr["IdFactor"] = DrArray[i]["IdFactor"].ToString();
                Dr["Type"] = DrArray[i]["Type"].ToString();
                Dr["Price"] = DrArray[i]["Price"].ToString();
                Dr["Date"] = DrArray[i]["Date"].ToString();
                Dr["Id"] = DrArray[i]["Id"].ToString();
                Dr["Max"] = DrArray[i]["Max"].ToString();

                Account1.Rows.Add(Dr);
            }

            return Account1;
        }

        private void GridViewProperties_CellContentClick(object sender, DataGridViewCellEventArgs e)
        {

        }

        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 = "MGF";
            Add.StrIndex = "Add";
            Add.ShowDialog();
        }

        private void Edit_Click(object sender, EventArgs e)
        {
            Add_EditFactor Add = new Add_EditFactor();
            Add.StrFrm = "MGF";
            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.MGF.GridViewDetail.RowCount - 1; i++)
                {

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

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




            }
        }

        private void BtnSearch_Click(object sender, EventArgs e)
        {
            Program.SF = new FrmSearchFactors();
            Program.SF.ShowDialog();
        }

        private void GridViewDetail_CellContentClick(object sender, DataGridViewCellEventArgs e)
        {

        }

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

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

               

            }
            else
            {

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


            }
        }

        private void toolStripButton2_Click(object sender, EventArgs e)
        {
            Account Ac = new Account(GridViewProperties.CurrentRow.Cells[0].Value.ToString(), GridViewProperties.CurrentRow.Cells[3].Value.ToString(), GridViewProperties.CurrentRow.Cells[4].Value.ToString());
            Ac.ShowDialog();

        }

        private void GridViewProperties_ChangeUICues(object sender, UICuesEventArgs e)
        {

        }

    }
}
