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 Factor : Form
    {
        string XmlDataFileNameProperties = System.Environment.GetFolderPath(System.Environment.SpecialFolder.ApplicationData) + "\\ParsZSDB" + "\\PropertiesFactor.xml";
        string XmlDataFileNameDetail = System.Environment.GetFolderPath(System.Environment.SpecialFolder.ApplicationData) + "\\ParsZSDB" + "\\DetailFactor.xml";
        bool Reg = false;
        public Factor()
        {
            InitializeComponent();
            Created = true;
        }

        private void Factor_Load(object sender, EventArgs e)
        {
            LoadSettingCalendar();
            TxtIDFactor.Text = Program.FC.GenerateIDFactor();

            GridViewFactor.DataSource = Program.FC.DSFactor.Tables["TDetailFactor"];
            
            GridViewFactor.Columns[0].Visible = false;

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

          

            for (int i = 0; i <= GridViewFactor.RowCount - 1; i++)
            {
                if (GridViewFactor.RowCount == 0) return;

                Program.F.TxtSum.Text = (Int64.Parse(Program.F.TxtSum.Text) + Int64.Parse(GridViewFactor.Rows[i].Cells[8].Value.ToString())).ToString();
            }

        }


    

        private void BtnClear_Click(object sender, EventArgs e)
        {
            TxtName.Text = "";
            TxtTel.Text = "0";
            TxtAddress.Text = "";
            TxtMod.Text = "0";
      
        }

        private void BtnNew_Click(object sender, EventArgs e)
        {

                GridViewFactor.SelectAll();
                foreach (DataGridViewRow r in GridViewFactor.SelectedRows)
                    GridViewFactor.Rows.Remove(r);
                Program.FC.DSFactor.Tables["TDetailFactor"].AcceptChanges();
               
                TxtIDFactor.Text = Program.FC.GenerateIDFactor();

                TxtName.Text = "";
                TxtTel.Text = "0";
                TxtAddress.Text = "";
                TxtMod.Text = "0";
                TxtSum.Text = "0";
                BtnNew.Enabled = false;
                BtnPrePrint.Enabled = false;
                BtnSave.Enabled = true;
                BtnAddFactorDetail.Enabled = true;
                BtnAccount.Enabled = false;
        }

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

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

        private void BtnManagmentFactors_Click(object sender, EventArgs e)
        {
            if (Program.MGF != null && !Program.MGF.IsDisposed)
            {
                Program.MGF.Activate();
                return;

            }

            Program.MGF = new ManagmentFactors();
            if (Properties.Settings.Default.FrmPosition == 0)
                Program.MGF.StartPosition = FormStartPosition.CenterScreen;
            if (Properties.Settings.Default.FrmPosition == 1)
                Program.MGF.StartPosition = FormStartPosition.CenterParent;
            if (Properties.Settings.Default.FrmStatus == 0)
                Program.MGF.WindowState = FormWindowState.Normal;
            if (Properties.Settings.Default.FrmStatus == 1)
                Program.MGF.WindowState = FormWindowState.Maximized;
            if (Properties.Settings.Default.FrmStatus == 2)
                Program.MGF.WindowState = FormWindowState.Minimized;

            if (Properties.Settings.Default.FrmMin == false)
                Program.MGF.MinimizeBox = false;
            if (Properties.Settings.Default.FrmMax == false)
                Program.MGF.MaximizeBox = false;
            Program.MGF.Show();
        }

        private void BtnPrePrint_Click(object sender, EventArgs e)
        {

          
         
            DSForPrint.Tables.Clear();
            DSForPrint.Tables.Add(Program.FC.DSFactor.Tables["TDetailFactor"].Copy());
            DSForPrint.Tables.Add(Program.FC.DSFactor.Tables["PropertiesHelp"].Copy());
            DSForPrint.Tables.Add(Program.FC.DSFactor.Tables["Information"].Copy());
            stiReport1.Load(Application.StartupPath + "\\FactorPrint.mrt");
            stiReport1.RegData(DSForPrint);
            stiReport1.Show();
          
        }

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

                foreach (DataGridViewRow r in GridViewFactor.SelectedRows)
                    GridViewFactor.Rows.Remove(r);
                Program.FC.DSFactor.Tables["TDetailFactor"].AcceptChanges();
                TxtSum.Text = "0";
                for (int i = 0; i <= GridViewFactor.RowCount - 1; i++)
                {

                    Program.F.TxtSum.Text = (Int64.Parse(Program.F.TxtSum.Text) + Int64.Parse(GridViewFactor.Rows[i].Cells[8].Value.ToString())).ToString();

                }
            }
        }

        private void BtnDelAll_Click(object sender, EventArgs e)
        {
            if (MessageBox.Show("آیا شما مطمئن هستید؟", "زمرد", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes)
            {
                GridViewFactor.SelectAll();
                foreach (DataGridViewRow r in GridViewFactor.SelectedRows)
                    GridViewFactor.Rows.Remove(r);
                Program.FC.DSFactor.Tables["TDetailFactor"].AcceptChanges();
                TxtSum.Text = "0";
            }
        }

        private void GridViewFactor_SelectionChanged(object sender, EventArgs e)
        {

            if(BtnSave.Enabled == false)
            {
                BtnDelAll.Enabled = false;
                BtnDelChoice.Enabled = false;
                BtnEdit.Enabled = false;
                BtnAddFactorDetail.Enabled = false;
                return;
            }



            if (GridViewFactor.RowCount == 0)
             {

                 BtnDelAll.Enabled = false;
                 BtnDelChoice.Enabled = false;
                 BtnEdit.Enabled = false;

             }
            else
             {

                 BtnDelAll.Enabled = true;
                 BtnDelChoice.Enabled = true;
                 BtnEdit.Enabled = true;

             }

        }

        private void BtnSave_Click(object sender, EventArgs e)
        {



            if (GridViewFactor.RowCount == 0)
            {
                MessageBox.Show("جدول فاکتور خالی است لطفا فاکتور را به دقت پر کنید", "زمرد", MessageBoxButtons.OK, MessageBoxIcon.Information);
                return;
            }



            if (Program.FC.DSFactor.Tables["TPropertiesFactor"].Rows.Count == 0)
            {
                DataRow dr = Program.FC.DSFactor.Tables["TPropertiesFactor"].NewRow();
                dr["IdFactorProperties"] = Int64.Parse(TxtIDFactor.Text);
                dr["Name"] = TxtName.Text;
                dr["Tel"] = Int64.Parse(TxtTel.Text);
                dr["Address"] = TxtAddress.Text;
                dr["PriceFactor"] = Int64.Parse(TxtSum.Text);
                dr["Mod"] = Int64.Parse(TxtMod.Text);
                dr["Date"] = PerDate.SelectedDateInStringPersian;
                dr["StringMoney"] = PDT.PersianDate.c15d(TxtSum.Text) + " ریــال ";
                Program.FC.DSFactor.Tables["TPropertiesFactor"].Rows.Add(dr);
                Program.FC.DSFactor.Tables["TPropertiesFactor"].AcceptChanges();
                Program.FC.DSFactor.Tables["TPropertiesFactor"].WriteXml(XmlDataFileNameProperties);
                CopyInTFactorManagment();
                CopyInTPropertisHelp();
                Program.FC.DSFactor.Tables["TDetailFactorManagment"].WriteXml(XmlDataFileNameDetail);

                BtnNew.Enabled = true;
                BtnPrePrint.Enabled = true;
                BtnSave.Enabled = false;


                BtnDelChoice.Enabled = false;
                BtnDelAll.Enabled = false;
                BtnEdit.Enabled = false;
                BtnAddFactorDetail.Enabled = false;
                BtnAccount.Enabled = true;
                Reg = true;
                MessageBox.Show("فاکتور جاری ثبت شد", "زمرد", MessageBoxButtons.OK, MessageBoxIcon.Information);

                return;
            }


            if (Program.FC.DSFactor.Tables["TPropertiesFactor"].Rows.Count > 0)
            {

                DataRow[] dr = Program.FC.DSFactor.Tables["TPropertiesFactor"].Select("IdFactorProperties =" + TxtIDFactor.Text);

                if (dr.Length == 0)
                {

                    DataRow dr1 = Program.FC.DSFactor.Tables["TPropertiesFactor"].NewRow();
                    dr1["IdFactorProperties"] = Int64.Parse(TxtIDFactor.Text);
                    dr1["Name"] = TxtName.Text;
                    dr1["Tel"] = Int64.Parse(TxtTel.Text);
                    dr1["Address"] = TxtAddress.Text;
                    dr1["PriceFactor"] = Int64.Parse(TxtSum.Text);
                    dr1["Mod"] = Int64.Parse(TxtMod.Text);
                    dr1["Date"] = PerDate.SelectedDateInStringPersian;
                    dr1["StringMoney"] = PDT.PersianDate.c15d(TxtSum.Text) + " ریــال ";
                    Program.FC.DSFactor.Tables["TPropertiesFactor"].Rows.Add(dr1);
                    Program.FC.DSFactor.Tables["TPropertiesFactor"].AcceptChanges();
                    Program.FC.DSFactor.Tables["TPropertiesFactor"].WriteXml(XmlDataFileNameProperties);
                    CopyInTFactorManagment();
                    CopyInTPropertisHelp();
                    Program.FC.DSFactor.Tables["TDetailFactorManagment"].WriteXml(XmlDataFileNameDetail);

                    BtnNew.Enabled = true;
                    BtnPrePrint.Enabled = true;
                    BtnSave.Enabled = false;
                    BtnDelChoice.Enabled = false;
                    BtnDelAll.Enabled = false;
                    BtnEdit.Enabled = false;
                    BtnAddFactorDetail.Enabled = false;
                    BtnAccount.Enabled = true;
                    Reg = true;
                    MessageBox.Show("فاکتور جاری ثبت شد", "زمرد", MessageBoxButtons.OK, MessageBoxIcon.Information);
                    return;
                }

                dr[0]["Name"] = TxtName.Text;
                dr[0]["Tel"] = Int64.Parse(TxtTel.Text);
                dr[0]["Address"] = TxtAddress.Text;
                dr[0]["PriceFactor"] = Int64.Parse(TxtSum.Text);
                dr[0]["Mod"] = Int64.Parse(TxtMod.Text);
                dr[0]["Date"] = PerDate.SelectedDateInStringPersian;
                dr[0]["StringMoney"] = PDT.PersianDate.c15d(TxtSum.Text) + " ریــال ";
                Program.FC.DSFactor.Tables["TPropertiesFactor"].AcceptChanges();
                Program.FC.DSFactor.Tables["TPropertiesFactor"].WriteXml(XmlDataFileNameProperties);
                CopyInTFactorManagment();
                CopyInTPropertisHelp();
                Program.FC.DSFactor.Tables["TDetailFactorManagment"].WriteXml(XmlDataFileNameDetail);

                BtnNew.Enabled = true;
                BtnPrePrint.Enabled = true;
                BtnSave.Enabled = false;
                BtnDelChoice.Enabled = false;
                BtnDelAll.Enabled = false;
                BtnEdit.Enabled = false;
                BtnAddFactorDetail.Enabled = false;
                BtnAccount.Enabled = true;
                Reg = true;
                MessageBox.Show("فاکتور جاری ثبت شد", "زمرد", MessageBoxButtons.OK, MessageBoxIcon.Information);
                return;
            }






        }

        private void CopyInTFactorManagment()
        {

            for (int i = 0; i <= Program.FC.DSFactor.Tables["TDetailFactor"].Rows.Count - 1; i++)
            {

            DataRow Dr =  Program.FC.DSFactor.Tables["TDetailFactorManagment"].NewRow();


            Dr["IdFactor"] = Program.FC.DSFactor.Tables["TDetailFactor"].Rows[i][0].ToString();
            Dr["Id"] = Program.FC.DSFactor.Tables["TDetailFactor"].Rows[i][1].ToString();
            Dr["KindStone"] = Program.FC.DSFactor.Tables["TDetailFactor"].Rows[i][2].ToString();
            Dr["Count"] = Program.FC.DSFactor.Tables["TDetailFactor"].Rows[i][3].ToString();
            Dr["Length"] = Program.FC.DSFactor.Tables["TDetailFactor"].Rows[i][4].ToString();
            Dr["Width"] = Program.FC.DSFactor.Tables["TDetailFactor"].Rows[i][5].ToString();
            Dr["Measure"] = Program.FC.DSFactor.Tables["TDetailFactor"].Rows[i][6].ToString();
            Dr["Price"] = Program.FC.DSFactor.Tables["TDetailFactor"].Rows[i][7].ToString();
            Dr["PriceAll"] = Program.FC.DSFactor.Tables["TDetailFactor"].Rows[i][8].ToString();
            Dr["Date"] = Program.FC.DSFactor.Tables["TDetailFactor"].Rows[i][9].ToString();
            Program.FC.DSFactor.Tables["TDetailFactorManagment"].Rows.Add(Dr);

            }

         
          
        }

        private void CopyInTPropertisHelp()
        {

            Program.FC.DSFactor.Tables["PropertiesHelp"].Clear();
            DataRow dr = Program.FC.DSFactor.Tables["PropertiesHelp"].NewRow();
            dr["IdFactorProperties"] = TxtIDFactor.Text;
            dr["Name"] = TxtName.Text;
            dr["Tel"] = Int64.Parse(TxtTel.Text);
            dr["Address"] = TxtAddress.Text;
            dr["PriceFactor"] = Int64.Parse(TxtSum.Text);
            dr["Mod"] = Int64.Parse(TxtMod.Text);
            dr["Date"] = PerDate.SelectedDateInStringPersian;
            dr["StringMoney"] = PDT.PersianDate.c15d(TxtSum.Text) + " ریــال ";
            Program.FC.DSFactor.Tables["PropertiesHelp"].Rows.Add(dr);

            
        }

        private void LoadSettingCalendar()
        {
           
                PerDate.CalendarDaysBackColor = Properties.Settings.Default.DaysBGColor;
                PerDate.CalendarDaysForeColor = Properties.Settings.Default.DaysColor;
                PerDate.CalendarHolidayForeColor = Properties.Settings.Default.HolidayDaysColor;
                PerDate.CalendarTodayBackColor = Properties.Settings.Default.TodayDateBGColor;
                PerDate.CalendarTitleBackColor = Properties.Settings.Default.YearMonBGColor;
                PerDate.CalendarTodayForeColor = Properties.Settings.Default.TodayDateColor;
                PerDate.CalendarWeekDaysForeColor = Properties.Settings.Default.WeekColor;
                PerDate.CalendarWeekDaysBackColor = Properties.Settings.Default.WeekBGColor;
                PerDate.CalendarTrailingForeColor = Properties.Settings.Default.BeforeAfterMonDayColor;
                PerDate.CalendarBorderColor = Properties.Settings.Default.BorderColor;
                PerDate.CalendarDaysFont = Properties.Settings.Default.DaysFont;
                PerDate.CalendarTodayFont = Properties.Settings.Default.TodayDateFont;
                PerDate.CalendarShowToday = Properties.Settings.Default.ShowTodayDate;
                PerDate.CalendarShowTrailing = Properties.Settings.Default.ShowBeforeAfterMonDay;
        }

        private void GridViewFactor_Scroll(object sender, ScrollEventArgs e)
        {

        }

        private void Factor_FormClosed(object sender, FormClosedEventArgs e)
        {
            if (Reg == true)
            {
                Reg = false;

                Program.FC.DSFactor.Tables["TDetailFactor"].Clear();
            }
        }

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

        private void TxtMod_TextChanged(object sender, EventArgs e)
        {
            if (TxtMod.Text == "") TxtMod.Text = "0";

          
        }

        private void GridViewFactor_CellFormatting(object sender, DataGridViewCellFormattingEventArgs e)
        {

        }

        private void BtnAccount_Click(object sender, EventArgs e)
        {
            Account Ac = new Account(TxtIDFactor.Text , TxtSum.Text,TxtMod.Text);
            Ac.ShowDialog();
        }
    }
}
