private async Task <bool> SaleContractVMExists(long id)
        {
            if (id == 0)
            {
                return(false);
            }

            SaleContractVM saleContractVM = new SaleContractVM();
            HttpClient     client         = _helperAPI.InitializeClient();

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

            if (res.IsSuccessStatusCode)
            {
                var result = res.Content.ReadAsStringAsync().Result;
                saleContractVM = JsonConvert.DeserializeObject <SaleContractVM>(result);
            }
            if (saleContractVM == null)
            {
                return(false);
            }

            return(true);
            //return _context.SaleContractVM.Any(e => e.SaleContractId == id);
        }
        // GET: SaleContract/Delete/5
        public async Task <IActionResult> Delete(long?id)
        {
            if (id == null)
            {
                return(NotFound());
            }

            SaleContractVM saleContractVM = new SaleContractVM();
            HttpClient     client         = _helperAPI.InitializeClient();

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

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

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

            //var saleContractVM = await _context.SaleContractVM
            //    .Include(s => s.Currency)
            //    .Include(s => s.Customer)
            //    .FirstOrDefaultAsync(m => m.SaleContractId == id);
            //if (saleContractVM == null)
            //{
            //    return NotFound();
            //}

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

            SaleContractVM saleContractVM = new SaleContractVM();
            HttpClient     client         = _helperAPI.InitializeClient();

            //client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", HttpContext.Session.GetString("token"));
            client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", TempData.Peek("Token").ToString());
            HttpResponseMessage res = await client.GetAsync("api/SaleContracts/" + 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
                saleContractVM = JsonConvert.DeserializeObject <SaleContractVM>(result);
            }
            if (saleContractVM == null)
            {
                return(NotFound());
            }
            #region   Get Sale Contract Items
            List <SaleContractItemVM> saleContractItemVM = new List <SaleContractItemVM>();



            var contentType = new MediaTypeWithQualityHeaderValue("application/json");
            client.DefaultRequestHeaders.Accept.Add(contentType);

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

            HttpResponseMessage res1 = await client.GetAsync("api/SaleContractItems/GetSaleContractItemBySaleContractId/" + id);

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

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


                //Deserializing the response recieved from web api and storing into the role list
                saleContractItemVM = JsonConvert.DeserializeObject <List <SaleContractItemVM> >(result);
                foreach (var item in saleContractItemVM)
                {
                    #region Unit
                    UnitVM unitVM = new UnitVM();
                    //client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", HttpContext.Session.GetString("token"));
                    HttpResponseMessage resUnit = await client.GetAsync("api/Units/" + item.UnitId);

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

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


                        //Deserializing the response recieved from web api and storing into the Role list
                        unitVM = JsonConvert.DeserializeObject <UnitVM>(resultUnit);

                        item.Unit = unitVM;
                    }
                    if (unitVM == null)
                    {
                        return(NotFound());
                    }

                    #endregion
                }
            }
            //returning the role list to view
            #endregion

            saleContractVM.SaleContractItems = saleContractItemVM;
            #region Customer Information

            CustomerVM customerVM = new CustomerVM();
            client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", HttpContext.Session.GetString("token"));
            HttpResponseMessage res2 = await client.GetAsync("api/Customers/" + saleContractVM.CustomerId);

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

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


                //Deserializing the response recieved from web api and storing into the Role list
                customerVM = JsonConvert.DeserializeObject <CustomerVM>(result);
            }
            if (customerVM == null)
            {
                return(NotFound());
            }
            saleContractVM.Customer = customerVM;
            #endregion

            #region Currency
            CurrencyVM currencyVM = new CurrencyVM();

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

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

            HttpResponseMessage currencyVMRes = await client.GetAsync("api/Currencies/" + saleContractVM.CurrencyId);

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

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


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

            saleContractVM.Currency = currencyVM;
            #endregion

            #region Bank Details
            BankInfoVM bankInfoVM = new BankInfoVM();
            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 bankInfoVMRes = await client.GetAsync("api/BankInfo/" + saleContractVM.BankDetails);

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

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


                //Deserializing the response recieved from web api and storing into the Role list
                bankInfoVM = JsonConvert.DeserializeObject <BankInfoVM>(result);
                saleContractVM.BankDetailsNavigation = bankInfoVM;
            }
            #endregion Bank Details

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

            //var saleContractVM = await _context.SaleContractVM
            //    .Include(s => s.Currency)
            //    .Include(s => s.Customer)
            //    .FirstOrDefaultAsync(m => m.SaleContractId == id);
            //if (saleContractVM == null)
            //{
            //    return NotFound();
            //}

            //return View(saleContractVM);
        }
        public async Task <IActionResult> Edit(long id, [Bind("SaleContractId,SaleContractNumber,CustomerId,Tolerance,Packing,ShipmentDetails,PaymentTerms,DeliverySchedule,CurrencyId,BankDetails,Description")] SaleContractVM saleContractVM)//TotalAmount,TotalFabric,IsActive,DateAdded,
        {
            HttpClient client = _helperAPI.InitializeClient();

            if (id != saleContractVM.SaleContractId)
            {
                return(NotFound());
            }

            if (ModelState.IsValid)
            {
                try
                {
                    var userString = TempData.Peek("User").ToString();
                    var userVM     = JsonConvert.DeserializeObject <UserVM>(userString);
                    saleContractVM.DateUpdated = DateTime.Now;
                    saleContractVM.UpdatedBy   = userVM.Id;
                    //client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", HttpContext.Session.GetString("token"));
                    client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", TempData.Peek("Token").ToString());
                    var content             = new StringContent(JsonConvert.SerializeObject(saleContractVM), Encoding.UTF8, "application/json");
                    HttpResponseMessage res = client.PutAsync("api/SaleContracts/" + id, content).Result;
                    if (res.IsSuccessStatusCode)
                    {
                        return(RedirectToAction("Index"));
                    }
                }
                catch (DbUpdateConcurrencyException)
                {
                    //if (!RoleVMExists(roleVM.Id))
                    //{
                    //    return NotFound();
                    //}
                    //else
                    //{
                    throw;
                    //}
                }
                return(RedirectToAction(nameof(Index)));
            }

            //Get DDL
            #region Get DDL
            List <CurrencyVM> CurrencyVM = new List <CurrencyVM>();

            var contentType = new MediaTypeWithQualityHeaderValue("application/json");
            client.DefaultRequestHeaders.Accept.Add(contentType);

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

            HttpResponseMessage CurrencyVMsRes = await client.GetAsync("api/Currencies");

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

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


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

            ViewData["CurrencyId"] = new SelectList(CurrencyVM, "CurrencyId", "CurrencyName");



            List <CustomerVM> customerVM = new List <CustomerVM>();
            //client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", HttpContext.Session.GetString("token"));

            HttpResponseMessage resCust = await client.GetAsync("api/Customers");

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

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


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

            ViewData["CustomerId"] = new SelectList(customerVM, "CustomerId", "CustomerName");

            List <BankInfoVM> bankInfoVM = new List <BankInfoVM>();
            client.DefaultRequestHeaders.Accept.Add(contentType);
            //var token = TempData.Peek("Token").ToString();
            //client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", token);

            HttpResponseMessage bankInfoVMsRes = await client.GetAsync("api/BankInfo");

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

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


                //Deserializing the response recieved from web api and storing into the role list
                bankInfoVM = JsonConvert.DeserializeObject <List <BankInfoVM> >(result);
            }
            ViewData["BankId"] = new SelectList(bankInfoVM, "Id", "BankName");
            #endregion Get DDL
            //Get DDL End

            return(View(customerVM));

            //if (id != saleContractVM.SaleContractId)
            //{
            //    return NotFound();
            //}

            //if (ModelState.IsValid)
            //{
            //    try
            //    {
            //        _context.Update(saleContractVM);
            //        await _context.SaveChangesAsync();
            //    }
            //    catch (DbUpdateConcurrencyException)
            //    {
            //        if (!SaleContractVMExists(saleContractVM.SaleContractId))
            //        {
            //            return NotFound();
            //        }
            //        else
            //        {
            //            throw;
            //        }
            //    }
            //    return RedirectToAction(nameof(Index));
            //}
            //ViewData["CurrencyId"] = new SelectList(_context.Set<CurrencyVM>(), "CurrencyId", "CurrencyId", saleContractVM.CurrencyId);
            //ViewData["CustomerId"] = new SelectList(_context.Set<CustomerVM>(), "CustomerId", "CustomerId", saleContractVM.CustomerId);
            //return View(saleContractVM);
        }
        // GET: SaleContract/Edit/5
        public async Task <IActionResult> Edit(long?id)
        {
            if (id == null)
            {
                return(NotFound());
            }

            SaleContractVM saleContractVM = new SaleContractVM();
            HttpClient     client         = _helperAPI.InitializeClient();

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

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

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

            //Get DDL
            #region Get DDL
            List <CurrencyVM> CurrencyVM = new List <CurrencyVM>();

            var contentType = new MediaTypeWithQualityHeaderValue("application/json");
            client.DefaultRequestHeaders.Accept.Add(contentType);

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

            HttpResponseMessage CurrencyVMsRes = await client.GetAsync("api/Currencies");

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

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


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

            ViewData["CurrencyId"] = new SelectList(CurrencyVM, "CurrencyId", "CurrencyName");



            List <CustomerVM> customerVM = new List <CustomerVM>();
            //client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", HttpContext.Session.GetString("token"));

            HttpResponseMessage resCust = await client.GetAsync("api/Customers");

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

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


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

            ViewData["CustomerId"] = new SelectList(customerVM, "CustomerId", "CustomerName");

            List <BankInfoVM> bankInfoVM = new List <BankInfoVM>();
            client.DefaultRequestHeaders.Accept.Add(contentType);
            //var token = TempData.Peek("Token").ToString();
            //client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", token);

            HttpResponseMessage bankInfoVMsRes = await client.GetAsync("api/BankInfo");

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

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


                //Deserializing the response recieved from web api and storing into the role list
                bankInfoVM = JsonConvert.DeserializeObject <List <BankInfoVM> >(result);
            }
            ViewData["BankId"] = new SelectList(bankInfoVM, "Id", "BankName");
            #endregion Get DDL
            //Get DDL End
            return(View(saleContractVM));

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

            //var saleContractVM = await _context.SaleContractVM.FindAsync(id);
            //if (saleContractVM == null)
            //{
            //    return NotFound();
            //}
            //ViewData["CurrencyId"] = new SelectList(_context.Set<CurrencyVM>(), "CurrencyId", "CurrencyId", saleContractVM.CurrencyId);
            //ViewData["CustomerId"] = new SelectList(_context.Set<CustomerVM>(), "CustomerId", "CustomerId", saleContractVM.CustomerId);
            //return View(saleContractVM);
        }