using LibraryMangementSystem.Util;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace LibraryMangementSystem.BookManage
{
    public partial class BookAdd : Form
    {
        public BookAdd()
        {
            InitializeComponent();
            StartPosition = FormStartPosition.CenterScreen;
        }
        LibraryEntities1 dbContext = new LibraryEntities1();

        private void button1_Click_1(object sender, EventArgs e)
        {
            var BookId = textBoxBookId.Text;
            var ISBN = textBoxISBN.Text;
            var Title = textBoxTitle.Text;
            var Publish = dateTimePickerPublish.Value;
            var Author = textBoxAuthor.Text;
            var Words = textBoxWords.Text;
            var PublisherID = ((ComboBoxItem)comboBoxPublisherId.SelectedItem).Value.ToString();
            var Number = textBoxNumber.Text;
            var Price = textBoxPrice.Text;
            var KeyWord = textBoxKeyWord.Text;
            var Abstract = textBoxAbstract.Text;
            var BookClass = ((ComboBoxItem)comboBoxClass.SelectedItem).Value.ToString();
            if (BookId != "")
            {
                BookInfo bookInfo = new BookInfo();
                bookInfo.BookID = BookId;
                bookInfo.ISBN = ISBN;
                bookInfo.Title = Title;
                bookInfo.Publish = Publish.ToString("yyyy-MM-dd");
                bookInfo.Author = Author;
                bookInfo.PublisherID = PublisherID;
                bookInfo.ClassID = BookClass;
                if (Words != "")
                {
                    bookInfo.Words = int.Parse(Words);
                }
                if(Number != "")
                {
                    bookInfo.Number = int.Parse(Number);
                }
                if (Price != "")
                {
                    bookInfo.Price = decimal.Parse(Price);
                }

                BookInfo bookInfo2 = dbContext.BookInfo.FirstOrDefault<BookInfo>(B => B.BookID == bookInfo.BookID);
                if (bookInfo2 != null)
                {
                    Util_UI_Alert alert3 = new Util_UI_Alert("图书ID重复！");
                    alert3.ShowDialog();
                    return;
                }
                bookInfo.Keyword = KeyWord;
                bookInfo.Abstract = Abstract;
                dbContext.BookInfo.Add(bookInfo);
                int result = dbContext.SaveChanges();
                String str = result == 1 ? "添加成功" : "添加失败";
                /*MessageBox.Show();*/
                Util_UI_Alert alert=new Util_UI_Alert("添加图书成功！");
                alert.ShowDialog();
                this.Close();
                //刷新窗体
                /*this.Controls.Clear();
                InitializeComponent();
                comBoxLoad(sender, e);*/
                

            }
            else
            {
                MessageBox.Show("图书编号不得为空！");
                return;
            }

        }
        private void comBoxLoad(object sender, EventArgs e)
        {
            //新增图书：图书分类下拉框
            foreach (var item in dbContext.BookClass)
            {
                ComboBoxItem cbi = new ComboBoxItem();
                cbi.Text = item.Classname.Trim();
                cbi.Value = item.ClassID;
                comboBoxClass.Items.Add(cbi);
            }
            comboBoxClass.SelectedIndex = 0;
            //新增图书：图书出版社下拉框
            foreach (var item in dbContext.Publisherlnfo)
            {
                ComboBoxItem cbi = new ComboBoxItem();
                cbi.Text = item.Publisher.Trim();
                cbi.Value = item.PublisherId;
                comboBoxPublisherId.Items.Add(cbi);
            }
            comboBoxPublisherId.SelectedIndex = 0;
        }

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