<?php defined('BASEPATH') OR exit('No direct script access allowed'); ?>
<style>
.detail-grid { display:grid; grid-template-columns:1fr 1fr; gap:0; }
.detail-item { padding:10px 16px; border-bottom:1px solid #f0f2f5; }
.detail-item.full { grid-column:1/-1; }
.detail-item .dl { font-size:10px; font-weight:700; color:#aaa; text-transform:uppercase; letter-spacing:.5px; display:block; margin-bottom:3px; }
.detail-item .dv { font-size:13px; color:#222; }
.client-avatar-lg {
    width:80px; height:80px; border-radius:50%;
    background:linear-gradient(135deg,#4a9fd4,#1e3a5f);
    display:flex; align-items:center; justify-content:center;
    font-size:28px; font-weight:700; color:#fff;
    margin:0 auto 12px;
}
@media(max-width:767px){ .detail-grid{ grid-template-columns:1fr; } .detail-item.full{ grid-column:1; } .col-md-8,.col-md-4{ width:100%!important;float:none!important; } }
</style>

<section class="content-header">
    <h1><i class="fa fa-user"></i> <?php echo lang('clients') ?> <small><?php echo lang('view') ?></small></h1>
    <ol class="breadcrumb">
        <li><a href="<?php echo site_url('admin') ?>"><i class="fa fa-dashboard"></i> Dashboard</a></li>
        <li><a href="<?php echo site_url('admin/clients') ?>"><?php echo lang('clients') ?></a></li>
        <li class="active"><?php echo lang('view') ?></li>
    </ol>
</section>

<section class="content">
<div class="row">

<div class="col-md-8">
    <div class="box box-primary">
        <div class="box-header with-border" style="background:linear-gradient(135deg,#1e3a5f,#2e75b6);border-radius:10px 10px 0 0">
            <h3 class="box-title" style="color:#fff"><i class="fa fa-user"></i> <?php echo htmlspecialchars($clients->name) ?></h3>
            <div class="box-tools pull-right">
                <?php if(check_user_role(34)==1): ?>
                <a href="<?php echo site_url('admin/clients/edit/'.$clients->id) ?>" class="btn btn-xs" style="background:rgba(255,255,255,.2);color:#fff">
                    <i class="fa fa-pencil"></i> Editar
                </a>
                <?php endif; ?>
            </div>
        </div>
        <div class="box-body no-padding">
            <div class="detail-grid">
                <div class="detail-item">
                    <span class="dl">Nome Completo</span>
                    <span class="dv"><strong><?php echo htmlspecialchars($clients->name) ?></strong></span>
                </div>
                <div class="detail-item">
                    <span class="dl">Gênero</span>
                    <span class="dv"><?php echo htmlspecialchars($clients->gender ?? '—') ?></span>
                </div>
                <div class="detail-item">
                    <span class="dl">Data de Nascimento</span>
                    <span class="dv"><?php echo date_convert($clients->dob) ?: '—' ?></span>
                </div>
                <div class="detail-item">
                    <span class="dl">Telefone</span>
                    <span class="dv"><?php echo htmlspecialchars($clients->contact ?? '—') ?></span>
                </div>
                <div class="detail-item">
                    <span class="dl">E-mail</span>
                    <span class="dv">
                        <?php if(!empty($clients->email)): ?>
                        <a href="mailto:<?php echo $clients->email ?>"><?php echo htmlspecialchars($clients->email) ?></a>
                        <?php else: ?>—<?php endif; ?>
                    </span>
                </div>
                <div class="detail-item">
                    <span class="dl">Usuário</span>
                    <span class="dv"><?php echo htmlspecialchars($clients->username ?? '—') ?></span>
                </div>
                <div class="detail-item full">
                    <span class="dl">Endereço</span>
                    <span class="dv"><?php echo htmlspecialchars($clients->address ?? '—') ?></span>
                </div>

                <?php
                // Campos personalizados
                $CI2 = get_instance();
                if(!empty($fields)):
                    foreach($fields as $doc):
                        $result = $CI2->db->query("SELECT * FROM rel_form_custom_fields WHERE custom_field_id='".$doc->id."' AND table_id='".$clients->id."' AND form='".$doc->form."'")->row();
                ?>
                <div class="detail-item">
                    <span class="dl"><?php echo htmlspecialchars($doc->name) ?></span>
                    <span class="dv">
                        <?php if($doc->field_type==6): ?>
                        <a href="<?php echo @$result->reply ?>" target="_blank"><?php echo @$result->reply ?: '—' ?></a>
                        <?php elseif($doc->field_type==7): ?>
                        <a href="mailto:<?php echo @$result->reply ?>"><?php echo @$result->reply ?: '—' ?></a>
                        <?php else: ?>
                        <?php echo htmlspecialchars(@$result->reply ?: '—') ?>
                        <?php endif; ?>
                    </span>
                </div>
                <?php endforeach; endif; ?>
            </div>
        </div>
        <div class="box-footer">
            <a href="<?php echo site_url('admin/clients') ?>" class="btn btn-default btn-sm"><i class="fa fa-arrow-left"></i> Voltar</a>
            <?php if(check_user_role(34)==1): ?>
            <a href="<?php echo site_url('admin/clients/edit/'.$clients->id) ?>" class="btn btn-warning btn-sm"><i class="fa fa-pencil"></i> Editar</a>
            <?php endif; ?>
        </div>
    </div>
</div>

<!-- Painel lateral -->
<div class="col-md-4">
    <div class="box" style="border-top:3px solid #1e3a5f">
        <div class="box-body" style="text-align:center;padding:20px">
            <?php if(!empty($clients->image)): ?>
            <img src="<?php echo base_url('assets/uploads/images/'.$clients->image) ?>" style="width:80px;height:80px;border-radius:50%;object-fit:cover;border:3px solid #e2e6ea;margin-bottom:10px">
            <?php else: ?>
            <?php
            $parts = explode(' ', trim($clients->name));
            $ini = strtoupper(substr($parts[0],0,1).(isset($parts[1])?substr($parts[1],0,1):''));
            ?>
            <div class="client-avatar-lg"><?php echo $ini ?></div>
            <?php endif; ?>
            <h4 style="margin:0 0 4px;font-weight:700"><?php echo htmlspecialchars($clients->name) ?></h4>
            <small class="text-muted"><?php echo htmlspecialchars($clients->email ?? '') ?></small>
        </div>
    </div>

    <div class="box" style="border-top:3px solid #2e7d32">
        <div class="box-header with-border"><h3 class="box-title"><i class="fa fa-bolt"></i> Ações Rápidas</h3></div>
        <div class="box-body" style="padding:10px">
            <a href="<?php echo site_url('admin/cases?client_id='.$clients->id) ?>" class="btn btn-primary btn-block btn-sm" style="margin-bottom:6px">
                <i class="fa fa-folder-open"></i> Ver Processos
            </a>
            <a href="<?php echo site_url('admin/message/send_message/'.$clients->id) ?>" class="btn btn-info btn-block btn-sm" style="margin-bottom:6px">
                <i class="fa fa-envelope"></i> Enviar Mensagem
            </a>
            <?php if(check_user_role(34)==1): ?>
            <a href="<?php echo site_url('admin/clients/edit/'.$clients->id) ?>" class="btn btn-warning btn-block btn-sm">
                <i class="fa fa-pencil"></i> Editar Cadastro
            </a>
            <?php endif; ?>
        </div>
    </div>
</div>

</div>
</section>
