<?php defined('BASEPATH') or exit('No direct script access allowed'); ?>

<?= message_box('success'); ?>
<?= message_box('error'); ?>

<?php
$created = can_action('151', 'created');
$edited = can_action('151', 'edited');
$deleted = can_action('151', 'deleted');

if (!empty($created) || !empty($edited)) {
	$supplier_id = !empty($supplier_info) ? $supplier_info->supplier_id : null;
	$supplier_name = !empty($supplier_info->name) ? $supplier_info->name : '';
	$supplier_mobile = !empty($supplier_info->mobile) ? $supplier_info->mobile : '';
	$supplier_phone = !empty($supplier_info->phone) ? $supplier_info->phone : '';
	$supplier_email = !empty($supplier_info->email) ? $supplier_info->email : '';
	$supplier_address = !empty($supplier_info->address) ? $supplier_info->address : '';
	$supplier_vat = !empty($supplier_info->vat) ? $supplier_info->vat : '';
	$permission_value = !empty($supplier_info->permission) ? $supplier_info->permission : null;

	$page_title = !empty($supplier_info) ? (lang('edit') . ' ' . lang('supplier')) : (lang('new') . ' ' . lang('supplier'));
	$page_subtitle = !empty($supplier_info) ? $supplier_name : (lang('supplier') . ' ' . lang('details'));
?>

<?php echo form_open(
	base_url('admin/supplier/saved_supplier/' . $supplier_id),
	array('class' => 'space-y-6', 'enctype' => 'multipart/form-data', 'data-parsley-validate' => 'true', 'id' => 'supplier-form')
); ?>

<div class="space-y-6">
	<div class="flex flex-col gap-3 md:flex-row md:items-center md:justify-between">
		<div>
			<h2 class="text-xl font-semibold text-foreground"><?= $page_title; ?></h2>
			<p class="text-sm text-muted-foreground"><?= html_escape($page_subtitle); ?></p>
		</div>
		<div class="flex flex-wrap gap-2">
			<a href="<?= base_url('admin/supplier'); ?>" class="rounded-lg border border-border px-4 py-2 text-sm font-semibold text-foreground hover:border-primary">
				<?= lang('supplier') . ' ' . lang('list'); ?>
			</a>
		</div>
	</div>

	<div class="grid gap-6 xl:grid-cols-2">
		<div class="space-y-6">
			<div class="rounded-2xl border border-border bg-card shadow-sm">
				<div class="flex items-center justify-between border-b border-border px-5 py-4">
					<h3 class="text-sm font-semibold text-foreground"><?= lang('general'); ?></h3>
				</div>
				<div class="p-5 grid gap-4 md:grid-cols-2">
					<?= form_input_group('name', lang('name'), $supplier_name, 'text', true); ?>
					<?= form_input_group('mobile', lang('mobile'), $supplier_mobile, 'text', true, array('data-parsley-type' => 'number')); ?>
					<?= form_input_group('phone', lang('phone'), $supplier_phone, 'text', false, array('data-parsley-type' => 'number')); ?>
					<?= form_input_group('email', lang('email'), $supplier_email, 'email', true); ?>
				</div>
			</div>

			<div class="rounded-2xl border border-border bg-card shadow-sm">
				<div class="flex items-center justify-between border-b border-border px-5 py-4">
					<h3 class="text-sm font-semibold text-foreground"><?= lang('additional_info'); ?></h3>
				</div>
				<div class="p-5 grid gap-4 md:grid-cols-2">
					<?= form_textarea_group('address', lang('address'), $supplier_address, 3, false); ?>
					<?= form_input_group('vat', lang('vat_number'), $supplier_vat, 'text', false); ?>
				</div>
			</div>
		</div>

		<div class="space-y-6">
			<div class="rounded-2xl border border-border bg-card shadow-sm">
				<div class="flex items-center justify-between border-b border-border px-5 py-4">
					<h3 class="text-sm font-semibold text-foreground"><?= lang('additional_fields'); ?></h3>
				</div>
				<div class="p-5 space-y-4">
					<?= custom_form_Fields(19, $supplier_id); ?>
				</div>
			</div>

			<div class="rounded-2xl border border-border bg-card shadow-sm">
				<div class="flex items-center justify-between border-b border-border px-5 py-4">
					<h3 class="text-sm font-semibold text-foreground"><?= lang('permission'); ?></h3>
				</div>
				<div class="p-5 space-y-4">
					<?= form_permission_modern($permission_user, $permission_value, lang('assined_to')); ?>
				</div>
			</div>
		</div>
	</div>

	<div class="flex justify-end gap-2">
		<?php if (!empty($supplier_info)) { ?>
			<?= btn_update('updates', 'sm'); ?>
			<button type="button" onclick="goBack()" class="btn btn-danger btn-sm mb-1"><?= lang('cancel'); ?></button>
		<?php } else { ?>
			<?= btn_update('create', 'sm'); ?>
		<?php } ?>
	</div>
</div>

<?php echo form_close(); ?>

<?php } ?>
