using System;
using System.Collections.Generic;
using System.Windows.Forms;
using QMAPP.ServicesAgent;
using QMAPP.WinForm.Common;
using QMAPP.Entity;
using QMAPP.MD.Entity.Bucket;

namespace QMAPP.WinForm.Forms.Injection
{
    /// <summary>
    /// 模块编号:M12-1
    /// 作    用:投料记录
    /// 作    者:王庆男
    /// 编写日期:2015年06月08日
    ///</summary>
    public partial class InjectionMoldingNEWEditForm1 : Form
    {
        RawMaterial recorder = new RawMaterial();//编辑保存实体
        //获取服务代理
        QMAPP.ServicesAgent.ServiceAgent _agent = ClientContext.GetServiceAgent();

        /// <summary>
        /// 错误的声音文件路径
        /// </summary>
        private string errorSoundPath = null;

        /// <summary>
        /// 新建
        /// </summary>
        public InjectionMoldingNEWEditForm1()
        {
            InitializeComponent();
            BucketCode.Focus();
        }

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

        /// <summary>
        /// 窗体加载
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void Form_Load(object sender, EventArgs e)
        {
            BucketCode.Focus();
            new KeyEvent().SetKeyToTxt(this);
            #region 绑定下拉列表
            BasicData basicData = new BasicData();
            
            #endregion

            #region 加载编辑
            //判断是否为编辑加载编辑数据
            if (string.IsNullOrEmpty(recorder.PID) == false)
            {
                recorder = _agent.InvokeServiceFunction<DataResult<RawMaterial>>(B9BasicService.RawMaterialBLL_Get.ToString(), recorder).Result;
                
                this.BatchCode.Text = recorder.BatchCode;
                this.BucketCode.Text = recorder.BucketCode;
                this.textBox1.Text = recorder.PartCode;
                this.SerialCode.Text = recorder.SerialCode;
            }
            #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 加载保存信息
            
            //批次
            recorder.BatchCode = this.BatchCode.Text;
            recorder.BucketCode = this.BucketCode.Text;
            recorder.PartCode = this.textBox1.Text;
            recorder.SerialCode = this.SerialCode.Text;

            #endregion

            #region 保存
            DataResult<int> result;//判断是否成功
            if (string.IsNullOrEmpty(recorder.PID) == true)
            {
                result = _agent.InvokeServiceFunction<DataResult<int>>(B9BasicService.RawMaterialBLL_Insert.ToString(), recorder);
            }
            else
            {
                result = _agent.InvokeServiceFunction<DataResult<int>>(B9BasicService.RawMaterialBLL_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 (!string.IsNullOrEmpty(this.BucketCode.Text) && !string.IsNullOrEmpty(this.textBox1.Text))
            {
                var List = _agent.InvokeServiceFunction<List<BucketMaterial>>(DictService.BucketMaterialBLL_GetBucketMaterialList.ToString(), new BucketMaterial() { BucketCode = this.BucketCode.Text });
                if (List != null && List.Count > 0)
                {
                    var count = 0;
                    foreach (var t in List)
                    {
                        if (System.Text.RegularExpressions.Regex.IsMatch(this.textBox1.Text, t.RawMaterial))
                            count++;

                    }
                    if (count == 0)
                    {
                        strMessage += this.BucketCode.Text + "原料上料错误 \r\n";
                    }

                }
            }
            
            //批次
            if (this.BatchCode.Text == string.Empty)
            {
                strMessage += Resource1.MaterialBatchNullValidate + "\r\n";
            }

            //料筒
            if (this.BucketCode.Text == string.Empty)
            {
                strMessage += "料筒不能为空\r\n";
            }

            //零件号
            if (this.textBox1.Text == string.Empty)
            {
                strMessage += "零件号不能为空\r\n";
            }

            return strMessage;
            #endregion
        }

        private void PartCode_TextChanged(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter && (sender as TextBox).Name == "textBox1")
            {
                if (!string.IsNullOrEmpty(this.BucketCode.Text) && !string.IsNullOrEmpty(this.textBox1.Text))
                {
                    var List = _agent.InvokeServiceFunction<List<BucketMaterial>>(DictService.BucketMaterialBLL_GetBucketMaterialList.ToString(), new BucketMaterial() { BucketCode = this.BucketCode.Text });
                    if (List != null && List.Count > 0)
                    {
                        var count = 0;
                        foreach (var t in List)
                        {
                            if (this.textBox1.Text== t.RawMaterial)
                                count++;

                        }
                        if (count == 0)
                        {
                            //SpVoice voice = new SpVoice();
                            //voice.Voice = voice.GetVoices(string.Empty, string.Empty).Item(0);
                            //voice.Speak(this.BucketCode.Text + "原料上料错误");
                            //PlaySound.PlaySounds.Play();
                        }

                    }
                }
                else
                {
                    //PlaySound.PlaySounds.Play("条码扫描不规范");
                    //SpVoice voice = new SpVoice();
                    //voice.Voice = voice.GetVoices(string.Empty, string.Empty).Item(0);
                    //voice.Speak("条码扫描不规范");
                }
                BatchCode.Focus();
            }
        }
        
        private void BucketCode_TextChanged(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter && (sender as TextBox).Name == "BucketCode")
            {
                textBox1.Focus();
            }
                
        }

        private void BatchCode_TextChanged(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter && (sender as TextBox).Name == "BatchCode")
                SerialCode.Focus();
        }
    }
}