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.BookRental
{
    public partial class ReturnBook : Form
    {
        string book_id;
        LibraryEntities1 dbContext = new LibraryEntities1();
        public ReturnBook()
        {
            InitializeComponent();
            StartPosition = FormStartPosition.CenterScreen;
        }
        public ReturnBook(string k)
        {
            InitializeComponent();
            book_id = k;
        }

        private void button_loan_Click(object sender, EventArgs e)
        {
            try
            {
                if (textBox_bookId.Text == "")
                {
                    Util_UI_Alert alert5 = new Util_UI_Alert("请输入图书编号");
                    alert5.ShowDialog();
                    return;
                }

                Loan loan = dbContext.Loan.FirstOrDefault<Loan>(L => L.BookID == textBox_bookId.Text && L.Return == null);
                loan.Return = BookUtil.Format_date(DateTime.Now);
                loan.flag = 0;



            }
            catch (System.NullReferenceException ex)
            {
                Util_UI_Alert alert5 = new Util_UI_Alert("您已还过此书，无需再还。");
                alert5.ShowDialog();
            }

            Util_UI_Alert alert4 = new Util_UI_Alert("还书成功");
            alert4.ShowDialog();
            this.DialogResult = DialogResult.OK;
            dbContext.SaveChanges();
        }

        private void button2_Click(object sender, EventArgs e)
        {
            this.DialogResult = DialogResult.No;
            this.Close();

        }






        /* private void button_loan_Click(object sender, EventArgs e)
         {
             if (textBox_bookId.Text != "" && textBox_certId.Text != "")
             {
                 LibraryEntities1 library = new LibraryEntities1();
                 *//*library.Loan*//*
                 Loan loan = new Loan();
                 loan.BookID = textBox_bookId.Text;
                 loan.Cert_id = textBox_certId.Text;
                 loan.Loandates = BookUtil.Format_date(dateTimePicker_loanDate.Value);
                 *//*loan.Return = BookUtil.Format_date(DateTime.Now);*/
        /* BaseSet baseSet=library.BaseSet.FirstOrDefault(B=>B.flag=="1");
         string borrow=baseSet.Borrow_days.ToString();
         loan.Return = DateTime.Now.AddDays(double.Parse(borrow));*//*
         //将要修改的对象放入上下文对象中
         library.Loan.Attach(loan);

         IQueryable<Loan> IQueryable_loan = library.Loan.Where<Loan>(L => L.BookID == loan.BookID && L.Cert_id == loan.Cert_id && L.Loandates == loan.Loandates);
         long sum = IQueryable_loan.LongCount();

         if (sum == 0)
         {
             MessageBox.Show("没有此借书证信息，或者借书证ID填写错误\n");
             return;
         }

         //判断是否书是否已还
         Loan loans = library.Loan.FirstOrDefault(L => L.BookID == loan.BookID && L.Cert_id == loan.Cert_id && L.Loandates == loan.Loandates);
         string str;
         //查询Flag是否是0，若是0表示已经还过
         if (loans.flag == 0)
         {
             str = "还书失败,您已还过此书了";
             MessageBox.Show(str);
             return;
         }

         //判断是否超时及过期罚款信息
         Loan query_loan = IQueryable_loan.FirstOrDefault();
         //获取基础设置里的可借天数
         BaseSet baseSet = library.BaseSet.FirstOrDefault(B => B.flag == "1");
         double borrowDays = double.Parse(baseSet.Borrow_days.ToString());
         if (query_loan.Loandates.AddDays(borrowDays) < loan.Return)
         {
             int rates = (int)baseSet.Rates;
             TimeSpan ts = (TimeSpan)(loan.Return - query_loan.Loandates.AddDays(borrowDays));
             int days = ts.Days;
             int money = days * rates;
             MessageBox.Show("已超时,请支付过期罚款：" + money.ToString() + "元");
         }

       *//*  Loan query_loan = IQueryable_loan.FirstOrDefault();
         if (query_loan.Return < loan.Return)
         {
             MessageBox.Show("已超时");
             return;
         }*//*

         //修改flag为0，表示为已还
         loans.flag = 0;
         //修改还书日期为今天
         loan.Return = DateTime.Now;
         //事务执行（还书-加库存-还原借书次数一并执行，若失败都失败）
         //加库存
         int Stock_result = BookUtil.addStock(loans.BookID, library);
         //加借书次数
         *//*int Cert_result = BookUtil.addCert(loans.Cert_id, library);*//*
         int i = 0;
         if(Stock_result ==1 *//*&& Cert_result == 1*//*)
         {
             i = library.SaveChanges();
         }
         str = i >= 1 ? "还书成功" : "还书事务失败";
         MessageBox.Show(str);
     }
 }*/
    }
}
