using System;
using System.Data;
using System.Windows;
using SSWMS.Common;

namespace SSWMS.Client
{
    public partial class MANAGE_MOVE_CELL : AvalonDock.DocumentContent
    {
        public MANAGE_MOVE_CELL()
        {
            InitializeComponent();
            DocumentContent_Loaded(null, null);
        }

        private void DocumentContent_Loaded(object sender, RoutedEventArgs e)
        {
            this.ucGridStorage.U_WindowName = "COMMON";
            this.ucGridStorage.U_XmlTableName = "V_STORAGE_LIST";
            this.ucGridStorage.U_TableName = "V_STORAGE_LIST";
            this.ucGridStorage.U_OrderField = "STORAGE_LIST_ID";
            this.ucGridStorage.U_Where = "1<>1";
            this.ucGridStorage.U_AllowOperatData = false;
            this.ucGridStorage.U_InitControl();
        }

        private void bStartDevice_Click(object sender, RoutedEventArgs e)
        {
            try
            {
                string sCellCode = string.Empty;
                DataView dv = null;
                if (WAREHOUSE_CELL_SELECT.GetSelectedCell(false, SystemCode.WAREHOUSE_CODE.Warehouse, out sCellCode, out dv))
                {
                    this.cbStartDevice.ItemsSource = dv;
                    this.cbStartDevice.SelectedValue = sCellCode;
                    this.ucGridStorage.U_Where = $"CELL_CODE='{sCellCode}'";
                    this.ucGridStorage.U_InitControl();
                }
            }
            catch (Exception ex)
            {
                MessageDialog.ShowException(ex);
            }
        }

        private void bEndDevice_Click(object sender, RoutedEventArgs e)
        {
            try
            {
                string sCellCode = string.Empty;
                DataView dv = null;
                if (WAREHOUSE_CELL_SELECT.GetSelectedCell(true, SystemCode.WAREHOUSE_CODE.Warehouse, out sCellCode, out dv))
                {
                    this.cbEndDevice.ItemsSource = dv;
                    this.cbEndDevice.SelectedValue = sCellCode;
                }
            }
            catch (Exception ex)
            {
                MessageDialog.ShowException(ex);
            }
        }

        private void bMoveCell_Click(object sender, RoutedEventArgs e)
        {
            try
            {
                if (this.cbStartDevice.SelectedIndex == -1)
                {
                    MessageDialog.ShowException("请选择起始位置");
                    return;
                }
                if (this.cbEndDevice.SelectedIndex == -1)
                {
                    MessageDialog.ShowException("请选择目标位置");
                    return;
                }

                if (MessageDialog.ShowDialog("确认执行立库移库"))
                {
                    string sResult = string.Empty;
                    bool bResult = WCFChannel._I_ManageService.ManageMoveCell(this.cbStartDevice.SelectedValue as string,
                        this.cbEndDevice.SelectedValue as string, MainApp._USER.USER_NAME, out sResult);
                    if (bResult)
                    {
                        this.bRefresh_Click(null, null);
                    }
                    MessageDialog.ShowResult(bResult, sResult);
                }
            }
            catch (Exception ex)
            {
                MessageDialog.ShowException(ex);
            }
        }

        private void bRefresh_Click(object sender, RoutedEventArgs e)
        {
            this.cbStartDevice.SelectedIndex = -1;
            this.cbEndDevice.SelectedIndex = -1;
            this.ucGridStorage.U_Where = "1<>1";
            this.ucGridStorage.U_InitControl();
        }
    }
}