public async Task <IActionResult> Edit(int id, [Bind("IndicatorID,SchoolID,ImageURL,Verified,IsUpload,DateOfUpload,CreatedBy,CreateDate,UpdatedBy,UpdatedDate,VerifiedBy,VerifiedDate")] IndicatorTracking incdicatorTracking)
        {
            if (id != incdicatorTracking.SchoolID)
            {
                return(NotFound());
            }

            if (ModelState.IsValid)
            {
                try
                {
                    _context.Update(incdicatorTracking);
                    await _context.SaveChangesAsync();
                }
                catch (DbUpdateConcurrencyException)
                {
                    //if (!IncdicatorTrackingExists(incdicatorTracking.SchoolID))
                    //{
                    //    return NotFound();
                    //}
                    //else
                    //{
                    //    throw;
                    //}
                }
                return(RedirectToAction(nameof(Index)));
            }
            ViewData["SchoolID"] = new SelectList(_context.Schools, "SchoolID", "SName", incdicatorTracking.SchoolID);
            return(View(incdicatorTracking));
        }
        //[ValidateAntiForgeryToken]
        public async Task <IActionResult> UpdatePost(int sID, int iID, DateTime EDate, int SecID)
        {
            // Save uploaded files
            var files = Request.Form.Files;

            string District = _context.Schools.Include(a => a.UC.Tehsil.District)
                              .Where(a => a.SchoolID == sID)
                              .Select(a => a.UC.Tehsil.District.DistrictName).FirstOrDefault();
            //string
            var rootPath = Path.Combine(
                Directory.GetCurrentDirectory(), "wwwroot\\Documents\\IndicatorEvidences\\");

            //string sPath = Path.Combine(rootPath + District + "/" + iID + "/", sID.ToString());
            string sPath = Path.Combine(rootPath + District + "/" + iID + "/");

            if (!System.IO.Directory.Exists(sPath))
            {
                System.IO.Directory.CreateDirectory(sPath);
            }
            short  i        = 1;
            string fileName = sID.ToString() + "-";

            foreach (var file in files)
            {
                string FullPathWithFileName = Path.Combine(sPath, fileName + i++ + Path.GetExtension(file.FileName));
                using (var stream = new FileStream(FullPathWithFileName, FileMode.Create))
                {
                    await file.CopyToAsync(stream);
                }
            }
            try
            {
                //create record
                IndicatorTracking IndiTrack = new IndicatorTracking();
                IndiTrack.IndicatorID        = iID;
                IndiTrack.SchoolID           = sID;
                IndiTrack.IsUpload           = true;
                IndiTrack.TotalFilesUploaded = (short)files.Count;
                IndiTrack.DateOfUpload       = EDate;
                IndiTrack.ImageURL           = Path.Combine("/Documents/IndicatorEvidences/", District + "//" + iID);//Server Path
                IndiTrack.CreateDate         = DateTime.Now;
                IndiTrack.CreatedBy          = User.Identity.Name;


                IndiTrack.Verified = false;

                _context.Add(IndiTrack);

                await _context.SaveChangesAsync();
            }
            catch (Exception ex)
            {
                Console.Write(ex.InnerException.Message);
                return(Json(new { success = false, responseText = ex.InnerException.Message }));
            }
            //ViewData["SchoolID"] = new SelectList(_context.Schools, "SchoolID", "SName", incdicatorTracking.SchoolID);
            //return RedirectToAction(nameof(Update), new { id = sID, SecID = SecID });
            return(Json(new { success = true, responseText = "Sucessfully Updated" })); //, sID = sID, SecID = SecID });
        }
        public async Task <IActionResult> Create([Bind("IndicatorID,SchoolID,ImageURL,Verified,IsUpload,DateOfUpload,CreatedBy,CreateDate,UpdatedBy,UpdatedDate,VerifiedBy,VerifiedDate")] IndicatorTracking incdicatorTracking)
        {
            if (ModelState.IsValid)
            {
                _context.Add(incdicatorTracking);
                await _context.SaveChangesAsync();

                return(RedirectToAction(nameof(Index)));
            }
            ViewData["SchoolID"] = new SelectList(_context.Schools, "SchoolID", "SName", incdicatorTracking.SchoolID);
            return(View(incdicatorTracking));
        }
        public async Task <ActionResult <pictureRepoitoryModel> > Post(pictureRepoitoryModel indicatorTrackDevApp)
        {
            bool   status  = true;
            string message = "Success";

            IndicatorTracking indicatorTracking = new IndicatorTracking
            {
                SchoolID           = indicatorTrackDevApp.school_id,
                IndicatorID        = indicatorTrackDevApp.indicatorID,
                IsUpload           = true,
                TotalFilesUploaded = indicatorTrackDevApp.picture_count,
                DateOfUpload       = DateTime.Now
            };

            _context.IncdicatorTracking.Add(indicatorTracking);
            // Saving Image code go here
            //foreach (var i in indicatorTrackDevApp.IndicatorDevApps)
            //{

            //}


            try
            {
                // await _context.SaveChangesAsync();
            }
            catch (DbUpdateException)
            {
                if (IndicatorTrackingExists(indicatorTracking.SchoolID, indicatorTracking.IndicatorID))
                {
                    status  = false;
                    message = "Record Already Exist against this school and Indicator";
                    //return Conflict();
                }
                else
                {
                    throw;
                }
            }

            return(Ok(new { status, message }));
            //  return CreatedAtAction("GetIndicatorTracking", new { id = indicatorTracking.SchoolID }, indicatorTracking);
        }
        //[ValidateAntiForgeryToken]
        public async Task <IActionResult> MneVerifyIndicatorsPost(int sID, int iID, bool verified, bool reUpload)
        {
            try
            {
                //create record
                IndicatorTracking IndiTrack = await _context.IncdicatorTracking.Where(a => a.SchoolID == sID && a.IndicatorID == iID).FirstOrDefaultAsync();

                // IndiTrack.IndicatorID = iID;
                //IndiTrack.SchoolID = sID;
                // IndiTrack.IsUpload = true;
                //IndiTrack.TotalFilesUploaded = (short)files.Count;
                // IndiTrack.DateOfUpload = EDate;
                //  IndiTrack.ImageURL = Path.Combine("/Documents/IndicatorEvidences/", District + "//" + iID);//Server Path
                // // IndiTrack.CreateDate = DateTime.Now;
                //  IndiTrack.CreatedBy = User.Identity.Name;
                IndiTrack.Verified = verified;
                IndiTrack.ReUpload = reUpload;
                if (IndiTrack.Verified == true)
                {
                    IndiTrack.VerifiedBy      = User.Identity.Name;
                    IndiTrack.DevVerifiedDate = DateTime.Now;
                }
                //IndiTrack.Verified = false;

                _context.Update(IndiTrack);

                await _context.SaveChangesAsync();
            }
            catch (Exception ex)
            {
                Console.Write(ex.InnerException.Message);
                return(Json(new { success = false, responseText = ex.InnerException.Message }));
            }
            //ViewData["SchoolID"] = new SelectList(_context.Schools, "SchoolID", "SName", incdicatorTracking.SchoolID);
            //return RedirectToAction(nameof(Update), new { id = sID, SecID = SecID });
            return(Json(new { success = true, responseText = "Sucessfully Updated" })); //, sID = sID, SecID = SecID });
        }
        public async Task <ActionResult <pictureRepoitoryModel> > Post(schoolIndicatorPictureRepository schoolIndicatorPictureRepositoryList)
        {
            bool   status      = true;
            string message     = "Success";
            bool   isDuplicate = false;

            // List<IndicatorDevApp> indicatorDevAppList = new List<IndicatorDevApp>();
            foreach (var RepoList in schoolIndicatorPictureRepositoryList.schoolIndicatorPictureRepositoryList)
            {
                var repo = RepoList.pictureRepoitoryModel;

                //Check if record already exist
                if (IndicatorTrackingExists(repo.school_id, repo.indicatorID))
                {
                    // status = false;
                    message = "Record Already Exist of School ID: " + repo.school_id + " and Indicator ID:" + repo.indicatorID;
                    //return Conflict();
                    //return Ok(new { status, message });
                    isDuplicate = true;
                }


                IndicatorTracking indicatorTracking = new IndicatorTracking
                {
                    IndicatorID        = repo.indicatorID,
                    SchoolID           = repo.school_id,
                    DateOfUpload       = Convert.ToDateTime(repo.current_date),
                    IsUpload           = true,
                    ReUpload           = false,
                    Verified           = false,
                    CreateDate         = DateTime.Now,
                    TotalFilesUploaded = repo.picture_count,
                };
                if (isDuplicate)
                {
                    indicatorTracking = _context.IncdicatorTracking.First(a => a.SchoolID == repo.school_id && a.IndicatorID == repo.indicatorID);
                }
                else
                {
                    _context.Add(indicatorTracking);
                }

                var school = _context.Schools.Find(repo.school_id);
                if (school.NewConstruction && repo.indicatorID < 35 && !isDuplicate)
                {
                    school.CurrentStage += 1;
                }
                if (school.RepairRennovation && repo.indicatorID > 35 && !isDuplicate)
                {
                    school.RepairRennovationStatus += 1;
                }
                _context.Update(school);

                short i = 1;
                //
                string District = _context.Schools.Include(a => a.UC.Tehsil.District)
                                  .Where(a => a.SchoolID == repo.school_id)
                                  .Select(a => a.UC.Tehsil.District.DistrictName).FirstOrDefault();
                //string
                var rootPath = Path.Combine(
                    Directory.GetCurrentDirectory(), "wwwroot");

                //string sPath = Path.Combine(rootPath + District + "/" + iID + "/", sID.ToString());
                string sPath = Path.Combine("\\Documents\\DevelopmentApp\\" + District + "/" + repo.indicatorID + "/", repo.school_id.ToString());
                if (isDuplicate)
                {
                    sPath = Path.Combine("\\Documents\\DevelopmentApp\\_Duplicate\\" + District + "/" + repo.indicatorID + "/", repo.school_id.ToString());
                }
                if (!System.IO.Directory.Exists(rootPath + sPath) & RepoList.repositoryDetailList.Any())
                {
                    System.IO.Directory.CreateDirectory(rootPath + sPath);
                }
                List <IndicatorDevApp> IndicatorDevAppList = new List <IndicatorDevApp>();
                foreach (var repoDetail in RepoList.repositoryDetailList)
                {
                    //byte[] byteImage = new byte[repoDetail.picture_data.Length];
                    //Buffer.BlockCopy(repoDetail.picture_data, 0, byteImage, 0, repoDetail.picture_data.Length);
                    string          FullPathWithFileName = Path.Combine(rootPath + sPath, repo.school_id + "-" + i + ".jpg");
                    IndicatorDevApp indicatorDevApp      = new IndicatorDevApp
                    {
                        SchoolID    = repo.school_id,
                        IndicatorID = repo.indicatorID,
                        ImagePath   = Path.Combine(sPath, repo.school_id + "-" + i + ".jpg"),
                        //ImageByte = repoDetail.picture_path,
                        //ImagePath= repoDetail.picture_path,
                        Longitude = repoDetail.picture_logitude,
                        Latitude  = repoDetail.picture_latitude,
                        DateTime  = Convert.ToDateTime(repoDetail.current_date),
                        SyncDate  = DateTime.Now,
                        Remarks   = repoDetail.picture_comment,
                        ImageID   = i++,
                    };

                    _context.Add(indicatorDevApp);


                    using (MemoryStream ms = new MemoryStream(Convert.FromBase64String(repoDetail.picture_path)))
                    {
                        using (Bitmap bm2 = new Bitmap(ms))
                        {
                            //bm2.SetPropertyItem()
                            Geotag(bm2, repoDetail.picture_latitude, repoDetail.picture_logitude, Convert.ToDateTime(repoDetail.current_date))
                            .Save(FullPathWithFileName);
                        }
                    }
                }
            }
            try
            {
                if (!isDuplicate)
                {
                    await _context.SaveChangesAsync();
                }
            }
            catch (Exception ex)
            {
                //if (IndicatorTrackingExists(repo.school_id, repo.indicatorID))
                //{
                status  = false;
                message = "Data mis matched from server";
                //return Conflict();
                //}
                //else
                //{
                //    throw;
                //}
            }


            return(Ok(new { status, message }));
            //  return CreatedAtAction("GetIndicatorTracking", new { id = indicatorTracking.SchoolID }, indicatorTracking);
        }