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();
            OlvHolidayName.AspectGetter = delegate (object x) { return ((HolidayInfo)x).Title; };
            OlvHolidayType.AspectGetter = delegate (object x) { return ((HolidayInfo)x).TypeStr; };
            OlvHolidayStartYear.AspectGetter = delegate (object x) { return ((HolidayInfo)x).StartYear; };
            OlvHolidayEndYear.AspectGetter = delegate (object x) { return ((HolidayInfo)x).EndYear; };
            OlvHolidayTime.AspectGetter = delegate (object x) { return ((HolidayInfo)x).Holiday_Time_str; };

        }
        #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 重新载入数据
            LvHoliday.ClearObjects();
            List<HolidayInfo> list = new List<HolidayInfo>();
            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];
                    var holiday= new HolidayInfo()
                    {
                        Id = row["id"].ToInt(),
                        Title = row["name"].ToString(),
                        Type = row["sType"].ToInt(),
                        Holiday_Time = row["Holiday_Time"].ToString(),
                        StartYear = row["startYear"].ToInt(),
                        EndYear = row["endYear"].ToInt(),
                    };
                    holiday.Update();
                    list.Add(holiday);               
                }
                LvHoliday.AddObjects(list);
                db.Free();
            }
            #endregion
        }
        private void GetRow(HolidayInfo row_holiday)
        {
            #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=" + row_holiday.Id);
                if (mydb.HaveData(ds))
                {
                    DataRow row = ds.Tables[0].Rows[0];
                    row_holiday.Title = row["name"].ToString();
                    row_holiday.Type = row["sType"].ToInt();
                    row_holiday.Holiday_Time = row["Holiday_Time"].ToString();
                    row_holiday.StartYear= row["startYear"].ToInt();
                    row_holiday.EndYear = row["endYear"].ToInt();
                    row_holiday.Update();
                }
                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)
        {
            var row = (HolidayInfo)LvHoliday.SelectedObject;
            if (row==null) { return; }
            FrmAddHoliday frm = new FrmAddHoliday()
            {
                Text = "修改节假日",
                Icon = Icon,
                isAdd = 0
            };
            frm.GetInfo(row.Id.ToString());
            frm.mr = new ModalForm(this, frm);
            frm.mr.OnDialogResult += new ModalForm.DialogResultHandler((object t, DialogResult dg) => {
                if (dg == DialogResult.OK)
                {
                    GetRow(row);
                }
            });
            frm.mr.ShowModal();
        }

        private void BtnDel_Click(object sender, EventArgs e)
        {
            var list =LvHoliday.SelectedObjects;
            if (list.Count== 0) { MessageBox.Show("请先选择要删除的项。", "提示", MessageBoxButtons.OK, MessageBoxIcon.Warning); return; }
            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)
            {
                for (int i = 0; i < list.Count; i++)
                {
                    var row = (HolidayInfo)list[i];
                    db.DelById(tableName, row.Id.ToString());
                }
                LvHoliday.RemoveObjects(list);
            }
        }

        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();
        }
    }
}
