using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
using System.Data;

namespace SiaSun.LMS.WPFClient.MANAGE
{
    public partial class MANAGE_BACK : AvalonDock.DocumentContent
    {
        public MANAGE_BACK()
        {
            InitializeComponent();
        }

        private void BtnSave_Click(object sender, RoutedEventArgs e)
        {
            bool bResult = true;
            string sResult = string.Empty;
            if (string.IsNullOrEmpty(this.tbStock_barcode.Text))
            {
                MessageBox.Show("托盘条码为空");
                return;
            }
            if (string.IsNullOrEmpty(this.tbGoods_code.Text))
            {
                MessageBox.Show("物料条码为空");
                return;
            }
            if (string.IsNullOrEmpty(this.tbStation.Text))
            {
                MessageBox.Show("站台号为空");
                return;
            }
            if (!this.tbStation.Text.StartsWith("140"))
            {
                MessageBox.Show("站台号错误");
                return;
            }
            int iZupanCellID = 0;
            DataTable dtZupan = MainApp._I_BaseService.GetList(string.Format("select CELL_ID from WH_CELL where CELL_CODE='{0}'", this.tbStation.Text));
            if (dtZupan.Rows.Count == 0)
            {
                MessageBox.Show("未找到货位");
                return;
            }
            else
            {
                iZupanCellID = Convert.ToInt32(dtZupan.Rows[0]["CELL_ID"]);
            }
            int iCellID = 0;
            DataTable dt = MainApp._I_BaseService.GetList(string.Format(@"select CELL_ID from WH_CELL where CELL_CODE='{0}'", this.tbStation.Text));
            if (dt.Rows.Count == 0)
            {
                MessageBox.Show("站台号不存在");
                return;
            }
            else
            {
                iCellID = Convert.ToInt32(dt.Rows[0]["CELL_ID"]);
            }


            if (!Common.RegexValid.GetCodeCheck(this.tbStock_barcode.Text, "StockCodeCheck"))
            {
                MessageBox.Show("托盘条码规则不正确");
                return;
            }
            string sGoodsBarcode = this.tbGoods_code.Text.Trim();
            string[] strArr = sGoodsBarcode.Split('-');
            if (strArr.Length != 7)
            {
                MessageBox.Show("物料条码规则不正确");
                return;
            }
            DataTable gOODS_MAIN = MainApp._I_BaseService.GetList(string.Format(@"select goods_id from goods_main where goods_code = '{0}'", strArr[1]));
            if (gOODS_MAIN == null || gOODS_MAIN.Rows.Count < 1)
            {
                MessageBox.Show(string.Format(@"{0} 物料信息不存在", strArr[1]));
                return;
            }
            Model.MANAGE_MAIN mANAGE_MAIN = new Model.MANAGE_MAIN();
            Model.MANAGE_LIST mANAGE_LIST = new Model.MANAGE_LIST();
            List<SiaSun.LMS.Model.MANAGE_LIST> lsMANAGE_LIST = new List<Model.MANAGE_LIST>();
            mANAGE_MAIN.STOCK_BARCODE = this.tbStock_barcode.Text;
            mANAGE_MAIN.MANAGE_TYPE_CODE = "ManageIn";
            mANAGE_MAIN.MANAGE_OPERATOR = MainApp._USER.USER_NAME;
            mANAGE_MAIN.MANAGE_BEGIN_TIME = Common.StringUtil.GetDateTime();
            mANAGE_MAIN.MANAGE_STATUS = "Waiting";
            mANAGE_MAIN.START_CELL_ID = iZupanCellID;
            mANAGE_MAIN.END_CELL_ID = 0;
            mANAGE_LIST.GOODS_ID = Convert.ToInt32(gOODS_MAIN.Rows[0]["GOODS_ID"].ToString());
            mANAGE_LIST.MANAGE_ID = 0;
            mANAGE_LIST.MANAGE_LIST_QUANTITY = Convert.ToDecimal(strArr[5]);
            mANAGE_LIST.PLAN_LIST_ID = 0;
            mANAGE_LIST.GOODS_PROPERTY1 = sGoodsBarcode;
            mANAGE_LIST.GOODS_PROPERTY2 = strArr[0] + "-" + strArr[1] + "-" + strArr[2] + "-" + strArr[4];
            mANAGE_LIST.GOODS_PROPERTY3 = strArr[2];
            //mANAGE_LIST.GOODS_PROPERTY8 = "0";
            mANAGE_LIST.GOODS_PROPERTY9 = "1";
            mANAGE_LIST.STORAGE_LIST_ID = 0;
            lsMANAGE_LIST.Add(mANAGE_LIST);
            bResult = MainApp._I_BaseService.Invoke("ManageIn", "ManageCreate",
                new object[] {
                    mANAGE_MAIN,
                    lsMANAGE_LIST,
                    true,//事务
                    true,//检查库存
                    true,//是否完成
                    false//是否下达任务
                },out sResult);

            if (!bResult)
            {
                MessageBox.Show(sResult);
                return;
            }
            SiaSun.LMS.Model.MANAGE_MAIN mMANAGE_MAIN = new Model.MANAGE_MAIN();
            mMANAGE_MAIN.MANAGE_TYPE_CODE = "ManageUp";
            mMANAGE_MAIN.STOCK_BARCODE = this.tbStock_barcode.Text;
            mMANAGE_MAIN.CELL_MODEL = "low";
            mMANAGE_MAIN.START_CELL_ID = iCellID;
            mMANAGE_MAIN.END_CELL_ID = 0;
            mMANAGE_MAIN.MANAGE_OPERATOR = MainApp._USER.USER_NAME;
            mMANAGE_MAIN.MANAGE_BEGIN_TIME = SiaSun.LMS.Common.StringUtil.GetDateTime();
            mMANAGE_MAIN.MANAGE_STATUS = SiaSun.LMS.Enum.MANAGE_STATUS.WaitingSend.ToString();
            mMANAGE_MAIN.MANAGE_LEVEL = string.Empty;
            mMANAGE_MAIN.MANAGE_REMARK = string.Empty;
            bResult = MainApp._I_BaseService.Invoke("ManageUp",
                "ManageCreate",
                new object[] {mMANAGE_MAIN,
                true,
                true,
                false},
                out sResult);

            if (bResult)
            {
                MessageBox.Show("回库成功");
                BtnClear_Click(null, null);
                return;
            }
            else
            {
                MessageBox.Show(sResult);
                return;
            }
        }

        private void BtnClear_Click(object sender, RoutedEventArgs e)
        {
            this.tbStock_barcode.Text = string.Empty;
            this.tbGoods_code.Text = string.Empty;
            this.tbStation.Text = string.Empty;
            this.tbStock_barcode.Focus();
        }

        private void tbStock_barcode_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.Key == Key.Enter)
            {
                if (string.IsNullOrWhiteSpace(this.tbStock_barcode.Text))
                {
                    return;
                }
                this.tbStock_barcode.Text = this.tbStock_barcode.Text.ToUpper();
                if (string.IsNullOrWhiteSpace(this.tbGoods_code.Text))
                {
                    this.tbGoods_code.Focus();
                }
                else if (string.IsNullOrWhiteSpace(this.tbStation.Text))
                {
                    this.tbStation.Focus();
                }
                else
                {
                    BtnSave_Click(null, null);
                }
            }
        }

        private void tbGoods_code_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.Key == Key.Enter)
            {
                if (string.IsNullOrWhiteSpace(this.tbGoods_code.Text))
                {
                    return;
                }
                this.tbGoods_code.Text = this.tbGoods_code.Text.ToUpper();
                if (string.IsNullOrWhiteSpace(this.tbStation.Text))
                {
                    this.tbStation.Focus();
                }
                else
                {
                    BtnSave_Click(null, null);
                }
            }
        }

        private void tbStation_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.Key == Key.Enter)
            {
                if (string.IsNullOrWhiteSpace(this.tbStation.Text))
                {
                    return;
                }
                else
                {
                    BtnSave_Click(null, null);
                }
            }
        }
    }
}