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.CertManage
{
    public partial class CertAdd : Form
    {
        public CertAdd()
        {
            InitializeComponent();
        }
        LibraryEntities1 dbContext = new LibraryEntities1();
        private void button1_Click(object sender, EventArgs e)
        {
            //查询基础设置表数据
            BaseSet baseSet = dbContext.BaseSet.Find("1");
            var Cert_ID = textBoxCertVal.Text;
            var Cert_Name = "学生证";
            var BorrowNums = baseSet.Stu_BorrowNums;
            if (radioButtonTeaher.Checked)
            {
                Cert_Name = "教师证";
                BorrowNums = baseSet.Tch_BorrowNums;
            }
            var Cert_Value = textBoxCertVal.Text;
            var Name = textBoxCertName.Text;
            var Depart = textBoxDepart.Text;
            if(Cert_ID != "")
            {

                CertificateInfo certificateInfo = new CertificateInfo();
                certificateInfo.Cert_id = Cert_ID;
                certificateInfo.Cert_name = Cert_Name;
                certificateInfo.Cert_value = Cert_Value;
                certificateInfo.Name = Name;
                certificateInfo.Depart = Depart;
                certificateInfo.flag = 1;
               /* certificateInfo.BorrowNums = BorrowNums;*/
                Boolean suc = IsExsitId(certificateInfo.Cert_id);
                if (suc)
                {
                    dbContext.CertificateInfo.Add(certificateInfo);
                    int res = dbContext.SaveChanges();
                    String str = res == 1 ? "办理成功！借书证号与证件号一致，请牢记！" : "办理失败！";
                    MessageBox.Show(str);
                }
                else
                {
                    MessageBox.Show("该证件已办过借书证！请勿重复办理");
                }
               
            }
            else
            {
                MessageBox.Show("证件号码不能为空！");
                return;
            }
        }
        private Boolean IsExsitId(String id)
        {
            CertificateInfo cert = dbContext.CertificateInfo.Find(id);
            if (cert != null)
            {
                return false;
            }
            else
            {
                return true;
            }

        }
    }
}
