/// <summary>
 /// Logins the submit action.
 /// </summary>
 /// <param name="parameter">The parameter.</param>
 public void LoginSubmitAction(object parameter)
 {
     var passwordBox = parameter as System.Windows.Controls.PasswordBox;
     if (passwordBox != null)
     {
         var password = passwordBox.Password;
         var requestparameter = new LoginRequest()
         {
             UserName = _userName,
             Password = password
         };
         var request = new HttpRequestRemote(ApiUrl.Current.LoginUrl, requestparameter);
         var response = request.PostResponse();
         if (response == null)
         {
             UiAction.ModalShow("Net request fail");
         }
         else if (response.status == 1)
         {
             var setting = _configService.GetSettingByUserName(_userName);
             setting.Accesstoken = response.detail.accesstoken;
             setting.VendorName = response.detail.vendorname;
             setting.UserName = UserName; 
             setting.AutoLogin = _autologin;
             setting.UpdateTime = DateTime.Now;
             var lanviewmodel = ServiceLocator.Current.GetInstance<ConfigViewModel>();
             setting.Culture = lanviewmodel.LanguageSelectItem.CultureInfo.Name;
             _configService.UpdateSetting(setting);
             AppLocator.Contenxt = setting;
             if (null != CompleteCommand)
                 CompleteCommand();
         }
         else
         {
             UiAction.ModalShow(response.detail.message.ToString());
         }
     }
 }
 /// <summary>
 /// Hots the start action.
 /// </summary>
 public void HotStartAction()
 {
     _configViewModel.Init();
     var request = new HttpRequestRemote(ApiUrl.Current.HotStartUrl, null);
     request.PostResponse();
 }
        /// <summary>
        /// Handles the AllProduct event of the Sync control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="RoutedEventArgs"/> instance containing the event data.</param>
        private void Sync_AllProduct(object sender, RoutedEventArgs e)
        {
            var synctask = new Task(() =>
            {


                var itemindex = 0;
                var currentpage = 0;


                var requestparameter = new AllProductRequest()
                {
                    Accesstoken = AppLocator.Contenxt.Accesstoken,
                    PageIndex = currentpage
                };

                var request = new HttpRequestRemote(ApiUrl.Current.AllProductUrl, requestparameter);
                var response = request.PostResponse<AllProductResponse>();
                if (null == response)
                {
                    this.Dispatcher.Invoke(() => UiAction.ModalShow("Net request fail"));
                    return;
                }

                _productViewModel.ProgressValue = 0;
                _productViewModel.ProgressVisible = Visibility.Visible;

                var itemcount = response.detail.TotalCount;
                var totalpage = 2;

                if (response.status == 1)
                {
                    ServerToLoalProduct(response.detail.ExportProductModels, itemcount, ref itemindex);
                }

                ++currentpage;
                if (totalpage > 1 && currentpage < totalpage)
                {
                    for (int i = currentpage; i < totalpage; i++)
                    {
                        requestparameter = new AllProductRequest()
                        {
                            Accesstoken = AppLocator.Contenxt.Accesstoken,
                            PageIndex = currentpage
                        };
                        request = new HttpRequestRemote(ApiUrl.Current.AllProductUrl, requestparameter);
                        response = request.PostResponse<AllProductResponse>();
                        if (null != response && response.status == 1)
                        {
                            ServerToLoalProduct(response.detail.ExportProductModels, itemcount, ref itemindex);
                        }
                        ++currentpage;
                    }
                }

                var products = _productService.GetAllProducts();
                this.Dispatcher.Invoke(() => _productViewModel.UpdateObservProduct(products));
                _productViewModel.ProgressVisible = Visibility.Collapsed;
            });
            synctask.Start();
        }
        /// <summary>
        /// Loads the category.
        /// </summary>
        public void PrepareStart()
        {
            var request = new HttpRequestRemote(ApiUrl.Current.PrepareStartUrl, String.Empty);
            var response = request.PostResponse<PrepareStartResponse>();
            IList<CategoryItem> categoryItems = null;
            if (response != null && response.status == 1)
            {
                categoryItems = response.detail.CategoryItems;
                SpecificationAttributes = new ObservableCollection<SpeAttriubute>(response.detail.SpecificationAttribute);
                AppLocator.Contenxt.CategoryItems = JsonConvert.SerializeObject(categoryItems);
                AppLocator.Contenxt.SpecificationAttributes =
                    JsonConvert.SerializeObject(response.detail.SpecificationAttribute);
                _configService.UpdateSetting(AppLocator.Contenxt);
            }
            else
            {
                SpecificationAttributes =
                    new ObservableCollection<SpeAttriubute>(
                        JsonConvert.DeserializeObject<IList<SpeAttriubute>>(AppLocator.Contenxt.SpecificationAttributes));
                categoryItems = JsonConvert.DeserializeObject<IList<CategoryItem>>(AppLocator.Contenxt.CategoryItems);
            }

            var treeitems = PreparedTreeItems(categoryItems);
            CategoryItems = new ObservableCollection<TreeItem>(treeitems);

            var treeviewitems = PreparedTreeViewItem(categoryItems);
            var root = new TreeViewItem();
            root.Header = LocalizationSearch.LocalizedDependency(root, HeaderedItemsControl.HeaderProperty,
                "Product_CategoryManager");
            foreach (var tvi in treeviewitems)
            {
                root.Items.Add(tvi);
            }
            var rootlist = new List<TreeViewItem>();
            rootlist.Add(root);
            TreeViewItems = new ObservableCollection<TreeViewItem>(rootlist);
        }
        /// <summary>
        /// Products the upload.
        /// </summary>
        /// <param name="entity">The entity.</param>
        /// <returns></returns>
        public bool ProductSync(ProductModel entity)
        {
            var requestparameter = new FetchProductRequest()
            {
                Accesstoken = AppLocator.Contenxt.Accesstoken,
                ProductId = entity.ServiceId
            };

            var request = new HttpRequestRemote(ApiUrl.Current.FetchProductUrl, requestparameter);
            var response = request.PostResponse<FetchProductResponse>();
            if (response.status != 1)
                return false;

            FetchProductTempToProductModel(response.detail, entity);
            return true;
        }
        /// <summary>
        /// Products the upload.
        /// </summary>
        /// <param name="entity">The entity.</param>
        /// <returns></returns>
        public ProductResponse ProductUpload(ProductModel entity)
        {
            var setting = AppLocator.Contenxt;
            var requestparameter = new ProductRequest()
            {
                Accesstoken = setting.Accesstoken,
                ProductName = entity.ProductName,
                MerchantProductNumber = entity.MerchantProductNumber,
                CategoryId = entity.CategoryId,
                Price = entity.Price,
                Descript = entity.Descript,
                Gtin = entity.Gtin,
                Sku = entity.Sku,
                UswopId=entity.USwopid
            };

            if (!String.IsNullOrEmpty(entity.AvailableQuantity))
                requestparameter.AvailableQuantity =
                    JsonConvert.DeserializeObject<IList<PropertyItem>>(entity.AvailableQuantity);

            if (null != entity.ProductInventoryAttributeNameModel)
                requestparameter.SizeCategory = entity.ProductInventoryAttributeNameModel.Name;

            if (!String.IsNullOrEmpty(entity.SpecificationAttribute))
                requestparameter.SpecificationAttributes =
                    JsonConvert.DeserializeObject<IList<SpeAttriItem>>(entity.SpecificationAttribute);

            var images = GetProductImageByProdutId(entity.Id);
            var imgsresponse = ImageUpload(images);
            var imageuploadstatus = true;
            if (null == imgsresponse || imgsresponse.status == 0)
            {
                imageuploadstatus = false;
                //image upload fail
                entity.Status = Status.UploadFail;
                entity.ErrorMsg = "Image upload fail";
            }

            requestparameter.Images = String.Join(",",
                entity.ProductImages.Where(pi => pi.ServerId != 0).Select(pi => pi.ServerId));

            var request = new HttpRequestRemote(ApiUrl.Current.EditProductUrl, requestparameter);
            var response = request.PostResponse<ProductResponse>();
            if (response == null || response.status == 0)
            {
                Save();
                return new ProductResponse()
                {
                    status = 400
                };
            }
            if (response.status == 1 && response.detail.code == 1)
            {
                if (imageuploadstatus)
                    entity.Status = Status.UploadSuccess;
                entity.USwopid = response.detail.USwopid;
                entity.ServiceId = response.detail.ServiceId;
                Save();
            }
            else
            {
                entity.Status = Status.UploadFail;
                entity.ErrorMsg = string.Join("\r\n", response.detail.ErrorMsgList);
                Save();
            }
            return response;
        }