import jingrow
from jingrow.custom.pagetype.custom_field.custom_field import create_custom_field


def execute():
	# auto repeat is not submittable in v12
	jingrow.reload_pg("automation", "pagetype", "Auto Repeat")
	jingrow.db.sql("update `tabPagePerm` set submit=0, cancel=0, amend=0 where parent='Auto Repeat'")
	jingrow.db.sql("update `tabAuto Repeat` set pagestatus=0 where pagestatus=1 or pagestatus=2")

	for entry in jingrow.get_all("Auto Repeat"):
		pg = jingrow.get_pg("Auto Repeat", entry.name)

		# create custom field for allow auto repeat
		fields = jingrow.get_meta(pg.reference_pagetype).fields
		insert_after = fields[len(fields) - 1].fieldname
		df = dict(
			fieldname="auto_repeat",
			label="Auto Repeat",
			fieldtype="Link",
			insert_after=insert_after,
			options="Auto Repeat",
			hidden=1,
			print_hide=1,
			read_only=1,
		)
		create_custom_field(pg.reference_pagetype, df)

		if pg.status in ["Draft", "Stopped", "Cancelled"]:
			pg.disabled = 1

		pg.flags.ignore_links = 1
		# updates current status as Active, Disabled or Completed on validate
		pg.save()
