<?php $this->load->helper('az_config'); ?>
<!DOCTYPE html>
<html>
<head>
<title>Jam Akses User</title>
<link rel="stylesheet" href="<?php echo base_url();?>assets/plugins/bootstrap/bootstrap.min.css">
<style>
body{padding:20px;background:#f5f5f5;}
.container{max-width:760px;margin:0 auto;background:#fff;padding:24px;border-radius:8px;box-shadow:0 2px 10px rgba(0,0,0,.1);}
h2{color:#333;border-bottom:2px solid #2196F3;padding-bottom:10px;}
.navbtn{margin-bottom:14px;}
.navbtn a{margin-right:6px;}
.ok{background:#d4edda;color:#155724;padding:10px;border-radius:5px;margin-bottom:14px;}
.err{background:#f8d7da;color:#721c24;padding:10px;border-radius:5px;margin-bottom:14px;}
.switch{padding:12px;border-radius:6px;margin-bottom:16px;font-weight:bold;}
.switch.on{background:#d4edda;color:#155724;}
.switch.off{background:#fff3cd;color:#856404;}
table.sched{width:100%;border-collapse:collapse;margin-top:10px;}
table.sched th,table.sched td{border:1px solid #ddd;padding:8px;text-align:center;}
table.sched th{background:#f5f5f5;}
.def{color:#888;font-size:12px;}
</style>
</head>
<body>
<div class="container">
<h2><i class="fa fa-clock-o"></i> Jam Akses User</h2>

<div class="navbtn">
  <a href="<?php echo base_url();?>index.php/access_schedule" class="btn btn-primary btn-sm">Jadwal User</a>
  <a href="<?php echo base_url();?>index.php/access_schedule/holiday" class="btn btn-default btn-sm">Hari Libur</a>
  <a href="<?php echo base_url();?>index.php/home" class="btn btn-default btn-sm">Kembali</a>
</div>

<?php if ($this->session->flashdata('ok')): ?><div class="ok"><?php echo $this->session->flashdata('ok'); ?></div><?php endif; ?>

<div class="switch <?php echo $enabled?'on':'off'; ?>">
  Saklar Pembatasan Jam: <b><?php echo $enabled?'NYALA (aktif)':'MATI (semua bebas)'; ?></b>
  &nbsp;
  <a href="<?php echo base_url();?>index.php/access_schedule/toggle_enabled" class="btn btn-<?php echo $enabled?'warning':'success'; ?> btn-xs"
     onclick="return confirm('Yakin mengubah saklar pembatasan jam?')">
     <?php echo $enabled?'Matikan sekarang':'Nyalakan sekarang'; ?>
  </a>
  <div class="def" style="font-weight:normal;margin-top:6px;">
    Default jam (kalau user belum diatur): <b><?php echo $default_start.' - '.$default_end; ?></b>.
    Administrator selalu bebas & tidak pernah terblokir.
  </div>
</div>

<form method="get" action="<?php echo base_url();?>index.php/access_schedule" style="margin-bottom:10px;">
  <label>Pilih User:</label>
  <select name="iduser" class="form-control" onchange="this.form.submit()">
    <?php foreach ($users as $u): ?>
      <option value="<?php echo (int)$u->iduser; ?>" <?php echo ((int)$u->iduser===$iduser)?'selected':''; ?>>
        <?php echo htmlspecialchars($u->name); ?> (<?php echo htmlspecialchars($u->username); ?>) - <?php echo htmlspecialchars($u->role_name); ?>
      </option>
    <?php endforeach; ?>
  </select>
</form>

<?php if ($iduser > 0): ?>
<form method="post" action="<?php echo base_url();?>index.php/access_schedule/save_schedule">
  <input type="hidden" name="iduser" value="<?php echo $iduser; ?>">
  <table class="sched">
    <tr><th>Hari</th><th>Libur?</th><th>Jam Mulai</th><th>Jam Selesai</th><th>Ket</th></tr>
    <?php foreach ($days as $d): $x=$d['data']; ?>
    <tr>
      <td><b><?php echo $d['name']; ?></b></td>
      <td><input type="checkbox" name="is_off_<?php echo $d['dow']; ?>" value="1" <?php echo $x['is_off']?'checked':''; ?>></td>
      <td><input type="time" name="start_<?php echo $d['dow']; ?>" value="<?php echo $x['start']; ?>"></td>
      <td><input type="time" name="end_<?php echo $d['dow']; ?>" value="<?php echo $x['end']; ?>"></td>
      <td class="def"><?php echo $x['custom']?'sudah diatur':'default'; ?></td>
    </tr>
    <?php endforeach; ?>
  </table>
  <div style="margin-top:12px;">
    <button type="submit" class="btn btn-primary"><i class="fa fa-save"></i> Simpan Jadwal</button>
    <a href="<?php echo base_url();?>index.php/access_schedule/reset_schedule?iduser=<?php echo $iduser; ?>"
       class="btn btn-default" onclick="return confirm('Kembalikan semua hari user ini ke default?')">Reset ke Default</a>
  </div>
  <div class="def" style="margin-top:8px;">Centang “Libur?” untuk menutup akses di hari itu. Kosongkan jam = pakai default saat simpan.</div>
</form>
<?php else: ?>
  <div class="err">Belum ada user (non‑admin) untuk diatur.</div>
<?php endif; ?>

</div>
</body>
</html>