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

namespace LibraryMangementSystem.BookRead
{
    public partial class BookAbstract : Form
    {
        public BookAbstract()
        {
            InitializeComponent();
        }
        public BookAbstract(object sender, EventArgs e)
        {
            InitializeComponent();
            button1_Click(sender, e);
        }

        LibraryEntities1 dbContext = new LibraryEntities1();
        List<BookInfo> bookInfo;


        private void button1_Click(object sender, EventArgs e)
        {
            string QueryByBookName = textBoxQueryByBookName.Text;
            string QueryByDate = comboBoxQueryByDate.Text;
            if (QueryByBookName != "")
            {
                String Title = QueryByBookName;
                bookInfo = dbContext.BookInfo.Where(B => B.Title.Contains(Title)).ToList();
            }
            else if (QueryByDate != "")
            {
                String nowDate = DateTime.Now.ToShortDateString();
                String oldDate;

                if (QueryByDate == "近半年")
                {
                    oldDate = DateTime.Now.AddDays(-180).ToShortDateString();

                }
                else if (QueryByDate == "近一年")
                {

                    oldDate = DateTime.Now.AddDays(-360).ToShortDateString();
                }
                else if (QueryByDate == "近五年")
                {

                    oldDate = DateTime.Now.AddDays(-1800).ToShortDateString();
                }
                else
                {
                    oldDate = DateTime.Now.AddDays(-3600).ToShortDateString();
                }


                label4.Text = oldDate + "至" + nowDate + "出版的新书";
                bookInfo = dbContext.BookInfo.SqlQuery("Select * from  bookInfo where Publish > @p0", oldDate).ToList<BookInfo>();


            }
            else
            {
                bookInfo = dbContext.BookInfo.ToList<BookInfo>();
            }
            dataGridView1.Rows.Clear();
            foreach (BookInfo book in bookInfo)
            {
                dataGridView1.Rows.Add(BookUtil.trim(book.Title));
            }
            //取消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);
            img_display(book.Title);
        }
        public void img_display(string title)
        {
            string imgUrl = "";
            try {
                string url = "http://127.0.0.1:5000/imgUrl/" + BookUtil.trim(title);
                System.Net.HttpWebRequest request;
                // 创建一个HTTP请求  
                request = (System.Net.HttpWebRequest)WebRequest.Create(url);
                //request.Method="get";  
                System.Net.HttpWebResponse response;
                response = (System.Net.HttpWebResponse)request.GetResponse();
                System.IO.StreamReader myreader = new System.IO.StreamReader(response.GetResponseStream(), Encoding.UTF8);
                string responseText = myreader.ReadToEnd();
                myreader.Close();
                imgUrl = responseText;
            }
            catch (Exception ee) {
                imgUrl = "D:\\VSProject\\Library\\LibraryMangementSystem\\Resources\\img.jpg";
            }

            
            if (imgUrl=="404") {
                imgUrl = "D:\\VSProject\\Library\\LibraryMangementSystem\\Resources\\img.jpg";

            }
            if (imgUrl!="") {
                this.pictureBox1.Image = Image.FromStream(System.Net.WebRequest.Create(imgUrl).GetResponse().GetResponseStream());
            }
            
            /*HttpWebRequest webRequest = (HttpWebRequest)WebRequest.Create(url);
            HttpWebResponse webResponse = (HttpWebResponse)webRequest.GetResponse();
            Stream stream = webResponse.GetResponseStream();
            StreamReader reader = new StreamReader(stream, System.Text.Encoding.GetEncoding("GB2312"));
            //下面bai将整个页du面zhi内容赋值dao给了zhuantextBox2了
            label5.Text = reader.ReadToEnd();*/


        }
    }
}
