using LibraryMangementSystem.Util;
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.UI
{
    public partial class UI_Cert : Form
    {
        myTextBox mytextBoxCertName = new myTextBox();
        myTextBox textBoxCertVal = new myTextBox();
        myTextBox textBoxCertID = new myTextBox();
        LibraryEntities1 dbContext = new LibraryEntities1();
        public UI_Cert()
        {
            InitializeComponent();
            StartPosition = FormStartPosition.Manual;

            AddControlsToPanel(mytextBoxCertName, panel4);
            AddControlsToPanel(textBoxCertVal, panel5);
            AddControlsToPanel(textBoxCertID, panel6);


        }
        private void AddControlsToPanel(TextBox c, Panel p)
        {
            c.Dock = DockStyle.Fill;
            p.Controls.Clear();
            p.Controls.Add(c);

        }

        private void button1_Click(object sender, EventArgs e)
        {

            string Cert_Name;
            var Cert_ID = textBoxCertID.Text;
            var Cert_Value = textBoxCertVal.Text;
            /*var Depart = textBoxDepart.Text;*/
            string cart_type = "0";
            if (Cert_ID == "")
            {
                Util_UI_Alert alert3 = new Util_UI_Alert("证件ID不能为空！");
                alert3.ShowDialog();
                return;
            }
            if (Cert_Value == "")
            {
                Util_UI_Alert alert3 = new Util_UI_Alert("证件号码不能为空！");
                alert3.ShowDialog();
                return;

            }
            var Name = mytextBoxCertName.Text;


            if (radioButtonStudent.Checked)
            {
                Cert_Name = "学生证";
                cart_type = "0";
            }
            else
            {
                cart_type = "1";
                Cert_Name = "教师证";
            }
            if (Cert_Value.Length == 18)
            {
                Cert_Name = "身份证";
            }





            CertificateInfo certificateInfo = new CertificateInfo();
            certificateInfo.Cert_id = Cert_ID;
            certificateInfo.Cert_name = Cert_Name;
            certificateInfo.Cert_value = Cert_Value;
            certificateInfo.Name = Name;
            certificateInfo.Depart = comboBox1.Text;
            certificateInfo.Cert_type = cart_type;
            certificateInfo.flag = 1;
            if (IsExsitCert_val(Cert_Value))
            {
                Util_UI_Alert alert1 = new Util_UI_Alert("证件号重复！");
                alert1.ShowDialog();
                return;
            }
            if (IsExsitId(certificateInfo.Cert_id))
            {
                Util_UI_Alert alert2 = new Util_UI_Alert("证件ID重复");
                alert2.ShowDialog();
                return;

            }
            dbContext.CertificateInfo.Add(certificateInfo);
            int res = dbContext.SaveChanges();
            String str = res == 1 ? "办理成功！" : "办理失败！";
            Util_UI_Alert alert = new Util_UI_Alert(str);
            alert.ShowDialog();
            this.DialogResult = DialogResult.Yes;
            this.Close();


        }

        private void comBoxLoad(object sender, EventArgs e)
        {
            //新增图书卡：部门分类下拉框
            foreach (var item in dbContext.Depart)
            {
                ComboBoxItem cbi = new ComboBoxItem();
                cbi.Text = item.Dept_Name.Trim();
                comboBox1.Items.Add(cbi);
            }
            comboBox1.SelectedIndex = 0;

        }
        private Boolean IsExsitId(String id)
        {
            CertificateInfo cert = dbContext.CertificateInfo.Find(id);
            if (cert == null)
            {
                return false;
            }
            else
            {
                return true;
            }

        }
        private Boolean IsExsitCert_val(String v)
        {
            CertificateInfo cert = dbContext.CertificateInfo.FirstOrDefault<CertificateInfo>(C => C.Cert_value == v);
            if (cert == null)
            {
                return false;
            }
            else
            {
                return true;
            }

        }

        private void button2_Click(object sender, EventArgs e)
        {
            this.Close();
        }


    }
}
