public ActionResult DeleteConfirmed(long id)
        {
            AssetPhoto assetPhoto = db.AssetPhotos.Find(id);

            db.AssetPhotos.Remove(assetPhoto);
            db.SaveChanges();
            return(Json("Deleted photo!"));
        }
        public ActionResult Create([Bind(Include = "ID,AssetID,AssetPhoto1")] AssetPhoto assetPhoto)
        {
            if (ModelState.IsValid)
            {
                var findPhoto = (from x in db.AssetPhotos
                                 where x.AssetID == assetPhoto.AssetID && x.AssetPhoto1 == assetPhoto.AssetPhoto1
                                 select x).FirstOrDefault();
                if (findPhoto != null)
                {
                    return(Json("Photo(s) saved successfully!"));
                }
                else
                {
                    db.AssetPhotos.Add(assetPhoto);
                    db.SaveChanges();
                    return(Json("Photo(s) saved successfully!"));
                }
            }


            return(Json("Unable to save photo(s)! Please try again later!"));
        }
        private async void btnSubmit_Clicked(object sender, EventArgs e)
        {
            if (pLocation.SelectedItem == null || pAccountable.SelectedItem == null || pAssetGroup.SelectedItem == null || string.IsNullOrWhiteSpace(entryAssetName.Text))
            {
                await DisplayAlert("Submit Changes", "Please ensure all fields are filled!", "Ok");
            }
            else
            {
                var client = new WebApi();
                if (_assetID != 0)
                {
                    _asset.EmployeeID = (from x in _employeeList
                                         where x.FirstName + " " + x.LastName == pAccountable.SelectedItem.ToString()
                                         select x.ID).FirstOrDefault();
                    if (string.IsNullOrWhiteSpace(editorDescription.Text))
                    {
                        _asset.Description = " ";
                    }
                    else
                    {
                        _asset.Description = editorDescription.Text;
                    }
                    _asset.WarrantyDate = dpWarranty.Date;
                    var jsonData = JsonConvert.SerializeObject(_asset);
                    var response = await client.PostAsync(jsonData, "Assets/Edit");

                    if (response == "\"Successfully edited Asset!\"")
                    {
                        await DisplayAlert("Edit Asset", "Successfully edited Asset!", "Ok");

                        var boolCheck = true;
                        foreach (var item in _assetPhotoList)
                        {
                            byte[] imageArray;
                            var    imageContent = await GetStreamFromImageSourceAsync((StreamImageSource)item.AssetPhoto);

                            using (var memoryStream = new MemoryStream())
                            {
                                Stream stream = imageContent;
                                stream.CopyTo(memoryStream);
                                imageArray = memoryStream.ToArray();
                            }
                            var newAssetPhotos = new AssetPhoto()
                            {
                                AssetID     = _asset.ID,
                                AssetPhoto1 = imageArray
                            };
                            var jsonData1     = JsonConvert.SerializeObject(newAssetPhotos);
                            var responsePhoto = await client.PostAsync(jsonData1, "AssetPhotoes/Create");

                            if (responsePhoto != "\"Photo(s) saved successfully!\"")
                            {
                                boolCheck = false;
                                break;
                            }
                        }
                        if (boolCheck == false)
                        {
                            await DisplayAlert("Edit Asset", "There was an issue with adding photos! Please contact our administrator!", "Ok");
                        }
                        else
                        {
                            await Navigation.PopAsync();
                        }
                    }
                    else
                    {
                        await DisplayAlert("Edit Asset", "An error occured while editing Asset! Please check and try again!", "Ok");
                    }
                }
                else
                {
                    var getAssetGroupID = (from x in _assetGroupList
                                           where x.Name == pAssetGroup.SelectedItem.ToString()
                                           select x.ID).FirstOrDefault();
                    var getDepartmentLoactionID = (from x in _departmentLocationList
                                                   join y in _departmentList on x.DepartmentID equals y.ID
                                                   join z in _locationList on x.LocationID equals z.ID
                                                   where y.Name == pDepartment.SelectedItem.ToString() && z.Name == pLocation.SelectedItem.ToString()
                                                   select x.ID).FirstOrDefault();
                    var getEmployeeID = (from x in _employeeList
                                         where x.FirstName + " " + x.LastName == pAccountable.SelectedItem.ToString()
                                         select x.ID).FirstOrDefault();
                    var newAsset = new Asset()
                    {
                        AssetName            = entryAssetName.Text,
                        AssetGroupID         = getAssetGroupID,
                        AssetSN              = lblAssetSN.Text,
                        DepartmentLocationID = getDepartmentLoactionID,
                        EmployeeID           = getEmployeeID
                    };
                    if (dpWarranty.Date == DateTime.Now.Date)
                    {
                        newAsset.WarrantyDate = null;
                    }
                    else
                    {
                        newAsset.WarrantyDate = dpWarranty.Date;
                    }
                    if (string.IsNullOrWhiteSpace(editorDescription.Text))
                    {
                        newAsset.Description = " ";
                    }
                    else
                    {
                        newAsset.Description = editorDescription.Text;
                    }
                    var jsonData = JsonConvert.SerializeObject(newAsset);
                    var response = await client.PostAsync(jsonData, "Assets/Create");

                    if (response == "\"Created Asset!\"")
                    {
                        await DisplayAlert("Add Asset", "Created Asset!", "Ok");

                        var boolCheck        = true;
                        var getAssetIDString = await client.PostAsync(null, $"Assets/GetAssetID?AssetSN={lblAssetSN.Text}");

                        var assetID = long.Parse(JsonConvert.DeserializeObject <string>(getAssetIDString));
                        foreach (var item in _assetPhotoList)
                        {
                            byte[] imageArray;
                            var    imageContent = await GetStreamFromImageSourceAsync((StreamImageSource)item.AssetPhoto);

                            using (var memoryStream = new MemoryStream())
                            {
                                Stream stream = imageContent;
                                stream.CopyTo(memoryStream);
                                imageArray = memoryStream.ToArray();
                            }
                            var newAssetPhotos = new AssetPhoto()
                            {
                                AssetID     = assetID,
                                AssetPhoto1 = imageArray
                            };
                            var jsonData1     = JsonConvert.SerializeObject(newAssetPhotos);
                            var responsePhoto = await client.PostAsync(jsonData1, " AssetPhotoes/Create");

                            if (responsePhoto != "\"Photo(s) saved successfully!\"")
                            {
                                boolCheck = false;
                                break;
                            }
                        }
                        if (boolCheck == false)
                        {
                            await DisplayAlert("Add Asset", "There was an issue with adding photos! Please contact our administrator!", "Ok");
                        }
                        else
                        {
                            await Navigation.PopAsync();
                        }
                    }
                    else if (response == "\"Asset already exist in the location of choice!\"")
                    {
                        await DisplayAlert("Add Asset", "Asset already exist in the location of choice!", "Ok");
                    }
                    else
                    {
                        await DisplayAlert("Add Asset", "Unable to create Asset! Please check and try again!", "Ok");
                    }
                }
            }
        }
        public JsonResult Save([FromBody] AssetDto pAsset)
        {
            var loUser = HttpContext.Session.GetObject <UserDto>("User");

            var loAssetFiles     = new List <AssetPhoto>();
            var loFileListToSave = new List <FileModel>();

            if (Directory.Exists(_environment.WebRootPath + "\\" + "Uploads\\Temp\\" + loUser.session_id))
            {
                string[] filePaths = Directory.GetFiles(_environment.WebRootPath + "\\" + "Uploads\\Temp\\" + loUser.session_id);
                foreach (var file in filePaths)
                {
                    var loFileModel = new FileModel
                    {
                        FileContent = System.IO.File.ReadAllBytes(file),
                        FileName    = Path.GetFileName(file),
                        FilePath    = loUser.session_id
                    };

                    var loAssetFile = new AssetPhoto
                    {
                        file_path = loFileModel.FilePath + "\\" + loFileModel.FileName
                    };

                    if (loFileModel.FileName.Contains("thumb_"))
                    {
                        pAsset.thumb_path    = loAssetFile.file_path;
                        loAssetFile.is_thumb = true;
                    }

                    loFileListToSave.Add(loFileModel);
                    loAssetFiles.Add(loAssetFile);
                }
            }

            if (!loFileListToSave.Any() || HelperMethods.SaveFilesToFileServer(_environment.WebRootPath + "\\" + "Uploads\\", loFileListToSave))
            {
                if (pAsset.asset_photos != null && pAsset.asset_photos.Any())
                {
                    pAsset.asset_photos.AddRange(loAssetFiles);
                }
                else
                {
                    pAsset.asset_photos = loAssetFiles;
                }

                pAsset.asset_photos.ForEach(x => x.file_path = x.file_path.Replace(",", "\\"));

                HelperMethods.DeleteFiles(pAsset.asset_photos.FindAll(x => x.is_deleted), _environment.WebRootPath + "\\" + "Uploads\\");

                //var loAsset = RestCalls.UpdateAsset(pAsset, loUser.token);

                var loAsset = RestCalls.SaveAsset(pAsset, loUser.token);

                if (!string.IsNullOrEmpty(loAsset.message) || loAsset.id <= 0)
                {
                    return(Json(loAsset));
                }

                try
                {
                    if (loFileListToSave.Any())                                                                                // yeni kayıt varsa sil templeri!
                    {
                        Directory.Delete(Path.Combine(_environment.WebRootPath, "Uploads\\Temp\\" + loUser.session_id), true); // temp resimleri sil
                    }
                }
                catch (Exception)
                {
                    //ignored;
                }


                return(Json(loAsset));
            }

            return(Json(new AssetDto()));
        }