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>
    /// MANAGE_MOVE.xaml 的交互逻辑
    /// </summary>
    public partial class MANAGE_ADJUST : AvalonDock.DocumentContent
    {
        Model.MANAGE_TYPE mMANAGE_TYPE = null;

        /// <summary>
        /// 构造函数
        /// </summary>
        public MANAGE_ADJUST(string MANAGE_TYPE_CODE)
        {
            InitializeComponent();

            this.mMANAGE_TYPE = (Model.MANAGE_TYPE)MainApp._I_BaseService.GetModel("MANAGE_TYPE_SELECT_BY_MANAGE_TYPE_CODE", MANAGE_TYPE_CODE).RequestObject;
        }
        /// <summary>
        /// 窗体加载
        /// </summary>
        private void DocumentContent_Loaded(object sender, RoutedEventArgs e)
        {
            this.InitManagePosotion();

            this.ucManagePosition.U_StockBarcodeKeyDown += new UC.ucManagePosition.U_StockBarcodeKeyDownHandler(ucManagePosition_U_StockBarcodeKeyDown);
        }

        void ucManagePosition_U_StockBarcodeKeyDown()
        {
            //校验托盘条码是否合法
            if (!SiaSun.LMS.Common.RegexValid.GetCodeCheck(this.ucManagePosition.U_STOCK_BARCODE, "StockCodeCheck"))
            {
                MainApp._MessageDialog.Show(Enum.MessageConverter.CheckStockBarCode, this.ucManagePosition.U_STOCK_BARCODE);
                return;
            }
            //显示库存
            StorageListBind();
        }

        /// <summary>
        /// 初始化输送位置控件
        /// </summary>
        private void InitManagePosotion()
        {
            //设置输送任务控件参数
            this.ucManagePosition.U_AllowAutoEndPostion = true;

            this.ucManagePosition.U_AllowAutoStartPostion = false;
            //初始化
            this.ucManagePosition.U_InitControl(mMANAGE_TYPE.MANAGE_TYPE_ID);

        }

        /// <summary>
        /// 库存明细绑定
        /// </summary>
        private void StorageListBind()
        {
            this.gridStorageList.U_WindowName = this.GetType().Name;
            this.gridStorageList.U_TableName = "V_STORAGE_LIST";
            this.gridStorageList.U_XmlTableName = "V_STORAGE_LIST";
            this.gridStorageList.U_TotalColumnName = "STORAGE_LIST_QUANTITY";
            this.gridStorageList.U_OrderField = "STORAGE_LIST_ID";
            this.gridStorageList.U_Where = string.Format("STOCK_BARCODE='{0}' AND AREA_TYPE= 'XuNiKu'", this.ucManagePosition.U_STOCK_BARCODE) ;
            this.gridStorageList.U_AllowOperatData = false;
            this.gridStorageList.U_AllowChecked = true;
            this.gridStorageList.U_AllowShowPage = false;

            //拆分列属性
            this.gridStorageList.U_SplitPropertyType = "GOODS_TYPE";
            this.gridStorageList.U_SplitGroupColumn = "GOODS_TYPE_ID";
            this.gridStorageList.U_SplitPropertyColumn = "GOODS_PROPERTY";
            this.gridStorageList.U_SplitGroupHeader = "GOODS_TYPE.GOODS_TYPE_NAME";
            this.gridStorageList.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 "btnConfirm":
                        this.CreateTask();
                        break;
                    case "btnRefresh":
                        this.Refresh();
                        break;
                }
            }
        }

        /// <summary>
        /// 创建输送任务
        /// </summary>
        private void CreateTask()
        {
            string strResult = string.Empty;

            try
            {
                MainWindow.mainWin.Cursor = Cursors.Wait;

                //结束当前编辑操作
                this.gridStorageList.U_EndCurrentEdit();

                DataRowView[] dataRowItems =  this.gridStorageList.U_GetCheckedDataRows();

                DataTable tableAdjust= this.gridStorageList.U_DataSource.Copy();

                tableAdjust.Clear();

                foreach (DataRowView dr in dataRowItems)
                {
                    tableAdjust.ImportRow(dr.Row);
                }

                foreach (DataRow dr in tableAdjust.Rows)
                {
                    dr["MANAGE_LIST_QUANTITY"] = Convert.ToDecimal(dr["MANAGE_LIST_QUANTITY"]) - Convert.ToDecimal(dr["STORAGE_LIST_QUANTITY"]);
                }

                //根据数据源获得数据列表
                List<Model.MANAGE_LIST> listMANAGE_LIST = new SiaSun.LMS.Common.CloneObjectValues().GetListFromDataTable<Model.MANAGE_LIST>(tableAdjust, null);

                var list = from v in listMANAGE_LIST
                           where v.MANAGE_LIST_QUANTITY != 0
                           select v;

                listMANAGE_LIST = list.ToList<Model.MANAGE_LIST>();

                #region     ------校验合法性

                //判断是否填写数据
                if (listMANAGE_LIST.Count==0)
                {
                    MainApp._MessageDialog.Show(Enum.MessageConverter.Input);
                    return;
                }


                //校验填写仓库信息是否合法
                if (!this.ucManagePosition.U_CHECK_WAREHOUSE())
                    return;

                #endregion



                //提示确认
                if (MainApp._MessageDialog.ShowDialog(Enum.MessageConverter.ConfirmAssembly, this.ucManagePosition.U_STOCK_BARCODE) == Sid.Windows.Controls.TaskDialogResult.Ok)
                {
                    SiaSun.LMS.Model.MANAGE_MAIN mMANAGE_MAIN = new Model.MANAGE_MAIN();

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

                    mMANAGE_MAIN.STOCK_BARCODE = this.ucManagePosition.U_STOCK_BARCODE;

                    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.MANAGE_LEVEL = string.Empty;

                    mMANAGE_MAIN.MANAGE_REMARK = string.Empty;

                    bool bResult = false;
                    //创建托盘任务

                    bResult = 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 (bResult)
                    {
                        this.Refresh();
                    }

                    MainApp._MessageDialog.ShowResult(bResult, strResult);

                }
            }
            catch (Exception ex)
            {
                MainApp._MessageDialog.ShowException(ex);
            }
            finally
            {
                MainWindow.mainWin.Cursor = Cursors.Arrow;
            }
        }

        /// <summary>
        /// 刷新
        /// </summary>
        private void Refresh()
        {
            this.StorageListBind();

            this.ucManagePosition.U_Update();
        }


    }
}