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

namespace LibraryMangementSystem.UI.CertManages
{
    public partial class CertManages : UserControl
    {
        public CertManages()
        {
            InitializeComponent();
        }
        public CertManages(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();

        //基础参数表
        int Stu_BorrowNums;
        int Tch_BorrowNums;
        private void StandLibWin_Load(object sender, EventArgs e)
        {
            BaseSet baseSet = dbContext.BaseSet.FirstOrDefault<BaseSet>(B => B.flag == "1");
            Stu_BorrowNums = (int)baseSet.Stu_BorrowNums;
            Tch_BorrowNums = (int)baseSet.Tch_BorrowNums;
            DataGridViewCheckBoxColumn CheckColunms = new DataGridViewCheckBoxColumn();
            CheckColunms.Name = checkboxName;
            CheckColunms.HeaderText = checkboxName;
            CheckColunms.Width = 60;
            CheckColunms.TrueValue = "1";
            CheckColunms.FalseValue = "0";
            dataGridViewCert.AutoGenerateColumns = false;
            dataGridViewCert.Columns.Insert(0, CheckColunms);
            dataGridViewCert.CellContentClick += dataGridView1_CellContentClick;

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

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



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

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

                var CertId = textBoxQueryById.Text;
                list = dbContext.CertificateInfo.Where(B => B.Cert_id == CertId).ToList();

            }
            else if (comboBoxQueryByClass.Text != "")
            {
                var Cart_name = comboBoxQueryByClass.Text;
                list = dbContext.CertificateInfo.Where(B => B.Cert_name == Cart_name).ToList();

            }
            else
            {
                list = dbContext.CertificateInfo.ToList();
            }

            /*
             
                     int Stu_BorrowNums;
        int Tch_BorrowNums;
             */
            foreach (CertificateInfo cert in list)
            {
                int borrowOfNumber = 0;//已借次数
                int surplusNumber = 0;//剩余可借次数
                borrowOfNumber = dbContext.Loan.Where<Loan>(L => L.Cert_id == cert.Cert_id&&L.flag==1).Count();
                if (Util.BookUtil.trim(cert.Cert_type) == "0")
                {
                    surplusNumber = Stu_BorrowNums - borrowOfNumber;
                }
                else {
                    surplusNumber = Tch_BorrowNums - borrowOfNumber;
                }
                if (surplusNumber<=0) {
                    surplusNumber = 0;
                }

                dataGridViewCert.Rows.Add("0", Util.BookUtil.trim(cert.Cert_id), Util.BookUtil.trim(cert.Cert_name), Util.BookUtil.trim(cert.Cert_value), Util.BookUtil.trim(cert.Name),
                   Util.BookUtil.trim(cert.Depart), Util.BookUtil.trim(cert.Cert_type) == "0" ? "学生" : "教师", borrowOfNumber, surplusNumber);
            }
            //取消dataGridView取消最后一行空白行
            dataGridViewCert.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();
            int flag = 0;
            if (confirm.DialogResult == DialogResult.Yes)
            {
                for (int i = 0; i < arr.Count; i++)
                {
                    string item = arr[i].ToString();
                    Loan loan=dbContext.Loan.FirstOrDefault<Loan>(L=>L.Cert_id==item&&L.Return==null);
                    if (loan!=null) {
                        flag += 1;
                        continue;
                    }
                    CertificateInfo certificateInfo = dbContext.CertificateInfo.FirstOrDefault<CertificateInfo>(B => B.Cert_id == item);
                    dbContext.CertificateInfo.Remove(certificateInfo);
                    for (int j = 0; j < this.dataGridViewCert.Rows.Count; j++)
                    {

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

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

                    }
                }

                if (flag>0) {
                    Util_UI_Confirm confirm2 = new Util_UI_Confirm("借书未还，无法注销，请先还书！");
                    confirm2.ShowDialog();
                }
                dataGridViewCert.Refresh();
                dbContext.SaveChanges();

            }


        }

        private void button1_Click(object sender, EventArgs e)
        {
            using (UI_Cert b = new UI_Cert())
            {
                b.ShowDialog();
                if (b.DialogResult == DialogResult.Yes)
                {
                    buttonQueryByClass_Click(sender, e);


                }

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