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

namespace LibraryMangementSystem.BookManage
{
    public partial class PublishUpdate : Form
    {
        private string publishId;
        LibraryEntities1 library = new LibraryEntities1();
        Publisherlnfo publisher;
        public PublishUpdate()
        {
            InitializeComponent();
            StartPosition = FormStartPosition.CenterScreen;
        }
        public PublishUpdate(string publishId)
        {
            InitializeComponent();
            StartPosition = FormStartPosition.CenterScreen;
            this.publishId = publishId;
            publisher= library.Publisherlnfo.FirstOrDefault<Publisherlnfo>(P => P.PublisherId == publishId);

        }
        private void PushValue(object sender, EventArgs e) {
            textBoxPublishId.Text = publisher.PublisherId.Trim();
            textBoxPublish.Text = publisher.Publisher.Trim();
            textBoxAddress.Text = publisher.Address.Trim();
            textBoxTel.Text = publisher.Tel.Trim();
            textBoxFax.Text = publisher.Fax.Trim();
            textBoxPostcode.Text = publisher.Postcode.ToString().Trim();
            textBoxhttp.Text = publisher.http.Trim();

        }

        private void button1_Click(object sender, EventArgs e)
        {
            if (textBoxPublish.Text != "" && textBoxPostcode != null)
            {
                

                if (textBoxPublishId.Text.Length > 4)
                {
                    Util_UI_Alert alert2 = new Util_UI_Alert("ID过长");
                    alert2.ShowDialog();
                    return;
                }
                
                publisher.Publisher = textBoxPublish.Text.Trim();
                publisher.Address = textBoxAddress.Text.Trim();
                publisher.Tel = textBoxTel.Text.Trim();
                publisher.Fax = textBoxFax.Text.Trim();
                if (textBoxPostcode.Text.Length > 6)
                {
                    Util_UI_Alert alert1 = new Util_UI_Alert("邮编过长");
                    alert1.ShowDialog();
                    return;
                }
                publisher.Postcode = decimal.Parse(textBoxPostcode.Text);
                publisher.http = textBoxhttp.Text.Trim();
                int result = library.SaveChanges();
                String str = result == 1 ? "修改出版社成功" : "修改出版社失败";
                Util_UI_Alert alert = new Util_UI_Alert(str);
                alert.ShowDialog();
                this.DialogResult = DialogResult.OK;
                this.Close();

            }
            else
            {
                Util_UI_Alert alert = new Util_UI_Alert("有非空项");
                alert.ShowDialog();
            }
        }

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