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

namespace Reservtion
{
    public partial class FullInfo : Form
    {
        RConection r;
        FullInformation fi;
        public FullInfo(RConection r1, FullInformation fi1)
        {
            int t = 0;
            r = r1;
            fi = fi1;
            InitializeComponent();
            richTextBox1.Text = "اطلاعات مسافر: \n نام: " + fi.name + "\n فامیل: " + fi.family + "\n تلفن همراه: " + fi.cellphone + "\n شماره شناسنامه: " + fi.ID + "\n تاریخ رزرو: " + fi.reservdate + "\n مبلغ پرداخت: " + fi.paymoney + "\n توضیحات: " + fi.expl+"\n تخفیف: ";
            if (fi.discount == "انتخاب کنید")
                richTextBox1.Text += ".\n";
            else
                richTextBox1.Text += fi.discount + "\n";
            richTextBox1.Text += "اطلاعات همراهان:\n";
            while (fi.ta[t] != null)
            {
                richTextBox1.Text += "نام: " + fi.ta[t].name + "\n" + "فامیل: " + fi.ta[t].family + "\n شماره شناسنامه: " + fi.ta[t].id + "\n توضیحات: " + fi.ta[t].expl + "\n";
                t++;
            }
        }

        private void button1_Click(object sender, EventArgs e)
        {
            try
            {
                int c;
                string s = "INSERT INTO Customer (Code, RoomNum, [Name], Family, ID_Number, ReserveDate, PaymentCost, Expl) VALUES(" + fi.code + "," + fi.roomnum + ",N'" + fi.name + "',N'" + fi.family + "',N'" + fi.ID + "'," + fi.reservdate.ToShortDateString() + "," + fi.paymoney + ",N'" + fi.expl + "');";
                r.cmd.CommandText = s;
                r.conn.Open();
                r.cmd.ExecuteNonQuery();
                r.cmd.CommandText = "select max(Code) from CustomerAfters";
                object ob = r.cmd.ExecuteScalar();
                if (ob == DBNull.Value)
                    c = 1;
                else
                    c = Convert.ToInt32(ob) + 1;
                int t = 0;
                while (fi.ta[t] != null)
                {
                    r.cmd.CommandText = "INSERT INTO CustomerAfters (Code, CustomerCode, [Name], Family) VALUES(" + c.ToString() + "," + fi.code + ",N'" + fi.ta[t].name + "',N'" + fi.ta[t].family + "')";
                    r.cmd.ExecuteNonQuery();
                    c++;
                    t++;
                }
                r.conn.Close();
                MessageBox.Show("اطلاعات مشتری ثبت شد", "پذیرش مشتری", MessageBoxButtons.OK, MessageBoxIcon.Information, MessageBoxDefaultButton.Button1);
                r.a = true;

                this.Close();
            }
            catch(SqlException ex)
            {
                MessageBox.Show(ex.Message + "\n" + ex.ErrorCode);
                this.Close();
            }
        }

        private void button2_Click(object sender, EventArgs e)
        {
            r.a = false;
            this.Close();
        }
    }
}