using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Collections.Specialized;
using System.Data;
 
 
using System.Collections;
namespace DeiNiu.Wms.CE
{
    public partial class StockPandianPick : basicForm
    {
        #region 界面初始化
        //public int page = 0;
        string orderNo = "";
        int skuId = 0;
        DataTable dt = new DataTable();
        public StockPandianPick()
        {
            setTitle();
            InitializeComponent();
            lacationid_txt.Focus();
        }
        private void StockPandianPick_Load(object sender, EventArgs e)
        {
            DataTable dta = null;
            Cursor.Current = System.Windows.Forms.Cursors.WaitCursor;
            try
            {
                dta = client.getPandianOrders(0, 1);//.getStockLocation(locId);
            }
            catch (Exception er)
            {
                Cursor.Current = System.Windows.Forms.Cursors.Default;
                MessageBox.Show("发生错误:" + er.Message);
                return;
            }
            Cursor.Current = System.Windows.Forms.Cursors.Default;
            skuId_txt.ReadOnly = dta.Rows.Count == 0;
            this.lacationid_txt.ReadOnly = dta.Rows.Count == 0;
            this.count_txt.ReadOnly = dta.Rows.Count == 0; 
            if (dta.Rows.Count > 0)
            {
                this.orderNo_lab.Text = dta.Rows[0]["orderNo"].ToString();//pandian orderno
                this.start_date_lab.Text = dta.Rows[0]["startTime"].ToString();//start date 
               
                orderNo = this.orderNo_lab.Text;
              
            }
            else
            {
                clear(); 
                MessageBox.Show("没有盘点任务");
            }
        }
        #endregion
        /// 
        /// 扫描SKUID 
        /// 
        /// 
        /// 
        /// 
        private void skuId_txt_KeyUp(object sender, KeyEventArgs e)
        {
            if (e.KeyCode != Keys.Enter)
            {
                return;
            }
            if (skuId_txt.Text.Length>0 && skuId_txt.Text.Length < 2)
            {
                return;
            }
            if (skuId_txt.Text.Length > 0)
            {
                try
                {
                    int skuId = Convert.ToInt16(skuId_txt.Text);
                }
                catch
                {
                    focusOn(skuId_txt, "Sku ID");
                    return;
                }
                count_txt.Enabled = true;
                lacationid_txt.Enabled = skuId_txt.Enabled = !count_txt.Enabled; 
                focusOn(count_txt);
                
            }
            else
            {
                txtBarcode.Enabled = true;
                focusOn(txtBarcode, "商品条码");
                lacationid_txt.Enabled = skuId_txt.Enabled = !txtBarcode.Enabled; 
            }
          
        }
        
        /// 
        /// 绑定数据到界面
        /// 
        /// 
        /// 
        private void SetValue(int index0, DataTable dt)
        {
            this.skuId_txt.Text = dt.Rows[0]["skuId"].ToString();//skuId
            this.count_txt.Text = dt.Rows[0]["count"].ToString();//count
            skuId_txt.SelectAll();
            skuId_txt.Focus();
        }
        /// 
        /// 扫描货位码
        /// 
        /// 
        /// 
        private void lacationid_txt_KeyUp(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
            {
                string locId = lacationid_txt.Text;
                //lacationid_txt.Text = "";
             
                if (locId.Length < 7)
                {
                    MessageBox.Show("请扫货位条码");
                    return;
                }
                if (cb1.Checked)
                {
                    #region 根据货位显示已采集信息
                  
                    DataTable dta = null;
                  
                    Cursor.Current = System.Windows.Forms.Cursors.WaitCursor;
                    try
                    {
                        dta = client.getPandianItem(orderNo,locId);//.getStockLocation(locId);
                    }
                    catch (Exception er)
                    {
                        Cursor.Current = System.Windows.Forms.Cursors.Default;
                        MessageBox.Show("发生错误:" + er.Message);
                        return;
                    }
                    Cursor.Current = System.Windows.Forms.Cursors.Default;  
                   if (dta.Rows.Count > 0)
                   { 
                       SetValue(0, dta); 
                   }
                   else
                   { 
                       clear();
                       MessageBox.Show("货位为空!!!");
                   }
                    
                    #endregion
                }
                else
                {
                   
                    #region 
                    /*
                    label17.Visible = oldcount_lab.Visible = true;
                    decimal count =0m;
                    try
                    {
                        count = Convert.ToDecimal(count_txt.Text);
                        if (count  <= 0)
                        {
                            MessageBox.Show("入库数量必须大于0");
                            count_txt.SelectAll();
                            count_txt.Focus();
                            return;
                        }
                    }
                    catch
                    {
                        MessageBox.Show("入库数量必须大于0的数字");
                        count_txt.SelectAll();
                        count_txt.Focus();
                        return;
                    }
                    lacationid_lab.Text = locId;
                    enumRepResult result ; 
                    Cursor.Current = System.Windows.Forms.Cursors.WaitCursor;
                    try
                    {
                        result = (enumRepResult)client.upDownStockLocation(locId, gooodsid, skuId, batch_lab.Text, count, (int)enumStockRecordType.初始盘点, LoginInfo.UserId, LoginInfo.Token, -1);
                    }
                    catch (Exception er)
                    {
                        Cursor.Current = System.Windows.Forms.Cursors.Default;
                        MessageBox.Show("发生错误:" + er.Message);
                        return;
                    }
                    Cursor.Current = System.Windows.Forms.Cursors.Default;  
 
                    MessageBox.Show(result.ToString());
                    if (result == enumRepResult.成功)
                    {
                        if (ye < 2)
                        {
                            clear();
                        }
                        lacationid_lab.Text = "";
                        skuId_txt.Focus();
                       
                    }
                    lacationid_lab.Text = lacationid_txt.Text = "";
  */
                    #endregion 
                }
                skuId_txt.Enabled = true;
                 lacationid_txt.Enabled = count_txt.Enabled = !skuId_txt.Enabled;
                skuId_txt.Focus();
            }
        }
        
        private void cb1_CheckStateChanged(object sender, EventArgs e)
        {/*
            if (cb1.Checked)
            {
                skuId_txt.ReadOnly = true;
                clear();
                lacationid_txt.Focus();
            }
            else
            {
                skuId_txt.ReadOnly = false;
                clear();
               
                skuId_txt.Focus();
            }
          */
        }
        private void clear()
        {
          txtBatch.Text=txtBarcode.Text= skuId_txt.Text = count_txt.Text = this.lacationid_txt.Text  = "";
           lacationid_txt.Enabled = true;
           skuId_txt.Enabled = count_txt.Enabled =  lacationid_txt.Enabled; 
           dtpProduct.Value = DateTime.Now.AddDays(2);
           lacationid_txt.Focus();
           txtBarcode.Text = "6902015000001";
           lacationid_txt.Text = "YL.26.06.45";
           count_txt.Text = "1";
        }
        private void count_txt_KeyUp(object sender, KeyEventArgs e)
        {
            //to record the record to db
            if (e.KeyCode != Keys.Enter)
            {
                return;
            }
            decimal count = 0m; 
            int skuId =0;
            string locId = lacationid_txt.Text;
            string batch = txtBatch.Text;
            string prdDate = dtpProduct.Value.ToShortDateString();
            string barcode = txtBarcode.Text;
            try
            {
                count = Convert.ToDecimal(count_txt.Text.ToString());
            }
            catch
            {
                
                focusOn( count_txt,"请输入数量!");
                return;
            }
           try
            {
                if (skuId_txt.Text.Trim().Length > 0)
                {
                    skuId = Convert.ToInt32(skuId_txt.Text);
                }
            }
            catch
            {
                focusOn(skuId_txt, "sku ID 错误");
                return;
            }
           if (String.IsNullOrEmpty(locId))
           {
               focusOn(lacationid_txt, "货位条码");
               return;
           }
           if (skuId == 0) //需输入批次信息
           {
               if(string.IsNullOrEmpty(barcode.Trim())){
                   focusOn(txtBarcode, "商品条码");
                   return;
               }
               if (dtpProduct.Value.Date == DateTime.Now.AddDays(2).Date)
               {
                   MessageBox.Show("请输入生产日期");
                   dtpProduct.Focus();
                   return;
               }
               if(string.IsNullOrEmpty(batch)){
                   batch = prdDate;
               }
           }
          
            try
            { 
                Cursor.Current = System.Windows.Forms.Cursors.WaitCursor;
               bool result =  client.newPandianItem(orderNo,locId,skuId,barcode,prdDate,batch, count , LoginInfo.UserId, LoginInfo.Token);
                Cursor.Current = System.Windows.Forms.Cursors.Default;
                if (!result )
                {
                    MessageBox.Show("盘点失败"  ); 
                    return;
                }
                else
                {
                    
                    clear();
                    return;
                }
                
            }
            catch (Exception er)
            {
                showErrorMsg(er);
                Cursor.Current = System.Windows.Forms.Cursors.Default;
            }
            clear();
        }
        private void dtpProduct_ValueChanged(object sender, EventArgs e)
        {
           
            txtBatch.Enabled = true;
            focusOn(txtBatch);
        }
        private void txtBarcode_KeyUp(object sender, KeyEventArgs e)
        {
            if (e.KeyCode != Keys.Enter)
            {
                return;
            }
            if (string.IsNullOrEmpty(txtBarcode.Text))
            {
                focusOn(txtBarcode, "商品条码");
            }
            else
            { 
                dtpProduct.Focus();
            }
        }
         
        private void txtBatch_KeyUp(object sender, KeyEventArgs e)
        {
            if (e.KeyCode != Keys.Enter)
            {
                return;
            }
            count_txt.Enabled = true;
            focusOn(count_txt);
            
        }
       
      
       
    }
}