using ryCommon;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace TimeClock.DbOp.CustomShow
{
    public partial class FrmAddBirthDay : Form
    {
        public FrmAddBirthDay()
        {
            InitializeComponent();
            CbbType.SelectedIndex = 0;
            CbbLunar.SelectedIndex = 0;
        }
        public ModalForm mr = null;
        /// <summary>
        /// 上一次选择的是阳历还是农历，0表示阳历,1表示农历
        /// </summary>
        int last_lunar_y_value = -1;
        private void CbbLunar_SelectedIndexChanged(object sender, EventArgs e)
        {
            #region 获取当前选择的时间
            var getdate = DateTime.Now;
            try
            {
                if (last_lunar_y_value == 0)
                {
                    getdate = new DateTime(Dt_Year.Value.Year, CbbMonth_y.SelectedIndex + 1, CbbDay_y.SelectedIndex + 1);
                }
                else if (last_lunar_y_value == 1)
                {
                    if (ChinaDate.GetDayFromLunar(Dt_Year.Value.Year, CbbMonth_y.SelectedIndex + 1, CbbDay_y.SelectedIndex + 1, false, out int AYear, out int AMonth, out int ADay))
                    {
                        getdate = new DateTime(AYear, AMonth, ADay);
                    }
                }
            }
            catch
            {
            }
            #endregion
            last_lunar_y_value = CbbLunar.SelectedIndex;
            if (CbbLunar.SelectedIndex == 0) //阳历
            {
                CbbMonth_y.Items.Clear();
                for (int i = 0; i < 12; i++)
                {
                    CbbMonth_y.Items.Add((i + 1).ToString() + "月");
                }
                CbbMonth_y.SelectedIndex = getdate.Month - 1;
            }
            else
            {
                CbbMonth_y.Items.Clear();
                string stext = "正二三四五六七八九十";
                for (int i = 0; i < 10; i++)
                {
                    CbbMonth_y.Items.Add(stext[i].ToString() + "月");
                }
                CbbMonth_y.Items.Add("十一月"); CbbMonth_y.Items.Add("十二月");
                ChinaDate.GetLunarFromDay(getdate.Year, getdate.Month, getdate.Day, out int LunarYear, out int LunarMonth, out int LunarDay, out bool IsLeapMonth);
                CbbMonth_y.SelectedIndex = LunarMonth - 1;
            }
            CbbDay_y.Items.Clear();
            if (CbbLunar.SelectedIndex == 0)
            {
                for (int i = 0; i < 31; i++)
                {
                    CbbDay_y.Items.Add((i + 1).ToString() + "日");
                }
                CbbDay_y.SelectedIndex = getdate.Day - 1;
            }
            else
            {
                for (int i = 0; i < 30; i++)
                {
                    CbbDay_y.Items.Add(ChinaDate.GetDay(i + 1));
                }
                ChinaDate.GetLunarFromDay(getdate.Year, getdate.Month, getdate.Day, out int LunarYear, out int LunarMonth, out int LunarDay, out bool IsLeapMonth);
                CbbDay_y.SelectedIndex = LunarDay - 1;
            }
        }

        private void BtnCancel_Click(object sender, EventArgs e)
        {
            ModalForm.SetDialogResult(this, mr, DialogResult.Cancel);
        }

        private void BtnOK_Click(object sender, EventArgs e)
        {
            if(CbbLunar.SelectedIndex==0)
            {
                try
                {
                    var dt = new DateTime(Dt_Year.Value.Year, CbbMonth_y.SelectedIndex + 1, CbbDay_y.SelectedIndex + 1);
                }
                catch
                {
                    MessageBox.Show("当前阳历不是一个合法的日期。", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                    return;
                }
            }
            else
            {
                if (!ChinaDate.GetDayFromLunar(Dt_Year.Value.Year, CbbMonth_y.SelectedIndex + 1, CbbDay_y.SelectedIndex + 1, false, out int AYear, out int AMonth, out int ADay))
                {
                    MessageBox.Show("当前农历不是一个合法的日期。", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                    return;
                }
            }
            ModalForm.SetDialogResult(this, mr, DialogResult.OK);
        }
    }
}
