using RGD.DBUtility;
using RGD.MdsAPI;
using RGD.MdsAPI.WMS;
using System;
using System.Data;
using System.Windows.Forms;

namespace RGD.ScanCode
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {
        }

        private void textBox1_TextChanged(object sender, EventArgs e)
        {
            if (this.textBox1.Text.Trim() != "" && this.textBox1.Text.Trim().Length == 12)
            {
                valiSubmit();
            }
        }

        /// <summary>
        /// 提交
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void button1_Click(object sender, EventArgs e)
        {
            //this.label5.Text = "验证成功:拣选出库;\r\n拣选数量：" + "3" + ";\r\n拣选明细：" + getREPICK_ARRAY("54545454,45454545,454545,88888888,88888999");

            //getREPICK_ARRAY("54545454,45454545,454545,88888888,88888999");
            valiSubmit();
        }

        private void valiSubmit()
        {
            try
            {
                string barCode = this.textBox1.Text.Trim();
                if (barCode.Equals(""))
                {
                    this.label5.Text = "箱条码不能为空！";
                    return;
                }

                DataTable dv_task = DbHelperSQL.Query("select * from T_Warehouse").Tables[0];
                if (dv_task == null)
                {
                    this.label5.Text = "没有出库任务！";
                    return;
                }
                if (dv_task.Rows.Count == 0)
                {
                    this.label5.Text = "没有出库任务！";
                    return;
                }
                if (dv_task.Rows[0]["F_TASK_NO"].ToString().Equals("0") || dv_task.Rows[0]["F_TASK_NO"].ToString().Equals(""))
                {
                    this.label5.Text = "没有出库任务";
                    return;
                }

                string sResult = "";
                if (dv_task != null)
                {
                    bool bResult = (new getInOutCheckInfo()).Notify(dv_task.Rows[0]["F_TASK_NO"].ToString(), "01", string.Empty, barCode, string.Empty, out sResult);
                    //返回成功
                    if (bResult)
                    {
                        getInOutCheckInfo.DATA op = (new Base()).DeSerialize<getInOutCheckInfo.DATA>(sResult);

                        switch (op.OUT_MODE)
                        {
                            case "1"://整箱出库
                                this.label5.Text = "验证成功:" + barCode + ";整箱出库";
                                break;

                            case "2"://整箱回库
                                this.label5.Text = "验证成功:" + barCode + ";整箱回库";
                                break;

                            case "3"://拣选出库
                                this.label5.Text = "验证成功:拣选出库;\r\n拣选数量：" + op.REPICK_NUM + ";\r\n拣选明细：" + getREPICK_ARRAY("请查看MDS出库表信息");//op.REPICK_ARRAY+ //getREPICK_ARRAY("54545454,45454545,454545,88888888,88888999")
                                break;

                            case "4"://贴标回库
                                this.label5.Text = "验证成功:整箱贴标回库";
                                break;

                            case "5"://贴标出库
                                this.label5.Text = "验证成功:贴标出库";
                                break;

                            case "6"://贴标拣选出库
                                this.label5.Text = "验证成功:贴标拣选出库";
                                break;
                        }
                    }
                    else //调用失败
                    {
                        this.label5.Text = "验证失败:" + sResult;
                    }
                }
            }
            catch (Exception ex)
            {
                this.label5.Text = "提交异常" + ex.Message;
            }
        }

        private string getREPICK_ARRAY(string repick)
        {
            string newText = "";

            string[] temp = repick.Split(',');
            for (int i = 0; i < temp.Length; i++)
            {
                newText += temp[i] + ";";
                if (i % 2 == 1)
                {
                    newText += "\r\n";
                }
            }

            return newText;
        }
    }
}