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
{
    /// <summary>
    /// 
    /// </summary>
    public partial class MANAGE_STOCK_IN_LFS : AvalonDock.DocumentContent
    {
        int strStockGoodsID ;         
        
        SiaSun.LMS.Model.MANAGE_TYPE mMANAGE_TYPE = null;

        public MANAGE_STOCK_IN_LFS()
        {
            InitializeComponent();
        }

        public MANAGE_STOCK_IN_LFS(string MANAGE_TYPE_CODE, int STOCK_GOODS_ID)
        {
            InitializeComponent();

            this.strStockGoodsID = STOCK_GOODS_ID;

            this.mMANAGE_TYPE = (Model.MANAGE_TYPE)MainApp._I_BaseService.GetModel("MANAGE_TYPE_SELECT_BY_MANAGE_TYPE_CODE",
                                                                                     MANAGE_TYPE_CODE).RequestObject;
        }

        private void DocumentContent_Loaded(object sender, RoutedEventArgs e)
        {

            try
            {
                if (mMANAGE_TYPE == null)
                {
                    MainApp._MessageDialog.ShowException("未找到任务类型");
                    return;
                }
                else
                {
                    ManagePositin_Init();

                }
            }
            catch (Exception ex)
            {
                MainApp._MessageDialog.ShowException(ex);
            }
        }

        private void ManagePositin_Init()
        {
            try
            {
                this.ucManagePosition.U_InitControl(this.mMANAGE_TYPE.MANAGE_TYPE_ID);
            }
            catch (Exception ex)
            {
                MainApp._MessageDialog.ShowException(ex);
            }
        }

        private void Refresh()
        {
            try
            {
                this.ucManagePosition.U_Refresh();

            }
            catch (Exception ex)
            {
                MainApp._MessageDialog.ShowException(ex);
            }
        }


        private void StorageListBind()
        {
            this.ucStockStorageGroup.U_WindowName = this.GetType().Name;
            this.ucStockStorageGroup.U_TableName = "V_STORAGE_LIST";
            this.ucStockStorageGroup.U_XmlTableName = "V_STORAGE_LIST";
            this.ucStockStorageGroup.U_TotalColumnName = "STORAGE_LIST_QUANTITY";
            this.ucStockStorageGroup.U_OrderField = "STORAGE_LIST_ID";
            this.ucStockStorageGroup.U_Where = string.Format(" goods_id = {0} ", this.strStockGoodsID);
            this.ucStockStorageGroup.U_AllowOperatData = false;
            this.ucStockStorageGroup.U_AllowChecked = false;
            this.ucStockStorageGroup.U_AllowShowPage = true;

            //拆分列属性
            this.ucStockStorageGroup.U_SplitPropertyType = "GOODS_TYPE";
            this.ucStockStorageGroup.U_SplitGroupColumn = "GOODS_TYPE_ID";
            this.ucStockStorageGroup.U_SplitGroupHeader = "GOODS_TYPE.GOODS_TYPE_NAME";
            this.ucStockStorageGroup.U_SplitPropertyColumn = "GOODS_PROPERTY";

            this.ucStockStorageGroup.U_InitControl();
        }

        /// <summary>
        /// 按钮事件
        /// </summary>
        private void WrapPanel_Click(object sender, RoutedEventArgs e)
        {
            Button btn = e.OriginalSource as Button;
            if (btn != null)
            {
                switch (btn.Name)
                {
                    case "btnCreateTask":
                        this.CreateStockTask();
                        break;
                    case "btnRefresh":
                        this.Refresh();
                        break;
                }
            }
        }

        private void CreateStockTask()
        {
            bool boolResult = true;

            bool boolBatchResult = true;

            string strResult = string.Empty;

            string sBatchResult = string.Empty;


            if (!boolResult)
            {
                MainApp._MessageDialog.Show(false, strResult);
                return;
            }

            //判断输送位置是否合法
            boolResult = this.ucManagePosition.U_CHECK_WAREHOUSE();

            if (!boolResult)
            {
                return;
            }


            if (MainApp._MessageDialog.ShowDialog("您确认创建任务?") == Sid.Windows.Controls.TaskDialogResult.Cancel)
                return;

            try
            {
                int intStartPositionID = this.ucManagePosition.U_START_POSITION_ID;

                List<Model.MANAGE_LIST> listMANAGE_LIST = new List<Model.MANAGE_LIST>();

                Model.MANAGE_LIST mMANAGE_LIST = new Model.MANAGE_LIST();

                mMANAGE_LIST.GOODS_ID = this.strStockGoodsID;

                mMANAGE_LIST.MANAGE_LIST_QUANTITY = 1;

                listMANAGE_LIST.Add(mMANAGE_LIST);



                    SiaSun.LMS.Model.MANAGE_MAIN mMANAGE_MAIN = new Model.MANAGE_MAIN();

                    mMANAGE_MAIN.PLAN_ID = 0;

                    mMANAGE_MAIN.MANAGE_TYPE_CODE = mMANAGE_TYPE.MANAGE_TYPE_CODE.TrimEnd();

                    mMANAGE_MAIN.STOCK_BARCODE = string.Empty;

                    mMANAGE_MAIN.CELL_MODEL = this.ucManagePosition.U_CELL_MODEL;

                    mMANAGE_MAIN.START_CELL_ID = this.ucManagePosition.U_START_POSITION_ID;

                    mMANAGE_MAIN.END_CELL_ID =  this.ucManagePosition.U_END_POSITION_ID;

                    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.FULL_FLAG = this.ucManagePosition.U_OCCUPY_PERCENT;

                    mMANAGE_MAIN.MANAGE_LEVEL = string.Empty;

                    mMANAGE_MAIN.MANAGE_REMARK = string.Empty;

                    boolResult = MainApp._I_BaseService.Invoke(mMANAGE_TYPE.MANAGE_TYPE_CLASS.TrimEnd(),
                                                                 "ManageCreate",
                                                                  new object[] { mMANAGE_MAIN,
                                                                                 listMANAGE_LIST,                                                                                
                                                                                 true, 
                                                                                 this.ucManagePosition.U_CheckStockExistStorage,
                                                                                 this.ucManagePosition.U_AutoCompleteTask,
                                                                                 this.ucManagePosition.U_AutoDownloadControlTask
                                                                                 },
                                                                  out strResult);

                    if (!boolResult)
                    {
                        MainApp._MessageDialog.Show(boolResult, strResult);

                        boolBatchResult = false;


                    }
                    else
                    {
                        sBatchResult += mMANAGE_MAIN.STOCK_BARCODE + "\n";

                    }




            }
            catch (Exception ex)
            {
                boolResult = false;

                strResult = ex.Message;
            }

            if (sBatchResult != string.Empty)
                MainApp._MessageDialog.Show(true, sBatchResult + "\n" + "任务下达成功");


            if (boolBatchResult)
                this.Refresh();

        }



    }
}