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 DeiNiu.Wms.CE.Util;
namespace DeiNiu.Wms.CE
{
    public partial class LocationMgr : basicForm 
    {
        //StockLocationServiceClient client { get; set; }
        #region 自定义
      
        string goods_id = string.Empty;
        string null_locationid = string.Empty;
        int volType = -100;
        int tag=-1;
        decimal volCnt =0m;
        DataTable dt;
        int ye = 0;
        int index = 0;
        string locationVolume = string.Empty;
        #endregion
        public LocationMgr()
        {
            setTitle();
            InitializeComponent();
            location_txt.Focus();
            initialStatusCom();
           // client = new StockLocationServiceClient(WcfHelp.getBasicBinding(), WcfHelp.getEndpoint("StockLocationService"));
        }
        /// 
        /// 扫描货位触发enter事件,获取货位信息
        /// 0,锁定 1,解锁 2,修改库容 3,初始化界面
        /// 
        /// 
        /// 
        private void location_txt_KeyUp(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
            {
                lacationid_lab.Text = "";
                string str = location_txt.Text.Trim();
                if (string.IsNullOrEmpty(str) || str.Length < 8)
                {
                    MessageBox.Show("货位码无效!");
                    location_txt.Focus();
                    return;
                }
                lacationid_lab.Text = location_txt.Text.Trim();
                location_txt.Text = "";
                showeq(str);
               // button1.Enabled = warehousetype >=-100 ? false : true;
            }
        }
        /// 
        /// 设置货位
        /// 
        /// 
        /// 
     
        /// 
        /// 更改库容量
        /// 
        /// 
        /// 
        private void button1_Click(object sender, EventArgs e)
        {
         
            try
            {
                
                //if (warehousetype == -1)
                //{
                //    MessageBox.Show("空货位不能修改库容量!");
                //    return;
                //}
                if (Convert.ToDouble(textBox1.Text) > 0)
                {
                    UpdateVolume();
                }
                else
                    MessageBox.Show("请输入数字!");
                textBox1.Focus();
            }
            catch
            {
                MessageBox.Show("格式不对!");
            }
        }
        /// 
        /// 清除界面数据
        /// 
        private void clear()
        {
            null_locationid = string.Empty;
          goods_name.Text =
            spec.Text =
            count.Text =
            batch.Text =
            product_date.Text =
            ext_date.Text =
            company_lab.Text =
            unit.Text = "";
            goods_id = string.Empty;
            bigcount_lab.Text = "200";
            textBox1.Text = "0";
            label12.Text = "0";
            ye = 0;
            index = 0;
         
            }
        /// 
        /// 
        /// 
        /// 
        private void showeq(string location_id)//获取货位上商品的基本信息
        {
            if (string.IsNullOrEmpty(bigcount_lab.Text.Trim()))
                bigcount_lab.Text = "200";
            if (string.IsNullOrEmpty(textBox1.Text.Trim()))
                textBox1.Text = "0";
            
            dt = new DataTable();
            Cursor.Current = System.Windows.Forms.Cursors.WaitCursor;
            try
            {
                dt = client.getStockLocation(location_id); //查询货位上商品的基本信息
            }
            catch (Exception er)
            {
                Cursor.Current = System.Windows.Forms.Cursors.Default;
                //MessageBox.Show("发生错误:" + er.Message);
                showErrorMsg(er);
                return;
            } 
            Cursor.Current = System.Windows.Forms.Cursors.Default;
          
            SetValue(0, dt);
            
        }
        private void UpdateStatic()
        {           
           
            bool succeed = false; 
           Cursor.Current = System.Windows.Forms.Cursors.WaitCursor;
           try
           {
               succeed = client.upLocationStatus(lacationid_lab.Text, tag, 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;  
           if (succeed)
            {
               /* if (tag == 1)
                {
                    MessageBox.Show("货位锁定成功!");
                    location_txt.Focus();
                    return;
                }
                else
                {
                    MessageBox.Show("货位解锁成功!");
                    location_txt.Focus();
                    return;
                }
               */
                MessageBox.Show("货位状态更新成功");
            }
            else
            {
                MessageBox.Show("更改状态失败");
                location_txt.Focus();
                return;
            }
        }
        private void UpdateVolume()
        {
            if (string.IsNullOrEmpty(bigcount_lab.Text.Trim()))
                bigcount_lab.Text = "200";
            if (string.IsNullOrEmpty(textBox1.Text.Trim()))
                textBox1.Text = "0";
            Cursor.Current = System.Windows.Forms.Cursors.WaitCursor; 
                decimal sl = Convert.ToDecimal(textBox1.Text.ToString()) * Convert.ToDecimal(bigcount_lab.Text.ToString());
             
                bool succeed = false;
                Cursor.Current = System.Windows.Forms.Cursors.WaitCursor;
                try
                {
                    succeed = client.updateGoodsLocationVolume(goods_id, volType, sl, LoginInfo.UserId, LoginInfo.Token);
                }
                catch (Exception er)
                {
                    Cursor.Current = System.Windows.Forms.Cursors.Default;
                    MessageBox.Show("发生错误:" + er.Message);
                    return;
                }
                Cursor.Current = System.Windows.Forms.Cursors.Default;  
            
            if (succeed)
                {
                    MessageBox.Show("更改库容成功");
                    label12.Text = sl.ToString();
                    location_txt.Focus();
                    volCnt = Convert.ToDecimal(textBox1.Text.ToString());
                    return;
                }
                else
                {
                    MessageBox.Show("更改库容失败!!!");
                    textBox1.Focus();
                    return;
                }
            
            
           
        }
        private void label15_ParentChanged(object sender, EventArgs e)
        {
        }
 
        private void button2_Click(object sender, EventArgs e)
        {
            if (lacationid_lab.Text == "")
            {
                MessageBox.Show("请扫描货位!!");
                location_txt.Focus();
                return;
            }
            try
            {
               
              //  if(checkBox1.Checked != (tag==1)){
                if (comStatus.SelectedIndex!= tag)
                {
                    tag = comStatus.SelectedIndex;
                    UpdateStatic();
                }
               
                if(volCnt ==-100 || !textBox1.Enabled){
                    return;
                }
                decimal newVolCnt = 0m;
                try
                {
                    newVolCnt = Convert.ToDecimal(textBox1.Text.ToString());
                    if (newVolCnt <= 0)
                    {
                        MessageBox.Show("请输入正确的数量");
                        textBox1.Focus();
                        return;
                    }
                }
                catch
                {
                    MessageBox.Show("请输入正确的数量");
                    textBox1.Focus();
                    return;
                }
                if (volCnt != newVolCnt && newVolCnt > 0)
                {
                    UpdateVolume();
                   
                }
              
            }
            catch(Exception ex)
            {
                //MessageBox.Show(ex.Message); 
                showErrorMsg(ex);
            }
        }
        private void textBox1_TextChanged(object sender, EventArgs e)
        {
        }
        private void label2_ParentChanged(object sender, EventArgs e)
        {
        }
       
     
        /// 
        /// 上一页
        /// 
        /// 
        /// 
        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)
        {
            Cursor.Current = System.Windows.Forms.Cursors.WaitCursor; 
            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 = dt.Rows[y]["locationId"].ToString();
                tag = Convert.ToInt16(dt.Rows[y]["locState"].ToString());
                checkBox1.Checked = tag == 1;
                comStatus.SelectedIndex = tag;
                volType = Convert.ToInt16(dt.Rows[y]["volType"].ToString());
                goods_name.Text = dt.Rows[y]["goodsName"].ToString();
                spec.Text = dt.Rows[y]["spec"].ToString();
                count.Text = dt.Rows[y]["avCount"].ToString();
                batch.Text = dt.Rows[y]["batch"].ToString();
                product_date.Text = dt.Rows[y]["productdate"].ToString();
                ext_date.Text = dt.Rows[y]["validdate"].ToString();
                company_lab.Text = dt.Rows[y]["manufacturer"].ToString();
                unit.Text = dt.Rows[y]["unit"].ToString();
                goods_id = dt.Rows[y]["goodsid"].ToString();
                bigcount_lab.Text = dt.Rows[y]["bigcount"].ToString();
                if (dt.Rows[y]["VolType"].ToString() == "0")
                {
                    locationVolume = dt.Rows[y]["bulkmax"].ToString();
                }
                if (dt.Rows[y]["VolType"].ToString() == "-1")
                {
                    locationVolume = dt.Rows[y]["bulkmax1"].ToString();
                }
                if (dt.Rows[y]["VolType"].ToString() == "1")
                {
                    locationVolume = dt.Rows[y]["batchMax1"].ToString();
                }
                if (dt.Rows[y]["VolType"].ToString() == "2")
                {
                    locationVolume = dt.Rows[y]["batchMax2"].ToString();
                }
                if (dt.Rows[y]["VolType"].ToString() == "3")
                {
                    locationVolume = dt.Rows[y]["batchMax3"].ToString();
                }
                if (string.IsNullOrEmpty(locationVolume) == true)
                {
                    locationVolume = "0";
                }
                volCnt = Convert.ToDecimal(locationVolume) / Convert.ToDecimal(bigcount_lab.Text.Trim());
                textBox1.Text = (volCnt).ToString();
                label12.Text = locationVolume;
                textBox1.Enabled = true;
            }
            else
            {
                /*
                dt = new DataTable();
                Cursor.Current = System.Windows.Forms.Cursors.WaitCursor;
                try
                {
                    dt = client.getLocation(location_id); //查空货位信息
                }
                catch (Exception er)
                {
                    Cursor.Current = System.Windows.Forms.Cursors.Default;
                    MessageBox.Show("发生错误:" + er.Message);
                    return;
                }
                Cursor.Current = System.Windows.Forms.Cursors.Default;
                */
                clear();
                volType = -100;
                if (dt.Rows.Count > 0)
                {
                    tag = Convert.ToInt16(dt.Rows[y]["state"].ToString());
                    checkBox1.Checked = tag == 1;
                    textBox1.Enabled = false;
                }
                else
                {
                    lacationid_lab.Text = "";
                    MessageBox.Show("货位不存在!!!");
                    location_txt.Focus();
                }
                location_txt.Focus();
                
            }
            Cursor.Current = System.Windows.Forms.Cursors.Default; 
        }
        private void initialStatusCom()
        {
            Dictionary dic = new Dictionary();
            dic[0] = ((enumStockLocationState)0).ToString();
            dic[1] = ((enumStockLocationState)1).ToString(); ;
            dic[2] = ((enumStockLocationState)2).ToString(); ;
            dic[3] = ((enumStockLocationState)3).ToString();
            dic[4] = ((enumStockLocationState)4).ToString();
            Utils.initialComboBoxs(comStatus, dic);
        }
       
    }
}