using System;
using System.Collections.Generic;
using System.Linq;
using System.Windows.Forms;
using QMAPP.FJC.Entity;
using QMAPP.FJC.Entity.Basic;
using QMAPP.FJC.Entity.Equipment;
using QMAPP.ServicesAgent;
using QMAPP.WinForm.Common;
using QMAPP.Entity;
namespace QMAPP.WinForm.Forms.Injection
{
    /// <summary>
    /// 模块编号:M11-3
    /// 作    用:设备停机维护
    /// 作    者:王庆男
    /// 编写日期:2015年06月01日
    ///</summary>
    public partial class EquipmentMaintainEditForm : Form
    {
        EquipmentMaintain recorder = new EquipmentMaintain();

        /// <summary>
        /// 新建 t
        /// </summary>
        public EquipmentMaintainEditForm()
        {
            InitializeComponent();
        }

        /// <summary>
        /// 编辑
        /// </summary>
        /// <param name="pid"></param>
        public EquipmentMaintainEditForm(string pid)
        {
            recorder.PID = pid;
            InitializeComponent();
        }

        /// <summary>
        /// 窗体加载
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void Form_Load(object sender, EventArgs e)
        {
            #region 绑定下拉列表
            BasicData basicData = new BasicData();
            //加载设备下拉列表
            this.comMachineCode.DataSource = basicData.GetMachineInfoList(new string[]{}, true);
            this.comMachineCode.DisplayMember = "MACHINENAME";
            this.comMachineCode.ValueMember = "MACHINECODDE";
            #endregion
            if (ClientContext.MachineInfo != null)
                this.comMachineCode.SelectedValue = ClientContext.MachineInfo.MACHINECODDE;
            #region 编辑
            //判断是否为编辑加载编辑数据
            if (string.IsNullOrEmpty(recorder.PID) == false)
            {
                QMAPP.ServicesAgent.ServiceAgent agent = ClientContext.GetServiceAgent();
                recorder = agent.InvokeServiceFunction<EquipmentMaintain>(B9BasicService.EquipmentMaintainBLL_Get.ToString(), recorder);
                this.comMachineCode.SelectedValue =   recorder.MACHINECODDE;
                this.dtpStart.ValueX = recorder.DOWNTIMESTART;
                if (recorder.DOWNTIMEEND != DateTime.MinValue)
                {
                    this.dtpEnd.ValueX = recorder.DOWNTIMEEND;
                }
                this.txtReason.Text = recorder.DOWNTIMEREMARK;
            }
            #endregion
        }

        /// <summary>
        /// 窗体保存事件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void tsbSave_Click(object sender, EventArgs e)
        {
            //窗体数据校验
            string errorMessae = ErrorCheck();
            if (errorMessae != "")
            {
                MessageBox.Show(errorMessae, Resource1.ConfirmTitle, MessageBoxButtons.OK, MessageBoxIcon.Warning);
                return;
            }
            #region 绑定保存信息
            //获取设备信息
            MachineInfo machineInfo = this.comMachineCode.SelectedItem as MachineInfo;
            recorder.MACHINECODDE = machineInfo.MACHINECODDE;
            recorder.MACHINENAME = machineInfo.MACHINENAME;
            recorder.MID = machineInfo.PID;
            recorder.PROCESSTYPE = machineInfo.PROCESSTYPE;
            recorder.WARNTYPE = "0";
            recorder.DOWNTIMESTART = Convert.ToDateTime(this.dtpStart.Text);
            machineInfo.STATUS = EnumGeter.MACHINESTATE.ABNORMAL.GetHashCode().ToString();
            if (!string.IsNullOrEmpty(this.dtpEnd.Text.Trim()))
            {
                recorder.DOWNTIMEEND = Convert.ToDateTime(this.dtpEnd.Text);
                machineInfo.STATUS = EnumGeter.MACHINESTATE.NORMAL.GetHashCode().ToString();
            }
            recorder.DOWNTIMEREMARK = this.txtReason.Text;
            recorder.machine = machineInfo;
            recorder.PRODUCELINE = EnumGeter.PRODUCELINE.B9.ToString();
            #endregion

            #region 保存
            QMAPP.ServicesAgent.ServiceAgent agent = ClientContext.GetServiceAgent();
            DataResult<int> result;//判断是否成功
            if (string.IsNullOrEmpty(recorder.PID) == true)
            {
                result = agent.InvokeServiceFunction<DataResult<int>>(B9BasicService.EquipmentMaintainBLL_Insert.ToString(), recorder);
            }
            else
            {
                result = agent.InvokeServiceFunction<DataResult<int>>(B9BasicService.EquipmentMaintainBLL_Update.ToString(), recorder);
            }
            //保存成功
            MessageBox.Show(result.Msg);
            if (result.IsSuccess)
            {
                this.DialogResult = System.Windows.Forms.DialogResult.OK;
                this.Close();
            }
            #endregion
        }

        /// <summary>
        /// 窗体关闭
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void tsbClose_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        ///<summary>
        ///错误检测
        ///</summary>
        ///<returns></returns>
        private string ErrorCheck()
        {
            #region
            string strMessage = "";
            if (this.comMachineCode.SelectedValue== null || this.comMachineCode.SelectedValue == string.Empty)
            {
                strMessage += Resource1.MachineNullValidate + "\r\n";
            }
            else
            {
                //判断是否当前设备状态符合新建,只有当前设备为正常才可以新建
                if (string.IsNullOrEmpty(recorder.PID) && ((MachineInfo)this.comMachineCode.SelectedItem).STATUS.Equals(EnumGeter.MACHINESTATE.ABNORMAL.GetHashCode().ToString()))
                {
                    strMessage += Resource1.MachineNewValidate + "\r\n";
                }
            }
            if (string.IsNullOrEmpty(this.dtpStart.Text.Trim()))
            {
                strMessage += Resource1.StartTimeNullValidate + "\r\n";
            }
            if (!string.IsNullOrEmpty(this.dtpEnd.Text.Trim()))
            {
                if (this.dtpStart.Value > this.dtpEnd.Value)
                {
                    strMessage += Resource1.StartCompareEndTimeValidate + "\r\n";
                }
            }
            if (this.txtReason.Text == string.Empty)
            {
                strMessage += Resource1.MachineReasonValidate + "\r\n";
            }
            return strMessage;
            #endregion
        }
    }
}