//
        // GET: /Opportunity/Create

        public ActionResult Create()
        {
            var accounts = _accountService.ListAccounts();
            var viewModel = new OpportunityViewModel
            {
                Accounts = new SelectList(accounts, "Id", "Name")
            };

            return View(viewModel);
        }
        public ActionResult Create([Bind(Exclude = "Id")]Opportunity opportunity)
        {           
            string userName = this.User.Identity.Name;
            int accountId = Int32.Parse(Request.Form["Opportunity.AccountId"]);/*we prefix, as the account is inside the Opportunity editor*/
            
            //we get the user responsible for the opportunity
            User responsibleUser = _userService.GetUser(userName);
            //we get the account from the db
            Account account = _accountService.GetAccount(accountId);

            opportunity.ResponsibleUser = responsibleUser;
            opportunity.Account = account;

            if (!_opportunityService.CreateOpportunity(opportunity))
            {
                var accounts = _accountService.ListAccounts();
                var viewModel = new OpportunityViewModel(opportunity)
                {
                    Accounts = new SelectList(accounts, "Id", "Name")
                };

                return View(viewModel);
            }
            
            return RedirectToAction("Index");
        }
        public ActionResult Edit(int id, FormCollection formValues)
        {
            // we retrieve existing opportunity from the db
            Opportunity opportunity = _opportunityService.GetOpportunity(id);

            if (opportunity == null)
            {
                Response.StatusCode = (int)HttpStatusCode.NotFound;
                return View("NotFound");
            }

            // we update the opportunity with form posted values
            TryUpdateModel(opportunity, "Opportunity" /*prefix, as the opportunity is inside the Opportunity editor"*/);

            int accountId = Int32.Parse(Request.Form["Opportunity.AccountId"]);/*we prefix, as the account is inside the Opportunity editor*/

            //if the opportunity account has changed, we update it in the model
            if (accountId != opportunity.Account.Id)
            {
                opportunity.Account = _accountService.GetAccount(accountId);
            }


            if (!_opportunityService.EditOpportunity(opportunity))
            {
                var accounts = _accountService.ListAccounts();
                var viewModel = new OpportunityViewModel(opportunity)
                {
                    Accounts = new SelectList(accounts, "Id", "Name")
                };

                return View(viewModel);
            }

            return RedirectToAction("Index");
        }
        //
        // GET: /Opportunity/Edit/5

        public ActionResult Edit(int id)
        {
            //getting the opportunity to edit
            Opportunity opportunity = _opportunityService.GetOpportunity(id);

            if (opportunity == null)
            {
                Response.StatusCode = (int)HttpStatusCode.NotFound;
                return View("NotFound");
            }

            var accounts = _accountService.ListAccounts();
            var viewModel = new OpportunityViewModel(opportunity)
            {
                Accounts = new SelectList(accounts, "Id", "Name")
            };

            return View(viewModel);

        }