<?php require_once __DIR__.'/../includes/functions.php'; $admin=require_admin(); $adminTitle='States & Cities'; verify_csrf();
if($_SERVER['REQUEST_METHOD']==='POST'){
    if($_POST['type']==='state') q('INSERT INTO mj_states(country_id,name,state_code,slug,timezone,is_active) VALUES(?,?,?,?,?,1) ON DUPLICATE KEY UPDATE name=VALUES(name),timezone=VALUES(timezone),is_active=1',[(int)$_POST['country_id'],trim($_POST['name']),trim($_POST['state_code']),slugify($_POST['name']),trim($_POST['timezone'])]);
    if($_POST['type']==='city') q('INSERT INTO mj_cities(country_id,state_id,name,slug,timezone,is_active) VALUES(?,?,?,?,?,1)',[(int)$_POST['country_id'],$_POST['state_id']?:null,trim($_POST['name']),slugify($_POST['name']),trim($_POST['timezone'])]);
    flash('success','Location saved.'); redirect('locations.php');
}
$countries=all_rows('SELECT * FROM mj_countries ORDER BY name'); $states=all_rows('SELECT s.*,c.name country FROM mj_states s JOIN mj_countries c ON c.id=s.country_id ORDER BY s.id DESC LIMIT 150'); $cities=all_rows('SELECT ci.*,c.name country,s.name state FROM mj_cities ci JOIN mj_countries c ON c.id=ci.country_id LEFT JOIN mj_states s ON s.id=ci.state_id ORDER BY ci.id DESC LIMIT 150'); include __DIR__.'/_header.php'; ?>
<div class="row"><div class="col-lg-6"><div class="card card-primary card-outline"><div class="card-header"><h3 class="card-title">Add State / Province</h3></div><form method="post"><div class="card-body"><?=csrf_input()?><input type="hidden" name="type" value="state"><div class="form-group"><label>Country</label><select class="form-control" name="country_id"><?php foreach($countries as $c): ?><option value="<?=e($c['id'])?>"><?=e($c['flag_emoji'].' '.$c['name'])?></option><?php endforeach; ?></select></div><div class="form-row"><div class="form-group col-md-6"><label>State name</label><input class="form-control" name="name" required></div><div class="form-group col-md-6"><label>State code</label><input class="form-control" name="state_code"></div></div><div class="form-group"><label>Timezone</label><input class="form-control" name="timezone" placeholder="Asia/Singapore" required></div></div><div class="card-footer"><button class="btn btn-primary">Add State</button></div></form></div></div><div class="col-lg-6"><div class="card card-success card-outline"><div class="card-header"><h3 class="card-title">Add City</h3></div><form method="post"><div class="card-body"><?=csrf_input()?><input type="hidden" name="type" value="city"><div class="form-group"><label>Country</label><select class="form-control" name="country_id"><?php foreach($countries as $c): ?><option value="<?=e($c['id'])?>"><?=e($c['flag_emoji'].' '.$c['name'])?></option><?php endforeach; ?></select></div><div class="form-row"><div class="form-group col-md-4"><label>State ID</label><input class="form-control" name="state_id"></div><div class="form-group col-md-4"><label>City name</label><input class="form-control" name="name" required></div><div class="form-group col-md-4"><label>Timezone</label><input class="form-control" name="timezone" placeholder="Asia/Singapore" required></div></div></div><div class="card-footer"><button class="btn btn-success">Add City</button></div></form></div></div></div>
<div class="row"><div class="col-lg-6"><div class="card"><div class="card-header"><h3 class="card-title">Latest States</h3></div><div class="card-body"><table class="table table-striped mj-data-table"><thead><tr><th>ID</th><th>Country</th><th>State</th><th>Code</th><th>Timezone</th></tr></thead><tbody><?php foreach($states as $s): ?><tr><td><?=e($s['id'])?></td><td><?=e($s['country'])?></td><td><?=e($s['name'])?></td><td><?=e($s['state_code'])?></td><td><?=e($s['timezone'])?></td></tr><?php endforeach; ?></tbody></table></div></div></div><div class="col-lg-6"><div class="card"><div class="card-header"><h3 class="card-title">Latest Cities</h3></div><div class="card-body"><table class="table table-striped mj-data-table"><thead><tr><th>ID</th><th>Country</th><th>State</th><th>City</th><th>Timezone</th></tr></thead><tbody><?php foreach($cities as $c): ?><tr><td><?=e($c['id'])?></td><td><?=e($c['country'])?></td><td><?=e($c['state'])?></td><td><?=e($c['name'])?></td><td><?=e($c['timezone'])?></td></tr><?php endforeach; ?></tbody></table></div></div></div></div>
<?php include __DIR__.'/_footer.php'; ?>
