using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Collections.Specialized;
using System.Collections;
namespace DeiNiu.Wms.CE
{
    public partial class locationUpDown : basicForm
    {
        #region 界面初始化
        string goodsName = "";
        string goodsId = string.Empty;
        string batch = string.Empty;
        string product_date = string.Empty;
        string save_date = string.Empty;
        int skuId =0;
        int id = 0;
        string warehouse = string.Empty;
        string company = string.Empty;
        string locationid = string.Empty;
        decimal count = 0m;
        decimal oldCnt = 0m;
        string goodsType = null;
        DataTable dt;
        int ye = 0;
        int index = 0;
        DataTable dtIns;
        public locationUpDown()
        {
            setTitle();
            InitializeComponent();
            tb1.SelectedIndex = 1; // to show down history
           // locationid_txt.Focus();
        }
        #endregion
        #region 清除数据
        private void clear()
        {
            ye = 0;
            index = 0;
            up_bt.Enabled = down_bt.Enabled = false;
            id = 0;
            goodsId = batch = product_date = save_date = warehouse = company = "";
            count_txt.Text = "0";
            goodsType = null;
            count = 0m;
            oldCnt = 0m;
            row_lab.Text= code_txt.Text = lacationid_lab.Text = product_date_lab.Text = PCompany_lab.Text = name_lab.Text = spec_lab.Text = bigcout_lab.Text = oldcount_lab.Text = batch_lab.Text = save_date_lab.Text = unit0_lab.Text = warehouse =   id_lab.Text = "";
        }
        #endregion
        #region 切换选项卡时,重新加载数据
        private void tb1_SelectedIndexChanged(object sender, EventArgs e)
        {
         
            if (tb1.SelectedIndex == 0)
            {
                locationid_txt.Focus();
                count_txt.Text = "0";
            }
            else if (tb1.SelectedIndex == 1)
            {
                showdata();
                code_txt.Focus();
            }
            else if (tb1.SelectedIndex == 2)
            {
                setDetail();
            }
        }
        #endregion
        void setDetail()
        {
            clearDetail();
            if (dg1.DataSource == null)
            {
                return;
            }
            if (dg1.CurrentRowIndex < 0)
            {
                return;
            }
            skuId = Convert.ToInt16(dtIns.Rows[dg1.CurrentRowIndex]["skuId"].ToString());
            goodsId = dtIns.Rows[dg1.CurrentRowIndex]["商品编码"].ToString();// dg1[dg1.CurrentRowIndex, 14].ToString();
            goodsName = dtIns.Rows[dg1.CurrentRowIndex]["商品"].ToString();// dg1[dg1.CurrentRowIndex, 0].ToString();
            batch = dtIns.Rows[dg1.CurrentRowIndex]["批号"].ToString();// dg1[dg1.CurrentRowIndex, 4].ToString();
            count = Convert.ToDecimal(dtIns.Rows[dg1.CurrentRowIndex]["数量"].ToString()); //Convert.ToDecimal(dg1[dg1.CurrentRowIndex, 1].ToString());
            goodsType = dtIns.Rows[dg1.CurrentRowIndex]["商品类别"].ToString(); //dg1[dg1.CurrentRowIndex, 6].ToString();
            lbGoods.Text = goodsName;
            lbBatch.Text = batch;
            lbCount.Text = count + "";
            lbType.Text = goodsType;
            id = Convert.ToInt32(dtIns.Rows[dg1.CurrentRowIndex]["id"].ToString());
          //  this.textBoxLoc.Text = "";
            textBoxLoc.Focus();
            textBoxNum.Text = count + "";
            labelproductDate.Text = dtIns.Rows[dg1.CurrentRowIndex]["生产日期"].ToString();
            this.labelValiddate .Text = dtIns.Rows[dg1.CurrentRowIndex]["失效日期"].ToString();
            this.labelManufacturer .Text = dtIns.Rows[dg1.CurrentRowIndex]["厂家"].ToString();
            labelbigcount .Text = dtIns.Rows[dg1.CurrentRowIndex]["大包装"].ToString();
            labelUnit.Text = dtIns.Rows[dg1.CurrentRowIndex]["单位"].ToString();
        }
        void clearDetail()
        {
            goodsId =
            batch = "";
            goodsName = "";
            count = 0m;
            lbGoods.Text = goodsId;
            lbBatch.Text = batch;
            lbCount.Text = 
            labelproductDate.Text =
            labelValiddate.Text = 
            labelManufacturer.Text =
             labelUnit.Text =
            labelbigcount.Text = "";
            skuId = 0;
        }
        #region 下架前,扫描货位显示货位信息
        private void locationid_txt_KeyUp(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
            {
                if (locationid_txt.Text.Length < 8)
                {
                    clear();
                    locationid_txt.Text = "";
                    MessageBox.Show("货位格式有误!");
                    return;
                }
                Cursor.Current = Cursors.WaitCursor;
                locationid = locationid_txt.Text;
                locationid_txt.Text = "";
                setLocationDetail();
                Cursor.Current = Cursors.Default;
            }
           
        }
        void setLocationDetail()
        {
            clear();
            
            Cursor.Current = System.Windows.Forms.Cursors.WaitCursor;
            try
            {
                dt = client.getStockLocation(locationid);
            }
            catch (Exception er)
            {
                Cursor.Current = System.Windows.Forms.Cursors.Default;
                MessageBox.Show("发生错误:" + er.Message);
                return;
            }
            Cursor.Current = System.Windows.Forms.Cursors.Default;
            SetValue(0, dt);
            count_txt.SelectAll();
            count_txt.Focus();
            count_txt.Enabled = oldCnt > 0;
           
        }
        #endregion
        #region 提交下架
        private void button1_Click(object sender, EventArgs e)
        {
            goodsDown();
        }
        void goodsDown()
        {
            try
            {
                if (!string.IsNullOrEmpty(lacationid_lab.Text.Trim()) && Convert.ToDouble(count_txt.Text.Trim()) > 0)
                {
                    try
                    {
                        count = Convert.ToDecimal(count_txt.Text);
                    }
                    catch
                    {
                        MessageBox.Show("请输入正确的数量");
                        count_txt.Focus();
                    }
                    if (count > oldCnt)
                    {
                        MessageBox.Show("下架数量【不能大于】库存数量");
                        return;
                    }
                    if (dtIns.Rows.Count >15)
                     {
                         MessageBox.Show("下架数量太多,请先处理已下架商品");
                         return;
                     }
                    //else if (BL.login.CheckMove3(lacationid_lab.Text) == false)
                    //{
                    //    MessageBox.Show("请上架完商品再下架!");
                    //    return;
                    //}
                    locationid = lacationid_lab.Text;
                     
                    enumRepResult res;
                 
                    Cursor.Current = System.Windows.Forms.Cursors.WaitCursor;
                    try
                    {
                       res = (enumRepResult)client.upDownStockLocation(locationid, goodsId,skuId, batch, 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(res.ToString());
                    setLocationDetail();
                    Cursor.Current = Cursors.Default;
                    locationid_txt.Focus();
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show("下架数量有误或货位无效 :" + ex.Message);
                count_txt.Focus();
            }
        }
        #endregion
        #region 执行上下架,i=0 下架 i=1 上架
        #endregion
        /// 
        /// 选择上架商品
        /// 
        /// 
        /// 
        private void code_txt_KeyUp(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
            {
                DataView dv = dtIns.DefaultView;
                dv.RowFilter = string.Format("barcode ={0}", code_txt.Text);
                if (dv.Count == 1)
                {
                    tb1.SelectedIndex = 2;
                    setDetail();
                    dv.RowFilter = "";
                    return;
                }
            }
            //if (code_txt.Text.Length < 8)
            //{
            //    code_txt.Text = "";
            //    //code_txt.Focus();
            //    MessageBox.Show("货位格式有误!");
            //    return;
            //}
        }
        #region 数据展示
        private void showdata()
        {
            //  dg1.DataSource = new DataTable();
         
            Cursor.Current = System.Windows.Forms.Cursors.WaitCursor;
            try
            {
               //dtIns = client.getDownStockList4Up(LoginInfo.UserId);
                dtIns = client.getDownStockList4Up(0); 
            }
            catch (Exception er)
            {
                Cursor.Current = System.Windows.Forms.Cursors.Default;
                MessageBox.Show("发生错误:" + er.Message);
            }
            Cursor.Current = System.Windows.Forms.Cursors.Default;
            DataGridTableStyle dgts = new DataGridTableStyle();
            dgts.MappingName = dtIns.TableName;// dt.TableName; // 必须用 ds.TableName, dt.TableSpace 不行!!!! 
            dg1.DataSource = dtIns;
            dg1.TableStyles.Clear();
            dg1.TableStyles.Add(dgts);
            dg1.TableStyles[0].GridColumnStyles["id"].Width = 0;
            dg1.TableStyles[0].GridColumnStyles["商品"].Width = 80;
            dg1.TableStyles[0].GridColumnStyles["批号"].Width = 70;
            dg1.TableStyles[0].GridColumnStyles["数量"].Width = 50;
            dg1.TableStyles[0].GridColumnStyles["原货位"].Width =80;
            dg1.TableStyles[0].GridColumnStyles["商品类别"].Width = 50;
            // dg1.TableStyles[0].GridColumnStyles["状态"].Width = 0;
            // dg1.TableStyles[0].GridColumnStyles["货位"].Width = 0;
            for (int i = 0; i < dtIns.Columns.Count; i++)
            {
                if (i >= 12) //如果某个条件满足就执行该列是否隐藏
                {
                    dg1.TableStyles[0].GridColumnStyles[i].Width = 0;
                }
            }
            //for (int i = 0; i < dtIns.Columns.Count; i++)
            //{ 
            //    if (i >= 12) //如果某个条件满足就执行该列是否隐藏
            //    {
            //        DataGridColumnStyle ColStyle = new DataGridTextBoxColumn();
            //        ColStyle.MappingName = dtIns.Columns[i].ColumnName.ToString();
            //        ColStyle.HeaderText = dtIns.Columns[i].ColumnName.ToString(); 
            //        ColStyle.Width = 0;//当宽度等于0的时候就可以隐藏这列 
            //        dgts.GridColumnStyles.Add(ColStyle);
            //    }
               
            //}    
            //dgts = null;
            //GC.Collect();
             
        }
        #endregion
        #region 双击dg1行,给界面控件赋值
        private void dg1_DoubleClick(object sender, EventArgs e)
        {
            if (dg1.CurrentRowIndex > -1)
            {
                setDetail();
                tb1.SelectedIndex = 2;
                /*
                id_lab.Text = dg1[dg1.CurrentCell.RowNumber, 19].ToString();
                goodsId = dg1[dg1.CurrentCell.RowNumber, 12].ToString();
                batch = dg1[dg1.CurrentCell.RowNumber, 5].ToString();
                product_date = dg1[dg1.CurrentCell.RowNumber, 6].ToString();
                save_date = dg1[dg1.CurrentCell.RowNumber, 7].ToString();
                warehouse = dg1[dg1.CurrentCell.RowNumber, 15].ToString();
                id = Convert.ToInt32( dg1[dg1.CurrentCell.RowNumber, 19].ToString());
              //  count0 = dg1[dg1.CurrentCell.RowNumber, 3].ToString();
                company = dg1[dg1.CurrentCell.RowNumber, 8].ToString();
                */
            }
        }
        #endregion
        private void btnOkay_Click(object sender, EventArgs e)
        {
        }
        private void btnComplete_Click(object sender, EventArgs e)
        {
        }
        private void textBox1_KeyUp(object sender, KeyEventArgs e)
        {
        }
        private void btnOkay_Click_1(object sender, EventArgs e)
        {
            goodsUp();
        }
        void goodsUp()
        {
            if (string.IsNullOrEmpty(goodsId))
            {
                textBoxLoc.Text = "";
                MessageBox.Show("请选择待上架记录");
                return;
            }
            string locatioin_newid = textBoxLoc.Text;
            if (string.IsNullOrEmpty(locatioin_newid))
            {
                MessageBox.Show("请选扫描要上架的货架号码");
                textBoxLoc.Focus();
                return;
            }
            try
            {
                count = Convert.ToDecimal(textBoxNum.Text);
            }
            catch
            {
                MessageBox.Show("请输入正确的数量");
                textBoxNum.Focus();
                return;
            }
            if (count > Convert.ToDecimal(lbCount.Text))
            {
                MessageBox.Show("上架数量【不能大于】下架数量");
                textBoxNum.Focus();
                return;
            }
            enumRepResult result;
            Cursor.Current = System.Windows.Forms.Cursors.WaitCursor;
            try
            {
                result = (enumRepResult)client.upDownStockLocation(locatioin_newid, goodsId, skuId, batch, count, (int)enumStockRecordType.移库上架, LoginInfo.UserId, LoginInfo.Token, id);
            }
            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.成功)
            { 
                clear();
                tb1.SelectedIndex = 1;
             /*
                if (Convert.ToDecimal(lbCount.Text) == 0) //商品全部上架
                {
                    clear();
                    tb1.SelectedIndex = 1;
                    return;
                }
                setDetail();
                textBoxLoc.Text = "";
                code_txt.Focus();
                return;
             */
            }
         textBoxLoc.SelectAll();
         textBoxLoc.Focus();
        }
        private void textBoxLoc_KeyUp(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
            {
                if (!string.IsNullOrEmpty(goodsId) && textBoxLoc.Text.Trim().Length > 6)
                {
                    textBoxNum.Focus();
                }
            }
        }
      
        private void count_txt_KeyUp(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
            {
                goodsDown();
            }
        }
        private void textBoxNum_KeyUp(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
            {
                goodsUp();
            }
        }
        /// 
        /// 上一页
        /// 
        /// 
        /// 
        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 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;
            if (dt.Rows.Count > 0)
            {
                lacationid_lab.Text = locationid;
                goodsId =dt.Rows[y]["goodsid"].ToString();
                name_lab.Text =dt.Rows[y]["goodsName"].ToString();
                spec_lab.Text =dt.Rows[y]["spec"].ToString();
                bigcout_lab.Text =dt.Rows[y]["bigcount"].ToString();
                PCompany_lab.Text =dt.Rows[y]["manufacturer"].ToString();
                oldcount_lab.Text =dt.Rows[y]["avcount"].ToString(); ;
                oldCnt = Convert.ToDecimal(oldcount_lab.Text);
                batch_lab.Text =dt.Rows[y]["batch"].ToString(); ;
                batch =dt.Rows[y]["batch"].ToString(); ;
                product_date_lab.Text =dt.Rows[y]["productDate"].ToString(); ;
                product_date = product_date_lab.Text;
                save_date_lab.Text =dt.Rows[y]["validdate"].ToString(); ;
                save_date = save_date_lab.Text;
                unit0_lab.Text =dt.Rows[y]["unit"].ToString(); ;
                // warehouse =dt.Rows[y]["库房"].ToString();;
            }
        }
    }
}