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();
        bool isFromErpStock = false;
        public LocationStockInit()
        {
            setTitle();
            InitializeComponent();
           focusOn( goodscode_txt,"商品条码");
        }
        private void BarCodeStorage_Load(object sender, EventArgs e)
        {
           
        }
        #endregion
        /// 
        /// 扫描商品条码,获取数据
        /// 
        /// 
        /// 
        /// 
        private void goodscode_txt_KeyUp(object sender, KeyEventArgs e)
        {
            if (e.KeyCode != Keys.Enter)
            {
                return;
            }
            isFromErpStock = chkErp.Checked;
            string barcode = goodscode_txt.Text.Trim();
            if (barcode.Length < 8)
            {
                focusOn(goodscode_txt, "商品条码"); return;
            }
            goodscode_txt.Text = "";
             
            Cursor.Current = System.Windows.Forms.Cursors.WaitCursor;
            try
            {
                if (isFromErpStock)
                {
                    dt = client.getGoodsERPStoreByBarcode(barcode);
                }
                else
                {
                    dt = client.getGoodsPackByBarcode(barcode);
                }
            }
            catch (Exception er)
            {
                Cursor.Current = System.Windows.Forms.Cursors.Default;
                showErrorMsg(er);
                return;
            }
            Cursor.Current = System.Windows.Forms.Cursors.Default;  
            if (dt.Rows.Count > 0)
            {  
               
                ye = dt.Rows.Count;
                up_bt.Visible = down_bt.Visible = true;
                SetValue(index, dt);
                focusOn(count_txt);
            }
            else
            {
                clear();
            }
        }
        
        /// 
        /// 绑定数据到界面
        /// 
        /// 
        /// 
        private void SetValue(int index0, DataTable dt)
        {
            ye = dt.Rows.Count;
            row_lab.Text = String.Format("{0}/{1}页", ye > 0 ? index0 + 1 : index0, ye);
            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();//生产厂商
            if (isFromErpStock)
            {
                txtBatch.Text = dt.Rows[y]["batch"].ToString();//批号
                txtPrdDate.Text = dt.Rows[y]["productDate"].ToString();//生产日期
                txtValidDate.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(); ;//单位 
            
        }
        /// 
        /// 扫描货位码
        /// 
        /// 
        /// 
        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;
                    }
                    string  batch = "";
                    string prdDate = txtPrdDate.Text;
                    string validDate = txtValidDate.Text;
                   
                    batch = txtBatch.Text;
                    if (String.IsNullOrEmpty(batch))
                    {
                        batch = prdDate;
                    }
                    if (String.IsNullOrEmpty(prdDate))
                    {
                        focusOn(txtPrdDate );
                        return;
                    }
                    if (String.IsNullOrEmpty(validDate))
                    {
                        focusOn(txtValidDate);
                        return;
                    }
                    string[] skuKeys = { "生产日期", "到期日期", "厂家批次" };
                    string[] skuValues = { prdDate, validDate, batch };
                     
                    lacationid_lab.Text = locId;
                    enumRepResult result ; 
                    Cursor.Current = System.Windows.Forms.Cursors.WaitCursor;
                    try
                    {
                       // result = (enumRepResult)client.upDownStockLocation(locId, gooodsid, skuId, txtBatch.Text, count, (int)enumStockRecordType.初始盘点, LoginInfo.UserId, LoginInfo.Token, -1);
                        result = (enumRepResult)client.initialStockIn(gooodsid,locId,   count,skuKeys, skuValues,  LoginInfo.UserId, LoginInfo.Token );
                                                  
         
                    }
                    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()
        {
           row_lab.Text= name_lab.Text = txtBatch.Text = txtPrdDate.Text = txtValidDate.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 count_txt_KeyUp(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
            {
                decimal count = 0m;
                try
                {
                    count = Convert.ToDecimal(count_txt.Text);
                    if (count <= 0)
                    {
                        focusOn(count_txt, "入库数量必须大于0");
                         
                        return;
                    }
                }
                catch
                { 
                    focusOn(count_txt, "入库数量必须大于0的数字");
                    return;
                }
                focusOn(lacationid_txt);
            }
        }
       
      
       
    }
}