<% Option Explicit %>
<!--#include file="../../include_all.asp"-->
<!--#include file="../../ViewModels/KitLabelsViewModels.asp"-->
<%
Class KitLabelsController
	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                 = "KitLabels"
		set Model.KitLabels          = KitLabelsRepository.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/KitLabels/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                 = "KitLabels"
		set Model.KitLabels          = KitLabelsRepository.SearchTablePaged(Array("ID","%" & searchValue & "%","KitId","%" & searchValue & "%","OutboundSerial","%" & searchValue & "%","InBoundSerial","%" & searchValue & "%","OutboundIMB","%" & searchValue & "%","InBoundIMB","%" & searchValue & "%","OutboundIMBDigits","%" & searchValue & "%","InBoundIMBDigits","%" & 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/KitLabels/Index.asp"--> <%

	End Sub

	Public Sub Edit

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

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

	Public Sub Create

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

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

	Public Sub CreatePost

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

			dim new_KitLabels_model : set new_KitLabels_model = Automapper.AutoMap(Request.Form, new KitLabelsModel_Class)

			'new_KitLabels_model.Validator.Validate

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

		HTMLSecurity.SetAntiCSRFToken "KitLabelsDeleteForm"

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

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

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

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

End Class
MVC.Dispatch
%>