<?php
require_once __DIR__.'/../includes/functions.php';
require_once __DIR__.'/../includes/v292_admin_portal_helpers.php';
$admin=require_admin(); mj292_ensure_schema(); verify_csrf();
$adminTitle='Automation Settings'; $adminSubtitle='Automate everything possible, except manual payment proof approval and wallet top-up approval.';
$keys=['auto_approve_listings','auto_approve_companies','auto_listing_quality_check','auto_duplicate_listing_check','auto_create_renewal_invoice','auto_renewal_invoice_days','abandoned_invoice_followup_days','abandoned_invoice_cancel_days','manual_payment_approval_required','wallet_topup_admin_approval_required','high_amount_approval_threshold'];
if($_SERVER['REQUEST_METHOD']==='POST'){
 foreach($keys as $k){ $v=(string)($_POST[$k] ?? '0'); try{ q('INSERT INTO mj_settings(setting_key,setting_value) VALUES(?,?) ON DUPLICATE KEY UPDATE setting_value=VALUES(setting_value)',[$k,$v]); $GLOBALS['mj_setting_cache'][$k]=$v; }catch(Throwable $e){} }
 admin_log('automation_settings_update','mj_settings',0,['keys'=>$keys]); flash('success','Automation settings saved.'); redirect('automation-settings.php');
}
include __DIR__.'/_header.php';
?>
<div class="alert alert-warning"><b>Important:</b> User-to-admin manual payment proof approval and wallet top-up approval stay manual. Customer-to-seller payments stay disabled. After approval, invoice, receipt, wallet ledger, activation and reports are automatic.</div>
<form method="post" class="card mj-v292-card"><div class="card-body"><?=csrf_input()?>
<div class="row">
<?php foreach($keys as $k): $label=ucwords(str_replace('_',' ',$k)); $val=setting($k,''); ?>
  <div class="col-md-6 mb-3"><label><?=e($label)?></label><input class="form-control" name="<?=e($k)?>" value="<?=e($val)?>"><small class="text-muted"><?=in_array($k,['manual_payment_approval_required','wallet_topup_admin_approval_required'],true)?'Keep 1 for finance safety.':'Automation control value.'?></small></div>
<?php endforeach; ?>
</div><button class="btn btn-primary"><i class="fas fa-save mr-1"></i>Save Automation Settings</button></div></form>
<?php include __DIR__.'/_footer.php'; ?>
