using System;
using System.Collections.Generic;
using System.Collections.Specialized;
using System.Windows.Forms;
using QM.Common;
using QMAPP.FJC.Entity;
using QMAPP.FJC.Entity.Basic;
using QMAPP.FJC.Entity.Operation;
using QMAPP.ServicesAgent;
using QMAPP.WinForm.Common;
using QMAPP.Entity;

namespace QMAPP.WinForm.Forms.Injection
{
    /// <summary>
    /// 模块编号:M11-2
    /// 作    用:搪塑条码打印
    /// 作    者:王庆男
    /// 编写日期:2015年05月28日
    ///</summary>
    public partial class SlushMoldingPrintEditForm : Form
    {
        Product recorder = new Product();

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

        /// <summary>
        /// 编辑
        /// </summary>
        /// <param name="pid"></param>
        public SlushMoldingPrintEditForm(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[] { EnumGeter.ProcessType_M.Slush.GetHashCode().ToString() }, true);
            this.comMachineCode.DisplayMember = "MACHINENAME";
            this.comMachineCode.ValueMember = "MACHINECODDE";
            ////产品类型
            //this.comProType.DataSource = basicData.GetDictionaryList(EnumGeter.DictKind.PROTYPE.ToString());
            //this.comProType.DisplayMember = "VALUE";
            //this.comProType.ValueMember = "KEY";
            //颜色
            this.comColor.DataSource = basicData.GetDictionaryList(EnumGeter.DictKind.COLOR.ToString());
            this.comColor.DisplayMember = "VALUE";
            this.comColor.ValueMember = "KEY";
            //投料口
            this.comPosition.DataSource = basicData.GetDictionaryList(EnumGeter.DictKind.CAVITYTYPE.ToString());
            this.comPosition.DisplayMember = "VALUE";
            this.comPosition.ValueMember = "KEY";
            ////高低配
            //this.comHOrL.DataSource = basicData.GetDictionaryList(EnumGeter.DictKind.HAndL.ToString());
            //this.comHOrL.DisplayMember = "VALUE";
            //this.comHOrL.ValueMember = "KEY";
            //模腔号
            this.cBoxModelCode.DataSource = basicData.GetDictionaryList(EnumGeter.DictKind.MODELCODE.ToString());
            this.cBoxModelCode.DisplayMember = "VALUE";
            this.cBoxModelCode.ValueMember = "KEY";
            #endregion
            //加工日期
            //this.dtpDate.Text = DateTime.Now.ToString();

            #region 编辑
            //判断是否为编辑加载编辑数据
            if (string.IsNullOrEmpty(recorder.PID) == false)
            {
                QMAPP.ServicesAgent.ServiceAgent agent = ClientContext.GetServiceAgent();
                recorder = agent.InvokeServiceFunction<Product>(B9BasicService.ProductBLL_Get.ToString(), recorder);
                this.txtProductCode.Text = recorder.PRODUCTCODE;
            }
            #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 加载保存信息
            //零件属性
            ProductAttribute productAttribute = new ProductAttribute();
            //判断条码是否为空
            if (this.txtProductCode.Text.Trim() == string.Empty)//条码为空
            {
                //搪塑机
                MachineInfo machineInfo = this.comMachineCode.SelectedItem as MachineInfo;
                //搪塑机编号
                productAttribute.VAL1 = machineInfo.MACHINENUM;
                //产品类型
                //ListItem typeItem = this.comProType.SelectedItem as ListItem;
                //productAttribute.VAL2 = this.comProType.SelectedValue.ToString();
                //产品类型(车型)成都固定
                productAttribute.VAL2 = "V";
                //高低配
                //ListItem hAndLItem = this.comHOrL.SelectedItem as ListItem;
                //productAttribute.VAL3 = this.comHOrL.SelectedValue.ToString();
                //高低配 成都无,且固定
                productAttribute.VAL3 = "B";
                //颜色
                //ListItem colorItem = this.comColor.SelectedItem as ListItem;
                productAttribute.VAL4 = this.comColor.SelectedValue.ToString();
                //腔
               // ListItem posiItem = this.comPosition.SelectedItem as ListItem;
                productAttribute.VAL5 = this.comPosition.SelectedValue.ToString();
                //模腔
                productAttribute.VAL6 = this.cBoxModelCode.Text.Trim();
                //加工日期
                if (string.IsNullOrEmpty( this.dtpDate.Text.Trim()))
                {
                    productAttribute.VAL7 = Convert.ToDateTime(DateTime.Now).ToString("yyyy-MM-dd HH:mm:ss");
                }
                else
                {
                    productAttribute.VAL7 = Convert.ToDateTime(this.dtpDate.Text.Trim()).ToString("yyyy-MM-dd HH:mm:ss");
                }

                //产品条码表
                recorder.MACHINECODDE = machineInfo.MACHINECODDE;//暂时用这个码
                recorder.MACHINENAME = machineInfo.MACHINENAME;
                recorder.PRODUCTATTRIBUTE = productAttribute; 
            }
            else
            {
                string proCode = this.txtProductCode.Text.Trim();
                recorder.PRODUCTCODE = proCode;
                //解析条码
                try
                {
                    //搪塑机编号
                    productAttribute.VAL1 = proCode.Substring(10, 1);
                    //搪塑机
                    MachineInfo machineInfo = ((List<MachineInfo>)this.comMachineCode.DataSource).Find(m => m.MACHINENUM == productAttribute.VAL1 && m.PROCESSTYPE.Equals(EnumGeter.ProcessType.tangsu.GetHashCode().ToString()));
                    if (machineInfo != null)
                    {
                        recorder.MACHINENAME = machineInfo.MACHINENAME;
                    }
                    //产品类型
                    productAttribute.VAL2 = proCode.Substring(11, 1);
                    //高低配
                    productAttribute.VAL3 = proCode.Substring(12, 1);
                    //颜色
                    productAttribute.VAL4 = proCode.Substring(13, 1);
                    //腔
                    productAttribute.VAL5 = proCode.Substring(14, 1);
                    //模腔
                    productAttribute.VAL6 = proCode.Substring(15);
                    //加工日期
                    productAttribute.VAL7 = DateTime.ParseExact(proCode.Substring(0, 10), "yyMMddHHmm", System.Globalization.CultureInfo.CurrentCulture).ToString("yyyy-MM-dd");
                }
                catch (Exception)
                {
                    MessageBox.Show(Resource1.ProductCodeError, Resource1.ConfirmTitle, MessageBoxButtons.OK, MessageBoxIcon.Warning);
                    return;
                }
               
            }
            recorder.PRODUCTSOURCE = EnumGeter.PRODUCTSOURCE.SELFMADE.GetHashCode().ToString();
            recorder.PRODUCTTYPE = EnumGeter.ProductType.biaopi.GetHashCode().ToString();
            recorder.STATUS = EnumGeter.PRODUCTSTAUTS.QUALIFIED.GetHashCode().ToString();
            recorder.CURRENTPROCESS = EnumGeter.ProcessType.lengdaoruohua.GetHashCode().ToString();
            recorder.PRODUCELINE = EnumGeter.PRODUCELINE.VW371.ToString();
            recorder.OUTFLAG = EnumGeter.PRODUCTOUTFLAG.INLIBRARY.GetHashCode().ToString();
            recorder.USINGSTATE = EnumGeter.PRODUCTUSESTATE.UNUSED.GetHashCode().ToString();
            recorder.PRODUCESHIFTTCODE = ClientContext.produceShift.PRODUCESHIFTTCODE;
            recorder.PRODUCESHIFTNAME = ClientContext.produceShift.PRODUCESHIFTNAME;
            recorder.CAPACITY = 1;
            recorder.USINGCOUNT = 0;
            recorder.PRODUCTATTRIBUTE = productAttribute;
            recorder.AddM = this.cbM.Checked;
            #endregion

            #region 保存
            QMAPP.ServicesAgent.ServiceAgent agent = ClientContext.GetServiceAgent();

            DataResult<List<string>> result;//判断是否成功
            if (string.IsNullOrEmpty(recorder.PID) == true)
            {
                result = agent.InvokeServiceFunction<DataResult<List<string>>>(B9BasicService.ProductBLL_InsertEpidermis.ToString(), recorder);
            }
            else
            {
                result = agent.InvokeServiceFunction<DataResult<List<string>>>(B9BasicService.ProductBLL_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.txtProductCode.Text.Trim() == string.Empty)//条码为空那么除去条码所有均不能为空
            {
                //搪塑设备
                if (this.comMachineCode.SelectedValue == null || this.comMachineCode.SelectedValue == string.Empty)
                {
                    strMessage += Resource1.SlushMachineNullValidate + "\r\n";
                }
                ////产品类别
                //if (this.comProType.SelectedValue == null || this.comProType.SelectedValue == string.Empty)
                //{
                //    strMessage += Resource1.ProductTypeNullValidate + "\r\n";
                //}
                //颜色
                if (this.comColor.SelectedValue == null || this.comColor.SelectedValue == string.Empty)
                {
                    strMessage += Resource1.ColorNullValidate + "\r\n";
                }
                ////高低配
                //if (this.comHOrL.SelectedValue == null || this.comHOrL.SelectedValue == string.Empty)
                //{
                //    strMessage += Resource1 .HLNullValidate+ "\r\n";
                //}
                //腔
                if (this.comPosition.SelectedValue == null || this.comPosition.SelectedValue == string.Empty)
                {
                    strMessage += Resource1.PositionNullValidate + "\r\n";
                }
                //模腔号
                if (this.cBoxModelCode.Text == string.Empty)
                {
                    strMessage += Resource1.ModelCodeNullValidate + "\r\n";
                }
            }
            else//校验条码是否符合要求(包括是否重复)
            {
                //调用服务判断产品类型

                //判断是否重复
                Product product = new Product();
                product.PRODUCTCODE = this.txtProductCode.Text.Trim();
                QMAPP.ServicesAgent.ServiceAgent agent = ClientContext.GetServiceAgent();
                bool result = agent.InvokeServiceFunction<bool>(B9BasicService.ProductBLL_ExistsProduct.ToString(), product);
                if (result)
                {
                    strMessage += Resource1.ProductCodeRepeartValidate + "\r\n";
                }
            }
            return strMessage;
            #endregion
        }
    }
}