using HandyControl.Controls;
using SqlSugar;
using System;
using System.Windows;
using WCS.BLL.DbModels;
using WCS.Model;
using WCS.Model.ApiModel.MatDetailCurrentInfo;
using WCS.Model.ApiModel.StoreInfo;
using WCS.Model.ApiModel.User;
using 智慧物流软件系统.Api;
using 智慧物流软件系统.ViewModel;

namespace 智慧物流软件系统
{
    public partial class MatDetailCurrentInfoUpdateView : System.Windows.Window
    {
        public MatDetailCurrentInfoUpdateViewModel ViewModel = new MatDetailCurrentInfoUpdateViewModel();

        public MatDetailCurrentInfoUpdateView(string _titleText, MatDetailCurrentInfoModel _matDetailCurrentInfoModel = null)
        {
            InitializeComponent();

            this.DataContext = ViewModel;
            ViewModel.textBox = txtMatQty;
            ViewModel.SetValues(_matDetailCurrentInfoModel);
            //绑定标题
            Title.Text = _titleText;

           
        }


        private void btnOk_Click(object sender, RoutedEventArgs e)
        {
            try
            {
                #region 校验填写的值
                try
                {
                    txtMatQty.Text = txtMatQty.Text.Trim();
                    var qty = int.Parse(txtMatQty.Text);
                    if (qty < 0)
                    {
                        HandyControl.Controls.MessageBox.Show("数量请输入大于0的值！");
                        return;
                    }
                    else if (qty == 0)
                    {
                        HandyControl.Controls.MessageBox.Show("数量请输入大于0的值!\r\n如果需要设置为0请使用【删除】功能！");
                        return;
                    }
                }
                catch (Exception ex)
                {
                    HandyControl.Controls.MessageBox.Show("数量请输入有效的数字！");
                    return;
                }
                #endregion

                var url = LocalFile.Config.ApiIpHost + "matDetailCurrenInfo/updateMatDetailCurrentInfo";
                if (this.Title.Text == "修改绑定数据")
                {
                     url = LocalFile.Config.ApiIpHost + "batchBindMatDetail/updateMatDetailCurrentInfoForBind";
                }

                #region 保存/修改值
                var body = new AddLocaionInfoRequest<MatDetailCurrentInfoModel>()
                {
                    UserName = LocalStatic.CurrentUser,
                    DeviceType = LocalFile.Config.DeviceType,
                    LocationInfo = ViewModel.GetValues(),
                    AddOrUpdate = AddOrUpdate.Update
                };
                var Result = ApiHelp.GetDataFromHttp<ResponseBase<object>>(url, body, "POST");
                if (Result != null && Result.Code == 200)
                {

                    Growl.Success("修改成功！");
                    this.DialogResult = true;
                    this.Close();
                }
                else
                {
                    Growl.Error($"{Result?.Message?.ToString()}");
                }
                #endregion
            }
            //绑定数据
            catch (Exception ex)
            {
                Growl.Error($"操作异常：{ex.Message}");
                return;
            }
        }

        private void closeClick(object sender, RoutedEventArgs e)
        {
            this.DialogResult = false;
            this.Close();
        }

        private void txtMatQty_GotFocus(object sender, RoutedEventArgs e)
        {
            try
            {
                var textBox = sender as System.Windows.Controls.TextBox;
                if (textBox != null)
                {
                    textBox.CaretIndex = textBox.Text.Length;
                }
            }
            catch
            {
            }
        }
    }
}
