<?php echo message_box('success') ?>
<?php echo message_box('error') ?>

<div class="space-y-6">
	<form action="<?php echo base_url() ?>admin/settings/save_invoice" enctype="multipart/form-data" method="post" class="space-y-6">
		<div class="rounded-2xl border border-border bg-card shadow-sm">
			<div class="flex flex-col rounded-t-2xl sm:flex-row sm:items-center sm:justify-between gap-4 border-b border-border bg-muted/50 px-6 py-4">
				<div>
					<h3 class="text-lg font-semibold text-foreground"><?= lang('invoice_settings') ?></h3>
					<p class="text-sm text-muted-foreground mt-1"><?= lang('manage') . ' ' . lang('invoice_settings') ?></p>
				</div>
				<button type="submit" class="inline-flex items-center justify-center gap-2 rounded-lg bg-primary px-6 py-2.5 text-sm font-medium text-primary-foreground shadow-sm hover:bg-primary/90 transition-colors">
					<i data-lucide="save" class="h-4 w-4"></i>
					<span><?= lang('save_changes') ?></span>
				</button>
			</div>
			<div class="p-6 space-y-6">
				<input type="hidden" name="settings" value="<?= $load_setting ?>">

				<div class="grid grid-cols-1 lg:grid-cols-2 gap-6 !mt-0">
					<div class="rounded-xl border border-border bg-muted/20 p-5 space-y-4">
						<div class="flex items-center gap-2 border-b border-border pb-3">
							<i data-lucide="hash" class="h-4 w-4 text-primary"></i>
							<h4 class="text-base font-semibold text-foreground"><?= lang('invoice') ?> <?= lang('number_format') ?></h4>
						</div>
						<?= form_input_group('invoice_prefix', lang('invoice_prefix'), config_item('invoice_prefix'), 'text', true, array('class' => '')) ?>
						<?= form_input_group('invoices_due_after', lang('invoices_due_after'), config_item('invoices_due_after'), 'text', true, array('class' => ''), '', lang('days')) ?>
						<?= form_input_group('invoice_start_no', lang('invoice_start_no'), config_item('invoice_start_no'), 'text', true, array('class' => '')) ?>

						<div class="space-y-2">
							<?=
							form_input_group(
								'invoice_number_format',
								lang('invoice') . ' ' . lang('number_format'),
								empty(config_item('invoice_number_format'))
									? '[' . config_item('invoice_prefix') . ']' . '[yyyy][mm][dd][number]'
									: config_item('invoice_number_format'),
								'text',
								false
							)
							?>
							<p class="text-xs text-muted-foreground mt-1.5">ex [<?= config_item('invoice_prefix') ?>] = <?= lang('invoice_prefix') ?>,[yyyy] = 'Current Year (<?= date('Y') ?>)'[yy] ='Current Year (<?= date('y') ?>)',[mm] = 'Current Month(<?= date('M') ?>)',[m] = 'Current Month(<?= date('m') ?>)',[dd] = 'Current Date (<?= date('d') ?>)',[number] = 'Invoice Number (<?= sprintf('%04d', config_item('invoice_start_no')) ?>)'</p>
						</div>
					</div>

					<div class="rounded-xl border border-border bg-muted/20 p-5 space-y-4">
						<div class="flex items-center gap-2 border-b border-border pb-3">
							<i data-lucide="settings" class="h-4 w-4 text-primary"></i>
							<h4 class="text-base font-semibold text-foreground"><?= lang('invoice') ?> <?= lang('settings') ?></h4>
						</div>
						<?= form_checkbox('qty_calculation_from_items', lang('qty_calculation_from_items'), config_item('qty_calculation_from_items') == 'Yes', false, array('value' => 'Yes')) ?>
						<?= form_checkbox('item_total_qty_alert', lang('item_total_qty_alert'), config_item('item_total_qty_alert') == 'Yes', false, array('value' => 'Yes')) ?>
						<?= form_checkbox('amount_to_words', lang('amount_to_words'), config_item('amount_to_words') == 'Yes', false, array('value' => 'Yes')) ?>
						<p class="text-xs text-muted-foreground -mt-2"> <?= lang('output_total_amount') . ' ' . lang('in') . ' ' . lang('invoice') . ',' . lang('payments') . ',' . lang('estimate') . ',' . lang('proposal') . ' ' . lang('and') . ' ' . lang('purchase') ?></p>
						<?= form_checkbox('amount_to_words_lowercase', lang('amount_to_words_lowercase'), config_item('amount_to_words_lowercase') == 'Yes', false, array('value' => 'Yes')) ?>
						<?= form_checkbox('allow_customer_edit_amount', lang('allow_customer_edit_amount'), config_item('allow_customer_edit_amount') == 'Yes', false, array('value' => 'Yes')) ?>
						<?= form_checkbox('increment_invoice_number', lang('increment_invoice_number'), config_item('increment_invoice_number') == 'TRUE', false, array('value' => 'TRUE')) ?>
						<?= form_checkbox('show_invoice_tax', lang('show_item_tax'), config_item('show_invoice_tax') == 'TRUE', false, array('value' => 'TRUE')) ?>
					</div>
				</div>

				<div class="grid grid-cols-1 lg:grid-cols-2 gap-6">
					<div class="rounded-xl border border-border bg-muted/20 p-5 space-y-4">
						<div class="flex items-center gap-2 border-b border-border pb-3">
							<i data-lucide="file-text" class="h-4 w-4 text-primary"></i>
							<h4 class="text-base font-semibold text-foreground"><?= lang('invoice_layout_view') ?></h4>
						</div>
						<?php
						$opt_inv = array(1 => lang('tax_invoice'), 0 => lang('standard'), 2 => lang('indian_gst'));
						?>
						<?= form_select_group('invoice_view', lang('invoice_view'), $opt_inv, config_item('invoice_view'), true, array('id' => 'invoice_view')) ?>
						<div id="states" style="display:none;">
							<?php
							$states = $this->gst->getIndianStates();
							?>
							<?= form_select_group('gst_state', lang('gst_state'), $states, config_item('gst_state'), true, array('id' => 'state', 'class' => 'select_box')) ?>
						</div>
						<?= form_checkbox('show_watermark', lang('show_watermark'), config_item('show_watermark') == 'Yes', false, array('value' => 'Yes')) ?>
						<?php
						$this->load->model('invoice_model');
						$invoice_layout_view = (!empty(config_item('invoice_layout_view')) ? config_item('invoice_layout_view') : 'default');
						$all_invoice_layout = $this->invoice_model->get_sale_layout();
						?>
						<div class="space-y-2">
							<div class="flex flex-col sm:flex-row gap-2 items-end">
								<div class="flex-1">
									<?= form_select_group('invoice_layout_view', lang('invoice_layout_view'), $all_invoice_layout, $invoice_layout_view, true, array('id' => 'invoice_layout_view')) ?>
								</div>
								<a id="invoice_layout_preview" href="<?= base_url('admin/settings/invoice_layout/' . $invoice_layout_view) ?>" target="_blank" class="inline-flex items-center justify-center gap-2 rounded-lg border border-border bg-background px-4 py-2 text-sm font-medium text-foreground hover:bg-muted transition-colors whitespace-nowrap">
									<i data-lucide="eye" class="h-4 w-4"></i>
									<span><?= lang('preview') ?></span>
								</a>
							</div>
						</div>

						<?php
						$print_options = array('landscape' => lang('landscape'), 'portrait' => lang('portrait'));
						?>
						<?= form_select_group('invoice_print_view', lang('invoice_print_type'), $print_options, config_item('invoice_print_view')) ?>
						<?= form_checkbox('send_email_when_recur', lang('send_email_when_recur'), config_item('send_email_when_recur') == 'TRUE', false, array('value' => 'TRUE')) ?>
					</div>

					<div class="rounded-xl border border-border bg-muted/20 p-5 space-y-4">
						<div class="flex items-center gap-2 border-b border-border pb-3">
							<i data-lucide="image" class="h-4 w-4 text-primary"></i>
							<h4 class="text-base font-semibold text-foreground"><?= lang('invoice_logo') ?></h4>
						</div>
						
						<div class="space-y-4">
							<div class="rounded-lg overflow-hidden border border-border bg-muted/10" style="width: 210px; height: 140px; display: flex; align-items: center; justify-content: center;">
								<?php if (!empty(config_item('invoice_logo'))) : ?>
									<img src="<?php echo base_url() . config_item('invoice_logo'); ?>" alt="<?= lang('invoice_logo') ?>" class="w-full h-full object-contain">
								<?php else: ?>
									<img src="<?= base_url('uploads/default_avatar.jpg') ?>" alt="<?= lang('invoice_logo') ?>" class="w-full h-full object-contain">
								<?php endif; ?>
							</div>
							<?= form_attachment_group('invoice_logo', '', 'photo', array(
								'wrap' => false,
								'select_label' => lang('choose_file'),
								'remove_label' => lang('remove'),
								'no_file_label' => lang('no_file_selected')
							)) ?>
						</div>

						<?= form_editor('default_terms', lang('default_terms'), config_item('default_terms'), false, array('class' => 'textarea')) ?>
						<?= form_editor('invoice_footer', lang('invoice_footer'), config_item('invoice_footer'), false, array('class' => 'textarea')) ?>
					</div>
				</div>
			</div>
		</div>
	</form>
</div>

<script>
	$(document).ready(function() {
		$('body').on('change', '#invoice_layout_view', function() {
			var layout = $(this).val();
			$('#invoice_layout_preview').attr('href', '<?= base_url('admin/settings/invoice_layout/') ?>' + layout);
		});
		$('#invoice_view').change(function() {
			if ($(this).val() == 2) {
				$('#states').show();
			} else {
				$('#states').hide();
			}
		});
		if ($('#invoice_view').val() == 2) {
			$('#states').show();
		} else {
			$('#states').hide();
		}
	});
</script>
