using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;

using TopWinCMS.BLL;
using TopWinCMS.Common;
using TopWinCMS.Model;
namespace TopWinCMS.WebUI.Admin.Task
{
    public partial class Task_Add : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
             

            if (!IsPostBack)
            {
                RightsCheck.CheckWorkPower("TA_2");

                if (Request["ID"] != null)
                {
                    Edit_Load();
                }
            }
        }

        #region 激活类型
        /// <summary>
        /// 激活类型
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void RBL_TaskActive_SelectedIndexChanged(object sender, EventArgs e)
        {
            switch (RBL_TaskActive.SelectedIndex)
            {
                case 0:
                    Panel1.Visible = true;
                    Panel2.Visible = false;
                    Panel3.Visible = false;
                    Panel4.Visible = false;

                    break;
                case 1:
                    Panel1.Visible = false;
                    Panel2.Visible = true;
                    Panel3.Visible = false;
                    Panel4.Visible = false;

                    break;
                case 2:
                    Panel1.Visible = false;
                    Panel2.Visible = false;
                    Panel3.Visible = true;
                    Panel4.Visible = false;

                    break;
                case 3:
                    Panel1.Visible = false;
                    Panel2.Visible = false;
                    Panel3.Visible = false;
                    Panel4.Visible = true;

                    break;

            }
        }
        #endregion

        protected void BT_OK_Click(object sender, EventArgs e)
        {
            Add();
        }

        #region 添加或修改
        private void Add()
        {

            TaskInfo model = new TaskInfo();

            model.TaskName = TB_TaskName.Text.Trim();
            model.TaskActiveID = int.Parse(RBL_TaskActive.SelectedValue);


            model.AwakenTime = DateTime.MinValue;
            model.IntervalMinute = -1;
            model.Week = 0;
            model.EveryDayTime = null;
            switch (RBL_TaskActive.SelectedIndex)
            {
                case 0:
                    model.AwakenTime = DateTime.Parse(string.Concat(TB_RunDate.Text, " ", TB_RunTime.Text.Trim()));
                    break;
                case 1:

                    model.IntervalMinute = int.Parse(TB_IntervalMinute.Text);
                    break;
                case 2:
                    model.EveryDayTime = TB_RunTime2.Text.Trim();

                    break;
                case 3:
                    model.AwakenTime = DateTime.Parse(string.Concat("1949-10-01 ", TB_RunTime3.Text.Trim()));//只是用AwakenTime字段保存时间
                    model.Week = int.Parse(RBL_Week.SelectedValue);
                    break;

            }



            model.CommandLine = TB_CommandLine.Text;
            model.UnexecuteType = int.Parse(RBL_UnexecuteType.SelectedValue);
            model.IsUsed = int.Parse(RBL_IsUsed.SelectedValue);
            model.DLLName = TB_DLLName.Text.Trim();
            model.FullClassName = TB_FullClassName.Text.Trim();
            if (Request["ID"] == null)
            {
                model.ID = 0;
            }
            else
            {
                model.ID = int.Parse(Request["ID"]);
            }

            string strErr = null;

            new TaskManage().Add(model, ref strErr);

            if (string.IsNullOrEmpty(strErr) == false)
            {
                MsgBox.Alert(strErr, this.UpdatePanel1);

            }
            else
            {
                if (model.ID == 0)
                {
                    MsgBox.Alert("添加成功！", "Task_Manage.aspx", this.UpdatePanel1);
                }
                else
                {
                    MsgBox.Alert("修改成功！", "Task_Manage.aspx", this.UpdatePanel1);
                }
            }
        }
        #endregion

        #region 加载
        private void Edit_Load()
        {
            TaskInfo model = new TaskManage().EditLoad(Request["ID"]);
            if (model != null)
            {
                TB_TaskName.Text = model.TaskName;
                RBL_TaskActive.SelectedValue = model.TaskActiveID.ToString();

                RBL_TaskActive_SelectedIndexChanged(null, null);

                switch (RBL_TaskActive.SelectedIndex)
                {
                    case 0:
                        TB_RunDate.Text = model.AwakenTime.ToShortDateString();
                        TB_RunTime.Text = model.AwakenTime.ToString("HH:mm:ss");
                        break;
                    case 1:
                        TB_IntervalMinute.Text = model.IntervalMinute.ToString();
                        break;
                    case 2:
                        TB_RunTime2.Text = model.EveryDayTime;
                        break;
                    case 3:
                        TB_RunTime3.Text = model.AwakenTime.ToString("HH:mm:ss");
                        RBL_Week.SelectedValue = model.Week.ToString();
                        break;
                }
                RBL_UnexecuteType.SelectedValue = model.UnexecuteType.ToString();
                RBL_IsUsed.SelectedValue = model.IsUsed.ToString();
                TB_DLLName.Text = model.DLLName;
                TB_FullClassName.Text = model.FullClassName;
                TB_CommandLine.Text = model.CommandLine;
            }
            else
            {
                //转向错误页面
                ErrClass.Redirect("数据不存在。");
            }
        }
        #endregion
    }
}