<!--- 
  Filename: FilmEntry3.cfm
  Author:   Nate Weiss (NMW)
  Purpose:  Demonstrates use of stored procedures
--->


<!--- Is the form being submitted? --->
<cfset wasFormSubmitted = isDefined("FORM.RatingID")>

<!--- Insert film into database when form is submitted --->
<cfif wasFormSubmitted>
  <!--- Insert film via Stored Procedure (via custom tag) --->
  <cf_spInsertFilm
    movieTitle="#FORM.MovieTitle#"
    pitchText="#FORM.PitchText#"
    ratingID="#FORM.RatingID#"
    amountBudgeted="#FORM.AmountBudgeted#"
    summary="#FORM.Summary#"
    returnFilmID="InsertedFilmID"
    returnStatusCode="InsertStatus">
</cfif>

<!--- Get list of ratings from database --->
<cfstoredproc procedure="FetchRatingsList" datasource="owsSqlServer">
  <cfprocresult name="GetRatings">  
</cfstoredproc>


<html>
<head><title>Film Entry Form</title></head>
<body>
<h2>Film Entry Form</h2>

<!--- Data entry form --->
<cfform action="#CGi.script_name#" method="post" preservedata="Yes">

  <!--- Text entry field for film title --->
  <p><b>Title for New Film:</b><br>
  <cfinput name="movieTitle" size="50" maxlength="50" required="Yes" 
    message="Please don't leave the film's title blank."><br>

  <!--- Text entry field for pitch text --->
  <p><b>Short Description / One-Liner:</b><br>
  <cfinput name="pitchText" size="50" maxlength="100" required="Yes" 
    message="Please don't leave the one-liner blank."><br>
  
  <!--- Text entry field for expense description --->
  <p><b>New Film Budget:</b><br>
  <cfinput name="amountBudgeted" size="15" required="Yes" 
    message="Please enter a valid number for the film's budget."
    validate="float"><br>

  <!--- Drop-down list of ratings --->  
  <p><b>Rating:</b><br>
  <cfselect name="ratingID" query="getRatings" value="RatingID" display="Rating"/>

  <!--- Text areas for summary --->
  <p><b>Summary:</b><br>
  <cftextarea name="summary" cols="40" rows="3" wrap="soft"></cftextarea>  
    
  <!--- Submit button for form --->
  <p><cfinput type="submit" name="submit" value="Submit New Film">
</cfform>  



<!--- If we executed the stored procedure --->
<cfif wasFormSubmitted>
  <!--- Display message based on status code reported by stored procedure --->
  <cfswitch expression="#insertStatus#">
    <!--- If the stored procedure returned a "success" status --->
    <cfcase value="1">
      <cfoutput>
        <p>Film "#FORM.movieTitle#" was inserted as Film ID #insertedFilmID#.<br>
      </cfoutput>  
    </cfcase>
    <!--- If the status code was -1 --->
    <cfcase value="-1">
      <cfoutput>
        <p>Film "#FORM.movieTitle#" already exists in the database.<br>
      </cfoutput>  
    </cfcase>
    <!--- If the status code was -2 --->
    <cfcase value="-2">
      <p>An invalid rating was provided.<br>
    </cfcase>
    <!--- If any other status code was returned --->
    <cfdefaultcase>
      <p>The procedure returned an unknown status code.<br>
    </cfdefaultcase>
  </cfswitch>
</cfif>
  
  
</body>
</html>
