<?php
require_once __DIR__.'/../includes/functions.php';
$admin=require_admin(); verify_csrf(); mj_automation_ensure_schema(); $adminTitle='Invoice Settings';

if($_SERVER['REQUEST_METHOD']==='POST'){
  foreach([
    'invoice_prefix','invoice_seller_name','invoice_seller_address','invoice_seller_email',
    'invoice_tax_label','invoice_tax_rate','invoice_footer_note','invoice_show_payment_note'
  ] as $k){
    mj_setting_set($k, trim((string)($_POST[$k] ?? '')));
  }
  flash('success','Invoice settings saved.');
  redirect('invoice-settings.php');
}
include __DIR__.'/_header.php'; ?>
<div class="card card-primary card-outline">
  <div class="card-header"><h3 class="card-title"><i class="fas fa-file-invoice mr-2"></i>Invoice Settings</h3></div>
  <form method="post">
    <div class="card-body">
      <?php show_flash(); ?><?=csrf_input()?>
      <div class="alert alert-info">Printable invoice view is available for orders. PDF can be added later if needed.</div>
      <div class="row">
        <div class="col-md-4"><label>Invoice Prefix</label><input class="form-control" name="invoice_prefix" value="<?=e(setting('invoice_prefix','MJ-INV'))?>"></div>
        <div class="col-md-4"><label>Seller / Marketplace Name</label><input class="form-control" name="invoice_seller_name" value="<?=e(setting('invoice_seller_name',setting('site_name','MJ Marketplace')))?>"></div>
        <div class="col-md-4"><label>Seller Email</label><input class="form-control" name="invoice_seller_email" value="<?=e(setting('invoice_seller_email',''))?>"></div>
        <div class="col-md-8 mt-3"><label>Seller Address</label><textarea class="form-control" rows="3" name="invoice_seller_address"><?=e(setting('invoice_seller_address',''))?></textarea></div>
        <div class="col-md-2 mt-3"><label>Tax Label</label><input class="form-control" name="invoice_tax_label" value="<?=e(setting('invoice_tax_label','Tax'))?>"></div>
        <div class="col-md-2 mt-3"><label>Tax Rate %</label><input class="form-control" type="number" step="0.01" min="0" name="invoice_tax_rate" value="<?=e(setting('invoice_tax_rate','0'))?>"></div>
        <div class="col-md-12 mt-3"><label>Footer Note</label><textarea class="form-control" rows="3" name="invoice_footer_note"><?=e(setting('invoice_footer_note','Thank you for using MJ Marketplace.'))?></textarea></div>
      </div>
      <div class="custom-control custom-switch mt-3">
        <input type="checkbox" class="custom-control-input" id="paynote" name="invoice_show_payment_note" value="1" <?=setting('invoice_show_payment_note','1')==='1'?'checked':''?>>
        <label class="custom-control-label" for="paynote">Show payment status/note on invoice</label>
      </div>
    </div>
    <div class="card-footer"><button class="btn btn-primary"><i class="fas fa-save mr-1"></i>Save Invoice Settings</button></div>
  </form>
</div>
<?php include __DIR__.'/_footer.php'; ?>
