<?php
namespace My\Component\GrafikKeuangan\Administrator\Model;

defined('_JEXEC') or die;

use Joomla\CMS\MVC\Model\AdminModel;

class ItemModel extends AdminModel
{
	public function getTable($type = 'GrafikKeuangan', $prefix = 'Table', $config = [])
	{
		return new \My\Component\GrafikKeuangan\Administrator\Table\GrafikKeuangan(\Joomla\CMS\Factory::getContainer()->get('DatabaseDriver'));
	}

	public function getForm($data = [], $loadData = true)
	{
		$form = $this->loadForm(
			'com_grafik_keuangan.item',
			'item',
			['control' => 'jform', 'load_data' => $loadData]
		);

		if (empty($form)) {
			return false;
		}

		return $form;
	}

	protected function loadFormData()
	{
		$data = $this->getState('item.data', []);

		if (empty($data)) {
			$data = $this->getItem();
		}

		return $data;
	}

	protected function prepareTable($table)
	{
		// Auto-populate created/modified dates
		$date = date('Y-m-d H:i:s');
		$user = \Joomla\CMS\Factory::getUser();

		if (empty($table->id)) {
			$table->created = $date;
			$table->created_by = $user->id;
		} else {
			$table->modified = $date;
			$table->modified_by = $user->id;
		}
	}
}
