<% Option Explicit %>
<!--#include file="../../include_all.asp"-->
<!--#include file="../../ViewModels/JurisdictionViewModels.asp"-->
<%
Class JurisdictionController
	Public Model

	Public Sub Index

		dim page_size : page_size = 10
		dim page_num  : page_num  = Choice(Len(Request.Querystring("page_num")) > 0, Request.Querystring("page_num"), 1)
		dim page_count, record_count
		set Model                   = new PagedIndex_ViewModel_Class
		Model.Title                 = "Jurisdiction"
		set Model.Jurisdiction          = JurisdictionRepository.FindPaged(empty, "JCode", page_size, page_num, page_count, record_count)
		Model.CurrentPageNumber     = page_num
		Model.PageSize              = page_size
		Model.PageCount             = page_count
		Model.RecordCount           = record_count
		%> <!--#include file="../../Views/Jurisdiction/Index.asp"--> <%

	End Sub
	Public Sub Search

    	dim searchValue:searchValue = Choice(Request.Form.Count = 0,Request.QueryString("q"),Request.Form("q"))
		dim page_size : page_size = 10
		dim page_num  : page_num  = Choice(Len(Request.Querystring("page_num")) > 0, Request.Querystring("page_num"), 1)
		dim page_count, record_count
		set Model                   = new PagedIndex_ViewModel_Class
		Model.Title                 = "Jurisdiction"
		set Model.Jurisdiction          = JurisdictionRepository.SearchTablePaged(Array("JCode","%" & searchValue & "%","Name","%" & searchValue & "%","Mailing_Address","%" & searchValue & "%","CSZ","%" & searchValue & "%","IMB","%" & searchValue & "%","IMB_Digits","%" & searchValue & "%"),"JCode", page_size, page_num, page_count, record_count)
		Model.CurrentPageNumber     = page_num
		Model.PageSize              = page_size
		Model.PageCount             = page_count
		Model.RecordCount           = record_count
		%> <!--#include file="../../Views/Jurisdiction/Index.asp"--> <%

	End Sub

	Public Sub Edit

		dim id : id = Request.QueryString("Id")
		set Model = new Edit_ViewModel_Class
		set Model.Jurisdiction = JurisdictionRepository.FindByJCode(id)
		set Model.Contacts = ContactsRepository.Find(Array("JURISCODE = ?",id),empty)
		Model.Title = "Edit Jurisdiction"
		HTMLSecurity.SetAntiCSRFToken "JurisdictionEditForm"
		%> <!--#include file="../../Views/Jurisdiction/Edit.asp"--> <%
		Flash.Success = "Jurisdiction  Updated."
	End Sub

	Public Sub EditPost
		MVC.RequirePost
		HTMLSecurity.OnInvalidAntiCsrfTokenRedirectToActionExt "JurisdictionEditForm", Request.Form("nonce"), "Edit", Array("Id", Request.Form("Id"))
		dim JCode : JCode = Request.Form("Id")
		dim model : set model = JurisdictionRepository.FindByJCode(JCode)
		set model = Automapper.AutoMap(Request.Form, model)
		'model.Validate
		'If model.Validator.HasErrors then
			FormCache.SerializeForm "EditJurisdiction", Request.Form
		'	Flash.Errors = model.Validator.Errors
		'	MVC.RedirectToActionExt "Edit", Array("Id",JCode)
		'Else
			JurisdictionRepository.Update model
			FormCache.ClearForm "EditJurisdiction"
			Flash.Success = "Jurisdiction updated."
			MVC.RedirectToAction "Index"
		'End If
	End Sub

	Public Sub Create

		dim form_params : set form_params = FormCache.DeserializeForm("NewJurisdiction")
		If Not form_params Is Nothing then
			set Model = Automapper.AutoMap(form_params, New Create_ViewModel_Class)
		Else
			set Model = new Create_ViewModel_Class
		End If

  		HTMLSecurity.SetAntiCSRFToken "JurisdictionCreateForm"

		%> <!--#include file="../../Views/Jurisdiction/Create.asp"--> <%
	End Sub

	Public Sub CreatePost

		MVC.RequirePost
			HtmlSecurity.OnInvalidAntiCSRFTokenRedirectToAction "JurisdictionCreateForm", Request.Form("nonce"), "Create"

			dim new_Jurisdiction_model : set new_Jurisdiction_model = Automapper.AutoMap(Request.Form, new JurisdictionModel_Class)

			'new_Jurisdiction_model.Validator.Validate

			'If new_Jurisdiction_model.Validator.HasErrors then
			'	FormCache.SerializeForm "NewJurisdiction", Request.Form
			'	Flash.Errors = new_Jurisdiction_model.Validator.Errors
			'	MVC.RedirectToAction "Create"
			'Else
				JurisdictionRepository.AddNew new_Jurisdiction_model
			'	FormCache.ClearForm "NewJurisdiction"
				Flash.Success = "Jurisdiction added."
				MVC.RedirectToAction "Index"
			'End If
	End Sub

	Public Sub Delete
		dim id : id = Request.QueryString("Id")
		set Model = new Delete_ViewModel_Class
		set Model.Jurisdiction = JurisdictionRepository.FindByJCode(id)
			Model.Title = "Delete Jurisdiction"

		HTMLSecurity.SetAntiCSRFToken "JurisdictionDeleteForm"

		%> <!--#include file="../../Views/Jurisdiction/Delete.asp"--> <%
	End Sub

	Public Sub DeletePost
			MVC.RequirePost
			HtmlSecurity.OnInvalidAntiCSRFTokenRedirectToAction "JurisdictionDeleteForm", Request.Form("nonce"), "Create"

			dim id : id = Request.Form("Id")
			JurisdictionRepository.Delete id

			Flash.Success = "Jurisdiction deleted."
			MVC.RedirectToAction "Index"
	End Sub

	Public Sub Createkit
				dim id : id = Request.QueryString("Id")
		set Model = new Delete_ViewModel_Class
		set Model.Jurisdiction = JurisdictionRepository.FindByJCode(id)
			Model.Title = "Create Kit"

		HTMLSecurity.SetAntiCSRFToken "JurisdictionDeleteForm"

		%> <!--#include file="../../Views/Jurisdiction/createkit.asp"--> <%

	End sub

		Public Sub CreateKitPost
		MVC.RequirePost
		HtmlSecurity.OnInvalidAntiCSRFTokenRedirectToAction "CreateKitForm", Request.Form("nonce"), "Index"
		dim id : id = Request.QueryString("Id")
		set Model = new Delete_ViewModel_Class
		set Model.Jurisdiction = JurisdictionRepository.FindByJCode(id)
			Model.Title = "Create Kit"

		HTMLSecurity.SetAntiCSRFToken "JurisdictionDeleteForm"

		%> <!--#include file="../../Views/Jurisdiction/createkit.asp"--> <%

	End sub

		Public Sub Import

		dim page_size : page_size = 10
		dim page_num  : page_num  = Choice(Len(Request.Querystring("page_num")) > 0, Request.Querystring("page_num"), 1)
		dim page_count, record_count
		set Model                   = new PagedIndex_ViewModel_Class
		Model.Title                 = "Jurisdiction"
		set Model.Jurisdiction          = JurisdictionRepository.FindPaged(empty, "JCode", page_size, page_num, page_count, record_count)
		Model.CurrentPageNumber     = page_num
		Model.PageSize              = page_size
		Model.PageCount             = page_count
		'Model.RecordCount           = record_count
		%> <!--#include file="../../Views/Jurisdiction/import.asp"--> <%

	End Sub

		Public Sub ImportPost
	
		Dim Upload:Set Upload = New FreeASPUpload
		Upload.Save(server.mappath("/uploads"))
		Chilkat_CSV.LoadFile(Upload.UploadedFiles("filename").path)
		set Model                   = new PagedIndex_ViewModel_Class

		Model.RecordCount = Chilkat_CSV.NumRows
		Dim RowNumber
		On Error Resume Next
		for RowNumber = 1 to  Model.RecordCount

		Dim Record: Set Record = JurisdictionRepository.FindByJCode(Chilkat_CSV.GetCell(CLng(RowNumber),0))

		Next
		On Error Goto 0
		%> <!--#include file="../../Views/Jurisdiction/import.asp"--> <%

	End Sub
End Class
MVC.Dispatch
%>
