using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

namespace RezerveBeliteHavaPeimaCS
{
    public partial class frmCusturersStatus : Form
    {
        public frmCusturersStatus()
        {
            InitializeComponent();
        }

        protected void grdFill()
        {
            FirstClass db = new FirstClass();
            DataTable dt = new DataTable();

            dt = db.dbOut(@"SELECT     dbo.Custumers.CustomerID AS [کد مشتری], dbo.Custumers.NameSname AS [نام و نام خانوادگی], dbo.Custumers.PhoneNum AS [شماره تلفن], 
                      dbo.RezerveTickets.BianehPardakhti AS [بیعانه پرداختی], dbo.RezerveTickets.Mandeh AS [مانده بدهی], 
                      dbo.ServicesDef.ServiceDescript AS [مربوط به رزرو], dbo.ServicesDef.ServiceDate AS [تاریخ], dbo.RezerveTickets.RezerveID AS [کد رزرو]
                        FROM         dbo.Custumers INNER JOIN dbo.RezerveTickets ON dbo.Custumers.CustomerID = dbo.RezerveTickets.RezerverID INNER JOIN
                      dbo.ServicesDef ON dbo.RezerveTickets.ServiceID = dbo.ServicesDef.ServiceID WHERE     (dbo.ServicesDef.ExpireBit = 0) AND (dbo.RezerveTickets.Mandeh > 0)");

            dataGridView1.DataSource = dt;
            cmbFill();
        }

        protected void cmbFill()
        {
            FirstClass db = new FirstClass();
            DataTable dt = new DataTable();

            dt = db.dbOut(@"SELECT     Custumers.CustomerID,  Custumers.NameSname + N'- کد مشتری: ' + LTRIM(RTRIM(STR(Custumers.CustomerID))) AS NameCodStt
                            FROM         Custumers INNER JOIN  RezerveTickets ON Custumers.CustomerID = RezerveTickets.RezerverID INNER JOIN
                            ServicesDef ON RezerveTickets.ServiceID = ServicesDef.ServiceID WHERE     (ServicesDef.ExpireBit = 0) AND (RezerveTickets.Mandeh > 0)
                            GROUP BY Custumers.CustomerID, Custumers.NameSname + N'- کد مشتری: ' + LTRIM(RTRIM(STR(Custumers.CustomerID)))");
            comboBox1.DataSource = dt;
            comboBox1.ValueMember = "CustomerID";
            comboBox1.DisplayMember = "NameCodStt";
        }

        protected void cmbFilt()
        {
            FirstClass db = new FirstClass();
            DataTable dt = new DataTable();

            dt = db.dbOut(@"SELECT     dbo.Custumers.CustomerID AS [کد مشتری], dbo.Custumers.NameSname AS [نام و نام خانوادگی], dbo.Custumers.PhoneNum AS [شماره تلفن], 
                      dbo.RezerveTickets.BianehPardakhti AS [بیعانه پرداختی], dbo.RezerveTickets.Mandeh AS [مانده بدهی], 
                      dbo.ServicesDef.ServiceDescript AS [مربوط به رزرو], dbo.ServicesDef.ServiceDate AS [تاریخ حرکت], dbo.RezerveTickets.RezerveID AS [کد رزرو] FROM         dbo.Custumers INNER JOIN
                      dbo.RezerveTickets ON dbo.Custumers.CustomerID = dbo.RezerveTickets.RezerverID INNER JOIN
                      dbo.ServicesDef ON dbo.RezerveTickets.ServiceID = dbo.ServicesDef.ServiceID WHERE     (dbo.ServicesDef.ExpireBit = 0) AND (dbo.RezerveTickets.Mandeh > 0) AND (dbo.Custumers.CustomerID = '"+ comboBox1.SelectedValue +"')");
            dataGridView1.DataSource = dt;
        }

        private void frmCusturersStatus_Load(object sender, EventArgs e)
        {
            grdFill();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            cmbFilt();
        }

        private void dataGridView1_CellClick(object sender, DataGridViewCellEventArgs e)
        {
            Int32 selectedCellCount = dataGridView1.GetCellCount(DataGridViewElementStates.Selected);
            if (selectedCellCount > 0)
            {
                if (dataGridView1.AreAllCellsSelected(true))
                {
                    for (int i = 0; i < selectedCellCount; i++)
                    {
                        textBox1.Text = (dataGridView1.Rows[int.Parse(dataGridView1.SelectedCells[i].RowIndex.ToString())].Cells[4].Value.ToString());
                        textBox2.Text = (dataGridView1.Rows[int.Parse(dataGridView1.SelectedCells[i].RowIndex.ToString())].Cells[7].Value.ToString());
                        textBox4.Text = (dataGridView1.Rows[int.Parse(dataGridView1.SelectedCells[i].RowIndex.ToString())].Cells[3].Value.ToString());
                    }
                    MessageBox.Show("All cells are selected", "Selected Cells");
                }
                else
                {
                    for (int i = 0;i < selectedCellCount; i++)
                    {
                        textBox1.Text = (dataGridView1.Rows[int.Parse(dataGridView1.SelectedCells[i].RowIndex.ToString())].Cells[4].Value.ToString());
                        textBox2.Text = (dataGridView1.Rows[int.Parse(dataGridView1.SelectedCells[i].RowIndex.ToString())].Cells[7].Value.ToString());
                        textBox4.Text = (dataGridView1.Rows[int.Parse(dataGridView1.SelectedCells[i].RowIndex.ToString())].Cells[3].Value.ToString());
                    }

                }
            }
        }

        protected void txtClear()
        {
            foreach (Control ctrl in this.Controls)
            {
                if (ctrl is TextBox)
                {
                    ctrl.Text = "";
                }
            }
        }

        private void button2_Click(object sender, EventArgs e)
        {
            if (textBox1.Text == "" || textBox2.Text == "" || textBox3.Text == "" || textBox4.Text == "")
            {
                MessageBox.Show("کد رزرو ، مبلغ بیعانه و مانده بدهی و مبلغ پرداختی باید وارد شوند");
            }
            else
            {
                if (float.Parse(textBox3.Text.Trim()) != float.Parse(textBox1.Text))
                {
                    MessageBox.Show("مبلغ وارد شده با میزان بدهی برابر نیست");
                }
                else
                {
                    FirstClass db = new FirstClass();
                    
                    db.cmd.Parameters.Add("@RezerveID" , SqlDbType.Int).Value = int.Parse(textBox2.Text.Trim());
                    db.cmd.Parameters.Add("@BianehPardakhti", SqlDbType.Float).Value = (float.Parse(textBox3.Text.Trim()) + float.Parse(textBox4.Text.Trim()));
                    db.cmd.Parameters.Add("@Mandeh", SqlDbType.Float).Value = 0;

                    db.exeCommand("sp_RezerveTickets_Update201");
                    grdFill();
                    txtClear();
                    MessageBox.Show("تسویه گردید");

                }
            }

        }


    }
}