<?php
$pitem=$editItem??[];
$days=array_filter(explode(',',(string)($pitem['available_weekdays']??'')));
?>
<form method="post" action="actions.php" enctype="multipart/form-data">
<?= csrf_field() ?>
<input type="hidden" name="action" value="save_property">
<input type="hidden" name="return_page" value="properties">
<input type="hidden" name="id" value="<?= e($pitem['id']??'') ?>">
<?php if(is_admin()):?>
<div class="form-group"><label>Proprietário</label><select name="owner_id"><?php foreach($propertyOwners as $o):?><option value="<?= (int)$o['id'] ?>"<?= selected($pitem['owner_id']??'',$o['id']) ?>><?= e($o['name']) ?></option><?php endforeach;?></select></div>
<?php endif;?>
<div class="form-grid">
  <div class="form-group"><label>Tipo de cadastro</label><select name="property_kind"><option value="property"<?= selected($pitem['property_kind']??'property','property') ?>>Imóvel</option><option value="pool"<?= selected($pitem['property_kind']??'property','pool') ?>>Piscina para aluguel</option></select></div>
  <div class="form-group"><label>Título</label><input class="input" name="title" value="<?= e($pitem['title']??'') ?>" required></div>
  <div class="form-group"><label>Finalidade</label><select name="purpose"><option value="rent"<?= selected($pitem['purpose']??'rent','rent') ?>>Aluguel</option><option value="sale"<?= selected($pitem['purpose']??'','sale') ?>>Venda</option></select></div>
  <div class="form-group"><label>Tipo do imóvel</label><select name="type"><option value="house"<?= selected($pitem['type']??'house','house') ?>>Casa</option><option value="apartment"<?= selected($pitem['type']??'','apartment') ?>>Apartamento</option><option value="commercial"<?= selected($pitem['type']??'','commercial') ?>>Comercial</option><option value="land"<?= selected($pitem['type']??'','land') ?>>Terreno</option><option value="pool"<?= selected($pitem['type']??'','pool') ?>>Piscina</option></select></div>
  <div class="form-group"><label>Valor</label><input class="input" name="price" value="<?= e($pitem['price']??'') ?>"></div>
  <div class="form-group"><label>Chave Pix para receber</label><input class="input" name="pix_key" value="<?= e($pitem['pix_key']??(current_user()['pix_key']??'')) ?>" placeholder="Opcional"></div>
  <div class="form-group full"><label>Link para pagamento</label><input class="input" type="url" name="payment_link" value="<?= e($pitem['payment_link']??'') ?>" placeholder="https://..."></div>
  <div class="form-group full location-fields"><label>Endereço</label><div class="inline-filter"><input class="input" name="address" data-address-autocomplete data-lat-field="latitude" data-lng-field="longitude" value="<?= e($pitem['address']??'') ?>" required><button class="btn btn-secondary" type="button" data-geolocate-form data-address-field="address" data-lat-field="latitude" data-lng-field="longitude">📍 Usar localização atual</button></div><input type="hidden" name="latitude" value="<?= e($pitem['latitude']??'') ?>"><input type="hidden" name="longitude" value="<?= e($pitem['longitude']??'') ?>"></div>
  <div class="form-group"><label>Cidade</label><input class="input" name="city" value="<?= e($pitem['city']??'') ?>"></div>
  <div class="form-group"><label>Estado</label><input class="input" name="state" maxlength="2" data-state-mask value="<?= e($pitem['state']??'') ?>"></div>
  <div class="form-group"><label>Quartos</label><input class="input" type="number" name="bedrooms" value="<?= e($pitem['bedrooms']??0) ?>"></div>
  <div class="form-group"><label>Banheiros</label><input class="input" type="number" name="bathrooms" value="<?= e($pitem['bathrooms']??0) ?>"></div>
  <div class="form-group"><label>Garagens</label><input class="input" type="number" name="garage" value="<?= e($pitem['garage']??0) ?>"></div>
  <div class="form-group"><label>Área m²</label><input class="input" name="area" value="<?= e($pitem['area']??'') ?>"></div>
  <div class="form-group full"><label>Dias disponíveis (para piscina)</label><div class="weekday-grid"><?php foreach(['0'=>'Dom','1'=>'Seg','2'=>'Ter','3'=>'Qua','4'=>'Qui','5'=>'Sex','6'=>'Sáb'] as $val=>$lab):?><label><input type="checkbox" name="available_weekdays[]" value="<?= $val ?>"<?= in_array($val,$days,true)?' checked':'' ?>> <?= $lab ?></label><?php endforeach;?></div></div>
  <div class="form-group full"><label>Imagem</label><input class="input" type="file" name="image_file" accept="image/jpeg,image/png,image/webp"><?php if(!empty($pitem['image'])):?><label class="check-line"><input type="checkbox" name="remove_image" value="1"> Remover imagem atual ao salvar</label><?php endif;?></div>
  <div class="form-group"><label>Status</label><select name="status"><option value="available"<?= selected($pitem['status']??'available','available') ?>>Disponível</option><option value="unavailable"<?= selected($pitem['status']??'','unavailable') ?>>Não disponível</option></select></div>
  <div class="form-group full"><label>Descrição</label><textarea name="description"><?= e($pitem['description']??'') ?></textarea></div>
</div>
<div class="form-actions"><button class="btn btn-primary">Salvar</button></div>
</form>
