<?php 
require_once $_SERVER['DOCUMENT_ROOT'].'/admin/system/settings/db.php';
require_once $_SERVER['DOCUMENT_ROOT'].'/admin/system/settings/eral.php';
function id($text) {
    $find = array('Ç', 'Ş', 'Ğ', 'Ü', 'İ', 'Ö', 'ç', 'ş', 'ğ', 'ü', 'ö', 'ı', '+', '#');
    $replace = array('c', 's', 'g', 'u', 'i', 'o', 'c', 's', 'g', 'u', 'o', 'i', 'plus', 'sharp');
    $text = strtolower(str_replace($find, $replace, $text));
    $text = preg_replace("@[^A-Za-z0-9\-_\.\+]@i", ' ', $text);
    $text = trim(preg_replace('/\s+/', ' ', $text));
    $text = str_replace(' ', '', $text);

    return $text;
}
?>
<?php 
$serviceID = $_POST['serviceID'];
?>
<option selected="" disabled="" value=""><?=$translations['select']?></option>

<?php $providers = $DB->query("SELECT * from provider WHERE working='1' order by id ASC")->fetchAll(PDO::FETCH_ASSOC); 
foreach ($providers as $provider) {
?>
<?php 
$service = $provider['services']; 
$service = explode(",", $service);
if (in_array($serviceID, $service))
{
?>
<option id="provider<?=id($provider['name'])?>" data-label="<?=id($provider['name'])?>" value="<?=$provider['id']?>" ><?=$provider['name']?></option>
<?php } }  ?>

<?php
// Müşteri bilgilerini getirme fonksiyonu (eklendi)
if(isset($_POST['getMemberInfo']) && $_POST['getMemberInfo'] == '1' && isset($_POST['memberID'])) {
    $memberID = $_POST['memberID'];
    try {
        // Müşteri bilgilerini getir
        $memberInfo = $DB->prepare("SELECT * FROM members WHERE id = ?");
        $memberInfo->execute(array($memberID));
        $member = $memberInfo->fetch(PDO::FETCH_ASSOC);
        
        if($member) {
            // Müşterinin son randevusunu getir
            $lastAppt = $DB->prepare("SELECT * FROM appointments WHERE member = ? ORDER BY id DESC LIMIT 1");
            $lastAppt->execute(array($memberID));
            $appointment = $lastAppt->fetch(PDO::FETCH_ASSOC);
            
            $paymentStatus = isset($appointment['paymentStatus']) ? $appointment['paymentStatus'] : '0';
            $paymentMethod = isset($appointment['payment']) ? $appointment['payment'] : '';
            $coming = isset($appointment['coming']) ? $appointment['coming'] : '0';
            $note = isset($appointment['note']) ? $appointment['note'] : '';
            
            // HTML oluştur
            echo '<label for="mevcutMusteriAd" class="form-label">Müşteri</label>
                <input type="text" class="form-control" id="mevcutMusteriAd" name="mevcutMusteriAd" value="'.$member['name'].'" disabled/>
                <br>
                <label for="telefonara" class="form-label">Telefon</label><br>
                <a id="telefonara" href="tel:'.$member['phone'].'">'.$member['phone'].'</a>
                <br><br>
                <label for="mailat" class="form-label">E-Mail</label><br>
                <a id="mailat" href="mailto:'.$member['email'].'">'.$member['email'].'</a>
                <br><br>
                <div class="mb-1">
                    <label for="appPayment" class="form-label">Ödeme Durumu</label>
                    <select class="select2 select-label form-select w-100" id="appPayment" name="appPayment">
                        <option selected="" disabled="" value="">Seçin</option>
                        <option id="odemeyapmadi" value="0" '.($paymentStatus == '0' ? 'selected' : '').'>Ödeme Yapmadı</option>
                        <option id="havale" value="Havale" '.($paymentStatus == '1' && $paymentMethod == 'Havale' ? 'selected' : '').'>Havale</option>
                        <option id="nakit" value="Nakit" '.($paymentStatus == '1' && $paymentMethod == 'Nakit' ? 'selected' : '').'>Nakit</option>
                        <option id="kredi" value="Kredi" '.($paymentStatus == '1' && $paymentMethod == 'Kredi' ? 'selected' : '').'>Kredi Kartı</option>
                    </select>
                </div>
                <div class="mb-1">
                    <label for="appComing" class="form-label">Gelme Durumu</label>
                    <select class="select2 select-label form-select w-100" id="appComing" name="appComing">
                        <option selected="" disabled="" value="">Seçin</option>
                        <option id="geldi" value="1" '.($coming == '1' ? 'selected' : '').'>Geldi</option>
                        <option id="gelmedi" value="0" '.($coming != '1' ? 'selected' : '').'>Gelmedi</option>
                    </select>
                </div>
                <div class="mb-1" id="note">
                    <label class="form-label">Randevu Notu</label>
                    <input type="text" class="form-control" name="note" id="note" placeholder="Not yazınız" value="'.$note.'"/>
                </div>';
        } else {
            echo '<div class="alert alert-warning">Müşteri bilgisi bulunamadı.</div>';
        }
    } catch(PDOException $e) {
        echo '<div class="alert alert-danger">Müşteri bilgisi getirilemedi: '.$e->getMessage().'</div>';
    }
}
?>