// GET: Produto/Edit/5
        public async Task <IActionResult> Edit(int?id)
        {
            AnuncioVM Anuncio = new AnuncioVM();

            if (id == null)
            {
                return(NotFound());
            }

            Anuncio.Produto = await _context.T001_PRODUTO.SingleOrDefaultAsync(m => m.T001_ID_PRODUTO == id);

            if (Anuncio.Produto == null)
            {
                return(NotFound());
            }

            Anuncio.caminho = HttpContext.Request.Host.Value;
            String fullPath = String.Concat(_environment.WebRootPath, "\\uploadImages\\", id);

            if (Directory.Exists(fullPath))
            {
                DirectoryInfo dir = new DirectoryInfo(fullPath);
                Anuncio.Imagens = dir.GetFiles();
            }

            ViewData["T002_ID_CATEGORIA"] = new SelectList(_context.Set <T002_CATEGORIA>(), "T002_ID_CATEGORIA", "T002_ID_CATEGORIA", Anuncio.Produto.T002_ID_CATEGORIA);
            ViewData["T003_ID_UF"]        = new SelectList(_context.Set <T003_UF>(), "T003_ID_UF", "T003_ID_UF", Anuncio.Produto.T003_ID_UF);
            return(View(Anuncio));
        }
        // GET: Produto/Details/5
        public async Task <IActionResult> Details(int?id)
        {
            if (id == null)
            {
                return(NotFound());
            }

            AnuncioVM Anuncio = new AnuncioVM();

            Anuncio.caminho = HttpContext.Request.Host.Value;
            String fullPath = String.Concat(_environment.WebRootPath, "\\uploadImages\\", id);

            if (Directory.Exists(fullPath))
            {
                DirectoryInfo dir = new DirectoryInfo(fullPath);
                Anuncio.Imagens = dir.GetFiles();
            }
            //JsonFiles files = new JsonFiles(r);

            Anuncio.Produto = _context.T001_PRODUTO
                              .Include(t => t.T002_CATEGORIANavigation)
                              .Include(t => t.T003_UFNavigation)
                              .Where(x => x.T001_ID_PRODUTO == id).First();

            Anuncio.telefone = _context.Users.Where(x => x.Id == Anuncio.Produto.User_Id).Select(x => x.PhoneNumber).First();

            if (Anuncio.Produto == null)
            {
                return(NotFound());
            }

            return(View(Anuncio));
        }
        public IActionResult Editar(AnuncioVM anuncio)
        {
            try
            {
                using (var client = new HttpClient())
                {
                    client.DefaultRequestHeaders.Accept.Clear();
                    client.DefaultRequestHeaders.Accept.Add(
                        new MediaTypeWithQualityHeaderValue("application/json"));

                    HttpContent content = new StringContent(JsonConvert.SerializeObject(anuncio), Encoding.UTF8, "application/json");

                    HttpResponseMessage response = client.PutAsync("http://localhost:63631/api/Anuncios/", content).Result;

                    response.EnsureSuccessStatusCode();

                    string conteudo = response.Content.ReadAsStringAsync().Result;

                    return(RedirectToAction("Detalhes", "Home", new { id = conteudo }));
                }
            }
            catch (Exception ex)
            {
                ViewBag.Erro = ex.Message;
                return(View());
            }
        }
        public IActionResult Editar(int id)
        {
            DropMarca();

            using (var client = new HttpClient())
            {
                client.DefaultRequestHeaders.Accept.Clear();
                client.DefaultRequestHeaders.Accept.Add(
                    new MediaTypeWithQualityHeaderValue("application/json"));

                HttpResponseMessage response = client.GetAsync("http://localhost:63631/api/Anuncios/" + id).Result;

                response.EnsureSuccessStatusCode();

                string conteudo = response.Content.ReadAsStringAsync().Result;

                AnuncioVM resultado = JsonConvert.DeserializeObject <AnuncioVM>(conteudo);

                return(View(resultado));
            }
        }
        public IActionResult Postar(AnuncioVM anuncio)
        {
            try
            {
                var marca = anuncio.Marca.Split(" ").ToList();
                marca.RemoveAt(0);
                anuncio.Marca = marca.Aggregate((a, b) => a + b);
                var modelo = anuncio.Modelo.Split(" ").ToList();
                modelo.RemoveAt(0);
                anuncio.Modelo = modelo.Aggregate((a, b) => a + b);
                var versao = anuncio.Versao.Split(" ").ToList();
                versao.RemoveAt(0);
                anuncio.Versao = versao.Aggregate((a, b) => a + " " + b);



                using (var client = new HttpClient())
                {
                    client.DefaultRequestHeaders.Accept.Clear();
                    client.DefaultRequestHeaders.Accept.Add(
                        new MediaTypeWithQualityHeaderValue("application/json"));

                    HttpContent content = new StringContent(JsonConvert.SerializeObject(anuncio), Encoding.UTF8, "application/json");

                    HttpResponseMessage response = client.PostAsync("http://localhost:63631/api/Anuncios/", content).Result;

                    response.EnsureSuccessStatusCode();

                    string idAnuncioCriado = response.Content.ReadAsStringAsync().Result;

                    return(RedirectToAction("Detalhes", "Home", new { id = idAnuncioCriado }));
                }
            }
            catch (Exception ex)
            {
                ViewBag.Erro = ex.Message;
                return(View());
            }
        }