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 System.Data.SqlClient;
using System.Globalization;


namespace HotelProject
{
    public partial class FrmPazireshManagement : Form
    {
        public FrmPazireshManagement()
        {
            InitializeComponent();
        }



        SqlConnection con;
        string SearchDate1 = "";
        string SearchDate2 = "";
        string SType = "";
        SqlCommand cmd;


        private void FillText()
        {

            try
            {

                TxtCode.Text = GrdList.CurrentRow.Cells["CustomerID"].Value.ToString();
                TxtName.Text = GrdList.CurrentRow.Cells["CustomerName"].Value.ToString();
                TxtRoomNum.Text = GrdList.CurrentRow.Cells["RoomNum"].Value.ToString();
                TxtRoomType.Text = GrdList.CurrentRow.Cells["RoomType"].Value.ToString();
                TxtHamrah.Text = GrdList.CurrentRow.Cells["TedadHamrah"].Value.ToString();
                TxtBDate.Text = GrdList.CurrentRow.Cells["BDate"].Value.ToString();
                TxtEDate.Text = GrdList.CurrentRow.Cells["EDate"].Value.ToString();
                TxtMadrak.Text = GrdList.CurrentRow.Cells["Madrak"].Value.ToString();
                TxtPish.Text = GrdList.CurrentRow.Cells["PishPardakht"].Value.ToString();
                TxtSum.Text = GrdList.CurrentRow.Cells["MajmuHazine"].Value.ToString();
                BtnFish.Enabled = true;
                BtnServicesList.Enabled = true;

                if (TxtEDate.Text != "")
                {
                    BtnFinish.Enabled = false;
                    BtnGetService.Enabled = false;

                }
                else
                {
                    BtnFinish.Enabled = true;
                    BtnGetService.Enabled = true;
                }
            }
            catch
            {
                TxtCode.Text = "";
                TxtName.Text = "";
                TxtRoomNum.Text = "";
                TxtRoomType.Text = "";
                TxtHamrah.Text = "";
                TxtBDate.Text = "";
                TxtEDate.Text = "";
                TxtMadrak.Text = "";
                TxtPish.Text = "";
                TxtSum.Text = "";
                BtnFinish.Enabled = false;
                BtnGetService.Enabled = false;
                BtnFish.Enabled = false;
                BtnServicesList.Enabled = false;
            }
        }















        private void FrmPazireshManagement_Load(object sender, EventArgs e)
        {
            con = new SqlConnection("Server = . ; DataBase = DbHotel ; Integrated Security = True");
            SqlDataAdapter da = new SqlDataAdapter("SelectPaziresh", con);
            da.SelectCommand.CommandType = CommandType.StoredProcedure;

            DataTable dt = new DataTable();
            da.Fill(dt);

            GrdList.DataSource = dt;





            FillText();
            CmbSearchType.SelectedIndex = 0;



            Searching();
            FillText();


        }




        private void Searching()
        {



            if (SearchDate1 != "" & SearchDate2 != "")
            {

                if (CmbSearchType.SelectedIndex == 0)
                {
                    cmd = new SqlCommand();
                    cmd.Connection = con;
                    cmd.CommandText = "Select * From TblPaziresh Where ID like '" + TxtSearchCode.Text + "%' and Name like '%" + TxtSearchName.Text + "%' and BDate Between '" + SearchDate1 + "' and '" + SearchDate2 + "' and RoomNum like '" + TxtSearchRoomNum.Text + "%' and EDate = ''";


                    DataTable dt = new DataTable();

                    SqlDataAdapter da = new SqlDataAdapter();
                    da.SelectCommand = cmd;


                    da.Fill(dt);

                    GrdList.DataSource = dt;

                    con.Close();
                }

                else
                {
                    SqlCommand cmd = new SqlCommand();
                    cmd.Connection = con;
                    cmd.CommandText = "Select * From TblPaziresh Where ID like '" + TxtSearchCode.Text + "%' and Name like '%" + TxtSearchName.Text + "%' and BDate Between '" + SearchDate1 + "' and '" + SearchDate2 + "' and RoomNum like '" + TxtSearchRoomNum.Text + "%' and EDate != ''";


                    DataTable dt = new DataTable();

                    SqlDataAdapter da = new SqlDataAdapter();
                    da.SelectCommand = cmd;


                    da.Fill(dt);

                    GrdList.DataSource = dt;

                    con.Close();
                }

            }

            else
            {


                if (CmbSearchType.SelectedIndex == 0)
                {
                    SqlCommand cmd = new SqlCommand();
                    cmd.Connection = con;
                    cmd.CommandText = "Select * From TblPaziresh Where ID like '" + TxtSearchCode.Text + "%' and Name like '%" + TxtSearchName.Text + "%' and RoomNum like '" + TxtSearchRoomNum.Text + "%' and EDate = ''";


                    DataTable dt = new DataTable();

                    SqlDataAdapter da = new SqlDataAdapter();
                    da.SelectCommand = cmd;


                    da.Fill(dt);

                    GrdList.DataSource = dt;

                    con.Close();
                }

                else
                {
                    SqlCommand cmd = new SqlCommand();
                    cmd.Connection = con;
                    cmd.CommandText = "Select * From TblPaziresh Where ID like '" + TxtSearchCode.Text + "%' and Name like '%" + TxtSearchName.Text + "%' and RoomNum like '" + TxtSearchRoomNum.Text + "%' and EDate != ''";


                    DataTable dt = new DataTable();

                    SqlDataAdapter da = new SqlDataAdapter();
                    da.SelectCommand = cmd;


                    da.Fill(dt);

                    GrdList.DataSource = dt;

                    con.Close();
                }




            }


            FillText();

        }








        private void GrdList_Click(object sender, EventArgs e)
        {
            FillText();
        }

        private void GrdList_KeyUp(object sender, KeyEventArgs e)
        {
            FillText();
        }

        private void TxtSearchCode_TextChanged(object sender, EventArgs e)
        {
            NumCorrection.DoCorrection(TxtSearchCode);
            Searching();
        }

        private void MskSearchFrom_MaskInputRejected(object sender, MaskInputRejectedEventArgs e)
        {
         
        }

        private void MskSearchTo_MaskInputRejected(object sender, MaskInputRejectedEventArgs e)
        {
          
        }

        private void MskSearchFrom_TextChanged(object sender, EventArgs e)
        {
            if (MskSearchFrom.MaskFull.ToString() == "False")
            {
                SearchDate1 = "";
            }
            else
            {
                SearchDate1 = MskSearchFrom.Text;
            }
            Searching();
        }

        private void MskSearchTo_TextChanged(object sender, EventArgs e)
        {
            if (MskSearchTo.MaskFull.ToString() == "False")
            {
                SearchDate2 = "";
            }
            else
            {
                SearchDate2 = MskSearchTo.Text;
            }
            Searching();
        }

        private void TxtSearchName_TextChanged(object sender, EventArgs e)
        {
            Searching();
        }

        private void TxtSearchRoomNum_TextChanged(object sender, EventArgs e)
        {
            NumCorrection.DoCorrection(TxtSearchRoomNum);
            Searching();
        }

        private void TxtSearchType_SelectedIndexChanged(object sender, EventArgs e)
        {
            Searching();
        }

        private void BtnDate_Click(object sender, EventArgs e)
        {
            DateTime today = DateTime.Now;
            PersianCalendar pc = new PersianCalendar();
            String StrDate = pc.GetYear(today).ToString("0000") + pc.GetMonth(today).ToString("00") + pc.GetDayOfMonth(today).ToString("00");
            MskSearchTo.Text = StrDate;
        }

        private void BtnFinish_Click(object sender, EventArgs e)
        {
            DateTime today = DateTime.Now;
            PersianCalendar pc = new PersianCalendar();
            String StrDate = pc.GetYear(today).ToString("0000") + "/" +pc.GetMonth(today).ToString("00") + "/" + pc.GetDayOfMonth(today).ToString("00");

            cmd = new SqlCommand("SetEndDate", con);
            cmd.CommandType = CommandType.StoredProcedure;
            cmd.Parameters.AddWithValue("@EndDate", StrDate);
            cmd.Parameters.AddWithValue("@ID", GrdList.CurrentRow.Cells[0].Value.ToString());

            con.Open();
            cmd.ExecuteNonQuery();
            con.Close();


            
            cmd = new SqlCommand("UpdateRoomState", con);
            cmd.CommandType = CommandType.StoredProcedure;
            cmd.Parameters.AddWithValue("@RoomNumber", TxtRoomNum.Text);
            cmd.Parameters.AddWithValue("@State", "خالی");

            con.Open();
            cmd.ExecuteNonQuery();
            con.Close();




            

            DateTime dt1;
            DateTime dt2;

            dt1 = Convert.ToDateTime(StrDate);
            dt2 = Convert.ToDateTime(TxtBDate.Text);

            int Distance = Convert.ToInt32((dt1 - dt2).TotalDays.ToString());


            cmd = new SqlCommand();
            cmd.Connection = con;

            cmd.CommandText = "Select * From TblRoomTypes Where RoomType = N'" + TxtRoomType.Text + "'";

            DataTable dt = new DataTable();

            SqlDataAdapter da = new SqlDataAdapter();
            da.SelectCommand = cmd;

            da.Fill(dt);

            int RoomSumCost = Distance * Convert.ToInt32(dt.Rows[0]["Cost"].ToString());


            da = new SqlDataAdapter("GetSum", con);
            da.SelectCommand.CommandType = CommandType.StoredProcedure;
            da.SelectCommand.Parameters.AddWithValue("@CustomerID", TxtCode.Text);
            dt = new DataTable();
            da.Fill(dt);

            string OldSum = dt.Rows[0][0].ToString();





            //'''''''''''''''
            cmd = new SqlCommand("EditSum", con);
            cmd.CommandType = CommandType.StoredProcedure;
            cmd.Parameters.AddWithValue("@CustomerID", TxtCode.Text);
            cmd.Parameters.AddWithValue("@Sum", Convert.ToInt32(OldSum) + RoomSumCost);

            con.Open();
            cmd.ExecuteNonQuery();
            con.Close();





            FrmMessage Fm = new FrmMessage();
            Fm.LblText.Text = "اتمام پذیرش با موفقیت انجام شد";
            Fm.PicClose.Visible = true;
            Fm.PicNO.Visible = false;
            Fm.PicYes.Visible = false;
            Fm.ShowDialog();


            BtnFinish.Enabled = false;
            BtnGetService.Enabled = false;

            FrmPazireshManagement_Load(null, null);

        }

        private void BtnGetService_Click(object sender, EventArgs e)
        {
            if (TxtCode.Text != "")
            {
                FrmGetService Fg = new FrmGetService();
                Fg.CustomerID = Convert.ToInt32(TxtCode.Text);
                int ID = Convert.ToInt32(GrdList.CurrentRow.Cells[0].Value.ToString());
                Fg.ShowDialog();
                
                FrmPazireshManagement_Load(null, null);

                for (int i = 0; i < GrdList.Rows.Count; i++)
                {
                   
                    if (i == 0)
                        GrdList.Rows[i].Selected = true;
                    else
                    {
                        GrdList.Rows[i-1].Selected = false;
                        GrdList.Rows[i].Selected = true;
                        GrdList.CurrentCell = GrdList.Rows[i].Cells[0];
                    }

                    if (GrdList.CurrentRow.Cells[0].Value == ID.ToString())
                    {
                        break;
                    }

                    FillText();


                }






            }
        }

        private void GrdList_CellContentClick(object sender, DataGridViewCellEventArgs e)
        {
           
        }

        private void BtnServicesList_Click(object sender, EventArgs e)
        {
            FrmGivenServicesList fgl = new FrmGivenServicesList();
            fgl.LblCode.Text = TxtCode.Text;
            fgl.LblName.Text = TxtName.Text;
            fgl.LblCost.Text = TxtSum.Text;
            fgl.ShowDialog();
        }

        private void BtnFish_Click(object sender, EventArgs e)
        {

            SqlDataAdapter da = new SqlDataAdapter("SelectCustomer", con);
            da.SelectCommand.CommandType = CommandType.StoredProcedure;
            da.SelectCommand.Parameters.AddWithValue("@ID", GrdList.CurrentRow.Cells[0].Value.ToString());
            DataTable DtPazireshInfo = new DataTable();
            da.Fill(DtPazireshInfo);


            SqlDataAdapter da2 = new SqlDataAdapter("SelectGivenServices", con);
            da2.SelectCommand.CommandType = CommandType.StoredProcedure;
            da2.SelectCommand.Parameters.AddWithValue("CustomerID",TxtCode.Text);
            DataTable DtServiceList = new DataTable();
            da2.Fill(DtServiceList);


            DataSet ds = new DataSet();
            ds.Merge(DtPazireshInfo);
            ds.Tables[0].TableName = "DtPazireshInfo";

            ds.Merge(DtServiceList);
            ds.Tables[1].TableName = "DtServiceList";

            stiReport1.Load("StiFish.mrt");
            stiReport1.RegData(ds);
            stiReport1.Show();






        }
    }
}
