using HKLib.Dto;
using HKLib.Interfaces;
using HKLib.SQLHelper;
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;
using XExten.Advance.LinqFramework;
using static System.Windows.Forms.VisualStyles.VisualStyleElement;

namespace HKCardManager.UserPages
{
    public partial class MealSegmentSetPage : UserControl
    {
        public MealSegmentSetPage()
        {
            InitializeComponent();
            this.SizeChanged += MealSegmentSetPage_SizeChanged;
            Init();
        }

        private async void Init()
        {
            var res = await HKLibHelper.GetTime();
            var price = SqlLogic.GetPrice();
            if (res != null)
            {
                this.Invoke(() =>
                {
                    dateTimePicker1.Value = res.AMStartTime;
                    dateTimePicker4.Value = res.AMEndTime;
                    dateTimePicker2.Value = res.PMStartTime;
                    dateTimePicker5.Value = res.PMEndTime;
                    dateTimePicker3.Value = res.ATStartTime;
                    dateTimePicker6.Value = res.ATEndTime;
                    textBox1.Text = price?.AM.ToString();
                    textBox2.Text = price?.PM.ToString();
                    textBox3.Text = price?.AT.ToString();
                });
            }
            else MessageLogNotify.GetInstance.ShowError("获取餐段时间失败！");
        }

        private void MealSegmentSetPage_SizeChanged(object? sender, EventArgs e)
        {
            panel1.Left = (this.Width - panel1.Width) / 2;
            panel1.Top = (this.Height - panel1.Height) / 2;
        }

        private async void button1_Click(object sender, EventArgs e)
        {
            decimal.TryParse(textBox1.Text.Trim(), out decimal am);
            decimal.TryParse(textBox2.Text.Trim(), out decimal pm);
            decimal.TryParse(textBox3.Text.Trim(), out decimal at);
            if (am == 0 || pm == 0 || at == 0)
            {
                MessageBox.Show("请输入正确的金额");
                return;
            }

            var data = new TimeSpilte()
            {
                AMStartTime = dateTimePicker1.Value,
                AMEndTime = dateTimePicker4.Value,
                PMStartTime = dateTimePicker2.Value,
                PMEndTime = dateTimePicker5.Value,
                ATStartTime = dateTimePicker3.Value,
                ATEndTime = dateTimePicker6.Value
            };

            this.Invoke(() => { button1.Enabled = false; });
            if (await HKLibHelper.SetTime(data))
            {

                SqlLogic.AddPrice(new HKLib.SQLHelper.Entity.PriceModel
                {
                    AM = textBox1.Text.Trim().AsDecimal(),
                    PM = textBox2.Text.Trim().AsDecimal(),
                    AT = textBox3.Text.Trim().AsDecimal()
                });
                this.Invoke(() =>
                {
                    dateTimePicker1.Value = data.AMStartTime;
                    dateTimePicker4.Value = data.AMEndTime;
                    dateTimePicker2.Value = data.PMStartTime;
                    dateTimePicker5.Value = data.PMEndTime;
                    dateTimePicker3.Value = data.ATStartTime;
                    dateTimePicker6.Value = data.ATEndTime;
                });
                MessageLogNotify.GetInstance.Show("设置餐段时间成功");
            }
            else
            {
                MessageLogNotify.GetInstance.ShowError("设置时间段失败，请重试！");
            }
            this.Invoke(() => { button1.Enabled = true; });

        }
    }
}
