<?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; }
.emp-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-id-badge"></i> <?php echo lang('employees') ?> <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/employees') ?>"><?php echo lang('employees') ?></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-id-badge"></i> <?php echo htmlspecialchars($employee->name ?? '') ?></h3>
            <div class="box-tools pull-right">
                <?php if(check_user_role(39)==1): ?>
                <a href="<?php echo site_url('admin/employees/edit/'.$employee->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"><?php echo lang('employee_id') ?></span>
                    <span class="dv"><?php echo htmlspecialchars($employee->employee_id ?? '—') ?></span>
                </div>
                <div class="detail-item">
                    <span class="dl"><?php echo lang('name') ?></span>
                    <span class="dv"><strong><?php echo htmlspecialchars($employee->name ?? '') ?></strong></span>
                </div>
                <div class="detail-item">
                    <span class="dl"><?php echo lang('gender') ?></span>
                    <span class="dv"><?php echo htmlspecialchars($employee->gender ?? '—') ?></span>
                </div>
                <div class="detail-item">
                    <span class="dl"><?php echo lang('date_of_birth') ?></span>
                    <span class="dv"><?php echo date_convert($employee->dob ?? '') ?: '—' ?></span>
                </div>
                <div class="detail-item">
                    <span class="dl"><?php echo lang('user_role') ?></span>
                    <span class="dv"><span class="label label-default"><?php echo htmlspecialchars($employee->role ?? '—') ?></span></span>
                </div>
                <div class="detail-item">
                    <span class="dl"><?php echo lang('department') ?></span>
                    <span class="dv">
                        <?php foreach($departments as $new): if($new->id == ($employee->department_id ?? 0)) echo htmlspecialchars($new->name); endforeach; ?>
                    </span>
                </div>
                <div class="detail-item">
                    <span class="dl"><?php echo lang('designation') ?></span>
                    <span class="dv">
                        <?php foreach($designations as $new): if($new->id == ($employee->designation_id ?? 0)) echo htmlspecialchars($new->designation); endforeach; ?>
                    </span>
                </div>
                <div class="detail-item">
                    <span class="dl"><?php echo lang('joining_date') ?></span>
                    <span class="dv"><?php echo date_convert($employee->joining_date ?? '') ?: '—' ?></span>
                </div>
                <div class="detail-item">
                    <span class="dl"><?php echo lang('joining_salary') ?></span>
                    <span class="dv">R$ <?php echo number_format($employee->joining_salary ?? 0, 2, ',', '.') ?></span>
                </div>
                <div class="detail-item">
                    <span class="dl"><?php echo lang('status') ?></span>
                    <span class="dv">
                        <?php if(($employee->status ?? 0)==1): ?>
                        <span class="label label-success"><?php echo lang('active') ?></span>
                        <?php else: ?>
                        <span class="label label-default"><?php echo lang('inactive') ?></span>
                        <?php endif; ?>
                    </span>
                </div>
                <div class="detail-item">
                    <span class="dl"><?php echo lang('email') ?></span>
                    <span class="dv">
                        <?php if(!empty($employee->email)): ?>
                        <a href="mailto:<?php echo $employee->email ?>"><?php echo htmlspecialchars($employee->email) ?></a>
                        <?php else: ?>—<?php endif; ?>
                    </span>
                </div>
                <div class="detail-item">
                    <span class="dl"><?php echo lang('phone') ?></span>
                    <span class="dv"><?php echo htmlspecialchars($employee->contact ?? '—') ?></span>
                </div>
                <div class="detail-item">
                    <span class="dl"><?php echo lang('username') ?></span>
                    <span class="dv"><?php echo htmlspecialchars($employee->username ?? '—') ?></span>
                </div>
                <div class="detail-item full">
                    <span class="dl"><?php echo lang('address') ?></span>
                    <span class="dv"><?php echo htmlspecialchars($employee->address ?? '—') ?></span>
                </div>

                <?php
                $CI = get_instance();
                if(!empty($fields)):
                    foreach($fields as $doc):
                        $result = $CI->db->query("SELECT * FROM rel_form_custom_fields WHERE custom_field_id='".$doc->id."' AND table_id='".$employee->id."' AND form='".$doc->form."'")->row();
                        $reply = $result->reply ?? '';
                ?>
                <div class="detail-item">
                    <span class="dl"><?php echo htmlspecialchars($doc->name) ?></span>
                    <span class="dv">
                        <?php if($doc->field_type == 6 && !empty($reply)): ?>
                        <a href="<?php echo $reply ?>" target="_blank"><?php echo $reply ?></a>
                        <?php elseif($doc->field_type == 7 && !empty($reply)): ?>
                        <a href="mailto:<?php echo $reply ?>"><?php echo $reply ?></a>
                        <?php elseif(in_array($doc->field_type, [2,3,4])): ?>
                        <?php
                            $values = explode(',', $doc->values ?? '');
                            $matched = '';
                            foreach($values as $val) if(trim($val) == $reply) $matched = trim($val);
                            echo htmlspecialchars($matched ?: '—');
                        ?>
                        <?php else: ?>
                        <?php echo htmlspecialchars($reply ?: '—') ?>
                        <?php endif; ?>
                    </span>
                </div>
                <?php endforeach; endif; ?>
            </div>
        </div>
        <div class="box-footer">
            <a href="<?php echo site_url('admin/employees') ?>" class="btn btn-default btn-sm"><i class="fa fa-arrow-left"></i> Voltar</a>
            <?php if(check_user_role(39)==1): ?>
            <a href="<?php echo site_url('admin/employees/edit/'.$employee->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($employee->image)): ?>
            <img src="<?php echo base_url('assets/uploads/images/'.$employee->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($employee->name ?? ''));
            $ini = strtoupper(substr($parts[0],0,1).(isset($parts[1])?substr($parts[1],0,1):''));
            ?>
            <div class="emp-avatar-lg"><?php echo $ini ?></div>
            <?php endif; ?>
            <h4 style="margin:0 0 4px;font-weight:700"><?php echo htmlspecialchars($employee->name ?? '') ?></h4>
            <small class="text-muted"><?php echo htmlspecialchars($employee->role ?? '') ?></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">
            <?php if(check_user_role(123)==1): ?>
            <a href="<?php echo site_url('admin/employees/bank_details/'.$employee->id) ?>" class="btn btn-info btn-block btn-sm" style="margin-bottom:6px">
                <i class="fa fa-bank"></i> Dados Bancários
            </a>
            <?php endif; ?>
            <?php if(check_user_role(126)==1): ?>
            <a href="<?php echo site_url('admin/employees/documents/'.$employee->id) ?>" class="btn btn-success btn-block btn-sm" style="margin-bottom:6px">
                <i class="fa fa-file"></i> Documentos
            </a>
            <?php endif; ?>
            <?php if(check_user_role(39)==1): ?>
            <a href="<?php echo site_url('admin/employees/edit/'.$employee->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>
