<% Option Explicit %>
<!--#include file="../../include_all.asp"-->
<!--#include file="../../ViewModels/SettingsViewModels.asp"-->
<%
Class SettingsController
	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                 = "Settings"
		set Model.Settings          = SettingsRepository.FindPaged(empty, "ID", 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/Settings/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                 = "Settings"
		set Model.Settings          = SettingsRepository.SearchTablePaged(Array("ID","%" & searchValue & "%","Name","%" & searchValue & "%","Value","%" & searchValue & "%"),"ID", 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/Settings/Index.asp"--> <%

	End Sub

	Public Sub Edit

		dim id : id = Request.QueryString("Id")
		set Model = new Edit_ViewModel_Class
		set Model.Settings = SettingsRepository.FindByID(id)
		Model.Title = "Edit Settings"
		HTMLSecurity.SetAntiCSRFToken "SettingsEditForm"
		%> <!--#include file="../../Views/Settings/Edit.asp"--> <%
		Flash.Success = "Settings  Updated."
	End Sub

	Public Sub EditPost
		MVC.RequirePost
		HTMLSecurity.OnInvalidAntiCsrfTokenRedirectToActionExt "SettingsEditForm", Request.Form("nonce"), "Edit", Array("Id", Request.Form("Id"))
		dim ID : ID = Request.Form("Id")
		dim model : set model = SettingsRepository.FindByID(ID)
		set model = Automapper.AutoMap(Request.Form, model)
		'model.Validate
		'If model.Validator.HasErrors then
			FormCache.SerializeForm "EditSettings", Request.Form
		'	Flash.Errors = model.Validator.Errors
		'	MVC.RedirectToActionExt "Edit", Array("Id",ID)
		'Else
			SettingsRepository.Update model
			FormCache.ClearForm "EditSettings"
			Flash.Success = "Settings updated."
			MVC.RedirectToAction "Index"
		'End If
	End Sub

	Public Sub Create

		dim form_params : set form_params = FormCache.DeserializeForm("NewSettings")
		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 "SettingsCreateForm"

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

	Public Sub CreatePost

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

			dim new_Settings_model : set new_Settings_model = Automapper.AutoMap(Request.Form, new SettingsModel_Class)

			'new_Settings_model.Validator.Validate

			'If new_Settings_model.Validator.HasErrors then
			'	FormCache.SerializeForm "NewSettings", Request.Form
			'	Flash.Errors = new_Settings_model.Validator.Errors
			'	MVC.RedirectToAction "Create"
			'Else
				SettingsRepository.AddNew new_Settings_model
			'	FormCache.ClearForm "NewSettings"
				Flash.Success = "Settings 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.Settings = SettingsRepository.FindByID(id)
			Model.Title = "Delete Settings"

		HTMLSecurity.SetAntiCSRFToken "SettingsDeleteForm"

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

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

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

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

End Class
MVC.Dispatch
%>
