<?php
echo message_box('success');
$comment_details = get_result('tbl_task_comment', array('module' => 'goal_tracking', 'module_field_id' => $goal_info->goal_tracking_id));
$all_task_info = $this->db->where('goal_tracking_id', $goal_info->goal_tracking_id)->order_by('task_id', 'DESC')->get('tbl_task')->result();
$activities_info = $this->db->where(array('module' => 'goal_tracking', 'module_field_id' => $goal_info->goal_tracking_id))->order_by('activity_date', 'desc')->get('tbl_activities')->result();
$goal_type_info = $this->db->where('goal_type_id', $goal_info->goal_type_id)->get('tbl_goal_type')->row();

$progress = $this->items_model->get_progress($goal_info);
$can_edit = $this->items_model->can_action('tbl_goal_tracking', 'edit', array('goal_tracking_id' => $goal_info->goal_tracking_id));

$end_date = $goal_info->end_date;
$due_time = safe_strtotime($end_date);
$current_time = safe_strtotime(date('Y-m-d'));
if ($current_time > $due_time) {
    $text = 'text-danger';
    $ribbon = 'danger';
} else {
    $text = null;
}

if ($progress['progress'] == 100) {
    $prgs = '8ec165';
    $p_text = 'success';
    $ribbon = 'success';
    $text = null;
    $status = lang('achieved');
} elseif ($progress['progress'] >= 40 && $progress['progress'] <= 50) {
    $prgs = '5d9cec';
    $p_text = 'primary';
} elseif ($progress['progress'] >= 51 && $progress['progress'] <= 99) {
    $prgs = '7266ba';
    $p_text = 'purple';
} else {
    $prgs = 'fb6b5b';
    $p_text = 'primary';
}
$edited = can_action('69', 'edited');
?>

<div class="rounded-2xl border border-border bg-card shadow-sm">
    <div class="flex items-center justify-between border-b border-border px-5 py-4">
        <div>
            <h3 class="text-lg font-semibold text-foreground"><?= $goal_info->subject ?> - <?= lang('details') ?></h3>
            <p class="text-sm text-muted-foreground"><?= lang('goal_tracking') ?></p>
        </div>
        <div class="flex items-center gap-2">
            <?php if (!empty($can_edit) && !empty($edited)) { ?>
                <a href="<?php echo base_url() ?>admin/goal_tracking/create/<?= $goal_info->goal_tracking_id ?>"
                   class="inline-flex items-center gap-2 rounded-lg border border-border bg-card px-3 py-2 text-xs font-semibold text-foreground hover:bg-muted">
                    <i data-lucide="pencil" class="h-4 w-4"></i>
                    <?= lang('edit') ?>
                </a>
            <?php } ?>
            <span class="inline-flex items-center rounded-full px-2.5 py-0.5 text-xs font-semibold <?= !empty($text) ? 'bg-rose-100 text-rose-700' : (!empty($status) ? 'bg-emerald-100 text-emerald-700' : 'bg-amber-100 text-amber-700') ?>">
                <?php
                if (!empty($text)) {
                    echo lang('failed');
                } elseif (!empty($status)) {
                    echo $status;
                } else {
                    echo lang('ongoing');
                }
                ?>
            </span>
        </div>
    </div>

    <div class="p-5 space-y-6">
        <div class="grid grid-cols-1 lg:grid-cols-2 gap-6">
            <div class="rounded-xl border border-border bg-card p-4 space-y-4">
                <div class="text-sm font-semibold text-foreground border-b border-border pb-3 flex items-center gap-2">
                    <i data-lucide="info" class="h-4 w-4 text-muted-foreground"></i>
                    <?= lang('goal') . ' ' . lang('details') ?>
                </div>
                <div class="grid grid-cols-1 sm:grid-cols-2 gap-4 text-sm">
                    <div>
                        <div class="text-xs text-muted-foreground"><?= lang('subject') ?></div>
                        <div class="font-semibold text-foreground"><?= $goal_info->subject ?></div>
                    </div>
                    <div>
                        <div class="text-xs text-muted-foreground"><?= lang('type') ?></div>
                        <div class="font-semibold text-foreground" title="<?= $goal_type_info->description ?>"><?= lang($goal_type_info->type_name) ?></div>
                    </div>
                    <div>
                        <div class="text-xs text-muted-foreground"><?= lang('start_date') ?></div>
                        <div class="font-semibold text-foreground"><?= display_date($goal_info->start_date); ?></div>
                    </div>
                    <div>
                        <div class="text-xs text-muted-foreground"><?= lang('end_date') ?></div>
                        <div class="font-semibold <?= $text ? $text : 'text-foreground' ?>"><?= display_date($goal_info->end_date); ?></div>
                    </div>
                    <div>
                        <div class="text-xs text-muted-foreground"><?= lang('achievement') ?></div>
                        <div class="font-semibold text-foreground"><?= $goal_info->achievement ?></div>
                    </div>
                    <div>
                        <div class="text-xs text-muted-foreground"><?= lang('completed') ?></div>
                        <div class="font-semibold text-foreground"><?= $progress['achievement'] ?></div>
                    </div>
                </div>
                <?php if (!empty($goal_info->description)) { ?>
                    <div class="text-sm text-muted-foreground border-l-2 border-border pl-3"><?= $goal_info->description ?></div>
                <?php } ?>
            </div>

            <div class="rounded-xl border border-border bg-card p-4 space-y-4">
                <div class="text-sm font-semibold text-foreground border-b border-border pb-3 flex items-center gap-2">
                    <i data-lucide="activity" class="h-4 w-4 text-muted-foreground"></i>
                    <?= lang('status') ?>
                </div>
                <div class="flex items-center justify-between">
                    <div class="text-sm text-muted-foreground"><?= lang('progress') ?></div>
                    <div class="text-sm font-semibold text-foreground"><?= $progress['progress'] ?>%</div>
                </div>
                <div class="text-center">
                    <div style="display: inline-block">
                        <div id="easypie3" data-percent="<?= $progress['progress'] ?>" data-bar-color="#<?= $prgs ?>" class="easypie-chart">
                            <span class="h2"><?= $progress['progress'] ?>%</span>
                            <div class="easypie-text"><?= lang('done') ?></div>
                        </div>
                    </div>
                </div>
                <div class="flex flex-wrap gap-2">
                    <?php if (!empty($text)) { ?>
                        <a class="inline-flex items-center gap-2 rounded-lg bg-rose-600 px-3 py-2 text-xs font-semibold text-white" href="<?= base_url() ?>admin/goal_tracking/send_notifier/<?= $goal_info->goal_tracking_id; ?>/field"><?= lang('send_notifier') ?></a>
                    <?php } elseif (!empty($status)) { ?>
                        <a class="inline-flex items-center gap-2 rounded-lg bg-emerald-600 px-3 py-2 text-xs font-semibold text-white" href="<?= base_url() ?>admin/goal_tracking/send_notifier/<?= $goal_info->goal_tracking_id; ?>/success"><?= lang('send_notifier') ?></a>
                    <?php } ?>
                </div>
            </div>
        </div>

        <div class="rounded-xl border border-border bg-card p-4 space-y-4">
            <div class="text-sm font-semibold text-foreground border-b border-border pb-3 flex items-center gap-2">
                <i data-lucide="users" class="h-4 w-4 text-muted-foreground"></i>
                <?= lang('participants') ?>
            </div>
            <div class="flex flex-wrap items-center gap-2">
                <?php
                if ($goal_info->permission != 'all') {
                    $get_permission = json_decode($goal_info->permission);
                    if (!empty($get_permission)) :
                        foreach ($get_permission as $permission => $v_permission) :
                            $user_info = $this->db->where(array('user_id' => $permission))->get('tbl_users')->row();
                            $label = (!empty($user_info) && $user_info->role_id == 1) ? 'bg-rose-500' : 'bg-emerald-500';
                            $profile_info = $this->db->where(array('user_id' => $permission))->get('tbl_account_details')->row();
                            $avatar = !empty($profile_info->avatar) ? $profile_info->avatar : 'uploads/default_avatar.jpg';
                            $fullname = !empty($profile_info->fullname) ? $profile_info->fullname : lang('user');
                            ?>
                            <a href="#" class="relative inline-flex h-9 w-9 shrink-0 items-center justify-center rounded-full border border-border bg-muted p-0.5" data-toggle="tooltip" data-placement="top" title="<?= $fullname ?>">
                                <img src="<?= base_url() . $avatar ?>" class="h-8 w-8 rounded-full object-cover" alt="<?= $fullname ?>">
                                <span class="absolute -bottom-0.5 -right-0.5 h-3 w-3 rounded-full border-2 border-card <?= $label ?>"></span>
                            </a>
                        <?php endforeach; endif; ?>
                <?php } else { ?>
                    <span class="text-sm font-semibold text-foreground"><?= lang('everyone') ?></span>
                    <i title="<?= lang('permission_for_all') ?>" data-lucide="circle-help" data-toggle="tooltip" data-placement="top"></i>
                <?php } ?>

                <?php if (!empty($can_edit) && !empty($edited)) { ?>
                    <?= btn_modal('admin/goal_tracking/update_users/' . $goal_info->goal_tracking_id, '', lang('add_more'), 'md', 'plus', array(
                        'class' => 'inline-flex items-center justify-center rounded-md border border-border bg-card p-1 text-foreground hover:bg-muted'
                    )); ?>
                <?php } ?>
            </div>
        </div>
    </div>
</div>

