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

namespace MainTemplate
{
    public partial class frmTasviehHesab : Form
    {
        public frmTasviehHesab()
        {
            InitializeComponent();
        }

        private void frmTasviehHesab_Load(object sender, EventArgs e)
        {
            Class_Register_Logs LogsRegIn = new Class_Register_Logs(this.Text + " فرم باز شد ");
            GrdFill();
        }

        private void frmTasviehHesab_FormClosed(object sender, FormClosedEventArgs e)
        {
            Class_Register_Logs LogsRegIn = new Class_Register_Logs(this.Text + " فرم بسته شد ");
        }

        private void GrdFill()
        {
            Class_Db_Connection db = new Class_Db_Connection();
            DataTable dt = new DataTable();
            dt = db.dbOut(@"SELECT     TOP (100) PERCENT dbo.tblRoomTahvil.TahvilID AS [کد تحویل], dbo.tblUsers.NameOfUser + N' ' + dbo.tblUsers.SnameOfUser AS [نام و نام خانوادگی], 
                      dbo.tblRoomTahvil.RoomID AS [کد اتاق], dbo.tblRoomTypes.RoomTypeDescription AS [نوع اتاق], dbo.tblHotelRooms.PriceOfPerNight AS [مبلغ هر شب اقامت], 
                      dbo.tblRoomTahvil.EnterDateSh AS [زمان ورود] FROM         dbo.tblRoomTahvil INNER JOIN     dbo.tblHotelRooms ON dbo.tblRoomTahvil.RoomID = dbo.tblHotelRooms.RoomID INNER JOIN
                      dbo.tblUsers ON dbo.tblRoomTahvil.CustomerID = dbo.tblUsers.RowID INNER JOIN     dbo.tblRoomTypes ON dbo.tblHotelRooms.RoomTypeID = dbo.tblRoomTypes.RoomTypeID WHERE     (dbo.tblRoomTahvil.TasviehBit = 0) ORDER BY  TahvilID DESC");

            DGrd_HotelUsers.DataSource = dt;
        }

        private void DGrd_HotelUsers_CellClick(object sender, DataGridViewCellEventArgs e)
        {
            Int32 selectedCellCount = DGrd_HotelUsers.GetCellCount(DataGridViewElementStates.Selected);
            String Str_Fcode = String.Empty;

            if (selectedCellCount > 0)
            {
                if (DGrd_HotelUsers.AreAllCellsSelected(true))
                {
                    MessageBox.Show("All cells are selected", "Selected Cells");
                }
                else
                {
                    System.Text.StringBuilder sb = new System.Text.StringBuilder();

                    for (int i = 0; i < selectedCellCount; i++)
                    {
                        Txt_HazineiePeShab.Text = DGrd_HotelUsers.Rows[int.Parse(DGrd_HotelUsers.SelectedCells[i].RowIndex.ToString())].Cells[4].Value.ToString();
                        Txt_RoomsID.Text = DGrd_HotelUsers.Rows[int.Parse(DGrd_HotelUsers.SelectedCells[i].RowIndex.ToString())].Cells[2].Value.ToString();
                        Txt_TvilCode.Text = DGrd_HotelUsers.Rows[int.Parse(DGrd_HotelUsers.SelectedCells[i].RowIndex.ToString())].Cells[0].Value.ToString(); 
                        Class_Db_Connection db = new Class_Db_Connection();
                        Txt_DateKhorooj.Text = db.dtShamsi();

                        DateTime Enterdate = ShamsiToMiladi(DGrd_HotelUsers.Rows[int.Parse(DGrd_HotelUsers.SelectedCells[i].RowIndex.ToString())].Cells[5].Value.ToString());
                        DateTime ExitDate = ShamsiToMiladi(Txt_DateKhorooj.Text.Trim());
                        TimeSpan Tm_S = ExitDate.Subtract(Enterdate);
                        Txt_ModdateEgamat.Text = Convert.ToString(Tm_S.Days + 1);
                    }
                }

            }
        }

        public DateTime ShamsiToMiladi(String ShamsiDate)
        {
            DateTime MyMilDate;
            String p1, p2, p3;
            p1 = ShamsiDate.Substring(0, 4);
            p2 = ShamsiDate.Substring(5, 2);
            p3 = ShamsiDate.Substring(8, 2);
            PersianCalendar pers = new PersianCalendar();
            MyMilDate = pers.ToDateTime(int.Parse(p1), int.Parse(p2), int.Parse(p3), 0, 0, 0, 0);
            return MyMilDate;
        }

        private void DGrd_HotelUsers_CellEnter(object sender, DataGridViewCellEventArgs e)
        {
            Int32 selectedCellCount = DGrd_HotelUsers.GetCellCount(DataGridViewElementStates.Selected);
            String Str_Fcode = String.Empty;

            if (selectedCellCount > 0)
            {
                if (DGrd_HotelUsers.AreAllCellsSelected(true))
                {
                    MessageBox.Show("All cells are selected", "Selected Cells");
                }
                else
                {
                    System.Text.StringBuilder sb = new System.Text.StringBuilder();

                    for (int i = 0; i < selectedCellCount; i++)
                    {
                        Txt_HazineiePeShab.Text = DGrd_HotelUsers.Rows[int.Parse(DGrd_HotelUsers.SelectedCells[i].RowIndex.ToString())].Cells[4].Value.ToString();
                        Txt_RoomsID.Text = DGrd_HotelUsers.Rows[int.Parse(DGrd_HotelUsers.SelectedCells[i].RowIndex.ToString())].Cells[2].Value.ToString();
                        Txt_TvilCode.Text = DGrd_HotelUsers.Rows[int.Parse(DGrd_HotelUsers.SelectedCells[i].RowIndex.ToString())].Cells[0].Value.ToString(); 
                        Class_Db_Connection db = new Class_Db_Connection();
                        Txt_DateKhorooj.Text = db.dtShamsi();

                        DateTime Enterdate = ShamsiToMiladi(DGrd_HotelUsers.Rows[int.Parse(DGrd_HotelUsers.SelectedCells[i].RowIndex.ToString())].Cells[5].Value.ToString());
                        DateTime ExitDate = ShamsiToMiladi(Txt_DateKhorooj.Text.Trim());
                        TimeSpan Tm_S = ExitDate.Subtract(Enterdate);
                        Txt_ModdateEgamat.Text = Convert.ToString(Tm_S.Days + 1);

                    }
                }

            }
        }

        private void Txt_ModdateEgamat_KeyPress(object sender, KeyPressEventArgs e)
        {
            int ascii = Convert.ToInt16(e.KeyChar);
            if (ascii >= 48 && ascii <= 57 || ascii == 8)
            {
                e.Handled = false;
            }
            else
            {
                e.Handled = true;
            }
        }

        private void Txt_HazineiePeShab_KeyPress(object sender, KeyPressEventArgs e)
        {
            int ascii = Convert.ToInt16(e.KeyChar);
            if (ascii >= 48 && ascii <= 57 || ascii == 8)
            {
                e.Handled = false;
            }
            else
            {
                e.Handled = true;
            }
        }

        private void Txt_HazineieKhoraki_KeyPress(object sender, KeyPressEventArgs e)
        {
            int ascii = Convert.ToInt16(e.KeyChar);
            if (ascii >= 48 && ascii <= 57 || ascii == 8)
            {
                e.Handled = false;
            }
            else
            {
                e.Handled = true;
            }
        }

        private void Txt_EmkanateRefahi_KeyPress(object sender, KeyPressEventArgs e)
        {
            int ascii = Convert.ToInt16(e.KeyChar);
            if (ascii >= 48 && ascii <= 57 || ascii == 8)
            {
                e.Handled = false;
            }
            else
            {
                e.Handled = true;
            }
        }

        private void Txt_SaiereKhadamat_KeyPress(object sender, KeyPressEventArgs e)
        {
            int ascii = Convert.ToInt16(e.KeyChar);
            if (ascii >= 48 && ascii <= 57 || ascii == 8)
            {
                e.Handled = false;
            }
            else
            {
                e.Handled = true;
            }
        }

        private void Txt_EghamatHazineh_KeyPress(object sender, KeyPressEventArgs e)
        {
            int ascii = Convert.ToInt16(e.KeyChar);
            if (ascii >= 48 && ascii <= 57 || ascii == 8)
            {
                e.Handled = false;
            }
            else
            {
                e.Handled = true;
            }
        }

        private void Txt_Takhfif_KeyPress(object sender, KeyPressEventArgs e)
        {
            int ascii = Convert.ToInt16(e.KeyChar);
            if (ascii >= 48 && ascii <= 57 || ascii == 8)
            {
                e.Handled = false;
            }
            else
            {
                e.Handled = true;
            }
        }

        private void Txt_GabelePardakht_KeyPress(object sender, KeyPressEventArgs e)
        {
            int ascii = Convert.ToInt16(e.KeyChar);
            if (ascii >= 48 && ascii <= 57 || ascii == 8)
            {
                e.Handled = false;
            }
            else
            {
                e.Handled = true;
            }
        }

        private void Btn_Mohasebeh_Click(object sender, EventArgs e)
        {
            Class_Db_Connection db = new Class_Db_Connection();
            DataTable dt = new DataTable();

            Btn_SabteNahaii.Enabled = true;
            Btn_Mohasebeh.Enabled = false;
            button1.Enabled = true;
            DGrd_HotelUsers.Enabled = false;

            db.cmd.Parameters.Add("@Number1", SqlDbType.Decimal).Value = Decimal.Parse(Txt_HazineiePeShab.Text.Trim());
            db.cmd.Parameters.Add("@Number2", SqlDbType.Decimal).Value = Decimal.Parse(Txt_ModdateEgamat.Text.Trim());
            dt = db.dbOut(@"SELECT     dbo.Function_Zarb_DB(@Number1, @Number2) AS HaselZarb_Ba_Function");

            Txt_EghamatHazineh.Text = dt.Rows[0][0].ToString();

            Decimal TotalPrc = Decimal.Parse(Txt_EghamatHazineh.Text.Trim()) + Decimal.Parse(Txt_EmkanateRefahi.Text.Trim()) + Decimal.Parse(Txt_SaiereKhadamat.Text.Trim()) - Decimal.Parse(Txt_Takhfif.Text.Trim());
            Txt_GabelePardakht.Text = TotalPrc.ToString();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            Btn_SabteNahaii.Enabled = false;
            Btn_Mohasebeh.Enabled = true;
            DGrd_HotelUsers.Enabled = true;
            button1.Enabled = false;
        }

        private void Btn_SabteNahaii_Click(object sender, EventArgs e)
        {
            DialogResult dr = MessageBox.Show("آیا نسبت به عمل تسویه حساب اطمینان دارید", "اخطار تحویل", MessageBoxButtons.OKCancel, MessageBoxIcon.Warning);
            if (dr == DialogResult.OK)
            {
                Class_Db_Connection db;
                db = new Class_Db_Connection();
                db.cmd.Parameters.Add("@InUseBit", SqlDbType.Bit).Value = Boolean.Parse("false");
                db.cmd.Parameters.Add("@RoomID", SqlDbType.Int).Value = int.Parse(Txt_RoomsID.Text.Trim());

                db.exeSqlCmd(@"UPDATE  tblHotelRooms  SET InUseBit = @InUseBit  WHERE (RoomID = @RoomID)");

                Lsc_LinqToSqlDataContext dbl = new Lsc_LinqToSqlDataContext();
                tblRoomTahvil tblH = dbl.tblRoomTahvils.Single(n => n.TahvilID == int.Parse(Txt_TvilCode.Text.Trim()));
                tblH.PricePerNight = Decimal.Parse(Txt_HazineiePeShab.Text.Trim());

                tblH.ExitDateMiladi = ShamsiToMiladi(Txt_DateKhorooj.Text.Trim());
                tblH.ExitDateSh = Txt_DateKhorooj.Text.Trim();
                tblH.TasviehBit = Boolean.Parse("true");
                tblH.KhorakiGaza = Decimal.Parse(Txt_HazineieKhoraki.Text.Trim());
                tblH.VarzeshiSargarmi = Decimal.Parse(Txt_EmkanateRefahi.Text.Trim());
                tblH.OtherService = Decimal.Parse(Txt_SaiereKhadamat.Text.Trim());
                tblH.TotalEgamatPrice = Decimal.Parse(Txt_EghamatHazineh.Text.Trim());
                tblH.Takhfif = Decimal.Parse(Txt_Takhfif.Text.Trim());
                tblH.TotalPrice = Decimal.Parse(Txt_GabelePardakht.Text.Trim());

                dbl.SubmitChanges();
                Class_Register_Logs LogsRegIn = new Class_Register_Logs(this.Text + " - تحویل اتاق انجام شد");
                GrdFill();
                Btn_SabteNahaii.Enabled = false;
                Btn_Mohasebeh.Enabled = true;
                DGrd_HotelUsers.Enabled = true;
                button1.Enabled = false;
                MessageBox.Show("تحویل اتاق به مشتری ثبت شد.");
            }
            else
            {
                Btn_SabteNahaii.Enabled = false;
                Btn_Mohasebeh.Enabled = true;
                DGrd_HotelUsers.Enabled = true;
                button1.Enabled = false;
            }
        }
    }
}
