using LibraryMangementSystem.BookManage;
using LibraryMangementSystem.Util;
using System;
using System.Collections;
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.UI.BookManage
{
    public partial class BooksManage : UserControl
    {
        public BooksManage()
        {
            InitializeComponent();
        }
        public BooksManage(object sender, EventArgs e)
        {
            InitializeComponent();
            StandLibWin_Load(sender, e);
            dataViewByLoad(sender, e);
            buttonQueryByClass_Click(sender, e);
        }
        ArrayList arr = new ArrayList();
        public string checkboxName = "选择";
        LibraryEntities1 dbContext = new LibraryEntities1();
        private void StandLibWin_Load(object sender, EventArgs e)
        {
            DataGridViewCheckBoxColumn CheckColunms = new DataGridViewCheckBoxColumn();
            CheckColunms.Name = checkboxName;
            CheckColunms.HeaderText = checkboxName;
            CheckColunms.Width = 60;
            CheckColunms.TrueValue = "1";
            CheckColunms.FalseValue = "0";
            dataGridViewBook.Columns.Insert(0, CheckColunms);
            dataGridViewBook.CellContentClick += dataGridView1_CellContentClick;

        }
        private void dataGridView1_CellContentClick(object sender, DataGridViewCellEventArgs e)
        {
            try
            {
                if (e.RowIndex > -1)
                {
                    if (dataGridViewBook.Columns[e.ColumnIndex].Name == checkboxName)
                    {
                        arr.Clear();
                        for (int i = 0; i < this.dataGridViewBook.Rows.Count; i++)
                        {
                            if ((bool)dataGridViewBook.Rows[i].Cells[checkboxName].EditedFormattedValue == true)
                            {
                                if (dataGridViewBook.Rows[i].Cells[1].Value != null)
                                {
                                    string vClickGridId = dataGridViewBook.Rows[i].Cells[1].Value.ToString();
                                    arr.Add(vClickGridId);
                                }
                            }
                        }

                    }
                }
                dataGridViewBook.AllowUserToAddRows = false;
            }
            catch (System.ArgumentException ex)
            {
                Util_UI_Alert alert = new Util_UI_Alert("无有效数据");
                alert.ShowDialog();
                dataGridViewBook.Rows.Clear();
            }
        }



        private void buttonQueryByClass_Click(object sender, EventArgs e)
        {
            List<BookInfo> list;
            dataGridViewBook.Rows.Clear();

            if (textBoxQueryById.Text != "")
            {
                //根据id搜索

                var BookId = textBoxQueryById.Text;
                list = dbContext.BookInfo.Where(B => B.BookID == BookId).ToList();

            }
            else if (comboBoxQueryByClass.Text != "")
            {
                var ClassId = ((ComboBoxItem)comboBoxQueryByClass.SelectedItem).Value.ToString();
                list = dbContext.BookInfo.Where(B => B.ClassID == ClassId).ToList();
            }
            else {
                list = dbContext.BookInfo.ToList();
            }

            foreach (BookInfo book in list)
            {
                dataGridViewBook.Rows.Add("0", BookUtil.trim(book.BookID), BookUtil.trim(book.ISBN), BookUtil.trim(book.Title), BookUtil.trim(book.Publish),
                    BookUtil.trim(book.Author), book.Words, book.Publisherlnfo==null?"":BookUtil.trim(book.Publisherlnfo.Publisher), book.Number, book.Price,
                    BookUtil.trim(book.Keyword), BookUtil.trim(book.Abstract));
            }
            //取消dataGridView取消最后一行空白行
            dataGridViewBook.AllowUserToAddRows = false;
        }
        private void setBoxNull(object sender, EventArgs e)
        {
            textBoxQueryById.Text = "";

        }
        private void comboBoxQueryByClassNull(object sender, EventArgs e)
        {
            comboBoxQueryByClass.Text = "";

        }
        private void dataViewByLoad(object sender, EventArgs e)
        {
            foreach (var item in dbContext.BookClass)
            {
                ComboBoxItem cbi = new ComboBoxItem();
                cbi.Text = item.Classname.Trim();
                cbi.Value = item.ClassID;
                comboBoxQueryByClass.Items.Add(cbi);
            }
            comboBoxQueryByClass.SelectedIndex = 0;
        }

        //删除按钮
        private void button2_Click(object sender, EventArgs e)
        {
            if (arr.Count==0) {
                Util_UI_Alert alert = new Util_UI_Alert("您未勾选数据！");
                alert.ShowDialog();
                return;
            }


            Util_UI_Confirm confirm = new Util_UI_Confirm("您真的要删除么？");
            confirm.ShowDialog();
            if (confirm.DialogResult == DialogResult.Yes)
            {
                for (int i = 0; i < arr.Count; i++)
                {
                    string item = arr[i].ToString();
                    BookInfo bookInfo = dbContext.BookInfo.FirstOrDefault<BookInfo>(B => B.BookID == item);
                    dbContext.BookInfo.Remove(bookInfo);
                    for (int j = 0; j < this.dataGridViewBook.Rows.Count; j++)
                    {

                        if (dataGridViewBook.Rows[j].Cells[1].Value.ToString() == arr[i].ToString())
                        {

                            dataGridViewBook.Rows.Remove(dataGridViewBook.Rows[j]);
                        }

                    }
                }


                dataGridViewBook.Refresh();
                dbContext.SaveChanges();

            }


        }

        private void button1_Click(object sender, EventArgs e)
        {
            using (BookAdd b = new BookAdd())
            {
                b.ShowDialog();

            }
            /*MessageBox.Show(this.dataGridViewBook.Rows.Count.ToString());*/
            /*MessageBox.Show(arr.Count.ToString());*/
        }
    }
}
