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 FrmAddHoliday : Form
    {
        public ModalForm mr = null;
        public string tableName = "Holidays";
        public int isAdd = 1;
        public string selectId = "-1";
        public FrmAddHoliday()
        {
            InitializeComponent();
            CbbSType.SelectedIndex = 0;
        }
        #region 需要修改
        public void GetInfo(string id)
        {
            selectId = id;
            DataProvider mydb = new DataProvider();
            IDbInterface db = Itrycn_Db.CreateDataProvider(Itrycn_Db.dataType);
            if (db.ConnDb(Itrycn_Db.Holidays_ConnStr) == 1)
            {
                DataSet ds = db.ReadData(tableName, id);
                if (mydb.HaveData(ds))
                {
                    DataRow reader = ds.Tables[0].Rows[0];
                    #region 读取信息               
                    txtName.Text = reader["Name"].ToString();
                    NumSorts.Value = reader["Sorts"].ToInt();
                    TxtDes.Text = reader["Des"].ToString();
                    NumStartYear.Value = reader["startYear"].ToInt();
                    NumEndYear.Value = reader["endYear"].ToInt();
                    CbbSType.SelectedIndex= reader["sType"].ToInt();
                    TxtTime.Text= reader["Holiday_Time"].ToString();
                    ChkNoShowRL.Checked = reader["NoShowRL"].ToBool();
                    #endregion
                }
            }
            db.Free();
        }

        private void BtnOK_Click(object sender, EventArgs e)
        {
            if (txtName.Text == "")
            {
                MessageBox.Show("节日名称不能为空。", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                return;
            }
            if (TxtTime.Text.Length != 4)
            {
                MessageBox.Show("节日时间只能为4位数字。", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                return;
            }
            RyQuickSQL mySQL = new RyQuickSQL(tableName);
            mySQL.AddField("Name", txtName.Text);
            mySQL.AddField("Sorts", NumSorts.Value);
            mySQL.AddField("startYear", NumStartYear.Value);
            mySQL.AddField("endYear", NumEndYear.Value);
            mySQL.AddField("sType", CbbSType.SelectedIndex);
            mySQL.AddField("Holiday_Time", TxtTime.Text);
            mySQL.AddField("NoShowRL",ChkNoShowRL.Checked);
            mySQL.AddField("Des", TxtDes.Text);
            DataProvider mydb = new DataProvider();
            IDbInterface db = Itrycn_Db.CreateDataProvider(Itrycn_Db.dataType);
            if (db.ConnDb(Itrycn_Db.Holidays_ConnStr) == 1)
            {
                if (isAdd >= 1)
                {
                    mySQL.AddField("addTime", DateTime.Now);
                    db.ExecuteNonQuery(mySQL.GetInsertSQL(), db.GetParameter(mySQL));
                }
                else
                {
                    db.ExecuteNonQuery(mySQL.GetUpdateSQL() + " where id=" + selectId, db.GetParameter(mySQL));
                }
            }
            ModalForm.SetDialogResult(this, mr, DialogResult.OK);
        } 
        #endregion

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