using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Configuration;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using grproLib;
using MESClassLibrary.BLL.BasicInfo;
using MESClassLibrary.BLL.Log;
using MESClassLibrary.DAL;
using MESClassLibrary.Model;
using DataTable = System.Data.DataTable;

namespace InjectionSearch
{
    public partial class FrmBarCodeSearch : Form
    {
        public FrmBarCodeSearch()
        {
            InitializeComponent();
        }

        private void initProduct()
        {
            comboBox1.Items.Clear();

            ProductBLL bll = new ProductBLL();

            DataTable dt = bll.SearchInfoAllByType("2000");
            if (dt != null && dt.Rows.Count > 0)
            {
                for (int i = 0; i < dt.Rows.Count; i++)
                {
                    comboBox1.Items.Add(dt.Rows[i]["StockNo"].ToString() +"--"+dt.Rows[i]["ProductName"].ToString());
                }
            }
            dt.Dispose();
        }

        private void initBarCode()
        {
            BarCodeBLL bll = new BarCodeBLL();

            glacialList1.Items.Clear();
            DataTable dt = bll.SearchBarCode();
            if (dt != null && dt.Rows.Count > 0)
            {
                for (int i = 0; i < dt.Rows.Count; i++)
                {
                    glacialList1.Items.Add(i.ToString());
                    glacialList1.Items[i].SubItems[0].Text = dt.Rows[i]["BarCode"].ToString();
                    glacialList1.Items[i].SubItems[1].Text = dt.Rows[i]["ProductName"].ToString();
                    glacialList1.Items[i].SubItems[2].Text = dt.Rows[i]["PrintType"].ToString();
                    glacialList1.Items[i].SubItems[3].Text = dt.Rows[i]["PrintTime"].ToString() == "" ? "" : Convert.ToDateTime(dt.Rows[i]["PrintTime"].ToString()).ToString("yyyy-MM-dd HH:mm:ss.fff");
                    glacialList1.Items[i].SubItems[4].Text = dt.Rows[i]["MaterialName"].ToString();
                    glacialList1.Items[i].SubItems[5].Text = dt.Rows[i]["BatchNo"].ToString();
                }
            }
        }

        private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (string.IsNullOrEmpty(comboBox1.Text) ||  comboBox1.Text.Length<10)  return;
            string stockNo = comboBox1.Text.Substring(0, 10);

            ProductBLL bll=new ProductBLL();
            BomBLL bombll=new BomBLL();

            DataTable dt = bll.SearchInfoByStock(stockNo);
            if (dt != null && dt.Rows.Count > 0)
            {
                textBox1.Text = dt.Rows[0]["PartNo"].ToString();
                dt.Dispose();
            }
            else
            {
                textBox1.Text = "";
            }

            DataTable dt1 = bombll.SearchBom(textBox1.Text);
            if (dt1 != null && dt1.Rows.Count > 0)
            {
                textBox2.Text = dt1.Rows[0]["PartNo2"].ToString();
                dt.Dispose();
            }
            else
            {
                textBox2.Text = "";
            }
        }

        private void FrmBarCodeSearch_Load(object sender, EventArgs e)
        {
            initProduct();
            initBarCode();
            comboBox2.Items.Clear();
            comboBox2.Items.Add("IM01");
            comboBox2.Items.Add("IM02");
            comboBox2.Items.Add("IM03");
            comboBox2.Items.Add("IM04");
            comboBox2.Items.Add("IM05");
            comboBox2.Items.Add("IM06");
        }

        private void label18_Click(object sender, EventArgs e)
        {
            string code = "";
            BarCodeModel md = new BarCodeModel();
            BarCodeBLL bll = new BarCodeBLL();
            ProductBLL pbll=new ProductBLL();

            try
            {
                if (glacialList1.SelectedItems.Count == 0)
                {
                    MessageBox.Show("请选择要补打的条码!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                    return;
                }
                else
                {
                    GlacialComponents.Controls.GLItem gv;
                    gv = glacialList1.SelectedItems[0] as GlacialComponents.Controls.GLItem;
                    code = gv.Text;

                    DataTable dt = bll.SearchInfoByBarCode(code);
                    if (dt != null && dt.Rows.Count > 0)
                    {
                        md.ID = Guid.NewGuid().ToString();
                        md.BarCode = code;
                        md.StationID = Program.StationID;
                        md.StockNo = dt.Rows[0]["StockNo"].ToString();
                        md.OneBarCode = dt.Rows[0]["OneBarCode"].ToString();
                        md.StationID2 = dt.Rows[0]["StationID"].ToString();
                        md.PrintType = 2;

                        DataTable pdt = pbll.SearchIsImportByStockNo(md.StockNo);
                        if (pdt != null && pdt.Rows.Count > 0)
                        {
                            if (pdt.Rows[0]["isImport"].ToString() == "1")
                            {
                                md.Import = "国产料Kingfa";
                            }
                            else
                            {
                                md.Import = "";
                            }
                        }
                        //if (md.StockNo == "2200000064" || md.StockNo == "2200000093" || md.StockNo == "2200000096" ||
                        //    md.StockNo == "2200000052")
                        //{
                        //    md.Import = "国产料Kingfa";
                        //}
                        //else
                        //{
                        //    md.Import = "";
                        //}
                    }
                    dt.Dispose();
                    bll.Add_Info(md);

                    #region 打印条码

                    GridppReport report = new GridppReport();

                    report.Register("");
                    report.LoadFromFile(@"D:\P02 - 副本.grf");
                    report.ConnectionString = ConfigurationManager.ConnectionStrings["report"].ToString();

                    //report.PrintPreview(true);
                    report.Print(false);
                    #endregion

                    bll.Update_Info(md);
                    initBarCode();
                }
            }
            catch (Exception ex)
            {
                LogErrBLL.AddInfo(ex.ToString(), MethodBase.GetCurrentMethod());
            }
            
        }

        public DateTime GetDateTime()
        {
            string sql = "";
            DateTime time;
            DataTable dt;
            try
            {
                sql = @"select getdate() as time";

                dt = SqlHelper.ExecuteDataset(SqlHelper.GetConnSting(), CommandType.Text, sql, null).Tables[0];
                if (dt != null && dt.Rows.Count > 0)
                {
                    time = Convert.ToDateTime(dt.Rows[0]["time"].ToString());

                }
                else
                {
                    time = DateTime.Now;
                }
                return time;
            }
            catch (Exception ex)
            {
                LogErrBLL.AddInfo(ex.ToString(), MethodBase.GetCurrentMethod());
                return DateTime.Now;
            }
        }

        private static bool IsNumeric(string str)
        {
            System.Text.RegularExpressions.Regex reg1 = new System.Text.RegularExpressions.Regex(@"^[0-9]\d*$");
            return reg1.IsMatch(str);
        }

        private void label19_Click(object sender, EventArgs e)
        {
            string code = "", stationID = "", OneCode = "";
            string newcode = "", partNo = "", stockNo = "", NewOneCode = "";

            try
            {
                #region 数据合法性判断
                if (comboBox2.Text.Trim() == "")
                {
                    MessageBox.Show("请选择机台!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                    return;
                }

                if (comboBox1.Text.Trim() == "")
                {
                    MessageBox.Show("请选择产品!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                    return;
                }

                if (textBox3.Text.Trim() == "")
                {
                    MessageBox.Show("请输入批次!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                    return;
                }

                if (textBox4.Text.Trim() == "")
                {
                    MessageBox.Show("请输入数量!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                    return;
                }
                if (!IsNumeric(textBox4.Text.Trim()))
                {
                    MessageBox.Show("数量格式不正确!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                    return;
                }

                if (Convert.ToInt32(textBox4.Text.Trim()) > 1000)
                {
                    MessageBox.Show("数量不能大于1000!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                    return;
                }

                #endregion

                #region 获取工位编号

                StationBLL sbll = new StationBLL();
                DataTable sdt = sbll.SearchInfoByNo(comboBox2.Text.Trim());
                if (sdt != null && sdt.Rows.Count > 0)
                {
                    stationID = sdt.Rows[0]["StationID"].ToString();
                }
                sdt.Dispose();
                #endregion

                DialogResult result = MessageBox.Show("确定手工打印条码?", "提示", MessageBoxButtons.OKCancel, MessageBoxIcon.Information);
                if (result == DialogResult.OK)
                {
                    #region 生成条码

                    ProductBLL pbll = new ProductBLL();

                    //查询零件号
                    stockNo = comboBox1.Text.Substring(0, 10);
                    partNo = textBox1.Text.Trim();

                    //根据零件号、批次号查找

                    BarCodeBLL bll = new BarCodeBLL();
                    BarCodeModel md = new BarCodeModel();

                    //DataTable dt = bll.SearchSerialNoByBarCode(partNo + "." + textBox3.Text.Trim());
                    //DataTable dt = bll.SearchInfoByStock(stockNo);
                    DataTable dt = bll.SearchSerialNoByBarCode(stockNo, textBox3.Text.Trim());
                    if (dt != null && dt.Rows.Count > 0)
                    {
                        OneCode = dt.Rows[0]["OneBarCode"].ToString().Trim();
                        code = dt.Rows[0]["BarCode"].ToString().TrimEnd();
                        for (int i = 1; i <= Convert.ToInt32(textBox4.Text.Trim()); i++)
                        {
                            newcode = code.Substring(0, code.Length - 4) +
                                      (Convert.ToInt32(code.Substring(code.Length - 4, 4)) + i)
                                      .ToString()
                                      .PadLeft(4, '0');
                            NewOneCode = OneCode.Substring(0, OneCode.Length - 4) +
                                         (Convert.ToInt32(OneCode.Substring(OneCode.Length - 4, 4)) + i).ToString()
                                         .PadLeft(4, '0');
                            string[] part = newcode.Split('.');
                            //存入tb_BarCode表
                            md.ID = Guid.NewGuid().ToString();
                            md.StationID = Program.StationID;
                            md.StationID2 = stationID;
                            md.OneBarCode = NewOneCode;
                            md.BarCode = newcode;
                            md.BatchNo= textBox3.Text.Trim();

                            md.StockNo = stockNo;
                            md.PrintType = 1;

                            DataTable pdt = pbll.SearchIsImportByStockNo(stockNo);
                            if (pdt != null && pdt.Rows.Count > 0)
                            {
                                if (pdt.Rows[0]["isImport"].ToString() == "1")
                                {
                                    md.Import = "国产料Kingfa";
                                }
                                else
                                {
                                    md.Import = "";
                                }
                            }

                            //if (NewOneCode.Substring(0, 10) == "2200000064" ||
                            //    NewOneCode.Substring(0, 10) == "2200000093" ||
                            //    NewOneCode.Substring(0, 10) == "2200000096" ||
                            //    NewOneCode.Substring(0, 10) == "2200000084" ||
                            //    NewOneCode.Substring(0, 10) == "2200000090" ||
                            //    NewOneCode.Substring(0, 10) == "2200000052")
                            //{
                            //    md.Import = "国产料Kingfa";
                            //}
                            //else
                            //{
                            //    md.Import = "";
                            //}

                            if (bll.Add_Info(md))

                            {
                                #region 打印条码

                                GridppReport report = new GridppReport();

                                report.Register("");
                                //report.LoadFromFile(@"D:\P02.grf");
                                report.LoadFromFile(@"D:\P02 - 副本.grf");
                                report.ConnectionString = ConfigurationManager.ConnectionStrings["report"].ToString();

                                //report.PrintPreview(true);
                                report.Print(false);

                                #endregion

                                bll.Update_Info(md);
                            }
                        }
                    }
                    else
                    {
                        for (int i = 1; i <= Convert.ToInt32(textBox4.Text.Trim()); i++)
                        {
                            newcode = partNo + "." + textBox3.Text.Trim() + "." + i.ToString().PadLeft(4, '0');
                            NewOneCode = stockNo + textBox3.Text.Trim() + i.ToString().PadLeft(4, '0');
                            string[] part = newcode.Split('.');
                            //存入tb_BarCode表
                            md.ID = Guid.NewGuid().ToString();
                            md.StationID = Program.StationID;
                            md.StationID2 = stationID;
                            md.StockNo = stockNo;
                            md.OneBarCode = NewOneCode;
                            md.BarCode = newcode;
                            md.PrintType = 1;
                            md.BatchNo = textBox3.Text.Trim();

                            DataTable pdt = pbll.SearchIsImportByStockNo(stockNo);
                            if (pdt != null && pdt.Rows.Count > 0)
                            {
                                if (pdt.Rows[0]["isImport"].ToString() == "1")
                                {
                                    md.Import = "国产料Kingfa";
                                }
                                else
                                {
                                    md.Import = "";
                                }
                            }

                            bll.Add_Info(md);

                            #region 打印条码

                            GridppReport report = new GridppReport();

                            report.Register("");
                            //report.LoadFromFile(@"D:\P02.grf");
                            report.LoadFromFile(@"D:\P02 - 副本.grf");
                            report.ConnectionString = ConfigurationManager.ConnectionStrings["report"].ToString();

                            //report.PrintPreview(true);
                            report.Print(false);

                            #endregion

                            bll.Update_Info(md);
                        }
                    }

                    #endregion

                    initBarCode();
                }
                else
                {
                    return;
                }
            }
            catch (Exception ex)
            {
                LogErrBLL.AddInfo(ex.ToString(), MethodBase.GetCurrentMethod());
            }
        }

        private void button1_Click(object sender, EventArgs e)
        {
            BarCodeBLL bll = new BarCodeBLL();
            DataTable dt = null;

            try
            {
                glacialList1.Items.Clear();
                if (textBox5.Text.Trim() == "")
                {
                    dt = bll.SearchBarCode();
                }
                else
                {
                    if (textBox5.Text.Trim().Length == 20)
                    {
                        dt = bll.SearchBarCodeByOne(textBox5.Text.Trim());
                    }
                    else
                    {
                        dt = bll.SearchBarCodeByTwo(textBox5.Text.Trim());
                    }
                }
                

                if (dt != null && dt.Rows.Count > 0)
                {
                    for (int i = 0; i < dt.Rows.Count; i++)
                    {
                        glacialList1.Items.Add(i.ToString());
                        glacialList1.Items[i].SubItems[0].Text = dt.Rows[i]["BarCode"].ToString();
                        glacialList1.Items[i].SubItems[1].Text = dt.Rows[i]["ProductName"].ToString();
                        glacialList1.Items[i].SubItems[2].Text = dt.Rows[i]["PrintType"].ToString();
                        glacialList1.Items[i].SubItems[3].Text = dt.Rows[i]["PrintTime"].ToString() == "" ? "" : Convert.ToDateTime(dt.Rows[i]["PrintTime"].ToString()).ToString("yyyy-MM-dd HH:mm:ss.fff");
                        glacialList1.Items[i].SubItems[4].Text = dt.Rows[i]["MaterialName"].ToString();
                        glacialList1.Items[i].SubItems[5].Text = dt.Rows[i]["BatchNo"].ToString();
                    }
                }

            }
            catch (Exception ex)
            {
                LogErrBLL.AddInfo(ex.ToString(), MethodBase.GetCurrentMethod());
            }
        }

        private void FrmBarCodeSearch_FormClosing(object sender, FormClosingEventArgs e)
        {
            Application.Exit();
        }
    }
}