<?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; }
.pr-badge { display:inline-block; padding:2px 9px; border-radius:10px; font-size:11px; font-weight:600; }
.pr-baixa { background:#e3f2fd; color:#1565c0; }
.pr-media { background:#e8f5e9; color:#2e7d32; }
.pr-alta  { background:#ffebee; color:#c62828; }
.assigned-pill { display:inline-flex; align-items:center; gap:5px; background:#f0f4ff; color:#1e3a5f; padding:2px 9px; border-radius:10px; font-size:11px; font-weight:600; margin:1px 2px; }
.assigned-avatar { width:18px; height:18px; border-radius:50%; background:linear-gradient(135deg,#4a9fd4,#1e3a5f); display:inline-flex; align-items:center; justify-content:center; font-size:8px; font-weight:700; color:#fff; }
.progress-circle-wrap { display:flex; align-items:center; gap:14px; }
@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-tasks"></i> <?php echo lang('tasks') ?> <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/tasks') ?>"><?php echo lang('tasks') ?></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-tasks"></i> <?php echo htmlspecialchars($task->name ?? '') ?></h3>
            <div class="box-tools pull-right">
                <?php if(check_user_role(114)==1): ?>
                <a href="<?php echo site_url('admin/tasks/edit/'.$task->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('name') ?></span>
                    <span class="dv"><strong><?php echo htmlspecialchars($task->name ?? '') ?></strong></span>
                </div>
                <div class="detail-item">
                    <span class="dl"><?php echo lang('priority') ?></span>
                    <span class="dv">
                        <?php if(($task->priority ?? 0)==1): ?><span class="pr-badge pr-baixa">Baixa</span>
                        <?php elseif(($task->priority ?? 0)==2): ?><span class="pr-badge pr-media">Média</span>
                        <?php elseif(($task->priority ?? 0)==3): ?><span class="pr-badge pr-alta">Alta</span>
                        <?php else: ?>—<?php endif; ?>
                    </span>
                </div>
                <div class="detail-item">
                    <span class="dl"><?php echo lang('due_date') ?></span>
                    <span class="dv"><i class="fa fa-calendar-o"></i> <?php echo $task->due_date ?? '—' ?></span>
                </div>
                <div class="detail-item">
                    <span class="dl"><?php echo lang('case') ?></span>
                    <span class="dv">
                        <?php foreach($cases as $new): if(($task->case_id ?? 0) == $new->id): ?>
                        <a href="<?php echo site_url('admin/cases/view_case/'.$new->id) ?>">#<?php echo $new->case_no ?> — <?php echo htmlspecialchars($new->title) ?></a>
                        <?php endif; endforeach; ?>
                    </span>
                </div>
                <div class="detail-item full">
                    <span class="dl"><?php echo lang('assigned_to') ?></span>
                    <span class="dv">
                        <?php
                        $assigned_ids = [];
                        foreach($assigned_users as $au) $assigned_ids[] = $au->user_id;
                        $any = false;
                        foreach($employees as $emp):
                            if(in_array($emp->id, $assigned_ids)):
                                $any = true;
                                $parts = explode(' ', trim($emp->name));
                                $ini = strtoupper(substr($parts[0],0,1).(isset($parts[1])?substr($parts[1],0,1):''));
                        ?>
                        <span class="assigned-pill"><span class="assigned-avatar"><?php echo $ini ?></span> <?php echo htmlspecialchars($emp->name) ?></span>
                        <?php endif; endforeach; if(!$any): ?>
                        <span class="text-muted">—</span>
                        <?php endif; ?>
                    </span>
                </div>
                <div class="detail-item full">
                    <span class="dl"><?php echo lang('description') ?></span>
                    <span class="dv"><?php echo $task->description ?? '—' ?></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='".$task->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(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/tasks') ?>" class="btn btn-default btn-sm"><i class="fa fa-arrow-left"></i> Voltar</a>
            <?php if(check_user_role(114)==1): ?>
            <a href="<?php echo site_url('admin/tasks/edit/'.$task->id) ?>" class="btn btn-warning btn-sm"><i class="fa fa-pencil"></i> Editar</a>
            <?php endif; ?>
            <?php if(check_user_role(117)==1): ?>
            <a href="<?php echo site_url('admin/tasks/comments/'.$task->id) ?>" class="btn btn-info btn-sm"><i class="fa fa-comments"></i> Comentários</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-header with-border"><h3 class="box-title"><i class="fa fa-pie-chart"></i> Progresso</h3></div>
        <div class="box-body" style="text-align:center;padding:24px">
            <input type="text" class="knob" value="<?php echo $task->progress ?? 0 ?>" data-width="110" data-height="110" data-fgColor="<?php echo ($task->progress??0)==100?'#2e7d32':'#1e3a5f' ?>" data-readonly="true">
            <p style="margin-top:10px;font-size:13px;color:#888">
                <?php if(($task->progress??0)==100): ?>
                <span class="label label-success">Concluída</span>
                <?php elseif(isset($task->due_date) && $task->due_date < date('Y-m-d')): ?>
                <span class="label label-danger">Vencida</span>
                <?php else: ?>
                <span class="label label-info">Em andamento</span>
                <?php endif; ?>
            </p>
        </div>
    </div>
</div>

</div>
</section>

<script src="<?php echo base_url('assets/js/plugins/knob/jquery.knob.js') ?>"></script>
<script>
$(function() {
    $('.knob').knob();
});
</script>
