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 Morakhassi.dtsArzeshGozariTableAdapters;

namespace Morakhassi
{
    public partial class frmCalculatePrice : Form
    {
        int KeyStatus1 = 0;

        public frmCalculatePrice()
        {
            InitializeComponent();
        }

        private void tblMelksBindingNavigatorSaveItem_Click(object sender, EventArgs e)
        {
            this.Validate();
            this.tblMelksBindingSource.EndEdit();
            this.tableAdapterManager.UpdateAll(this.dtsArzeshGozari);

        }

        private void frmCalculatePrice_Load(object sender, EventArgs e)
        {
            Class.Class_Register_Logs _log = new Morakhassi.Class.Class_Register_Logs(this.Text + " Form Is Loaded.");
            // TODO: This line of code loads data into the 'dtsArzeshGozari.tblMelkDetailsPrices1' table. You can move, or remove it, as needed.
            this.tblMelkDetailsPrices1TableAdapter.Fill(this.dtsArzeshGozari.tblMelkDetailsPrices1);
            // TODO: This line of code loads data into the 'dtsArzeshGozari.tblMelks' table. You can move, or remove it, as needed.
            this.tblMelksTableAdapter.Fill(this.dtsArzeshGozari.tblMelks);

        }

        private void btnAddNew_Click(object sender, EventArgs e)
        {
            KeyStatus1 = 1;
            groupBox2.Visible = false;
            groupBox3.Visible = true;
            groupBox4.Enabled = false;
            dataGridView1.Enabled = false;
            this.tblMelksBindingSource.AddNew();
        }

        private void btnUpdate_Click(object sender, EventArgs e)
        {
            KeyStatus1 = 2;
            groupBox2.Visible = false;
            groupBox3.Visible = true;
            groupBox4.Enabled = false;
            dataGridView1.Enabled = false;
        }

        private void btnSave_Click(object sender, EventArgs e)
        {
            if (melkDescriptionTextBox.Text == "" || totalPriceTextBox.Text == "" || comboBox1.Text == "")
            {
                MessageBox.Show("ورود مقادیر تمامی فیلدها الزامی می باشد");
                return;
            }

            if (KeyStatus1 == 1)
            {
                UDF_Make_Save();
                groupBox2.Visible = true;
                groupBox3.Visible = false;
                groupBox4.Enabled = true;
                dataGridView1.Enabled = true;
                dataGridView1.Refresh();
                this.tblMelksTableAdapter.Fill(this.dtsArzeshGozari.tblMelks);
                //this.tblMelksTableAdapter.Fill(this.dtsArzeshGozari.tblMelks);
                MessageBoxOptions options = MessageBoxOptions.RightAlign;
                MessageBox.Show("رکورد مورد نظر ذخیره سازی گردید ", "تائید ثبت", MessageBoxButtons.OK, MessageBoxIcon.Warning, MessageBoxDefaultButton.Button1, options);
            }
            else if (KeyStatus1 == 2)
            {
                if (melkIDTextBox.Text == "")
                {
                    MessageBoxOptions options = MessageBoxOptions.RightAlign;
                    MessageBox.Show("رکوردي براي ويرايش انتخاب نشده است ", "اخطار", MessageBoxButtons.OK, MessageBoxIcon.Warning, MessageBoxDefaultButton.Button1, options);
                    return;
                }
                UDF_Make_Update();
                groupBox2.Visible = true;
                groupBox3.Visible = false;
                groupBox4.Enabled = true;
                dataGridView1.Enabled = true;
                dataGridView1.Refresh();
                this.tblMelksTableAdapter.Fill(this.dtsArzeshGozari.tblMelks);
                MessageBoxOptions options2 = MessageBoxOptions.RightAlign;
                MessageBox.Show("رکورد مورد نظر بروزرساني گردید ", "تائید ثبت", MessageBoxButtons.OK, MessageBoxIcon.Warning, MessageBoxDefaultButton.Button1, options2);
            }


        }

        private void UDF_Make_Update()
        {
            try
            {
                Double Dbl_Total_Price = Double.Parse(totalPriceTextBox.Text.Trim());

                tblMelkDetailsPricesTableAdapter Da = new tblMelkDetailsPricesTableAdapter();
                dtsArzeshGozari.tblMelkDetailsPricesDataTable Dt = new dtsArzeshGozari.tblMelkDetailsPricesDataTable();
                Da.FillBytblMelkDetailsPricesID(Dt, int.Parse(comboBox1.SelectedValue.ToString()));

                tblMelksTableAdapter.UpdateQuery(Double.Parse(totalPriceTextBox.Text.Trim()), long.Parse(comboBox1.SelectedValue.ToString()), melkDescriptionTextBox.Text.Trim()
                                     , ((Dbl_Total_Price * Double.Parse(Dt.Rows[0][2].ToString().Trim())) / 100), ((Dbl_Total_Price * Double.Parse(Dt.Rows[0][3].ToString().Trim())) / 100), ((Dbl_Total_Price * Double.Parse(Dt.Rows[0][4].ToString().Trim())) / 100)
                                     , ((Dbl_Total_Price * Double.Parse(Dt.Rows[0][5].ToString().Trim())) / 100), ((Dbl_Total_Price * Double.Parse(Dt.Rows[0][6].ToString().Trim())) / 100), ((Dbl_Total_Price * Double.Parse(Dt.Rows[0][7].ToString().Trim())) / 100)
                                     , ((Dbl_Total_Price * Double.Parse(Dt.Rows[0][8].ToString().Trim())) / 100), ((Dbl_Total_Price * Double.Parse(Dt.Rows[0][9].ToString().Trim())) / 100), ((Dbl_Total_Price * Double.Parse(Dt.Rows[0][10].ToString().Trim())) / 100)
                                     , ((Dbl_Total_Price * Double.Parse(Dt.Rows[0][11].ToString().Trim())) / 100), ((Dbl_Total_Price * Double.Parse(Dt.Rows[0][12].ToString().Trim())) / 100), ((Dbl_Total_Price * Double.Parse(Dt.Rows[0][13].ToString().Trim())) / 100)
                                     , ((Dbl_Total_Price * Double.Parse(Dt.Rows[0][14].ToString().Trim())) / 100), ((Dbl_Total_Price * Double.Parse(Dt.Rows[0][15].ToString().Trim())) / 100), ((Dbl_Total_Price * Double.Parse(Dt.Rows[0][16].ToString().Trim())) / 100)
                                     , ((Dbl_Total_Price * Double.Parse(Dt.Rows[0][17].ToString().Trim())) / 100), ((Dbl_Total_Price * Double.Parse(Dt.Rows[0][18].ToString().Trim())) / 100), ((Dbl_Total_Price * Double.Parse(Dt.Rows[0][19].ToString().Trim())) / 100)
                                     , ((Dbl_Total_Price * Double.Parse(Dt.Rows[0][20].ToString().Trim())) / 100), ((Dbl_Total_Price * Double.Parse(Dt.Rows[0][21].ToString().Trim())) / 100), ((Dbl_Total_Price * Double.Parse(Dt.Rows[0][22].ToString().Trim())) / 100)
                                     , ((Dbl_Total_Price * Double.Parse(Dt.Rows[0][23].ToString().Trim())) / 100), ((Dbl_Total_Price * Double.Parse(Dt.Rows[0][24].ToString().Trim())) / 100), ((Dbl_Total_Price * Double.Parse(Dt.Rows[0][25].ToString().Trim())) / 100)
                                     , ((Dbl_Total_Price * Double.Parse(Dt.Rows[0][26].ToString().Trim())) / 100), ((Dbl_Total_Price * Double.Parse(Dt.Rows[0][27].ToString().Trim())) / 100), ((Dbl_Total_Price * Double.Parse(Dt.Rows[0][28].ToString().Trim())) / 100)
                                     , ((Dbl_Total_Price * Double.Parse(Dt.Rows[0][29].ToString().Trim())) / 100), ((Dbl_Total_Price * Double.Parse(Dt.Rows[0][30].ToString().Trim())) / 100), ((Dbl_Total_Price * Double.Parse(Dt.Rows[0][31].ToString().Trim())) / 100)
                                     , ((Dbl_Total_Price * Double.Parse(Dt.Rows[0][32].ToString().Trim())) / 100), ((Dbl_Total_Price * Double.Parse(Dt.Rows[0][33].ToString().Trim())) / 100), ((Dbl_Total_Price * Double.Parse(Dt.Rows[0][34].ToString().Trim())) / 100)
                                     , ((Dbl_Total_Price * Double.Parse(Dt.Rows[0][35].ToString().Trim())) / 100), ((Dbl_Total_Price * Double.Parse(Dt.Rows[0][36].ToString().Trim())) / 100), ((Dbl_Total_Price * Double.Parse(Dt.Rows[0][37].ToString().Trim())) / 100)
                                     , ((Dbl_Total_Price * Double.Parse(Dt.Rows[0][38].ToString().Trim())) / 100), ((Dbl_Total_Price * Double.Parse(Dt.Rows[0][39].ToString().Trim())) / 100), ((Dbl_Total_Price * Double.Parse(Dt.Rows[0][40].ToString().Trim())) / 100)
                                     , long.Parse(melkIDTextBox.Text.Trim()));
                this.Validate();
                this.tblMelksBindingSource.EndEdit();
                this.tableAdapterManager.UpdateAll(this.dtsArzeshGozari);
            }
            catch
            { }
        }

        private void UDF_Make_Save()
        {
            Double Dbl_Total_Price = Double.Parse(totalPriceTextBox.Text.Trim());

            tblMelkDetailsPricesTableAdapter Da = new tblMelkDetailsPricesTableAdapter();
            dtsArzeshGozari.tblMelkDetailsPricesDataTable Dt = new dtsArzeshGozari.tblMelkDetailsPricesDataTable();
            Da.FillBytblMelkDetailsPricesID(Dt, int.Parse(comboBox1.SelectedValue.ToString()));

            tblMelksTableAdapter DD = new tblMelksTableAdapter();
            DD.Insert(Double.Parse(totalPriceTextBox.Text.Trim()), long.Parse(comboBox1.SelectedValue.ToString()), melkDescriptionTextBox.Text.Trim()
                      , ((Dbl_Total_Price * Double.Parse(Dt.Rows[0][2].ToString().Trim())) / 100), ((Dbl_Total_Price * Double.Parse(Dt.Rows[0][3].ToString().Trim())) / 100), ((Dbl_Total_Price * Double.Parse(Dt.Rows[0][4].ToString().Trim())) / 100)
                      , ((Dbl_Total_Price * Double.Parse(Dt.Rows[0][5].ToString().Trim())) / 100), ((Dbl_Total_Price * Double.Parse(Dt.Rows[0][6].ToString().Trim())) / 100), ((Dbl_Total_Price * Double.Parse(Dt.Rows[0][7].ToString().Trim())) / 100)
                      , ((Dbl_Total_Price * Double.Parse(Dt.Rows[0][8].ToString().Trim())) / 100), ((Dbl_Total_Price * Double.Parse(Dt.Rows[0][9].ToString().Trim())) / 100), ((Dbl_Total_Price * Double.Parse(Dt.Rows[0][10].ToString().Trim())) / 100)
                      , ((Dbl_Total_Price * Double.Parse(Dt.Rows[0][11].ToString().Trim())) / 100), ((Dbl_Total_Price * Double.Parse(Dt.Rows[0][12].ToString().Trim())) / 100), ((Dbl_Total_Price * Double.Parse(Dt.Rows[0][13].ToString().Trim())) / 100)
                      , ((Dbl_Total_Price * Double.Parse(Dt.Rows[0][14].ToString().Trim())) / 100), ((Dbl_Total_Price * Double.Parse(Dt.Rows[0][15].ToString().Trim())) / 100), ((Dbl_Total_Price * Double.Parse(Dt.Rows[0][16].ToString().Trim())) / 100)
                      , ((Dbl_Total_Price * Double.Parse(Dt.Rows[0][17].ToString().Trim())) / 100), ((Dbl_Total_Price * Double.Parse(Dt.Rows[0][18].ToString().Trim())) / 100), ((Dbl_Total_Price * Double.Parse(Dt.Rows[0][19].ToString().Trim())) / 100)
                      , ((Dbl_Total_Price * Double.Parse(Dt.Rows[0][20].ToString().Trim())) / 100), ((Dbl_Total_Price * Double.Parse(Dt.Rows[0][21].ToString().Trim())) / 100), ((Dbl_Total_Price * Double.Parse(Dt.Rows[0][22].ToString().Trim())) / 100)
                      , ((Dbl_Total_Price * Double.Parse(Dt.Rows[0][23].ToString().Trim())) / 100), ((Dbl_Total_Price * Double.Parse(Dt.Rows[0][24].ToString().Trim())) / 100), ((Dbl_Total_Price * Double.Parse(Dt.Rows[0][25].ToString().Trim())) / 100)
                      , ((Dbl_Total_Price * Double.Parse(Dt.Rows[0][26].ToString().Trim())) / 100), ((Dbl_Total_Price * Double.Parse(Dt.Rows[0][27].ToString().Trim())) / 100), ((Dbl_Total_Price * Double.Parse(Dt.Rows[0][28].ToString().Trim())) / 100)
                      , ((Dbl_Total_Price * Double.Parse(Dt.Rows[0][29].ToString().Trim())) / 100), ((Dbl_Total_Price * Double.Parse(Dt.Rows[0][30].ToString().Trim())) / 100), ((Dbl_Total_Price * Double.Parse(Dt.Rows[0][31].ToString().Trim())) / 100)
                      , ((Dbl_Total_Price * Double.Parse(Dt.Rows[0][32].ToString().Trim())) / 100), ((Dbl_Total_Price * Double.Parse(Dt.Rows[0][33].ToString().Trim())) / 100), ((Dbl_Total_Price * Double.Parse(Dt.Rows[0][34].ToString().Trim())) / 100)
                      , ((Dbl_Total_Price * Double.Parse(Dt.Rows[0][35].ToString().Trim())) / 100), ((Dbl_Total_Price * Double.Parse(Dt.Rows[0][36].ToString().Trim())) / 100), ((Dbl_Total_Price * Double.Parse(Dt.Rows[0][37].ToString().Trim())) / 100)
                      , ((Dbl_Total_Price * Double.Parse(Dt.Rows[0][38].ToString().Trim())) / 100), ((Dbl_Total_Price * Double.Parse(Dt.Rows[0][39].ToString().Trim())) / 100), ((Dbl_Total_Price * Double.Parse(Dt.Rows[0][40].ToString().Trim())) / 100));

        }

        private void btnCancel_Click(object sender, EventArgs e)
        {
            this.tblMelksBindingSource.CancelEdit();
            groupBox2.Visible = true;
            groupBox3.Visible = false;
            groupBox4.Enabled = true;
            dataGridView1.Enabled = true;
            this.tblMelkDetailsPrices1TableAdapter.Fill(this.dtsArzeshGozari.tblMelkDetailsPrices1);
            this.tblMelksTableAdapter.Fill(this.dtsArzeshGozari.tblMelks);
        }

        private void btnRefresh_Click(object sender, EventArgs e)
        {
            this.tblMelkDetailsPrices1TableAdapter.Fill(this.dtsArzeshGozari.tblMelkDetailsPrices1);
            // TODO: This line of code loads data into the 'dtsArzeshGozari.tblMelks' table. You can move, or remove it, as needed.
            this.tblMelksTableAdapter.Fill(this.dtsArzeshGozari.tblMelks);
        }

        private void btnDelete_Click(object sender, EventArgs e)
        {
            DialogResult dr = MessageBox.Show("آیا از حذف رکورد اطمینان دارید", "اخطار حذف", MessageBoxButtons.OKCancel, MessageBoxIcon.Warning);
            if (dr == DialogResult.OK)
            {
                if (melkIDTextBox.Text == "")
                {
                    MessageBox.Show("ركوردي براي حذف انتخاب نشده است");
                    return;
                }
                else
                {
                    try
                    {
                        tblMelksTableAdapter.DeleteQuery(long.Parse(melkIDTextBox.Text.Trim()));
                        this.tblMelkDetailsPrices1TableAdapter.Fill(this.dtsArzeshGozari.tblMelkDetailsPrices1);
                        // TODO: This line of code loads data into the 'dtsArzeshGozari.tblMelks' table. You can move, or remove it, as needed.
                        this.tblMelksTableAdapter.Fill(this.dtsArzeshGozari.tblMelks);

                        MessageBox.Show("رکورد مورد نظر حذف گردید");
                    }
                    catch (Exception ERR)
                    {
                        MessageBox.Show(ERR.Message);
                    }
                }
            }
        }

        private void btnSearch_Click(object sender, EventArgs e)
        {
            if (Txt_Search.Text == "")
            {
                MessageBox.Show("فیلتر جستجو مشخص نشده است");
            }
            else
            {
                this.tblMelksTableAdapter.FillBy(this.dtsArzeshGozari.tblMelks, "%" + Txt_Search.Text.Trim() + "%");
                if (this.dtsArzeshGozari.tblMelks.Rows.Count <= 0)
                {
                    MessageBox.Show("رکوردی جهت مشاهده پیدا نشد");
                }
                else
                {
                    MessageBox.Show("تعداد رکوردهای یافته شده = " + this.dtsArzeshGozari.tblMelks.Rows.Count.ToString());
                }
                labPos();
            }
        }

        protected void labPos()
        {
            this.label14.Text = this.tblMelksBindingSource.Position + 1 + " از " + this.tblMelksBindingSource.Count;
        }

        private void btmNext_Click(object sender, EventArgs e)
        {
            if (this.tblMelksBindingSource.Position + 1 < this.tblMelksBindingSource.Count)
            {
                this.tblMelksBindingSource.MoveNext();
                this.labPos();
            }
        }

        private void btnLast_Click(object sender, EventArgs e)
        {
            this.tblMelksBindingSource.MoveLast();
        }

        private void btnPrevoiuse_Click(object sender, EventArgs e)
        {
            this.tblMelksBindingSource.MovePrevious();
        }

        private void btnFirst_Click(object sender, EventArgs e)
        {
            this.tblMelksBindingSource.MoveFirst();
        }

        private void frmCalculatePrice_FormClosed(object sender, FormClosedEventArgs e)
        {
            Class.Class_Register_Logs _log = new Morakhassi.Class.Class_Register_Logs(this.Text + " Form Is Closed.");
        }
    }
}
