<?php $this->load->helper('az_config'); ?>
<!DOCTYPE html>
<html>
<head>
<title>Daftarkan Device Baru</title>
<meta name="viewport" content="width=device-width, initial-scale=1">
<link rel="stylesheet" href="<?php echo base_url();?>assets/plugins/bootstrap/bootstrap.min.css">
<link rel="stylesheet" href="<?php echo base_url();?>assets/plugins/fa/font-awesome.css">
<style>
body{padding:20px;background:#f5f5f5;} .kartu{max-width:680px;margin:0 auto;background:#fff;padding:24px;border-radius:8px;}
.codebox{background:#fff3cd;border:1px solid #ffeeba;border-radius:8px;padding:14px;margin:16px 0;}
.codebox .code{font-family:'Courier New',monospace;font-size:26px;letter-spacing:5px;font-weight:bold;color:#856404;background:#fff;display:inline-block;padding:6px 14px;border-radius:6px;border:1px dashed #c9a227;}
.listuser{max-height:240px;overflow:auto;border:1px solid #ddd;border-radius:6px;padding:8px;}
</style>
</head>
<body>
<div class="kartu">
	<h2><i class="fa fa-mobile"></i> Daftarkan Device Baru</h2>

	<?php if ($this->session->flashdata('success')): ?>
		<div style="background:#d4edda;color:#155724;padding:12px;border-radius:8px;margin:12px 0;"><?php echo $this->session->flashdata('success'); ?></div>
	<?php endif; ?>
	<?php if ($this->session->flashdata('activation_code')): ?>
		<div class="codebox">
			<b><i class="fa fa-key"></i> KODE AKTIVASI device "<?php echo htmlspecialchars($this->session->flashdata('activation_device_name')); ?>"</b><br>
			Berikan 10 karakter ini ke perangkat outlet untuk diketik di halaman login:<br>
			<span class="code" id="theCode"><?php echo htmlspecialchars($this->session->flashdata('activation_code')); ?></span>
			<button type="button" class="btn btn-sm btn-warning" onclick="var t=document.getElementById('theCode').innerText; navigator.clipboard.writeText(t).then(function(){ alert('Kode tersalin: '+t); });">Salin kode</button>
			<details style="margin-top:8px;"><summary style="cursor:pointer;color:#856404;">atau pakai link lengkap</summary>
				<input type="text" readonly onclick="this.select();document.execCommand('copy');" value="<?php echo $this->session->flashdata('activation_url'); ?>" style="width:100%;margin-top:6px;padding:6px;font-size:11px;">
			</details>
		</div>
	<?php endif; ?>

	<?php if (validation_errors()): ?><div style="background:#f8d7da;color:#721c24;padding:10px;border-radius:8px;margin:12px 0;"><?php echo validation_errors(); ?></div><?php endif; ?>
	<?php if (!empty($manual_err)): ?><div style="background:#f8d7da;color:#721c24;padding:10px;border-radius:8px;margin:12px 0;"><?php echo $manual_err; ?></div><?php endif; ?>

	<form method="POST" action="<?php echo base_url();?>login_device/register">
		<div class="form-group"><label>Outlet <span style="color:red">*</span></label>
			<select name="idoutlet" class="form-control" required><option value="">~ Pilih Outlet ~</option>
			<?php foreach ($outlets as $o): ?><option value="<?php echo (int)$o->idoutlet; ?>"><?php echo htmlspecialchars($o->outlet_name); ?></option><?php endforeach; ?>
			</select></div>
		<div class="form-group"><label>Tipe Device <span style="color:red">*</span></label>
			<select name="device_type" class="form-control" required><option value="PC">PC / Komputer</option><option value="HP">HP / Smartphone</option></select></div>
		<div class="form-group"><label>Nama Device <span style="color:red">*</span></label>
			<input type="text" name="device_name" class="form-control" placeholder="cth: PC-Kasir-01" required></div>
		<div class="form-group"><label>User yang boleh login otomatis <span style="color:red">*</span></label>
			<div class="listuser">
			<?php foreach ($users as $u): $chk = in_array($u->iduser, $selected) ? 'checked' : ''; ?>
				<div><label style="font-weight:normal;"><input type="checkbox" name="iduser[]" value="<?php echo (int)$u->iduser; ?>" <?php echo $chk; ?>>
				<b><?php echo htmlspecialchars($u->name); ?></b> (<?php echo htmlspecialchars($u->username); ?>) — <?php echo htmlspecialchars($u->role_name); ?> — <?php echo htmlspecialchars($u->outlet_name); ?></label></div>
			<?php endforeach; ?>
			</div><small>Pilih minimal 1 user.</small></div>
		<button type="submit" class="btn btn-primary"><i class="fa fa-save"></i> Daftarkan Device</button>
		<a href="<?php echo base_url();?>login_device/list_devices" class="btn btn-default">Lihat Daftar</a>
		<a href="<?php echo base_url();?>home" class="btn btn-default">Kembali</a>
	</form>
</div>
</body>
</html>