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.BookRead
{
    public partial class NewBook : Form
    {
        public NewBook()
        {
            InitializeComponent();
        }
        LibraryEntities1 dbContext = new LibraryEntities1();
        List<BookInfo> bookInfo;
        private void NewBook_Load(object sender, EventArgs e)
        {
            String nowDate = DateTime.Now.ToShortDateString();
            String oldDate = DateTime.Now.AddDays(-180).ToShortDateString();
            label2.Text = oldDate + "至" + nowDate +"出版的新书";
            bookInfo = dbContext.BookInfo.SqlQuery("Select * from  bookInfo where Publish > @p0", oldDate).ToList<BookInfo>();
            foreach (BookInfo book in bookInfo)
            {
                dataGridView1.Rows.Add(BookUtil.trim(book.Title));
            }
            //将dataGridView改成只读
            dataGridView1.ReadOnly = true;
            //取消dataGridView取消最后一行空白行
            dataGridView1.AllowUserToAddRows = false;
            //将dataGridView改成自适应宽度
            int width = 0;
            for (int i = 0; i < this.dataGridView1.Columns.Count; i++)
            {
                this.dataGridView1.AutoResizeColumn(i, DataGridViewAutoSizeColumnMode.AllCells);
                width += this.dataGridView1.Columns[i].Width;
            }
            if (width > this.dataGridView1.Size.Width)
            {
                this.dataGridView1.AutoSizeColumnsMode = DataGridViewAutoSizeColumnsMode.DisplayedCells;
            }
            else
            {
                this.dataGridView1.AutoSizeColumnsMode = DataGridViewAutoSizeColumnsMode.Fill;
            }
        }

        private void dataGridView1_CellDoubleClick(object sender, DataGridViewCellEventArgs e)
        {
            int index = int.Parse(dataGridView1.CurrentRow.Index.ToString());
            BookInfo book = bookInfo[index];
            richTextBox1.Text = BookUtil.trim(book.Abstract);
        }
    }
}
