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;

namespace video_club
{
    public partial class frmbargasht : Form
    {
        public frmbargasht()
        {
            InitializeComponent();
        }

        private void frmbargasht_Load(object sender, EventArgs e)
        {
            // TODO: This line of code loads data into the 'dataSet1.tblfilm' table. You can move, or remove it, as needed.
            labelX9.Visible = false;
            lbljarime1.Visible = false;
            labelX5.Visible = false;
            txtjarime.Visible = false;
            this.tblfilmTableAdapter.Fill(this.dataSet1.tblfilm);

            this.tblmoshtariTableAdapter1.Fill(this.dataSet11.tblmoshtari);
            // TODO: This line of code loads data into the 'dataSet11.tblkeraye' table. You can move, or remove it, as needed.
            //  this.tblkerayeTableAdapter.Fill(this.dataSet12.tblkeraye);
            btnsabt.Hide();
            btnmihasebe.Hide();
            btnedame.Hide();

            // TODO: This line of code loads data into the 'dataSet11.tblmoshtari' table. You can move, or remove it, as needed.
            // this.tblmoshtariTableAdapter1.Fill(this.dataSet11.tblmoshtari);
            // TODO: This line of code loads data into the 'dataSet11.tblkeraye' table. You can move, or remove it, as needed.
            // this.tblkerayeTableAdapter.Fill(this.dataSet11.tblkeraye);
            // TODO: This line of code loads data into the 'dataSet12.tblkeraye' table. You can move, or remove it, as needed.
            lbldate.Text = myfunction.convertDate(DateTime.Today.ToShortDateString(), myfunction.dataConvertMode.toPersian);
        }



        private void btnmihasebe_Click(object sender, EventArgs e)
        {


            if (dataGridViewX2.SelectedRows[0].Cells[8].Value.ToString() != "")
            {
                MessageBox.Show("این فیلم قبلا محاسبه و ثبت شده", "!!! اخطار");

            }
            else
            {

                btnmihasebe.Hide();

                if (dataGridViewX2.Rows.GetRowCount(DataGridViewElementStates.Selected) == 0)
                {
                    MessageBox.Show("این مشتری هیچ فیلمی را به امانت نبرده", "!!!اخطار", MessageBoxButtons.OK);
                    lbljarime.Text = "";
                    lblkeraye.Text = "";

                }
                else
                {
                    DateTime a = DateTime.Today.AddDays(31);
                    DateTime dt = Convert.ToDateTime(dataGridViewX2.SelectedRows[0].Cells[7].Value);
                    DateTime dt2 = Convert.ToDateTime(myfunction.convertDate(lbldate.Text, myfunction.dataConvertMode.toGregorian));
                    int d = int.Parse((dt2 - dt).TotalDays.ToString());
                    if (d <= 0)
                    {
                        MessageBox.Show(" این مشتری تاخیر نداشته  ");
                        lblkeraye.Text = dataGridViewX2.SelectedRows[0].Cells[5].Value.ToString();
                        lbljarime.Text = "0";
                        btnsabt.Show();
                    }
                    else
                    {


                        MessageBox.Show("  این مشتری" + " " + (d - 20).ToString() + " " + ".روز تاخیر داشته و جریمه میشود  ");
                        labelX9.Visible = true;
                        txtjarime.Visible = true;
                        lbljarime1.Visible = true;
                        labelX5.Visible = true;

                        MessageBox.Show("لطفا مبلغ جریمه به ازای هر روز رو وارد کنید و دکمه ی ادامه را کلیک کنید", "!!!اخطار", MessageBoxButtons.OK);
                        btnedame.Show();

                    }


                }


            }
        }


        //
        private void textBoxX1_TextChanged(object sender, EventArgs e)
        {
            tblmoshtariTableAdapter1.FillByfname(dataSet11.tblmoshtari, textBoxX1.Text);

            // this.tblmoshtariTableAdapter1.Fill(this.dataSet11.tblmoshtari);

        }

        private void btnshow_Click(object sender, EventArgs e)
        {


        }

        private void dataGridViewX1_CellClick(object sender, DataGridViewCellEventArgs e)
        {

            int shomare = int.Parse(dataGridViewX1.SelectedRows[0].Cells[0].Value.ToString());




            tblkerayeTableAdapter.FillBymid(dataSet11.tblkeraye, shomare);
            if (dataGridViewX2.Rows.Count == 0)
            {
                MessageBox.Show("این مشتری هیچ فیلمی را به امانت نبرده", "!!!اخطار", MessageBoxButtons.OK);
                lbljarime.Text = "";
                lblkeraye.Text = "";

            }
            else
            {
                btnmihasebe.Show();
            }
        }
        int filmid;
        private void dataGridViewX2_CellClick(object sender, DataGridViewCellEventArgs e)
        {
            tblfilmTableAdapter.FillBytitle(dataSet1.tblfilm, dataGridViewX2.SelectedRows[0].Cells[3].Value.ToString());
            btnmihasebe.Show();
            filmid = int.Parse(dataGridViewX2.Rows[e.RowIndex].Cells[10].Value.ToString());
        }

        private void btnsabt_Click(object sender, EventArgs e)
        {


            if (dataGridViewX2.SelectedRows[0].Cells[8].Value.ToString() == "")
            {


                decimal jari = Convert.ToDecimal(lbljarime.Text);
                decimal kera = Convert.ToDecimal(lblkeraye.Text);
                decimal sum = Convert.ToDecimal(lbljarime.Text) + Convert.ToDecimal(lblkeraye.Text);
                DateTime duedatemi = Convert.ToDateTime(myfunction.convertDate(lbldate.Text, myfunction.dataConvertMode.toGregorian));
                string duedatefa = lbldate.Text;
                int kid = int.Parse(dataGridViewX2.SelectedRows[0].Cells[0].Value.ToString());
                tblkerayeTableAdapter.UpdateQuery(jari, sum, lbldate.Text, duedatemi, kid);
                int shomare = int.Parse(dataGridViewX1.SelectedRows[0].Cells[0].Value.ToString());

                tblkerayeTableAdapter.FillBymid(dataSet11.tblkeraye, shomare);
                /*  MessageBox.Show((int.Parse(dataGridViewX3.SelectedRows[0].Cells[5].Value.ToString()) + 1).ToString());

                  MessageBox.Show(dataGridViewX3.SelectedRows[0].Cells[3].Value.ToString());
                  tblfilmTableAdapter.FillBytitle2(dataSet1.tblfilm, dataGridViewX3.SelectedRows[0].Cells[0].Value.ToString());

                  tblfilmTableAdapter.Updatecount(int.Parse(dataGridViewX3.SelectedRows[0].Cells[5].Value.ToString())+1, dataGridViewX2.SelectedRows[0].Cells[3].Value.ToString());

                  MessageBox.Show(dataGridViewX2.SelectedRows[0].Cells[10].Value.ToString());*/


                tblfilmTableAdapter.FillBytitle(dataSet1.tblfilm, dataGridViewX2.SelectedRows[0].Cells[3].Value.ToString());

                tblfilmTableAdapter.Updatecount(filmid);
                filmid = 0;
                btnsabt.Hide();

            }
            else
            {
                MessageBox.Show("!!!این فیلم قبلا ثبت شده", "!!!اخطار", MessageBoxButtons.OK);
                btnsabt.Hide();
            }


            //یادم باشه که جمع کل رو محاسبه کنم

        }

        private void fillBymidToolStripButton_Click(object sender, EventArgs e)
        {
            /* try
             {
                 this.tblkerayeTableAdapter.FillBymid(this.dataSet11.tblkeraye, ((int)(System.Convert.ChangeType(midToolStripTextBox.Text, typeof(int)))));
             }
             catch (System.Exception ex)
             {
                 System.Windows.Forms.MessageBox.Show(ex.Message);
             }
             */
        }

        private void groupPanel4_Click(object sender, EventArgs e)
        {

        }

        private void lbljarime_Click(object sender, EventArgs e)
        {

        }

        private void textBoxX2_Leave(object sender, EventArgs e)
        {
            string s = "";
            int k = 1;
            for (int i = txtjarime.Text.Length - 1; i >= 0; i--)

                if (k < 3)
                {
                    s = txtjarime.Text[i] + s;
                    k++;
                }
                else
                {
                    k = 1;
                    s = txtjarime.Text[i] + s;
                    s = "," + s;

                }
            try
            {

                if (s[0] == ',')
                {
                    s = s.Substring(1);
                    txtjarime.Text = s;

                }
            }
            catch { }

        }

        private void frmbargasht_Activated(object sender, EventArgs e)
        {

        }

        private void labelX4_Click(object sender, EventArgs e)
        {

        }

        private void groupPanel3_Click(object sender, EventArgs e)
        {

        }

        private void txtjarime_TextChanged(object sender, EventArgs e)
        {
            lbljarime1.Text = myfunction.numtostr(txtjarime.Text);
        }

        private void lbljarime_TextChanged(object sender, EventArgs e)
        {
            lbljarimekol.Text = myfunction.numtostr(lbljarime.Text);
        }

        private void lblkeraye_TextChanged(object sender, EventArgs e)
        {
            lblmablagh.Text = myfunction.numtostr(lblkeraye.Text);

        }

        private void btnedame_Click(object sender, EventArgs e)
        {
            if (txtjarime.Text == "")
                MessageBox.Show("لطفا مبلغ جریمه به ازای هر روز رو وارد کنید و دکمه ی ادامه را کلیک کنید", "!!!اخطار", MessageBoxButtons.OK);
            else
            {
                DateTime a = DateTime.Today.AddDays(31);
                DateTime dt = Convert.ToDateTime(dataGridViewX2.SelectedRows[0].Cells[7].Value);
                DateTime dt2 = Convert.ToDateTime(myfunction.convertDate(lbldate.Text, myfunction.dataConvertMode.toGregorian));
                int d = int.Parse((dt2 - dt).TotalDays.ToString());

                lblkeraye.Text = dataGridViewX2.SelectedRows[0].Cells[5].Value.ToString();
                lbljarime.Text = ((d - 20) * Convert.ToInt32(txtjarime.Text)).ToString();
                btnedame.Hide();
                btnsabt.Show();
                labelX9.Visible =false ;
                lbljarime1.Visible = false;
                labelX5.Visible = false;
                txtjarime.Visible = false;


            }
        }












    }
}
