<form id="formAdd" class="user-editor-form">
	<?php
		$komisi_desain_aktif = isset($arr->komisi_desain_aktif) ? (int) $arr->komisi_desain_aktif : 0;
		$komisi_desain_persen = isset($arr->komisi_desain_persen) ? rtrim(rtrim(number_format((float) $arr->komisi_desain_persen, 2, '.', ''), '0'), '.') : 0;
		$komisi_penjualan_aktif = isset($arr->komisi_penjualan_aktif) ? (int) $arr->komisi_penjualan_aktif : 0;
	?>
	<input type="hidden" value="<?=encrypt_url($arr->id_user);?>" class="form-control" id="id" name="id">
	<input type="hidden" value="edit" class="form-control" id="type" name="type">

	<div class="user-form-layout">
		<section class="user-form-section" aria-labelledby="identitySectionTitle">
			<div class="user-section-heading">
				<span class="user-section-icon"><i class="fa fa-id-card-o"></i></span>
				<div><h5 id="identitySectionTitle">Informasi pengguna</h5><p>Perbarui data identitas dan akun pengguna.</p></div>
			</div>
			<?php
				$profile_photo = !empty($arr->foto) ? base_url(ltrim($arr->foto, '/')) : '';
				$profile_initial = function_exists('mb_substr') ? mb_strtoupper(mb_substr(trim($arr->nama_lengkap), 0, 1)) : strtoupper(substr(trim($arr->nama_lengkap), 0, 1));
			?>
			<div class="profile-photo-field">
				<div class="profile-photo-preview" id="profilePhotoPreview"><span <?=!empty($profile_photo)?'hidden':'';?>><?=htmlspecialchars($profile_initial ?: 'U', ENT_QUOTES, 'UTF-8');?></span><img src="<?=htmlspecialchars($profile_photo, ENT_QUOTES, 'UTF-8');?>" alt="Foto profil <?=htmlspecialchars($arr->nama_lengkap, ENT_QUOTES, 'UTF-8');?>" <?=empty($profile_photo)?'hidden':'';?>></div>
				<div class="profile-photo-copy"><strong>Foto profil <span class="optional-label">Opsional</span></strong><p>Pilih foto baru untuk mengganti foto saat ini. File otomatis dioptimalkan ke WebP.</p><div class="profile-photo-actions"><label for="profile_photo" class="profile-photo-button"><i class="fa fa-camera"></i> <?=empty($profile_photo)?'Pilih foto':'Ganti foto';?></label><button type="button" class="profile-photo-remove" hidden><i class="fa fa-times"></i> Batalkan</button></div><input type="file" name="profile_photo" id="profile_photo" accept="image/jpeg,image/png,image/webp" hidden></div>
			</div>
			<div class="user-fields-grid">
				<div class="form-group"><label for="mail">Email</label><div class="readonly-field"><input type="email" name="mail" value="<?=$arr->email;?>" class="form-control" id="mail" readonly><i class="fa fa-lock" title="Email tidak dapat diubah"></i></div></div>
				<div class="form-group"><label for="password">Password baru <span class="optional-label">Opsional</span></label><div class="user-password-wrap"><input type="password" name="password" class="form-control" id="password" placeholder="Kosongkan jika tidak diubah"><button type="button" class="password-toggle" aria-label="Tampilkan password"><i class="fa fa-eye"></i></button></div></div>
				<div class="form-group"><label for="title">Nama lengkap <span class="required-mark">*</span></label><input type="text" name="title" value="<?=$arr->nama_lengkap;?>" class="form-control" id="title" placeholder="Nama lengkap pengguna" required></div>
				<div class="form-group"><label for="jabatan">Jabatan <span class="required-mark">*</span></label><input type="text" name="jabatan" value="<?=$arr->app_secret;?>" class="form-control" id="jabatan" placeholder="Contoh: Kasir" required></div>
				<div class="form-group"><label for="daftar">Tanggal daftar</label><input type="date" name="daftar" value="<?=$arr->tgl_daftar;?>" class="form-control dpd1" id="daftar"></div>
				<div class="form-group"><label for="phone">No. handphone</label><input type="text" name="phone" value="<?=$arr->no_hp;?>" class="form-control" id="phone" placeholder="08xxxxxxxxxx"></div>
				<div class="form-group user-field-full"><label for="alamat">Alamat</label><input type="text" name="alamat" value="<?=$arr->alamat;?>" class="form-control" id="alamat" placeholder="Alamat pengguna"></div>
			</div>

			<div class="user-subsection-title"><i class="fa fa-percent"></i><div><strong>Pengaturan komisi</strong><span>Aktifkan komisi sesuai peran pengguna.</span></div></div>
			<div class="commission-grid">
				<div class="form-group"><label for="komisi_desain_aktif">Komisi desain</label><select name="komisi_desain_aktif" id="komisi_desain_aktif" class="form-control custom-select"><option value="0" <?=$komisi_desain_aktif === 0 ? 'selected' : '';?>>Tidak aktif</option><option value="1" <?=$komisi_desain_aktif === 1 ? 'selected' : '';?>>Aktif</option></select></div>
				<div class="form-group"><label for="komisi_desain_persen">Persentase desain</label><div class="percentage-field"><input type="number" name="komisi_desain_persen" value="<?=$komisi_desain_persen;?>" min="0" max="100" step="0.01" class="form-control" id="komisi_desain_persen" placeholder="0"><span>%</span></div></div>
				<div class="form-group"><label for="komisi_penjualan_aktif">Komisi penjualan</label><select name="komisi_penjualan_aktif" id="komisi_penjualan_aktif" class="form-control custom-select"><option value="0" <?=$komisi_penjualan_aktif === 0 ? 'selected' : '';?>>Tidak aktif</option><option value="1" <?=$komisi_penjualan_aktif === 1 ? 'selected' : '';?>>Aktif</option></select></div>
			</div>
		</section>

		<section class="user-form-section" aria-labelledby="accessSectionTitle">
			<div class="user-section-heading">
				<span class="user-section-icon"><i class="fa fa-shield"></i></span>
				<div><h5 id="accessSectionTitle">Akses & status</h5><p>Atur ulang kewenangan dan status akun.</p></div>
			</div>
			<div class="access-menu-card">
				<div class="access-menu-header"><div><strong>Menu akses</strong><span>Pilih menu yang dapat dibuka pengguna.</span></div><label class="select-all-control" for="select_all"><input id="select_all" type="checkbox"><span>Pilih semua</span></label></div>
				<div class="over-users access-menu-list">
					<?php
						$lv = $arr->id_level;
						$resultz = $this->db->query("SELECT * FROM menuadmin where FIND_IN_SET('$lv', CONCAT(id_level, ',')) AND aktif='Y' order by urutan");
						foreach ($resultz->result_array() as $rowz){ $dataT[$rowz['idparent']][] = $rowz; }
						if(!empty($dataT)){ echo checkbox($dataT,0,$rowz['idparent'],$arr->idmenu); }
					?>
				</div>
			</div>
			<div class="user-fields-grid access-fields">
				<div class="form-group"><label for="id_level">Level akses</label><select id="id_level" name="id_level" class="form-control custom-select"><?php
					if($arr->level==="admin") { $tampil=$this->db->query("SELECT * FROM hak_akses"); foreach($tampil->result_array() AS $we){ echo '<option value="'.$we['id_level'].'" '.($arr->id_level==$we['id_level']?'selected':'').'>'.$we['nama'].'</option>'; }
					} else { $tampil=$this->db->query("select * FROM hak_akses where id_level IN ($arr->idlevel)"); if($arr->id_level==0){ echo '<option value="0" selected>Pilih Level Akses</option>'; } foreach($tampil->result_array() AS $we){ echo '<option value="'.$we['id_level'].'" '.($arr->id_level==$we['id_level']?'selected':'').'>'.$we['nama'].'</option>'; } }
				?></select></div>
				<div class="form-group"><label for="cat">Tipe akses</label><select id="cat" name="cat[]" class="form-control select2-multiple" multiple="multiple"><?php foreach($kategori as $rowz){ $dataTz[$rowz->id_parent][]=$rowz; } if(!empty($dataTz)){ echo select_kbox($dataTz,0,0,$arr->type_akses); } ?></select></div>
			</div>
			<div class="account-status-card"><div><strong>Status akun</strong><span>Akun aktif dapat masuk dan menggunakan aplikasi.</span></div><?php if($arr->level==="admin") { ?><input type="hidden" name="aktif" value="Y"><span class="admin-status-badge"><i class="fa fa-check-circle"></i> Selalu aktif</span><?php } else { ?><div class="user-status-options"><label><input type="radio" name="aktif" id="optionsRadios1" value="N" <?=$arr->aktif==='N'?'checked':'';?>><span><i class="fa fa-ban"></i> Tidak aktif</span></label><label><input type="radio" name="aktif" id="optionsRadios2" value="Y" <?=$arr->aktif==='Y'?'checked':'';?>><span><i class="fa fa-check-circle"></i> Aktif</span></label></div><?php } ?></div>
		</section>
	</div>
</form>
<script>
	$(function(){
		$('.select2-multiple').select2({placeholder:'Pilih tipe akses'});
		$('#select_all').on('change',function(){ $('.checkbox').prop('checked',this.checked); });
		$('.checkbox').on('change',function(){ $('#select_all').prop('checked',$('.checkbox:checked').length === $('.checkbox').length); });
		$('.password-toggle').on('click',function(){ var input=$('#password'); var show=input.attr('type')==='password'; input.attr('type',show?'text':'password'); $(this).find('i').toggleClass('fa-eye',!show).toggleClass('fa-eye-slash',show); $(this).attr('aria-label',show?'Sembunyikan password':'Tampilkan password'); });
		var originalPhoto=$('#profilePhotoPreview img').attr('src')||'';
		$('#profile_photo').on('change',function(){ var file=this.files&&this.files[0]; if(!file){return;} if(['image/jpeg','image/png','image/webp'].indexOf(file.type)===-1||file.size>2097152){ this.value=''; showNotif('top-center','Foto profil',file.size>2097152?'Ukuran foto maksimal 2 MB':'Format foto harus JPG, PNG, atau WebP','warning'); return; } var url=URL.createObjectURL(file); $('#profilePhotoPreview img').attr('src',url).prop('hidden',false); $('#profilePhotoPreview span').prop('hidden',true); $('.profile-photo-remove').prop('hidden',false); });
		$('.profile-photo-remove').on('click',function(){ $('#profile_photo').val(''); $('#profilePhotoPreview img').attr('src',originalPhoto).prop('hidden',!originalPhoto); $('#profilePhotoPreview span').prop('hidden',!!originalPhoto); $(this).prop('hidden',true); });
	});
</script>
