// GET: Account/Details/5
        public async Task <IActionResult> Details(long?id)
        {
            if (id == null)
            {
                return(NotFound());
            }

            TblAccountVM tblAccountVM = new TblAccountVM();

            HttpClient client = _helperAPI.InitializeClient();

            var contentType = new MediaTypeWithQualityHeaderValue("application/json");

            client.DefaultRequestHeaders.Accept.Add(contentType);

            client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", HttpContext.Session.GetString("token"));

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

            HttpResponseMessage tblAccountVMRes = await client.GetAsync("api/Account/" + id);

            if (tblAccountVMRes.StatusCode == HttpStatusCode.Unauthorized)
            {
                ViewBag.Message = "Unauthorized!";
            }

            //Checking the response is successful or not which is sent using HttpClient
            if (tblAccountVMRes.IsSuccessStatusCode)
            {
                //Storing the response details recieved from web api
                var result = tblAccountVMRes.Content.ReadAsStringAsync().Result;


                //Deserializing the response recieved from web api and storing into the Role list
                tblAccountVM = JsonConvert.DeserializeObject <TblAccountVM>(result);
            }
            if (tblAccountVM == null)
            {
                return(NotFound());
            }

            return(View(tblAccountVM));
            //if (id == null)
            //{
            //    return NotFound();
            //}

            //var tblAccountVM = await _context.TblAccountVM
            //    .FirstOrDefaultAsync(m => m.AccountId == id);
            //if (tblAccountVM == null)
            //{
            //    return NotFound();
            //}

            //return View(tblAccountVM);
        }
        // GET: Account/Delete/5
        public async Task <IActionResult> Delete(long?id)
        {
            if (id == null)
            {
                return(NotFound());
            }

            TblAccountVM tblAccountVM = new TblAccountVM();
            HttpClient   client       = _helperAPI.InitializeClient();

            client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", HttpContext.Session.GetString("token"));
            HttpResponseMessage res = await client.GetAsync("api/Account/" + id);

            if (res.IsSuccessStatusCode)
            {
                var result = res.Content.ReadAsStringAsync().Result;
                tblAccountVM = JsonConvert.DeserializeObject <TblAccountVM>(result);
            }
            if (tblAccountVM == null)
            {
                return(NotFound());
            }

            return(View(tblAccountVM));
            //if (id == null)
            //{
            //    return NotFound();
            //}

            //var tblAccountVM = await _context.TblAccountVM
            //    .FirstOrDefaultAsync(m => m.AccountId == id);
            //if (tblAccountVM == null)
            //{
            //    return NotFound();
            //}

            //return View(tblAccountVM);
        }
        public async Task <IActionResult> Edit(long id, [Bind("AccountId,AccountTitle,AccountCode,AccountDescription,ReceivablesId,PayableId,DateAdded,DateUpdated,IsActive,AccountTypeId")] TblAccountVM tblAccountVM)
        {
            HttpClient client = _helperAPI.InitializeClient();

            if (id != tblAccountVM.AccountId)
            {
                return(NotFound());
            }

            if (ModelState.IsValid)
            {
                try
                {
                    tblAccountVM.DateUpdated = DateTime.Now;
                    client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", HttpContext.Session.GetString("token"));
                    var content = new StringContent(JsonConvert.SerializeObject(tblAccountVM), Encoding.UTF8, "application/json");
                    HttpResponseMessage response = await client.PutAsync("api/Account/" + id, content);

                    if (response.IsSuccessStatusCode)
                    {
                        return(RedirectToAction("Index"));
                    }
                }
                catch (DbUpdateConcurrencyException)
                {
                    //if (!RoleVMExists(roleVM.Id))
                    //{
                    //    return NotFound();
                    //}
                    //else
                    //{
                    throw;
                    //}
                }
                return(RedirectToAction(nameof(Index)));
            }

            List <PayableVM> payableVM = new List <PayableVM>();

            var contentType = new MediaTypeWithQualityHeaderValue("application/json");

            client.DefaultRequestHeaders.Accept.Add(contentType);

            client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", HttpContext.Session.GetString("token"));

            HttpResponseMessage res = await client.GetAsync("api/Payable");

            if (res.StatusCode == HttpStatusCode.Unauthorized)
            {
                ViewBag.Message = "Unauthorized!";
            }

            //Checking the response is successful or not which is sent using HttpClient
            if (res.IsSuccessStatusCode)
            {
                //Storing the response details recieved from web api
                var result = res.Content.ReadAsStringAsync().Result;


                //Deserializing the response recieved from web api and storing into the role list
                payableVM = JsonConvert.DeserializeObject <List <PayableVM> >(result);
            }


            List <ReceivableVM> receivableVM = new List <ReceivableVM>();

            HttpResponseMessage resp = await client.GetAsync("api/Receivable");

            if (resp.StatusCode == HttpStatusCode.Unauthorized)
            {
                ViewBag.Message = "Unauthorized!";
            }

            //Checking the response is successful or not which is sent using HttpClient
            if (resp.IsSuccessStatusCode)
            {
                //Storing the response details recieved from web api
                var result = resp.Content.ReadAsStringAsync().Result;


                //Deserializing the response recieved from web api and storing into the role list
                receivableVM = JsonConvert.DeserializeObject <List <ReceivableVM> >(result);
            }


            List <AccountTypeVM> accountTypeVM = new List <AccountTypeVM>();

            HttpResponseMessage respo = await client.GetAsync("api/AccountType");

            if (respo.StatusCode == HttpStatusCode.Unauthorized)
            {
                ViewBag.Message = "Unauthorized!";
            }

            //Checking the response is successful or not which is sent using HttpClient
            if (respo.IsSuccessStatusCode)
            {
                //Storing the response details recieved from web api
                var result = respo.Content.ReadAsStringAsync().Result;


                //Deserializing the response recieved from web api and storing into the role list
                accountTypeVM = JsonConvert.DeserializeObject <List <AccountTypeVM> >(result);
            }
            ViewData["PayableId"]     = new SelectList(payableVM, "PayableId", "PayableName", tblAccountVM.PayableId);
            ViewData["ReceivablesId"] = new SelectList(receivableVM, "ReceivableId", "ReceivableName", tblAccountVM.ReceivablesId);
            ViewData["AccountTypeId"] = new SelectList(accountTypeVM, "AccountTypeId", "AccountTypeName", tblAccountVM.AccountTypeId);

            return(View(tblAccountVM));
            //if (id != tblAccountVM.AccountId)
            //{
            //    return NotFound();
            //}

            //if (ModelState.IsValid)
            //{
            //    try
            //    {
            //        _context.Update(tblAccountVM);
            //        await _context.SaveChangesAsync();
            //    }
            //    catch (DbUpdateConcurrencyException)
            //    {
            //        if (!TblAccountVMExists(tblAccountVM.AccountId))
            //        {
            //            return NotFound();
            //        }
            //        else
            //        {
            //            throw;
            //        }
            //    }
            //    return RedirectToAction(nameof(Index));
            //}
            //return View(tblAccountVM);
        }
        // GET: Account/Edit/5
        public async Task <IActionResult> Edit(long?id)
        {
            if (id == null)
            {
                return(NotFound());
            }

            TblAccountVM tblAccountVM = new TblAccountVM();
            HttpClient   client       = _helperAPI.InitializeClient();

            client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", HttpContext.Session.GetString("token"));
            HttpResponseMessage response = await client.GetAsync("api/Account/" + id);

            if (response.IsSuccessStatusCode)
            {
                var result = response.Content.ReadAsStringAsync().Result;
                tblAccountVM = JsonConvert.DeserializeObject <TblAccountVM>(result);
            }

            if (tblAccountVM == null)
            {
                return(NotFound());
            }
            List <PayableVM> payableVM = new List <PayableVM>();

            var contentType = new MediaTypeWithQualityHeaderValue("application/json");

            client.DefaultRequestHeaders.Accept.Add(contentType);

            client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", HttpContext.Session.GetString("token"));

            HttpResponseMessage res = await client.GetAsync("api/Payable");

            if (res.StatusCode == HttpStatusCode.Unauthorized)
            {
                ViewBag.Message = "Unauthorized!";
            }

            //Checking the response is successful or not which is sent using HttpClient
            if (res.IsSuccessStatusCode)
            {
                //Storing the response details recieved from web api
                var result = res.Content.ReadAsStringAsync().Result;


                //Deserializing the response recieved from web api and storing into the role list
                payableVM = JsonConvert.DeserializeObject <List <PayableVM> >(result);
            }


            List <ReceivableVM> receivableVM = new List <ReceivableVM>();

            HttpResponseMessage resp = await client.GetAsync("api/Receivable");

            if (resp.StatusCode == HttpStatusCode.Unauthorized)
            {
                ViewBag.Message = "Unauthorized!";
            }

            //Checking the response is successful or not which is sent using HttpClient
            if (resp.IsSuccessStatusCode)
            {
                //Storing the response details recieved from web api
                var result = resp.Content.ReadAsStringAsync().Result;


                //Deserializing the response recieved from web api and storing into the role list
                receivableVM = JsonConvert.DeserializeObject <List <ReceivableVM> >(result);
            }


            List <AccountTypeVM> accountTypeVM = new List <AccountTypeVM>();

            HttpResponseMessage respo = await client.GetAsync("api/AccountType");

            if (respo.StatusCode == HttpStatusCode.Unauthorized)
            {
                ViewBag.Message = "Unauthorized!";
            }

            //Checking the response is successful or not which is sent using HttpClient
            if (respo.IsSuccessStatusCode)
            {
                //Storing the response details recieved from web api
                var result = respo.Content.ReadAsStringAsync().Result;


                //Deserializing the response recieved from web api and storing into the role list
                accountTypeVM = JsonConvert.DeserializeObject <List <AccountTypeVM> >(result);
            }
            ViewData["PayableId"]     = new SelectList(payableVM, "PayableId", "PayableName", tblAccountVM.PayableId);
            ViewData["ReceivablesId"] = new SelectList(receivableVM, "ReceivableId", "ReceivableName", tblAccountVM.ReceivablesId);
            ViewData["AccountTypeId"] = new SelectList(accountTypeVM, "AccountTypeId", "AccountTypeName", tblAccountVM.AccountTypeId);

            return(View(tblAccountVM));
            //if (id == null)
            //{
            //    return NotFound();
            //}

            //var tblAccountVM = await _context.TblAccountVM.FindAsync(id);
            //if (tblAccountVM == null)
            //{
            //    return NotFound();
            //}
            //return View(tblAccountVM);
        }