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;
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;
        
        string locationVolume = string.Empty;
        #endregion
        public LocationMgr()
        {
            setTitle();
            InitializeComponent();
            location_txt.Focus();
           // 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";
         
            }
        /// 
        /// 
        /// 
        /// 
        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";
            
            DataTable 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);
                return;
            } 
            Cursor.Current = System.Windows.Forms.Cursors.Default; 
            
            
            
            if (dt.Rows.Count > 0)
            {
                lacationid_lab.Text = dt.Rows[0]["locationId"].ToString();
                tag = Convert.ToInt16( dt.Rows[0]["locState"].ToString());  
                checkBox1.Checked = tag ==1;
                volType =Convert.ToInt16( dt.Rows[0]["volType"].ToString());
                goods_name.Text = dt.Rows[0]["goodsName"].ToString();
                spec.Text = dt.Rows[0]["spec"].ToString();
                count.Text = dt.Rows[0]["avCount"].ToString();
                batch.Text = dt.Rows[0]["batch"].ToString();
                product_date.Text = dt.Rows[0]["productdate"].ToString();
                ext_date.Text = dt.Rows[0]["validdate"].ToString();
                company_lab.Text = dt.Rows[0]["manufacturer"].ToString();
                unit.Text = dt.Rows[0]["unit"].ToString();
                goods_id = dt.Rows[0]["goodsid"].ToString();
                bigcount_lab.Text = dt.Rows[0]["bigcount"].ToString();
                if (dt.Rows[0]["VolType"].ToString() == "0")
                {
                    locationVolume = dt.Rows[0]["bulkmax"].ToString();
                }
                if (dt.Rows[0]["VolType"].ToString() == "-1")
                {
                    locationVolume = dt.Rows[0]["bulkmax1"].ToString();
                   
                }
                if (dt.Rows[0]["VolType"].ToString() == "1")
                {
                    locationVolume = dt.Rows[0]["batchMax1"].ToString();
                 
                }
                if (dt.Rows[0]["VolType"].ToString() == "2")
                {
                    locationVolume = dt.Rows[0]["batchMax2"].ToString();
                  
                }
                if (dt.Rows[0]["VolType"].ToString() == "3")
                {
                    locationVolume = dt.Rows[0]["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[0]["state"].ToString());
                       checkBox1.Checked = tag == 1;
                       textBox1.Enabled = false;
                        
                   }
                   else
                   {
                       lacationid_lab.Text = "";
                       MessageBox.Show("货位不存在!!!");
                       location_txt.Focus();
                       
                   }
                location_txt.Focus();
            }
            
        }
        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);
               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;
                }
            }
            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)){
                    tag = checkBox1.Checked ?1 :0;
                    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);
            }
        }
        private void textBox1_TextChanged(object sender, EventArgs e)
        {
        }
        private void label2_ParentChanged(object sender, EventArgs e)
        {
        }
       
    }
}