using ryCommon;
using ryCommonDb;
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
{
    public partial class FrmHolidayView : Form
    {
        string orderSQL = "order by addTime desc";
        string tableName = "Holidays";
        public FrmHolidayView()
        {
            InitializeComponent();
        }
        #region 需要修改的内容
        private void RySearch1_OnSearch(object sender, EventArgs e)
        {
            //需要修改此处
            LoadDb("(name like @SearchText or des like @SearchText)");
        }

        private void CtlMyPage1_OnPageChange(object sender, EventArgs e)
        {
            #region 重新载入数据
            tableModel1.Rows.Clear();
            tableModel1.Selections.Clear();
            DataProvider mydb = new DataProvider();
            IDbInterface db = Itrycn_Db.CreateDataProvider(Itrycn_Db.dataType);
            if (db.ConnDb(Itrycn_Db.Holidays_ConnStr) == 1)
            {
                DataSet ds = db.ReadData(ctlMyPage1.GetSQLText, ctlMyPage1.T_Parameters);
                for (int i = 0; i < ds.Tables[0].Rows.Count; i++)
                {
                    DataRow row = ds.Tables[0].Rows[i];
                    XPTable.Models.Row itemList = new XPTable.Models.Row()
                    {
                        Tag = row["id"].ToString()
                    };
                    //需要修改此处
                    itemList.Cells.Add(new XPTable.Models.Cell(row["name"].ToString()));//示例
                    var Holiday_Time = row["Holiday_Time"].ToString();
                    var Holiday_Time_str = "";
                    switch (row["sType"].ToInt())
                    {
                        case 0:
                            if (Holiday_Time.Length == 4)
                            {
                                int day = Holiday_Time.Substring(2, 2).ToInt();
                                if (day == 0)
                                {
                                    Holiday_Time_str = Holiday_Time.Substring(0, 2).ToInt() + "月最后一天";
                                }
                                else
                                {
                                    Holiday_Time_str = Holiday_Time.Substring(0, 2).ToInt() + "月" + day + "日";
                                }
                            }
                            itemList.Cells.Add(new XPTable.Models.Cell("公历节日"));break;
                        case 1:
                            if (Holiday_Time.Length == 4)
                            {
                                Holiday_Time_str =ChinaDate.GetMonth(Holiday_Time.Substring(0, 2).ToInt())  + ChinaDate.GetDay(Holiday_Time.Substring(2, 2).ToInt());
                            }
                            itemList.Cells.Add(new XPTable.Models.Cell("农历节日")); break;
                        case 2:
                            if (Holiday_Time.Length == 4)
                            {
                                Holiday_Time_str = Holiday_Time.Substring(0, 2).ToInt() + "月" ;
                                var week = Holiday_Time.Substring(2, 1).ToInt();
                                if (week == 0) { Holiday_Time_str += "最后一个"; }
                                else { Holiday_Time_str += "第"+ week+"个"; }
                                Holiday_Time_str += "周"+RyDate.GetWeekName(Holiday_Time.Substring(3, 1).ToInt());
                            }
                            itemList.Cells.Add(new XPTable.Models.Cell("星期节日")); break;
                        case 3:
                            Holiday_Time_str = "";
                            itemList.Cells.Add(new XPTable.Models.Cell("节气")); break;
                        default:
                            itemList.Cells.Add(new XPTable.Models.Cell("未知节日")); break;
                    }
                    itemList.Cells.Add(new XPTable.Models.Cell(row["startYear"].ToString()));//示例
                    itemList.Cells.Add(new XPTable.Models.Cell(row["endYear"].ToString()));//示例
                    itemList.Cells.Add(new XPTable.Models.Cell(Holiday_Time_str));//示例
                    tableModel1.Rows.Add(itemList);
                }
                db.Free();
            }
            #endregion
        }
        private void GetRow(string id, int index)
        {
            #region 重新载入数据
            DataProvider mydb = new DataProvider();
            IDbInterface db = Itrycn_Db.CreateDataProvider(Itrycn_Db.dataType);
            if (db.ConnDb(Itrycn_Db.Holidays_ConnStr) == 1)
            {
                DataSet ds = db.ReadData("select * from " + tableName + " where id=" + id);
                if (mydb.HaveData(ds))
                {
                    DataRow row = ds.Tables[0].Rows[0];
                    XPTable.Models.Row itemList = tableModel1.Rows[index];
                    //需要修改此处
                    itemList.Cells[ColName.Index].Text = row["name"].ToString();
                    var Holiday_Time = row["Holiday_Time"].ToString();
                    var Holiday_Time_str = "";
                    switch (row["sType"].ToInt())
                    {
                        case 0:
                            if (Holiday_Time.Length == 4)
                            {
                                int day = Holiday_Time.Substring(2, 2).ToInt();
                                if (day == 0)
                                {
                                    Holiday_Time_str = Holiday_Time.Substring(0, 2).ToInt() + "月最后一天";
                                }
                                else
                                {
                                    Holiday_Time_str = Holiday_Time.Substring(0, 2).ToInt() + "月" + day + "日";
                                }
                            }
                            itemList.Cells[ColSType.Index].Text = "公历节日"; break;
                        case 1:
                            if (Holiday_Time.Length == 4)
                            {
                                Holiday_Time_str = ChinaDate.GetMonth(Holiday_Time.Substring(0, 2).ToInt()) + ChinaDate.GetDay(Holiday_Time.Substring(2, 2).ToInt());
                            }
                            itemList.Cells[ColSType.Index].Text = "农历节日"; break;
                        case 2:
                            if (Holiday_Time.Length == 4)
                            {
                                Holiday_Time_str = Holiday_Time.Substring(0, 2).ToInt() + "月";
                                var week = Holiday_Time.Substring(2, 1).ToInt();
                                if (week == 0) { Holiday_Time_str += "最后一个"; }
                                else { Holiday_Time_str += "第" + week + "个"; }
                                Holiday_Time_str += "周" + RyDate.GetWeekName(Holiday_Time.Substring(3, 1).ToInt());
                            }
                            itemList.Cells[ColSType.Index].Text = "星期节日"; break;
                        case 3:
                            Holiday_Time_str = "";
                            itemList.Cells[ColSType.Index].Text = "节气"; break;
                        default:
                            itemList.Cells[ColSType.Index].Text = "未知节日";  break;
                    }
                    itemList.Cells[ColStartYear.Index].Text = row["startYear"].ToString();
                    itemList.Cells[ColEndYear.Index].Text = row["endYear"].ToString();
                    itemList.Cells[ColTime.Index].Text = Holiday_Time_str;
                }
                db.Free();
            }
            #endregion
        }
        #endregion
        public void LoadDb(string whereSQL)
        {
            string _whereSQL = whereSQL;
            DataProvider mydb = new DataProvider();
            IDbInterface db = Itrycn_Db.CreateDataProvider(Itrycn_Db.dataType);
            if (db.ConnDb(Itrycn_Db.Holidays_ConnStr) == 1)
            {
                #region 重新载入数据
                db.AddParameter("SearchText", "%" + rySearch1.Text + "%");
                ctlMyPage1.RecordCount = db.GetCount(tableName, _whereSQL);
                ctlMyPage1.T_Parameters = db.GetParameter();
                ctlMyPage1.SQLText= db.GetPageSQL(tableName, _whereSQL, orderSQL);
                ctlMyPage1.SQLText2 = db.GetPageSQL2(tableName, _whereSQL, orderSQL);
                ctlMyPage1.GotoPageIndex(1);
                db.Free();
                #endregion
            }
        }

        private void BtnAdd_Click(object sender, EventArgs e)
        {
            FrmAddHoliday frm = new FrmAddHoliday()
            {
                Text = "添加节假日",
                Icon = Icon,
                isAdd = 1
            };
            frm.mr = new ModalForm(this, frm);
            frm.mr.OnDialogResult += new ModalForm.DialogResultHandler((object t, DialogResult dg) => {
                if (dg == DialogResult.OK)
                {
                    rySearch1.PerformClick();
                }
            });
            frm.mr.ShowModal();
        }

        private void BtnEdit_Click(object sender, EventArgs e)
        {
            if (table1.SelectedItems.Length == 0) { return; }
            string selectId = table1.SelectedItems[0].Tag.ToString();
            int index = table1.SelectedItems[0].Index;
            FrmAddHoliday frm = new FrmAddHoliday()
            {
                Text = "修改节假日",
                Icon = Icon,
                isAdd = 0
            };
            frm.GetInfo(selectId);
            frm.mr = new ModalForm(this, frm);
            frm.mr.OnDialogResult += new ModalForm.DialogResultHandler((object t, DialogResult dg) => {
                if (dg == DialogResult.OK)
                {
                    GetRow(selectId, index);
                }
            });
            frm.mr.ShowModal();
        }

        private void BtnDel_Click(object sender, EventArgs e)
        {
            if (table1.SelectedItems.Length == 0) { MessageBox.Show("请先选择要删除的项。", "提示", MessageBoxButtons.OK, MessageBoxIcon.Warning); return; }
            string selectId = table1.SelectedItems[0].Tag.ToString();
            if (MessageBox.Show("确定要删除该项吗？一旦删除将不可恢复。", "提示", MessageBoxButtons.YesNo, MessageBoxIcon.Information) == DialogResult.No)
            {
                return;
            }
            DataProvider mydb = new DataProvider();
            IDbInterface db = Itrycn_Db.CreateDataProvider(Itrycn_Db.dataType);
            if (db.ConnDb(Itrycn_Db.Holidays_ConnStr) == 1)
            {
                db.DelById(tableName, selectId);
                tableModel1.Rows.RemoveAt(table1.SelectedItems[0].Index);
            }
        }

        private void FrmView_Load(object sender, EventArgs e)
        {
            rySearch1.PerformClick();
        }

        private void 添加数据ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            btnAdd.PerformClick();
        }

        private void 修改数据ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            btnEdit.PerformClick();
        }

        private void 删除数据ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            btnDel.PerformClick();
        }
    }
}
