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;
using QMTask.Client.TaskService;

namespace QMTask.Client
{
    /// <summary>
    /// 计划编辑
    /// </summary>
    public partial class PlanEditForm : Form
    {
        //任务调度服务
        TaskService.TaskServiceClient tsc = ServiceUtil.GetTaskService();

        /// <summary>
        /// 计划信息
        /// </summary>
        public PlanInfo Plan { get; set; }

        /// <summary>
        /// 行为
        /// </summary>
        public string Action { get; set; }

        public PlanEditForm()
        {
            InitializeComponent();
        }

        #region 窗体载入

        private void PlanEditForm_Load(object sender, EventArgs e)
        {
            //绑定作业列表
            this.BindTask();

            //绑定执行周期类型
            this.BindPeriodType();

            //默认选中值
            this.DayStartTime.SelectedText = "00";
            this.DayEndTime.SelectedText = "23";

            //绑定执行频率类型
            this.BindIntervalType();

            if (this.Plan != null)
            {
                //绑定计划信息
                this.BindPlan();
            }
        }

        #endregion

        #region 绑定计划信息

        private void BindPlan()
        {
            this.PlanName.Text = this.Plan.PlanName;
            this.TaskID.SelectedValue = this.Plan.TaskID;
            this.PeriodType.SelectedValue = this.Plan.PeriodType;
            switch (this.Plan.PeriodType)
            {
                case "d":
                    this.DayPeriod.Value = this.Plan.Period;                    
                    break;
                case "w":
                    string[] weeks = this.Plan.ExecuteDay.Split(",".ToCharArray());
                    foreach (string w in weeks)
                    {
                        switch (w)
                        {
                            case "1":
                                this.cbWeek1.Checked = true;
                                break;
                            case "2":
                                this.cbWeek2.Checked = true;
                                break;
                            case "3":
                                this.cbWeek3.Checked = true;
                                break;
                            case "4":
                                this.cbWeek4.Checked = true;
                                break;
                            case "5":
                                this.cbWeek5.Checked = true;
                                break;
                            case "6":
                                this.cbWeek6.Checked = true;
                                break;
                            case "7":
                                this.cbWeek7.Checked = true;
                                break;
                        }
                    }
                    break;
                case "m":
                    if (this.Plan.ExecuteDay.IndexOf("/") > 0)
                    {
                        //间隔
                        this.MonthPeriod2.Value = this.Plan.Period;
                        string[] days = this.Plan.ExecuteDay.Split("/".ToCharArray());
                        this.monthDay3.Value = int.Parse(days[1]);
                    }
                    else
                    {
                        //范围
                        this.MonthPeriod1.Value = this.Plan.Period;
                        string[] days = this.Plan.ExecuteDay.Split("-".ToCharArray());
                        this.monthDay1.Value = int.Parse(days[0]);
                        this.monthDay2.Value = int.Parse(days[1]);
                    }
                    break;
            }

            if (string.IsNullOrEmpty(this.Plan.OnceTime) == false)
            {
                //每天执行一次
                this.rbDayMode1.Checked = true;
                if (string.IsNullOrEmpty(this.Plan.OnceTime) == false)
                    this.OnceTime.Value = DateTime.Parse(this.Plan.OnceTime);                
            }
            else
            {
                //循环执行
                this.rbDayMode2.Checked = true;
                this.Interval.Value = this.Plan.Interval;
                this.IntervalType.SelectedValue = this.Plan.IntervalType;
            }

            if (string.IsNullOrEmpty(this.Plan.DayStartTime) == false)
                this.DayStartTime.Text = this.Plan.DayStartTime;
            if (string.IsNullOrEmpty(this.Plan.DayEndTime) == false)
                this.DayEndTime.Text = this.Plan.DayEndTime;

            if (Convert.ToInt32(this.Plan.DayStartTime) > Convert.ToInt32(this.Plan.DayEndTime))
            {
                MessageBox.Show("起始时间不能大于结束时间。");
                return;
            }

            this.PlanStartDate.Value = this.Plan.PlanStartDate;
            if (this.Plan.PlanEndDate != new DateTime())
            {
                this.PlanEndDate.Value = this.Plan.PlanEndDate;
            }
            else
            {
                this.PlanEndDate.Value = this.PlanStartDate.Value;
            }
            this.Remark.Text = this.Plan.Remark;
            this.IsUse.Checked = bool.Parse(this.Plan.IsUse);
        }

        #endregion

        #region 绑定作业列表

        /// <summary>
        /// 绑定作业列表
        /// </summary>
        private void BindTask()
        {
            TaskInfo[] tis = tsc.GetTaskList();

            DataTable dt = new DataTable();
            dt.Columns.Add("key");
            dt.Columns.Add("value");

            foreach (var a in tis)
            {
                dt.Rows.Add(a.TaskID, a.TaskName);
            }

            this.TaskID.DataSource = dt;
            this.TaskID.DisplayMember = "value";
            this.TaskID.ValueMember = "key";
        }

        #endregion

        #region 绑定执行周期类型

        private void BindPeriodType()
        {
            DataTable dt = new DataTable();
            dt.Columns.Add("key");
            dt.Columns.Add("value");

            dt.Rows.Add("d","每天");
            dt.Rows.Add("w", "每周");
            dt.Rows.Add("m", "每月");

            this.PeriodType.DataSource = dt;
            this.PeriodType.DisplayMember = "value";
            this.PeriodType.ValueMember = "key";
        }

        #endregion

        #region 绑定执行频率类型

        private void BindIntervalType()
        {
            DataTable dt = new DataTable();
            dt.Columns.Add("key");
            dt.Columns.Add("value");

            dt.Rows.Add("s", "秒");
            dt.Rows.Add("m", "分");
            dt.Rows.Add("h", "小时");

            this.IntervalType.DataSource = dt;
            this.IntervalType.DisplayMember = "value";
            this.IntervalType.ValueMember = "key";
        }

        #endregion

        #region 保存

        private void button1_Click(object sender, EventArgs e)
        {
            if (this.PlanName.Text == "")
            {
                MessageBox.Show("计划名称不能为空。");
                return;
            }

            if (this.TaskID.SelectedValue == null)
            {
                MessageBox.Show("作业内容不能为空。");
                return;
            }

            if (this.Plan == null)
            {
                this.Plan = new PlanInfo();                
            }

            this.Plan.PlanName=this.PlanName.Text;
            this.Plan.TaskID=this.TaskID.SelectedValue.ToString();
            this.Plan.PeriodType=this.PeriodType.SelectedValue.ToString();
            switch (this.Plan.PeriodType)
            {
                case "d":
                    this.Plan.Period=int.Parse(this.DayPeriod.Value.ToString());                   
                    break;
                case "w":
                    string weeks = "";
                    weeks = string.Format("{0}{1}{2}{3}{4}{5}{6}"
                        ,this.cbWeek1.Checked==true?",1":""
                        , this.cbWeek2.Checked == true ? ",2" : ""
                        , this.cbWeek3.Checked == true ? ",3" : ""
                        , this.cbWeek4.Checked == true ? ",4" : ""
                        , this.cbWeek5.Checked == true ? ",5" : ""
                        , this.cbWeek6.Checked == true ? ",6" : ""
                        , this.cbWeek7.Checked == true ? ",7" : "");

                    if (weeks != "")
                    {
                        weeks = weeks.Substring(1);
                    }
                    this.Plan.ExecuteDay = weeks;

                    if (this.Plan.ExecuteDay == "")
                    {
                        MessageBox.Show("请至少选择一天。");
                        return;
                    }

                    break;
                case "m":
                    if (this.rbMonthMode1.Checked == true)
                    {
                        //范围执行
                        this.Plan.Period = int.Parse(this.MonthPeriod1.Value.ToString());
                        this.Plan.ExecuteDay =string.Format("{0}-{1}",this.monthDay1.Value.ToString(),this.monthDay2.Value.ToString());
                    }
                    else
                    {
                        //间隔执行
                        this.Plan.Period = int.Parse(this.MonthPeriod2.Value.ToString());
                        this.Plan.ExecuteDay = string.Format("1/{0}",this.monthDay3.Value.ToString());
                    }

                    break;
            }

            if (this.rbDayMode1.Checked == true)
            {
                //每天执行一次
                this.Plan.OnceTime = this.OnceTime.Value.ToString("HH:mm:ss");                
            }
            else
            {
                //循环执行
                this.Plan.OnceTime = "";
                this.Plan.Interval = int.Parse(this.Interval.Value.ToString());
                this.Plan.IntervalType = this.IntervalType.SelectedValue.ToString();                
            }

            this.Plan.DayStartTime = this.DayStartTime.Text;
            this.Plan.DayEndTime = this.DayEndTime.Text;

            if (Convert.ToInt32(this.Plan.DayStartTime) > Convert.ToInt32(this.Plan.DayEndTime))
            {
                MessageBox.Show("起始时间不能大于结束时间。");
                return;
            }

            this.Plan.PlanStartDate=this.PlanStartDate.Value;
            if (this.radioButton4.Checked == true)
            {
                this.Plan.PlanEndDate = new DateTime();
            }
            else
            {
                this.Plan.PlanEndDate = this.PlanEndDate.Value;
            }
            this.Plan.Remark=this.Remark.Text;
            this.Plan.IsUse=this.IsUse.Checked.ToString().ToLower();

            if (string.IsNullOrEmpty(this.Plan.PlanID) == true)
            {
                //新增
                this.Plan.PlanID = Guid.NewGuid().ToString("N");
                tsc.AddPlan(this.Plan);
            }
            else
            {
                tsc.UpdatePlan(this.Plan);
            }

            this.Action = "save";
            this.Close();
        }

        #endregion

        #region 取消

        private void btCancel_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        #endregion

        #region 切换执行周期

        private void PeriodType_SelectedValueChanged(object sender, EventArgs e)
        {
            switch (this.PeriodType.SelectedValue.ToString())
            {
                case "d":
                    this.panelDay.Visible = true;
                    this.panelWeek.Visible = false;
                    this.panelMonth.Visible = false;
                    break;
                case "w":
                    this.panelDay.Visible = false;
                    this.panelWeek.Visible = true;
                    this.panelMonth.Visible = false;
                    break;
                case "m":
                    this.panelDay.Visible = false;
                    this.panelWeek.Visible = false;
                    this.panelMonth.Visible = true;
                    break;
            }
        }

        #endregion
    }
}