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 LocationStockInit : basicForm
    {
        #region 界面初始化
        //public int page = 0;
        string gooodsid = string.Empty;
        int ye = 0;
        int index = 0;
        int skuId = 0;
        DataTable dt = new DataTable();
        public LocationStockInit()
        {
            setTitle();
            InitializeComponent();
            goodscode_txt.Focus();
        }
        private void BarCodeStorage_Load(object sender, EventArgs e)
        {
           
        }
        #endregion
        /// 
        /// 扫描商品条码,获取数据
        /// 
        /// 
        /// 
        /// 
        private void goodscode_txt_KeyUp(object sender, KeyEventArgs e)
        {
            if (e.KeyCode != Keys.Enter)
            {
                return;
            }
            string barcode = goodscode_txt.Text.Trim();
            if (barcode.Length < 8)
            {
                MessageBox.Show("请扫商品条码");
                return;
            }
            goodscode_txt.Text = "";
             
            Cursor.Current = System.Windows.Forms.Cursors.WaitCursor;
            try
            {
                dt = client.getGoodsERPStoreByBarcode(barcode);
            }
            catch (Exception er)
            {
                Cursor.Current = System.Windows.Forms.Cursors.Default;
                MessageBox.Show("发生错误:" + er.Message);
                return;
            }
            Cursor.Current = System.Windows.Forms.Cursors.Default;  
            if (dt.Rows.Count > 0)
            { 
                lacationid_txt.Focus();
                ye = dt.Rows.Count;
                up_bt.Visible = down_bt.Visible = true;
                SetValue(index, dt);
            }
            else
            {
                clear();
            }
        }
        
        /// 
        /// 绑定数据到界面
        /// 
        /// 
        /// 
        private void SetValue(int index0, DataTable dt)
        {
            ye = dt.Rows.Count;
            row_lab.Text = "共" + ye.ToString() + "页";
            down_bt.Enabled = up_bt.Enabled = true;
            if (ye < 2)
            {
                up_bt.Enabled = down_bt.Enabled = false;
            }
            else
            {
                if (index0 == 0)  //首页
                {
                    up_bt.Enabled = false;
                }
                else if ((index0 + 1) == ye) //最后一页
                {
                    down_bt.Enabled = false;
                }
            }
            int y =  index0;
          //  skuId = Convert.ToInt16(dt.Rows[y]["skuId"].ToString());  //初始盘点,skuid =0
            gooodsid = dt.Rows[y]["goodsId"].ToString();//商品id
      
            name_lab.Text = dt.Rows[y]["goodsName"].ToString();//品名
        
            spec_lab.Text = dt.Rows[y]["spec"].ToString();//规格
    
            PCompany_lab.Text = dt.Rows[y]["manufacturer"].ToString();//生产厂商
         
            batch_lab.Text = dt.Rows[y]["batch"].ToString();//批号
       
            product_date_lab.Text = dt.Rows[y]["productDate"].ToString();//生产日期
     
            save_date_lab.Text = dt.Rows[y]["validDate"].ToString();//有效期
      
            oldcount_lab.Text = dt.Rows[y]["count"].ToString();//erp库存数量
            count_txt.Text = oldcount_lab.Text;
           // Stocknum_lab.Text = "库存:" + oldcount_lab.Text;
      
            unit_lab.Text = dt.Rows[y]["unit"].ToString(); ;//单位 
            lacationid_txt.Focus();
        }
        /// 
        /// 扫描货位码
        /// 
        /// 
        /// 
        private void lacationid_txt_KeyUp(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
            {
                string locId = lacationid_txt.Text;
                lacationid_txt.Text = "";
                lacationid_lab.Text = locId;
                if (locId.Length < 7)
                {
                    MessageBox.Show("请扫货位条码");
                    return;
                }
                if (cb1.Checked)
                {
                    #region 根据货位显示信息
                  
                    DataTable dta = null;
                  
                    Cursor.Current = System.Windows.Forms.Cursors.WaitCursor;
                    try
                    {
                        dta = client.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)
                   {
                       up_bt.Visible = down_bt.Visible = label17.Visible = oldcount_lab.Visible = false;
                       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);
                        showErrorMsg(er );
                        return;
                    }
                    Cursor.Current = System.Windows.Forms.Cursors.Default;  
 
                    MessageBox.Show(result.ToString());
                    if (result == enumRepResult.成功)
                    {
                        if (ye < 2)
                        {
                            clear();
                        }
                        lacationid_lab.Text = "";
                        goodscode_txt.Focus();
                       
                    }
                    lacationid_lab.Text = lacationid_txt.Text = "";
 
                    #endregion
                }
            }
        }
        /// 
        /// 上一页
        /// 
        /// 
        /// 
        private void up_bt_Click(object sender, EventArgs e)
        {
            if (index < ye)
            {
                index--;
                SetValue(index, dt);
            }
        }
        /// 
        /// 下一页
        /// 
        /// 
        /// 
        private void down_bt_Click(object sender, EventArgs e)
        {
            if (index < ye)
            {
                index++;
                SetValue(index, dt);
            }
        }
        private void cb1_CheckStateChanged(object sender, EventArgs e)
        {
            if (cb1.Checked)
            {
                goodscode_txt.ReadOnly = true;
                clear();
                lacationid_txt.Focus();
            }
            else
            {
                goodscode_txt.ReadOnly = false;
                clear();
               
                goodscode_txt.Focus();
            }
        }
        private void clear()
        {
            name_lab.Text = batch_lab.Text = product_date_lab.Text = save_date_lab.Text
                           = count_txt.Text = lacationid_lab.Text = PCompany_lab.Text = spec_lab.Text = oldcount_lab.Text = row_lab.Text = "";
            ye = 0;
            index = 0;
            gooodsid = string.Empty;
            up_bt.Enabled = down_bt.Enabled = false;
        }
       
      
  
        //private void goodscode_txt_TextChanged(object sender, EventArgs e)
        //{
        //}
        //private void goodscode_txt_TextChanged_1(object sender, EventArgs e)
        //{
        //}
        //private void lacationid_txt_TextChanged(object sender, EventArgs e)
        //{
        //}
       
    }
}