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 QMAPP.FJC.Entity.Operation;
using QMAPP.FJC.Entity;
using QMAPP.ServicesAgent;
using QMAPP.Entity;
using QMAPP.WinForm.Common;
using QM.Common;
using QMAPP.MD.Entity;

namespace QMAPP.WinForm.Forms.WarehouseManage
{
    /// <summary>
    /// 日  期:2018年4月2日
    /// 模  块:计划外出库
    /// 作  者:郭兆福
    /// </summary>
    public partial class SendCodeForm : Form
    {
        List<Product> productList = new List<Product>();
        SendFinalForm sendmain = new SendFinalForm();
        public SendCodeForm()
        {
            InitializeComponent();
        }

        #region 窗体载入

        private void Form_Load(object sender, EventArgs e)
        {
            new KeyEvent().SetKeyToTxt(this);
            //出库原因下拉
            BasicData basicData = new BasicData();
            //初始化控件属性
            //this.comboBoxOutReason.DataSource = basicData.GetDictionaryList(EnumGeter.DictKind.OutReason.ToString());
            //this.comboBoxOutReason.DisplayMember = "VALUE";
            //this.comboBoxOutReason.ValueMember = "KEY";
            //this.comboBoxOutReason.SelectedValue = "";

            //加载物料类型分类
            OutReason searchModelType = new OutReason();
            this.comboBoxOutReason.DataSource = basicData.GetOutReasonList(true);
            this.comboBoxOutReason.DisplayMember = "OUTREASON_NAME";
            this.comboBoxOutReason.ValueMember = "OUTREASON_CODE";


        }

        #endregion

        /// <summary>
        /// 出库原因下拉改变
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void comboBoxOutReason_SelectedIndexChanged(object sender, EventArgs e)
        {
                this.ActiveControl = this.txtCode;
                this.txtCode.Focus();//落入光标
        }


        #region 保存出库信息到ProductOut表
        /// <summary>
        /// 保存出库信息到ProductOut表
        /// </summary>
        public void SaveInfo(List<Product> productList)
        {
            ProductOut outInfo = new ProductOut();
            outInfo.PID=Guid.NewGuid().ToString();//PID
            outInfo.productList = productList;
            if (ClientContext.produceShift != null)
            {
                outInfo.SHIFT_CODE = ClientContext.produceShift.PRODUCESHIFTTCODE;
            }
            outInfo.CREATEUSER = ClientContext.LoginInfo.UserID;
            outInfo.CREATEDATE = DateTime.Now;
            foreach (var model in productList)
            {
                outInfo.PRODUCT_CODE = model.PRODUCTCODE;
                outInfo.MATERIAL_CODE = model.MATERIAL_CODE;
                outInfo.WORKCENTER_CODE = model.WORKCENTER_CODE;
                outInfo.WORKCELL_CODE = model.WORKCELL_CODE;
                outInfo.OUTREASON = model.OUTREASON;
            }
            QMAPP.ServicesAgent.ServiceAgent agent = ClientContext.GetServiceAgent();
            DataResult<int> result = agent.InvokeServiceFunction<DataResult<int>>("ProductOutBLL_InsertProductOut", outInfo);
            //MessageBox.Show(result.Msg);
            //this.DialogResult = DialogResult.OK;
            productList.Clear();
            this.comboBoxOutReason.SelectedItem = null;
            this.txtCode.Text = "";
        }
        #endregion 

        # region 扫码枪
        /// <summary>
        /// 扫码枪
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private bool ValidateAndAddCode(string proCode)
        {

            if (!string.IsNullOrEmpty(proCode))
            {
                if (System.Text.RegularExpressions.Regex.IsMatch(proCode, "^DP\\w+(-\\w+)?-DF[LR][FR]\\w+$"))//扫描输入为门板计划标签
                {
                    //从计划标签中截取产品码
                    proCode = proCode.Substring(proCode.LastIndexOf('-') + 1);
                }
                //出库原因
                if (this.comboBoxOutReason.Text.ToString() == "")
                {
                    this.labelError.Text ="出库原因不能为空!";
                    return false;
                }
                #region 获取零件信息
                QMAPP.ServicesAgent.ServiceAgent agent = ClientContext.GetServiceAgent();
                Product searchModel = new Product();
                Main mainModel = new Main();
                //去Main表查询数据

                if (proCode.Substring(0, 3) == "052")
                {
                    //从计划标签中截取产品码
                    mainModel.MAINCODE = proCode;
                    Main mainInfo = agent.InvokeServiceFunction<Main>("MainBLL_GetByCondition", mainModel);
                    if (mainInfo != null)
                    {
                        searchModel.PRODUCTCODE = mainInfo.EPIDERMISCODE;
                    }
                }
                else 
                {
                    searchModel.PRODUCTCODE = proCode;
                }

                this.labelError.ForeColor = System.Drawing.Color.Red;
                List<Product> list = agent.InvokeServiceFunction<List<Product>>("ProductBLL_GetAllList", searchModel);

                if (list.Count <= 0)//是否存在该零件
                {
                    this.labelError.Text =this.txtCode.Text +"\r\n"+ Resource1.SendInfoNotFoundCode;
                    this.txtCode.Text = "";
                    return false;
                }
                list[0].OUTREASON = this.comboBoxOutReason.SelectedValue.ToString();
                Product model = list[0];
                #endregion



                #region 判断零件状态

                //合格返修报废
                if (!model.STATUS.Equals(EnumGeter.PRODUCTSTAUTS.QUALIFIED.GetHashCode().ToString()))//合格
                {
                    if (model.STATUS.Equals(EnumGeter.PRODUCTSTAUTS.REPAIR.GetHashCode().ToString()))//合格
                    {
                        this.labelError.Text = this.txtCode.Text + "\r\n" + Resource1.SendInfoRepaired;
                        this.txtCode.Text = "";
                        return false;
                    }
                    else
                    {
                        this.labelError.Text = this.txtCode.Text + "\r\n" + Resource1.SendInfoWadBad;
                        this.txtCode.Text = "";
                        return false;
                    }
                }
                //是否已出库
                if (EnumGeter.OUTFLAG.INWAREHOUSE.GetHashCode().ToString().Equals(model.OUTFLAG) == false)
                {
                    this.labelError.Text = this.txtCode.Text + "\r\n" + Resource1.SendInfoNotOk;
                    this.txtCode.Text = "";
                    return false;
                }
                //是否已经完成
                if (EnumGeter.Finish.ENDOFLINE.GetHashCode().ToString().Equals(model.ENDOFLINE) == false)
                {
                    this.labelError.Text = this.txtCode.Text + "\r\n" + "该本体正在加工中,未完成";
                    this.txtCode.Text = "";
                    return false;
                }
                #endregion

                //添加到零件列表
                productList.Add(model);
                return true;
            }

            this.labelError.Text = "请选择相应信息!";
            return false;
        }
        #endregion 

        #region 回车事件
        /// <summary>
        /// 回车事件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void txtCode_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
            {
                if (ValidateAndAddCode(this.txtCode.Text.Trim()))
                {
                    SaveInfo(productList);
                    this.txtCode.Text = "";
                    this.labelError.ForeColor = System.Drawing.Color.Green;
                    this.labelError.Text = "操作成功";
                }
            }
        }
        #endregion 

        #region 窗体关闭
        /// <summary>
        /// 点击关闭按钮关闭窗体
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void tsb_close_Click(object sender, EventArgs e)
        {
            this.DialogResult = DialogResult.OK;
            this.Close();
        }

        /// <summary>
        /// 点击右上角X关闭窗口
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void SendCodeForm_FormClosing(object sender, FormClosingEventArgs e)
        {
            this.DialogResult = DialogResult.OK;
            e.Cancel = false;
        }

        #endregion 


        //提交按钮
        private void submit_Click(object sender, EventArgs e)
        {
            if (ValidateAndAddCode(this.txtCode.Text.Trim().ToUpper()))
            {
                SaveInfo(productList);
                this.txtCode.Text = "";
                this.labelError.ForeColor = System.Drawing.Color.Green;
                this.labelError.Text = "操作成功";
            }
        }

    }
}