using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using BLWWS.PMS;

namespace MyQianLiMa
{
    public partial class WebForm1 : System.Web.UI.Page
    {

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                var q = Application["config"];
                var D = (ConfigData)q;
                int v = D.Interval;
                this.TextBox1.Text = v.ToString();
                this.GridView1.DataSource = D.DuiJieInfos;
                this.GridView1.DataBind();
            }
        }

        protected void Button1_Click(object sender, EventArgs e)
        {
            try
            {
                var q = Application["config"];
                var D = (ConfigData)q;
                int a = 10;
                int.TryParse(this.TextBox1.Text, out a);
                D.Interval = a;

                SaveData2File(D);

                this.Label12.Text = "保存成功";
            }
            catch (Exception ex)
            {
                this.Label12.Text = "保存失败。原因: " + ex.Message;
            }
        }

        protected void Button2_Click(object sender, EventArgs e)
        {
        }

        protected void Button3_Click(object sender, EventArgs e)
        {
            var q = Application["config"];
            var D = (ConfigData)q;

            DuiJieInfo ddd = new DuiJieInfo();
            ddd.hotel_code = this.txthotelcode.Text;
            D.DuiJieInfos.Add(ddd);
            SaveData2File(D);

            this.GridView1.DataSource = D.DuiJieInfos;
            this.GridView1.DataBind();
        }

        private void SaveData2File(ConfigData D)
        {
            string str = Newtonsoft.Json.JsonConvert.SerializeObject(D);
            string filepath = Server.MapPath("config.json");
            File.WriteAllText(filepath, str);
        }

        protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e)
        {
            if (e.CommandName == "Delete")
            {
                var q = Application["config"];
                var D = (ConfigData)q;

                var d = D.DuiJieInfos.FirstOrDefault(A => A.hotel_code == e.CommandArgument.ToString());
                D.DuiJieInfos.Remove(d);

                SaveData2File(D);

                this.GridView1.DataSource = D.DuiJieInfos;
                this.GridView1.DataBind();
            }
        }

        protected void GridView1_RowDeleting(object sender, GridViewDeleteEventArgs e)
        {

        }
    }
    public struct VVV
    {
        public string name;
    }
}