// GET: DroneService/Edit/5
        public ActionResult Edit(int id)
        {
            if (!exLogic.User.hasAccess("SERVICE.EDIT"))
            {
                return(RedirectToAction("NoAccess", "Home"));
            }

            int DroneId         = Util.GetDroneIdFromService(id);
            int TypeOfServiceId = Util.GetTypeOfIdFromService(id);

            ViewBag.ServiceId = id;
            var list = (from data in db.M2M_DroneServiceParts
                        where data.ServiceId == 39
                        select new {
                data.PartsId
            });
            var viewModel = new ViewModel.DroneServiceViewModel {
                DroneService = db.MSTR_DroneService.Find(id),

                ServiceType = Util.GetDropDowntLists("ServiceType", "DroneName", "Code", "usp_Portal_DroneServiceType"),
                DroneList   = Util.DroneList("usp_Portal_DroneNameList")
            };



            return(View(viewModel));
        }
        }//DroneFlightDeta

        // GET: DroneService/Create
        public ActionResult Create([Bind(Prefix = "ID")] int DroneID = 0)
        {
            if (!exLogic.User.hasAccess("SERVICE.CREATE"))
            {
                return(RedirectToAction("NoAccess", "Home"));
            }
            var viewModel = new ViewModel.DroneServiceViewModel {
                DroneService = new MSTR_DroneService(),
                DroneID      = DroneID.ToString(),
                ServiceType  = Util.GetDropDowntLists("ServiceType", "DroneName", "Code", "usp_Portal_DroneServiceType"),
                DroneList    = Util.DroneList("usp_Portal_DroneNameList"),

                //   DronePartsList=Util1.DronePartsList("usp_Portal_GetDroneParts")
            };

            return(View(viewModel));
        }
        public ActionResult Edit(ViewModel.DroneServiceViewModel DroneServiceView)
        {
            try {
                // TODO: Add update logic here
                if (!exLogic.User.hasAccess("SERVICE.EDIT"))
                {
                    return(RedirectToAction("NoAccess", "Home"));
                }
                if (ModelState.IsValid)
                {
                    MSTR_DroneService DroneService = DroneServiceView.DroneService;
                    if (Session["UserId"] == null)
                    {
                        Session["UserId"] = -1;
                    }

                    string SQL = "UPDATE MSTR_DRONESERVICE SET Description='" + DroneService.Description + "',CreatedBy=" + Session["UserId"] + ", CreatedOn='" + DroneService.DateOfService.Value.ToString("yyyy-MM-dd") +
                                 "', DroneId=" + DroneService.DroneId + ",TypeOfServiceId='" + (DroneService.TypeOfServiceId == null ? 0 : DroneService.TypeOfServiceId) + "' ,DateOfService='" + DroneService.DateOfService.Value.ToString("yyyy-MM-dd") + "', FlightHour=" + DroneService.FlightHour + " WHERE ServiceId=" + DroneService.ServiceId;

                    int ID = Util.doSQL(SQL);
                    SQL = "delete from M2M_DroneServiceParts where ServiceId =" + DroneService.ServiceId;
                    ID  = Util.doSQL(SQL);
                    if (DroneServiceView.SelectItemsForReplaced != null)
                    {
                        for (var count = 0; count < DroneServiceView.SelectItemsForReplaced.Count(); count++)
                        {
                            string PartsId = ((string[])DroneServiceView.SelectItemsForReplaced)[count];
                            int    Qty     = Util.toInt(Request["SelectItemsForReplaced_" + PartsId]);
                            SQL = "Insert into M2M_DroneServiceParts (ServiceId,PartsId,ServicePartsType,QtyCount) values(" + DroneService.ServiceId + "," + PartsId + ",'REP'," + Qty + ");";
                            ID  = Util.InsertSQL(SQL);
                        }
                    }

                    if (DroneServiceView.SelectItemsForRefurbished != null)
                    {
                        for (var count = 0; count < DroneServiceView.SelectItemsForRefurbished.Count(); count++)
                        {
                            string PartsId = ((string[])DroneServiceView.SelectItemsForRefurbished)[count];
                            int    Qty     = Util.toInt(Request["SelectItemsForRefurbished_" + PartsId]);
                            SQL = "Insert into M2M_DroneServiceParts (ServiceId,PartsId,ServicePartsType,QtyCount) values(" + DroneService.ServiceId + "," + PartsId + ",'REF'," + Qty + " );";

                            ID = Util.InsertSQL(SQL);
                        }
                    }

                    return(RedirectToAction("Index"));
                }



                var viewModel = new ViewModel.DroneServiceViewModel
                {
                    DroneService = db.MSTR_DroneService.Find(DroneServiceView.DroneService.ServiceId),

                    ServiceType = Util.GetDropDowntLists("ServiceType", "DroneName", "Code", "usp_Portal_DroneServiceType"),
                    DroneList   = Util.DroneList("usp_Portal_DroneNameList")
                };



                return(View(viewModel));
            } catch (Exception ex) {
                Util.ErrorHandler(ex);
                return(View());
            }
        }
        public ActionResult Create(ViewModel.DroneServiceViewModel DroneServiceView)
        {
            try {
                MSTR_DroneService DroneService = DroneServiceView.DroneService;
                if (!exLogic.User.hasAccess("SERVICE.CREATE"))
                {
                    return(RedirectToAction("NoAccess", "Home"));
                }
                if (DroneService.DroneId < 1 || DroneService.DroneId == null)
                {
                    ModelState.AddModelError("DroneId", "You must select a UAS.");
                }

                // TODO: Add insert logic here
                if (ModelState.IsValid)
                {
                    if (Session["UserId"] == null)
                    {
                        Session["UserId"] = -1;
                    }
                    if (DroneService.FlightHour == null)
                    {
                        DroneService.FlightHour = 0;
                    }
                    string SQL = @"INSERT INTO MSTR_DRONESERVICE(Description,CreatedBy,CreatedOn,DroneId,TypeOfServiceId,TypeOfService,DateOfService,FlightHour) 
                    VALUES('" + DroneService.Description + "'," + Session["UserId"] + ",'" + DroneService.DateOfService.Value.ToString("yyyy-MM-dd") + "','"
                                 + DroneService.DroneId + "'," + (DroneService.TypeOfServiceId == null ? 0 : DroneService.TypeOfServiceId) + ",'" + DroneService.TypeOfService + "','" + DroneService.DateOfService.Value.ToString("yyyy-MM-dd") + "'," + DroneService.FlightHour + "); ";

                    // int ID = Util.InsertSQL(SQL);
                    int ServiceId = Util.InsertSQL(SQL);

                    //   int ServiceId = Util.GetServiceId();
                    if (DroneServiceView.SelectItemsForReplaced != null)
                    {
                        if (DroneServiceView.SelectItemsForReplaced != null)
                        {
                            for (var count = 0; count < DroneServiceView.SelectItemsForReplaced.Count(); count++)
                            {
                                string PartsId = ((string[])DroneServiceView.SelectItemsForReplaced)[count];
                                int    Qty     = Util.toInt(Request["SelectItemsForReplaced_" + PartsId]);
                                SQL = "Insert into M2M_DroneServiceParts (ServiceId,PartsId,ServicePartsType,QtyCount) values(" + ServiceId + "," + PartsId + ",'REP'," + Qty + ");";

                                int ID = Util.InsertSQL(SQL);
                            }
                        }
                    }


                    if (DroneServiceView.SelectItemsForRefurbished != null)
                    {
                        for (var count = 0; count < DroneServiceView.SelectItemsForRefurbished.Count(); count++)
                        { //int PartsId = Int32.Parse((DroneServiceView.SelectItems)[2])
                            string PartsId = ((string[])DroneServiceView.SelectItemsForRefurbished)[count];
                            int    Qty     = Util.toInt(Request["SelectItemsForRefurbished_" + PartsId]);
                            SQL = "Insert into M2M_DroneServiceParts (ServiceId,PartsId,ServicePartsType,QtyCount) values(" + ServiceId + "," + PartsId + ",'REF'," + Qty + " );";

                            int ID = Util.InsertSQL(SQL);
                        }
                    }
                    return(RedirectToAction("Index"));
                }
                else
                {
                    var viewModel = new ViewModel.DroneServiceViewModel
                    {
                        DroneService = new MSTR_DroneService(),
                        ServiceType  = Util.GetDropDowntLists("ServiceType", "DroneName", "Code", "usp_Portal_DroneServiceType"),
                        DroneList    = Util.DroneList("usp_Portal_DroneNameList"),

                        //   DronePartsList=Util1.DronePartsList("usp_Portal_GetDroneParts")
                    };

                    return(View(viewModel));
                }

                // return RedirectToAction("Index");
            } catch {
                return(View());
            }
        }