<?php require_once ('model.php');
$url = isset($_GET['url']) ? $_GET['url'] : '';
$segments = explode('/', $url);
$id = get_id($segments[3]);
$post = post($id);
$featuresData = json_decode($post['features'], true);
// İlk görüntülemede okundu işaretle (features.is_read = 1)
try {
    if (is_array($featuresData) && empty($featuresData['is_read'])) {
        $tmp = $featuresData; $tmp['is_read'] = 1;
        $db = new DB();
        $db->table('posts')->where('id','=', $id)->update(['features' => json_encode($tmp, JSON_UNESCAPED_UNICODE|JSON_UNESCAPED_SLASHES)]);
        $featuresData = $tmp;
    }
} catch (Exception $e) { /* sessiz */ }
?>
<div class="row">
    <div class="col-xxl-12">
        <div class="alert alert-primary alert-border-left alert-dismissible fade show lang_alert" role="alert">
            <i class="ri-error-warning-line me-3 align-middle fs-16"></i>
            <?=lang('form_editing_lang')?> <?=lang_name($post['lang'])?>
        </div>
    </div>
</div>
<form action="#" onsubmit="return false;" class="needs-validation" novalidate method="POST" id="features_form">
    <input type="hidden" name="csrf_token" value="<?php echo $_SESSION['csrf_token']['value']; ?>">
    <input type="hidden" name="model" value="<?=$model?>">
    <input type="hidden" name="id" value="<?=$id?>">
    <input type="hidden" name="lang_ref_id" value="<?=$post['lang_ref_id']?>">
    <input type="hidden" name="lang" value="<?=$post['lang']?>">
    <div class="row">
        <div class="col-xxl-10">
            <div class="card">
                <div class="card-header align-items-center d-flex">
                    <h4 class="card-title mb-0 flex-grow-1 text-muted"><?=lang('form_detail')?></h4>
                </div><!-- end card header -->
                <div class="card-body">
                    <div class="live-preview">
                        <div class="row">


                            <div class="col-md-12">
                                <div class="mb-3 input-group-v ">
                                    <label for="title" class="form-label"><?=lang('form_input_title')?></label>
                                    <input type="text" name="title"  class="form-control input-group-flat" value="<?=$post['title']?>" placeholder="<?=lang('form_input_title_placeholder')?>" id="title" disabled>
                                    <input type="hidden" name="sef" value="NULL">
                                    <div class="invalid-feedback">
                                        <?=lang('form_vali_title')?>
                                    </div>
                                </div>
                            </div>
                            <!--end col-->
                        </div>
                    </div>
                </div>
            </div>
            <div class="card">
                <div class="card-header align-items-center d-flex">
                    <h4 class="card-title mb-0 flex-grow-1"><?=lang('request_demo.details', [], 'Talep Bilgileri')?></h4>
                </div><!-- end card header -->
                <div class="card-body">
                    <?php
                    $fd = is_array($featuresData) ? $featuresData : [];
                    $val = function($k) use ($fd){ return htmlspecialchars((string)($fd[$k] ?? ''), ENT_QUOTES, 'UTF-8'); };
                    $msg = htmlspecialchars((string)($post['content'] ?? ($fd['text'] ?? '')), ENT_QUOTES, 'UTF-8');
                    ?>
                    <div class="row gy-3">
                        <div class="col-md-6">
                            <div class="mb-2">
                                <div class="text-muted small mb-1"><?=lang('request_demo.name', [], 'Ad Soyad')?></div>
                                <div class="fw-semibold"><?= $val('name') ?: '-' ?></div>
                            </div>
                        </div>
                        <div class="col-md-6">
                            <div class="mb-2">
                                <div class="text-muted small mb-1"><?=lang('request_demo.email', [], 'E‑posta')?></div>
                                <div class="d-flex align-items-center gap-2">
                                    <div class="fw-semibold"><?= $val('email') ?: '-' ?></div>
                                    <?php if ($val('email')): ?>
                                        <button type="button" class="btn btn-outline-secondary btn-sm" onclick="copyVal('<?= $val('email') ?>')"><?= lang('copy', [], 'Kopyala') ?></button>
                                    <?php endif; ?>
                                </div>
                            </div>
                        </div>
                        <div class="col-md-6">
                            <div class="mb-2">
                                <div class="text-muted small mb-1"><?=lang('request_demo.phone', [], 'Telefon')?></div>
                                <div class="d-flex align-items-center gap-2">
                                    <div class="fw-semibold"><?= $val('phone') ?: '-' ?></div>
                                    <?php if ($val('phone')): ?>
                                        <button type="button" class="btn btn-outline-secondary btn-sm" onclick="copyVal('<?= $val('phone') ?>')"><?= lang('copy', [], 'Kopyala') ?></button>
                                    <?php endif; ?>
                                </div>
                            </div>
                        </div>
                        <div class="col-md-6">
                            <div class="mb-2">
                                <div class="text-muted small mb-1"><?=lang('request_demo.sector', [], 'Sektör')?></div>
                                <div class="fw-semibold"><?= $val('sektor') ?: '-' ?></div>
                            </div>
                        </div>
                        <div class="col-md-3">
                            <div class="mb-2">
                                <div class="text-muted small mb-1"><?=lang('request_demo.city', [], 'İl')?></div>
                                <div class="fw-semibold"><?= $val('il') ?: '-' ?></div>
                            </div>
                        </div>
                        <div class="col-md-3">
                            <div class="mb-2">
                                <div class="text-muted small mb-1"><?=lang('request_demo.town', [], 'İlçe')?></div>
                                <div class="fw-semibold"><?= $val('ilce') ?: '-' ?></div>
                            </div>
                        </div>
                        <div class="col-md-6">
                            <div class="mb-2">
                                <div class="text-muted small mb-1"><?=lang('request_demo.created_at', [], 'Gönderim Tarihi')?></div>
                                <div class="fw-semibold"><?= htmlspecialchars((string)($post['created_at'] ?? ''), ENT_QUOTES, 'UTF-8') ?></div>
                            </div>
                        </div>
                        <div class="col-md-12">
                            <div class="mb-2">
                                <div class="text-muted small mb-1"><?=lang('request_demo.message', [], 'Mesaj')?></div>
                                <div class="p-3 rounded border bg-light-subtle" style="white-space:pre-wrap;min-height:56px;">
                                    <?= $msg ?: '-' ?>
                                </div>
                            </div>
                        </div>
                        
                    </div>
                </div>
            </div>
        </div> <!-- end col -->

        <div class="col-xxl-2">
            <script>
            function copyVal(v){
                try {
                    navigator.clipboard.writeText(String(v));
                } catch(e) {
                    const ta = document.createElement('textarea');
                    ta.value = String(v); document.body.appendChild(ta); ta.select();
                    try { document.execCommand('copy'); } catch(err) {}
                    ta.remove();
                }
            }
            </script>
            
            

            <div class="card card-body">
                <h4 class="card-title  flex-grow-1 text-muted"><?=lang('request_demo.record', [], 'Talep Kaydı')?></h4>
                <hr>
                <div class="mb-2">
                    <div class="text-muted small mb-1"><?=lang('request_demo.id', [], 'Talep No')?></div>
                    <div class="d-flex align-items-center gap-2">
                        <div class="fw-semibold">#<?= (int)$post['id'] ?></div>
                        <button type="button" class="btn btn-outline-secondary btn-sm" onclick="copyVal('<?= (int)$post['id'] ?>')"><?= lang('copy', [], 'Kopyala') ?></button>
                    </div>
                </div>
                <div class="mb-2">
                    <div class="text-muted small mb-1"><?=lang('request_demo.status', [], 'Durum')?></div>
                    <div class="fw-semibold"><?= m_status($post['status']) ?></div>
                </div>
            </div>
            <div class="card card-body mt-3">
                <h4 class="card-title  flex-grow-1 text-muted"><?=lang('table_actions')?></h4>
                <hr>
                <?php if (function_exists('can') ? can('delete', $model) : true): ?>
                <a href="<?=ADMIN_URL?>/<?=$model?>/delete/<?=$post['id']?>" class="btn btn-danger delete" data-title="<?= lang('remove_title') ?>" data-question="<?= lang('remove_desc') ?>" data-no="<?= lang('no') ?>"  data-yes="<?= lang('yes') ?>">
                    <i class="bx bx-trash"></i> <?= lang('remove') ?>
                </a>
                <?php endif; ?>
            </div>
            

        </div> <!-- end col -->
    </div>


</form>

        </div> <!-- end col -->

    </div>





</form>
