﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace Poseidon.Energy.ClientDx
{
    using Poseidon.Base.Framework;
    using Poseidon.Base.System;
    using Poseidon.Winform.Base;
    using Poseidon.Energy.Core.BL;
    using Poseidon.Energy.Core.DL;
    using Poseidon.Energy.Core.Utility;

    /// <summary>
    /// 添加能源计量窗体
    /// </summary>
    public partial class FrmMeasureAdd : BaseSingleForm
    {
        #region Constructor
        public FrmMeasureAdd()
        {
            InitializeComponent();
        }
        #endregion //Constructor

        #region Function
        protected override void InitForm()
        {
            this.cmbEnergyType.Properties.Items.AddEnum(typeof(EnergyType));
            this.dpStartTime.DateTime = DateTime.Now.Date;
            this.dpEndTime.DateTime = DateTime.Now.Date;
            base.InitForm();
        }

        /// <summary>
        /// 输入检查
        /// </summary>
        /// <returns></returns>
        private Tuple<bool, string> CheckInput()
        {
            string errorMessage = "";

            if (string.IsNullOrEmpty(this.txtName.Text.Trim()))
            {
                errorMessage = "名称不能为空";
                return new Tuple<bool, string>(false, errorMessage);
            }
            if (string.IsNullOrEmpty(this.txtBelongTime.Text.Trim()))
            {
                errorMessage = "归属时间不能为空";
                return new Tuple<bool, string>(false, errorMessage);
            }
            if (this.cmbEnergyType.SelectedIndex == -1)
            {
                errorMessage = "请选择能源类型";
                return new Tuple<bool, string>(false, errorMessage);
            }
            if (this.dpStartTime.DateTime > this.dpEndTime.DateTime)
            {
                errorMessage = "起始时间不能大于结束时间";
                return new Tuple<bool, string>(false, errorMessage);
            }

            return new Tuple<bool, string>(true, "");
        }

        /// <summary>
        /// 设置实体
        /// </summary>
        /// <param name="entity"></param>
        private void SetEntity(Measure entity)
        {
            entity.Name = this.txtName.Text;
            entity.Year = Convert.ToInt32(this.spYear.Value);
            entity.BelongTime = this.txtBelongTime.Text;
            entity.EnergyType = Convert.ToInt32(this.cmbEnergyType.EditValue);
            entity.StartTime = this.dpStartTime.DateTime.Date;
            entity.EndTime = this.dpEndTime.DateTime.Date;
            entity.Included = this.chkIncluded.Checked;
            entity.Remark = this.txtRemark.Text;
        }
        #endregion //Function

        #region Event
        /// <summary>
        /// 保存
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnConfirm_Click(object sender, EventArgs e)
        {
            var input = CheckInput();
            if (!input.Item1)
            {
                MessageUtil.ShowError(input.Item2);
                return;
            }

            Measure entity = new Measure();
            SetEntity(entity);

            try
            {
                BusinessFactory<MeasureBusiness>.Instance.Create(entity, this.currentUser);

                MessageUtil.ShowInfo("保存成功");
                this.Close();
            }
            catch (PoseidonException pe)
            {
                MessageUtil.ShowError(string.Format("保存失败，错误消息:{0}", pe.Message));
            }
        }
        #endregion //Event
    }
}
