using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Data;
using System.Globalization;
using System.Data.SqlClient;
using Dish_Project;
namespace Publishmentation
{
    public partial class book_registeration : Form
    
    {
        public book_registeration()
        {
            InitializeComponent();
        }
        FormCollection frm = new FormCollection();

        enum radioButtons : short
        {
            Book = 1, Magazine, Advertizment
        }
        Connector connection = new Connector();

        void listBox()
        {
            Connector connection = new Connector();
           connection.connect();
           DataSet   dataReader =connection.selecte("select convert(nvarchar(5),customerId)+'-'+ customerName+' '+customerFamily from customer ");
           connection.disConnect();
           comboBox1.DataSource = dataReader;
           comboBox1.DataBindings.Add("DataSource", dataReader, dataReader.Tables[0].TableName);
           comboBox1.DisplayMember = dataReader.Tables[0].Columns[0].ColumnName;
            comboBox1.Refresh();

        }
        void adverTismentListBox()
        {
            Connector connection = new Connector();
            connection.connect();
            DataSet dataReader = connection.selecte("select Name from advertizmentType ");
            connection.disConnect();
            comboBox2.DataSource = dataReader;
            comboBox2.DataBindings.Add("DataSource", dataReader, dataReader.Tables[0].TableName);
            comboBox2.DisplayMember = dataReader.Tables[0].Columns[0].ColumnName;
            comboBox2.Refresh();

        }
        private void book_registeration_Load(object sender, EventArgs e)
        {
            InputLanguage.CurrentInputLanguage = InputLanguage.FromCulture(CultureInfo.GetCultureInfo("fa"));
            frm = Application.OpenForms;
            radioButton1.Select();
            listBox();
            adverTismentListBox();
        }

        private void radioButton2_CheckedChanged(object sender, EventArgs e)
        {
             groupBox2.Enabled = true;
            groupBox1.Enabled = false;
            groupBox3.Enabled = false;
        }

        private void radioButton3_CheckedChanged(object sender, EventArgs e)
        {
            groupBox3.Enabled = true;
            groupBox2.Enabled = false;
            groupBox1.Enabled = false;
        }
        private void radioButton1_CheckedChanged(object sender, EventArgs e)
        {
            groupBox1.Enabled = true;
            groupBox2.Enabled = false;
            groupBox3.Enabled = false;
        }

        private void book_registeration_FormClosed(object sender, FormClosedEventArgs e)
        {
            frm["Form1"].Enabled = true;
            frm["Form1"].Show();
            frm["Form1"].Focus();
        }
        private void button1_Click(object sender, EventArgs e)
        {
            radioButtons selected = new radioButtons();
            SqlCommand command = new SqlCommand();
            PersionCelender celender=new PersionCelender();
            command.CommandType = CommandType.StoredProcedure;
            Magazine magazine = new Magazine();
            BookPublishing book = new BookPublishing();
            DateTime requestDate, receiptDate;
            celender.ConvertToMiladi(Convert.ToInt32(textBox12.Text),Convert.ToInt32(textBox13.Text),Convert.ToInt32(textBox19.Text));
            requestDate = Convert.ToDateTime(celender.Date);
            celender.ConvertToMiladi(Convert.ToInt32(textBox20.Text), Convert.ToInt32(textBox18.Text), Convert.ToInt32(textBox21.Text));
            receiptDate = Convert.ToDateTime(celender.Date);
            if (radioButton1.Checked)
            {
                book.ISBN = Convert.ToInt32(textBox1.Text);
                book.Subject = textBox2.Text;
                book.Composer = textBox4.Text;
                book.Translator = textBox3.Text;
                book.PageCount = Convert.ToInt32(textBox5.Text);
                command.Parameters.Add(new SqlParameter("@ISBN", SqlDbType.NVarChar, 13));
                command.Parameters["@ISBN"].Value = book.ISBN.ToString();
                command.Parameters.Add(new SqlParameter("@Subject", SqlDbType.NVarChar, 50));
                command.Parameters["@Subject"].Value = book.Subject;
                command.Parameters.Add(new SqlParameter("@Translator", SqlDbType.NVarChar,50));
                command.Parameters["@Translator"].Value = book.Translator;
                command.Parameters.Add(new SqlParameter("@composer", SqlDbType.NVarChar,50));
                command.Parameters["@composer"].Value = book.Composer;
                command.Parameters.Add(new SqlParameter("@pageCount", SqlDbType.Int));
                command.Parameters["@pageCount"].Value = book.PageCount;
                command.Parameters.Add(new SqlParameter("@bookId",SqlDbType.Int,4));
                command.Parameters["@bookId"].Direction = ParameterDirection.Output;
                command.Parameters["@bookId"].Value = 0;
                command.CommandText = "newBook";
                book.PublishId=(int)connection.storeProcedure2(command);
                selected = radioButtons.Book;
            }
            else if (radioButton2.Checked)
            {
                magazine.MagazineNumber = textBox7.Text;
                magazine.PublishTitle = textBox10.Text;
                magazine.Subject = textBox9.Text;
                magazine.Manager = textBox6.Text;
                magazine.PageCount = Convert.ToInt32(textBox17.Text);
                magazine.Magazinyear = textBox8.Text;
                command.Parameters.Add(new SqlParameter("@Subject", SqlDbType.NVarChar, 20));
                command.Parameters["@Subject"].Value = magazine.Subject;
                command.Parameters.Add(new SqlParameter("@Title", SqlDbType.NVarChar, 20));
                command.Parameters["@Title"].Value =magazine.PublishTitle;
                command.Parameters.Add(new SqlParameter("@number_magaziner", SqlDbType.Int));
                command.Parameters["@number_magaziner"].Value = magazine.MagazineNumber;
                command.Parameters.Add(new SqlParameter("@year_magazine", SqlDbType.Int));
                command.Parameters["@year_magazine"].Value = magazine.Magazinyear;
                command.Parameters.Add(new SqlParameter("@manager", SqlDbType.NVarChar,50));
                command.Parameters["@manager"].Value =magazine.Manager;
                command.Parameters.Add(new SqlParameter("@pageCount", SqlDbType.Int));
                command.Parameters["@pageCount"].Value = magazine.PageCount;
                command.Parameters.Add(new SqlParameter("@magazine_ID", SqlDbType.Int, 4));
                command.Parameters["@magazine_ID"].Direction = ParameterDirection.Output;
                command.Parameters["@magazine_ID"].Value = 0;
                command.CommandText = "newMagazine";
                magazine.PublishId = connection.storeProcedure2(command);
                selected = radioButtons.Magazine;
            }
            else
            {
                selected = radioButtons.Advertizment;
            }

            command.Parameters.Clear();
            command.Parameters.Add(new SqlParameter("@CustomerId", SqlDbType.Int));
            command.Parameters["@CustomerId"].Value = selectId(comboBox1.Text);
            command.Parameters.Add(new SqlParameter("@publishType", SqlDbType.Int));
            command.Parameters["@publishType"].Value = Convert.ToInt32(selected);
            command.Parameters.Add(new SqlParameter("@PublishId", SqlDbType.Int));
            switch (selected)
            {
                case radioButtons.Book:
                    command.Parameters["@PublishId"].Value =book.PublishId ;
                    break;
                case radioButtons.Magazine:
                    command.Parameters["@PublishId"].Value = magazine.PublishId;
                    break;
                case radioButtons.Advertizment:
                    command.Parameters["@PublishId"].Value = book.PublishId;
                    break;
            }
            command.Parameters.Add(new SqlParameter("@requestDate", SqlDbType.DateTime));
            command.Parameters["@requestDate"].Value =requestDate;
            command.Parameters.Add(new SqlParameter("@receipttDate", SqlDbType.DateTime));
            command.Parameters["@receipttDate"].Value = receiptDate;
            command.Parameters.Add(new SqlParameter("@receipt", SqlDbType.Bit));
            command.Parameters["@receipt"].Value = 0;
            command.Parameters.Add(new SqlParameter("@price", SqlDbType.Int));
            command.Parameters["@price"].Value = Convert.ToInt32(textBox16.Text);
            command.Parameters.Add(new SqlParameter("@requestID", SqlDbType.Int, 4));
            command.Parameters["@requestID"].Direction = ParameterDirection.Output;
            command.Parameters["@requestID"].Value = 0;
            command.CommandText = "newRequest";
            int requestId=connection.storeProcedure2(command);
            string STR = "! سفارش با شماره ردیف " + requestId.ToString() + " با موفقیت ثبت شد";
            if (Convert.ToBoolean(requestId))
                MessageBox.Show(STR, "", MessageBoxButtons.OK, MessageBoxIcon.Information);
            else
                MessageBox.Show("!ثبت سفارش با مشکل روبرو شد", "", MessageBoxButtons.OK, MessageBoxIcon.Information);
             this.Close();

        }
        private int selectId(string  p)
        {
            string[] str=new string[4];
            str=p.Split('-');
            label1.Text = str[0];
            connection.connect();
            SqlDataReader dataReader = connection.select("select customerId from customer where customerId="
                +str[0]);
            dataReader.Read();
            int x=dataReader.GetInt32(0);
            connection.disConnect();
            return x;
        }

    }
}