<?php require_once __DIR__.'/../includes/functions.php'; $admin=require_admin(); $adminTitle='Import Locations'; verify_csrf();
if($_SERVER['REQUEST_METHOD']==='POST' && !empty($_FILES['csv']['tmp_name'])){
    $type=$_POST['type']; $fh=fopen($_FILES['csv']['tmp_name'],'r'); $header=fgetcsv($fh); $map=array_flip($header ?: []); $total=$ok=0; $errors=[];
    while(($row=fgetcsv($fh))!==false){ $total++; try{ $code=$row[$map['country_code']] ?? ''; $country=one('SELECT * FROM mj_countries WHERE iso2=?',[$code]); if(!$country) continue; if($type==='states'){ $name=$row[$map['name']]??''; if(!$name) continue; q('INSERT INTO mj_states(country_id,name,state_code,slug,timezone,latitude,longitude,is_active) VALUES(?,?,?,?,?,?,?,1) ON DUPLICATE KEY UPDATE timezone=VALUES(timezone),latitude=VALUES(latitude),longitude=VALUES(longitude)',[$country['id'],$name,$row[$map['state_code']]??'',slugify($name),$row[$map['timezone']]??$country['timezone_default'],$row[$map['latitude']]??null,$row[$map['longitude']]??null]); $ok++; } else { $name=$row[$map['name']]??''; if(!$name) continue; $stateId=null; if(!empty($row[$map['state_code']] ?? '')){ $st=one('SELECT id FROM mj_states WHERE country_id=? AND state_code=?',[$country['id'],$row[$map['state_code']]]); $stateId=$st['id']??null; } q('INSERT INTO mj_cities(country_id,state_id,name,slug,timezone,latitude,longitude,is_active) VALUES(?,?,?,?,?,?,?,1)',[$country['id'],$stateId,$name,slugify($name),$row[$map['timezone']]??$country['timezone_default'],$row[$map['latitude']]??null,$row[$map['longitude']]??null]); $ok++; }}catch(Throwable $e){ if(count($errors)<3)$errors[]=$e->getMessage(); }}
    q('INSERT INTO mj_import_logs(import_type,filename,total_rows,success_rows,failed_rows,created_at) VALUES(?,?,?,?,?,NOW())',[$type,$_FILES['csv']['name'],$total,$ok,$total-$ok]); flash('success',"Imported $ok rows from $total."); if($errors) flash('warning','Some rows skipped: '.implode(' | ',$errors)); redirect('location-import.php');
}
$logs=all_rows('SELECT * FROM mj_import_logs WHERE import_type IN ("states","cities") ORDER BY id DESC LIMIT 20'); include __DIR__.'/_header.php'; ?>
<div class="row"><div class="col-lg-7"><div class="card card-primary card-outline"><div class="card-header"><h3 class="card-title"><i class="fas fa-file-import mr-2"></i>Import Worldwide States & Cities</h3></div><form method="post" enctype="multipart/form-data"><div class="card-body"><?=csrf_input()?><div class="alert alert-info">Import states first, then cities. Timezone should be IANA format such as <b>Asia/Singapore</b> or <b>America/New_York</b>.</div><div class="form-group"><label>Import type</label><select class="form-control" name="type"><option value="states">States CSV</option><option value="cities">Cities CSV</option></select></div><div class="form-group"><label>CSV File</label><input class="form-control" type="file" name="csv" accept=".csv" required></div></div><div class="card-footer"><button class="btn btn-primary"><i class="fas fa-upload mr-1"></i>Import</button></div></form></div></div><div class="col-lg-5"><div class="card"><div class="card-header"><h3 class="card-title">CSV Columns</h3></div><div class="card-body"><p><b>States:</b><br><code>name,country_code,state_code,timezone,latitude,longitude</code></p><p><b>Cities:</b><br><code>name,country_code,state_code,state_name,timezone,latitude,longitude</code></p></div></div></div></div>
<div class="card"><div class="card-header"><h3 class="card-title">Recent Location Imports</h3></div><div class="card-body"><table class="table table-striped mj-data-table"><thead><tr><th>Date</th><th>Type</th><th>File</th><th>Total</th><th>Success</th><th>Failed</th></tr></thead><tbody><?php foreach($logs as $l): ?><tr><td><?=e($l['created_at'])?></td><td><?=e($l['import_type'])?></td><td><?=e($l['filename'])?></td><td><?=e($l['total_rows'])?></td><td><?=e($l['success_rows'])?></td><td><?=e($l['failed_rows'])?></td></tr><?php endforeach; ?></tbody></table></div></div>
<?php include __DIR__.'/_footer.php'; ?>
