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

            FactoryOverheadExpenseVM factoryOverheadExpenseVM = new FactoryOverheadExpenseVM();

            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 res = await client.GetAsync("api/FactoryOverheadExpense/" + id);

            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
                factoryOverheadExpenseVM = JsonConvert.DeserializeObject <FactoryOverheadExpenseVM>(result);
            }
            if (factoryOverheadExpenseVM == null)
            {
                return(NotFound());
            }

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

            //var factoryOverheadExpenseVM = await _context.FactoryOverheadExpenseVM
            //    .Include(f => f.PaymentSourceAccount)
            //    .FirstOrDefaultAsync(m => m.Fohid == id);
            //if (factoryOverheadExpenseVM == null)
            //{
            //    return NotFound();
            //}

            //return View(factoryOverheadExpenseVM);
        }
        public async Task <IActionResult> Create([Bind("Fohid,PaymentSourceAccountId,PaymentSourceAccountCode,Amount,Description,IsActive,DateAdded,DateUpdated,AddedBy,UpdatedBy")] FactoryOverheadExpenseVM factoryOverheadExpenseVM)
        {
            HttpClient client = _helperAPI.InitializeClient();

            if (ModelState.IsValid)
            {
                factoryOverheadExpenseVM.DateAdded         = DateTime.Now;
                factoryOverheadExpenseVM.IsActive          = true;
                client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", HttpContext.Session.GetString("token"));
                var content = new StringContent(JsonConvert.SerializeObject(factoryOverheadExpenseVM), Encoding.UTF8, "application/json");
                //Task has been cancelled exception occured here, and Api method never hits while debugging
                HttpResponseMessage res = client.PostAsync("api/FactoryOverheadExpense", content).Result;
                if (res.IsSuccessStatusCode)
                {
                    return(RedirectToAction("Index"));
                }
            }
            List <TblAccountVM> tblAccountVM = new List <TblAccountVM>();
            var contentType = new MediaTypeWithQualityHeaderValue("application/json");

            client.DefaultRequestHeaders.Accept.Add(contentType);

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

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

            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 <List <TblAccountVM> >(result);
            }
            ViewData["PaymentSourceAccountId"] = new SelectList(tblAccountVM, "AccountId", "AccountTitle", factoryOverheadExpenseVM.PaymentSourceAccountId);
            return(View(factoryOverheadExpenseVM));
            //if (ModelState.IsValid)
            //{
            //    _context.Add(factoryOverheadExpenseVM);
            //    await _context.SaveChangesAsync();
            //    return RedirectToAction(nameof(Index));
            //}
            //ViewData["PaymentSourceAccountId"] = new SelectList(_context.TblAccountVM, "AccountId", "AccountId", factoryOverheadExpenseVM.PaymentSourceAccountId);
            //return View(factoryOverheadExpenseVM);
        }
        // GET: FactoryOverheadExpense/Delete/5
        public async Task <IActionResult> Delete(long?id)
        {
            if (id == null)
            {
                return(NotFound());
            }

            FactoryOverheadExpenseVM factoryOverheadExpenseVM = new FactoryOverheadExpenseVM();
            HttpClient client = _helperAPI.InitializeClient();

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

            if (res.IsSuccessStatusCode)
            {
                var result = res.Content.ReadAsStringAsync().Result;
                factoryOverheadExpenseVM = JsonConvert.DeserializeObject <FactoryOverheadExpenseVM>(result);
            }
            if (factoryOverheadExpenseVM == null)
            {
                return(NotFound());
            }
            //if (id == null)
            //{
            //    return NotFound();
            //}

            //var factoryOverheadExpenseVM = await _context.FactoryOverheadExpenseVM
            //    .Include(f => f.PaymentSourceAccount)
            //    .FirstOrDefaultAsync(m => m.Fohid == id);
            //if (factoryOverheadExpenseVM == null)
            //{
            //    return NotFound();
            //}

            return(View(factoryOverheadExpenseVM));
        }
        public async Task <IActionResult> Edit(long id, [Bind("Fohid,PaymentSourceAccountId,PaymentSourceAccountCode,Amount,Description,IsActive,DateAdded,DateUpdated,AddedBy,UpdatedBy")] FactoryOverheadExpenseVM factoryOverheadExpenseVM)
        {
            HttpClient client = _helperAPI.InitializeClient();

            if (id != factoryOverheadExpenseVM.Fohid)
            {
                return(NotFound());
            }

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

                    if (res.IsSuccessStatusCode)
                    {
                        return(RedirectToAction("Index"));
                    }
                }
                catch (DbUpdateConcurrencyException)
                {
                    //if (!RoleVMExists(roleVM.Id))
                    //{
                    //    return NotFound();
                    //}
                    //else
                    //{
                    throw;
                    //}
                }
                return(RedirectToAction(nameof(Index)));
            }
            List <TblAccountVM> tblAccountVM = new List <TblAccountVM>();
            var contentType = new MediaTypeWithQualityHeaderValue("application/json");

            client.DefaultRequestHeaders.Accept.Add(contentType);

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

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

            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 <List <TblAccountVM> >(result);
            }
            //if (id != factoryOverheadExpenseVM.Fohid)
            //{
            //    return NotFound();
            //}

            //if (ModelState.IsValid)
            //{
            //    try
            //    {
            //        _context.Update(factoryOverheadExpenseVM);
            //        await _context.SaveChangesAsync();
            //    }
            //    catch (DbUpdateConcurrencyException)
            //    {
            //        if (!FactoryOverheadExpenseVMExists(factoryOverheadExpenseVM.Fohid))
            //        {
            //            return NotFound();
            //        }
            //        else
            //        {
            //            throw;
            //        }
            //    }
            //    return RedirectToAction(nameof(Index));
            //}
            ViewData["PaymentSourceAccountId"] = new SelectList(_context.TblAccountVM, "AccountId", "AccountTitle", factoryOverheadExpenseVM.PaymentSourceAccountId);
            return(View(factoryOverheadExpenseVM));
        }
        // GET: FactoryOverheadExpense/Edit/5
        public async Task <IActionResult> Edit(long?id)
        {
            if (id == null)
            {
                return(NotFound());
            }

            FactoryOverheadExpenseVM factoryOverheadExpenseVM = new FactoryOverheadExpenseVM();
            HttpClient client = _helperAPI.InitializeClient();

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

            if (res.IsSuccessStatusCode)
            {
                var result = res.Content.ReadAsStringAsync().Result;
                factoryOverheadExpenseVM = JsonConvert.DeserializeObject <FactoryOverheadExpenseVM>(result);
            }

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

            List <TblAccountVM> tblAccountVM = new List <TblAccountVM>();
            var contentType = new MediaTypeWithQualityHeaderValue("application/json");

            client.DefaultRequestHeaders.Accept.Add(contentType);

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

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

            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 <List <TblAccountVM> >(result);
            }
            //if (id == null)
            //{
            //    return NotFound();
            //}

            //var factoryOverheadExpenseVM = await _context.FactoryOverheadExpenseVM.FindAsync(id);
            //if (factoryOverheadExpenseVM == null)
            //{
            //    return NotFound();
            //}
            ViewData["PaymentSourceAccountId"] = new SelectList(tblAccountVM, "AccountId", "AccountTitle", factoryOverheadExpenseVM.PaymentSourceAccountId);
            return(View(factoryOverheadExpenseVM));
        }