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

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

        public LocationInfoAddOrUpdateView(string _titleText, LocationInfoModel _locationInfoModel = null)
        {
            InitializeComponent();

            this.DataContext = ViewModel;
            ViewModel.InitTypes();
            ViewModel.checkComboBox = this.Group;

            //绑定标题
            if (!string.IsNullOrEmpty(_titleText))
            {
                Title.Text = _titleText;
            }
            ViewModel.SetValues(_locationInfoModel);
        }


        private void btnOk_Click(object sender, RoutedEventArgs e)
        {
            try
            {
                #region 校验填写的值
                if (string.IsNullOrEmpty(ViewModel.LocationCode))
                {
                    Growl.Warning("请输入位置编码！");
                    txtLocationCode.Focus();
                    return;
                }
                #endregion

                #region 保存/修改值
                var IsAdd = Title.Text == "新增位置";
                var body = new AddLocaionInfoRequest<LocationInfoModel>()
                {
                    UserName = LocalStatic.CurrentUser,
                    DeviceType = LocalFile.Config.DeviceType,
                    LocationInfo = ViewModel.GetValues(),
                    AddOrUpdate = IsAdd ? AddOrUpdate.Add : AddOrUpdate.Update
                };
                var Result = ApiHelp.GetDataFromHttp<ResponseBase<object>>(LocalFile.Config.ApiIpHost + "locationInfo/addOrUpdateLocationInfo", body, "POST");
                if (Result != null && Result.Code == 200)
                {
                    if (IsAdd)
                        Growl.Success("添加成功！");
                    else
                        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();
        }
    }
}
