using LibraryMangementSystem.Util;
using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Windows.Forms;

namespace LibraryMangementSystem.BookManage
{
    public partial class PublisherInfoForm : Form
    {
        public PublisherInfoForm()
        {
            InitializeComponent();

        }
        /*DataGridView*/
        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";
            dataGridView1.Columns.Insert(0, CheckColunms);
            dataGridView1.CellContentClick += dataGridView1_CellContentClick;
            data_loca(sender,e);
        }
        private void dataGridView1_CellContentClick(object sender, DataGridViewCellEventArgs e)
        {
            try
            {
                if (e.RowIndex > -1)
                {
                    if (dataGridView1.Columns[e.ColumnIndex].Name == checkboxName)
                    {
                        arr.Clear();
                        for (int i = 0; i < this.dataGridView1.Rows.Count; i++)
                        {
                            if ((bool)dataGridView1.Rows[i].Cells[checkboxName].EditedFormattedValue == true)
                            {
                                if (dataGridView1.Rows[i].Cells[1].Value != null)
                                {
                                    string vClickGridId = dataGridView1.Rows[i].Cells[1].Value.ToString();
                                    arr.Add(vClickGridId);
                                }
                            }
                        }

                    }
                }
            }
            catch (Exception ex)
            {
                Util_UI_Alert alert = new Util_UI_Alert("无有效数据" + ex.Message);
                alert.ShowDialog();
                dataGridView1.Rows.Clear();
            }
            dataGridView1.AllowUserToAddRows = false;

        }


        private void data_loca(object sender, EventArgs e)
        {
            dataGridView1.Rows.Clear();

            LibraryEntities1 dbContext = new LibraryEntities1();
            List<Publisherlnfo> publisherinfo;
            if (textBoxQueryById.Text != "")
            {
                String publisherId = textBoxQueryById.Text;
                publisherinfo = dbContext.Publisherlnfo.Where(P => P.PublisherId == publisherId).ToList();
            }
            else
            {
                publisherinfo = dbContext.Publisherlnfo.ToList<Publisherlnfo>();
            }
            foreach (Publisherlnfo p in publisherinfo)
            {
                dataGridView1.Rows.Add("0", BookUtil.trim(p.PublisherId), BookUtil.trim(p.Publisher), BookUtil.trim(p.Address),
                    BookUtil.trim(p.Tel), BookUtil.trim(p.Fax), p.Postcode, BookUtil.trim(p.http));
            }


            dataGridView1.AllowUserToAddRows = false;
        }

        private void button_update(object sender, EventArgs e)
        {
            int n = arr.Count;//获取当前选中的行

            if (n != 1)
            {
                Util_UI_Alert alert = new Util_UI_Alert("只能选择一个出版社进行修改");
                alert.ShowDialog();
                return;
            }


            string k = arr[0].ToString();
            if (k != null && k != "")
            {
                PublishUpdate p = new PublishUpdate(k);
                p.ShowDialog();
                if (p.DialogResult == DialogResult.OK)
                {
                    data_loca(sender, e);
                }
            }

        }
        private void button_delete(object sender, EventArgs e)
        {
            int n = arr.Count;//获取当前选中的行
            string msg;
            if (n == 0)
            {
                msg = "请选择一行！";
                Util_UI_Alert alert = new Util_UI_Alert(msg);
                alert.ShowDialog();
                return;
            }
            msg = "您真的要删除么？";
            Util_UI_Confirm confirm = new Util_UI_Confirm(msg);
            confirm.ShowDialog();
            if (confirm.DialogResult == DialogResult.Yes)
            {
                for (int i = 0; i < arr.Count; i++)
                {
                    string item = arr[i].ToString();
                    Publisherlnfo publisherlnfo = dbContext.Publisherlnfo.FirstOrDefault<Publisherlnfo>(P => P.PublisherId == item);
                    dbContext.Publisherlnfo.Remove(publisherlnfo);
                    for (int j = 0; j < this.dataGridView1.Rows.Count; j++)
                    {

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

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

                    }
                }


                dataGridView1.Refresh();
                dbContext.SaveChanges();
            }




        }

        private void button2_Click(object sender, EventArgs e)
        {
            PublishAdd publishAdd = new PublishAdd();
            publishAdd.ShowDialog();
            if (publishAdd.DialogResult == DialogResult.OK)
            {
                data_loca(sender, e);
            }
        }

        //修改
        private void button4_Click(object sender, EventArgs e)
        {

        }

        private void button1_Click(object sender, EventArgs e)
        {

        }
    }
}
