<?php
$mdate = date('Y-m-d');
$last_7_days = date('Y-m-d', safe_strtotime('today - 7 days'));
$all_goal_tracking = $this->bugs_model->get_permission('tbl_goal_tracking');

$all_goal = 0;
$bank_goal = 0;
$complete_achivement = 0;
if (!empty($all_goal_tracking)) {
    foreach ($all_goal_tracking as $v_goal_track) {
        $goal_achieve = $this->bugs_model->get_progress($v_goal_track, true);

        if ($v_goal_track->goal_type_id == 9) {

            if ($v_goal_track->end_date <= $mdate) { // check today is last date or not

                if ($v_goal_track->email_send == 'no') {// check mail are send or not
                    if ($v_goal_track->achievement <= $goal_achieve['achievement']) {
                        if ($v_goal_track->notify_goal_achive == 'on') {// check is notify is checked or not check
                            $this->bugs_model->send_goal_mail('goal_achieve', $v_goal_track);
                        }
                    } else {
                        if ($v_goal_track->notify_goal_not_achive == 'on') {// check is notify is checked or not check
                            $this->bugs_model->send_goal_mail('goal_not_achieve', $v_goal_track);
                        }
                    }
                }
            }
            $all_goal += $v_goal_track->achievement;
            $complete_achivement += $goal_achieve['achievement'];
        }
    }
}
// 30 days before

for ($iDay = 7; $iDay >= 0; $iDay--) {
    $date = date('Y-m-d', safe_strtotime('today - ' . $iDay . 'days'));
    $where = array('update_time >=' => $date . " 00:00:00", 'update_time <=' => $date . " 23:59:59", 'bug_status' => 'resolved');

    $invoice_result[$date] = count(array($this->db->where($where)->get('tbl_bug')->result()));
}

$terget_achievement = $this->db->where(array('goal_type_id' => 9, 'start_date >=' => $last_7_days, 'end_date <=' => $mdate))->get('tbl_goal_tracking')->result();

$total_terget = 0;
if (!empty($terget_achievement)) {
    foreach ($terget_achievement as $v_terget) {
        $total_terget += $v_terget->achievement;
    }
}
$tolal_goal = $all_goal + $bank_goal;
$curency = $this->bugs_model->check_by(array('code' => config_item('default_currency')), 'tbl_currencies');
if ($this->session->userdata('user_type') == 1) {
    if ($tolal_goal < $complete_achivement) {
        $pending_goal = 0;
    } else {
        $pending_goal = $tolal_goal - $complete_achivement;
    }
    if (!empty($tolal_goal)) {
        if ($tolal_goal <= $complete_achivement) {
            $total_progress = 100;
        } else {
            $progress = ($complete_achivement / $tolal_goal) * 100;
            $total_progress = round($progress);
        }
    } else {
        $total_progress = 0;
    }
    $unconfirmed = 0;
    $confirmed = 0;
    $in_progress = 0;
    $resolved = 0;
    $verified = 0;
    $all_bugs_info = $this->bugs_model->get_permission('tbl_bug');
    if (!empty($all_bugs_info)):
        foreach ($all_bugs_info as $v_bugs):
        if ($v_bugs->bug_status == 'unconfirmed') {
            $unconfirmed += count(array($v_bugs->bug_id));
        }
        if ($v_bugs->bug_status == 'confirmed') {
            $confirmed += count(array($v_bugs->bug_id));
        }
        if ($v_bugs->bug_status == 'in_progress') {
            $in_progress += count(array($v_bugs->bug_id));
        }
        if ($v_bugs->bug_status == 'resolved') {
            $resolved += count(array($v_bugs->bug_id));
        }
        if ($v_bugs->bug_status == 'verified') {
            $verified += count(array($v_bugs->bug_id));
        }
        endforeach;
    endif;
    $total_bug_count = !empty($all_bugs_info) ? count($all_bugs_info) : 0;
    if (!empty($total_bug_count)) {
        $unconfirmed_width = ($unconfirmed / $total_bug_count) * 100;
        $confirmed_width = ($confirmed / $total_bug_count) * 100;
        $in_progress_width = ($in_progress / $total_bug_count) * 100;
        $resolved_width = ($resolved / $total_bug_count) * 100;
        $verified_width = ($verified / $total_bug_count) * 100;
    } else {
        $unconfirmed_width = 0;
        $confirmed_width = 0;
        $in_progress_width = 0;
        $resolved_width = 0;
        $verified_width = 0;
    }
    $status_cards = array(
        array(
            'id' => 'unconfirmed',
            'label' => lang('unconfirmed'),
            'count' => $unconfirmed,
            'width' => $unconfirmed_width,
            'bar_class' => 'bg-slate-500',
            'badge_class' => 'bg-slate-100 text-slate-700 dark:bg-slate-800 dark:text-slate-200',
        ),
        array(
            'id' => 'confirmed',
            'label' => lang('confirmed'),
            'count' => $confirmed,
            'width' => $confirmed_width,
            'bar_class' => 'bg-sky-500',
            'badge_class' => 'bg-sky-100 text-sky-700 dark:bg-sky-900/40 dark:text-sky-300',
        ),
        array(
            'id' => 'in_progress',
            'label' => lang('in_progress'),
            'count' => $in_progress,
            'width' => $in_progress_width,
            'bar_class' => 'bg-amber-500',
            'badge_class' => 'bg-amber-100 text-amber-700 dark:bg-amber-900/40 dark:text-amber-300',
        ),
        array(
            'id' => 'resolved',
            'label' => lang('resolved'),
            'count' => $resolved,
            'width' => $resolved_width,
            'bar_class' => 'bg-rose-500',
            'badge_class' => 'bg-rose-100 text-rose-700 dark:bg-rose-900/40 dark:text-rose-300',
        ),
        array(
            'id' => 'verified',
            'label' => lang('verified'),
            'count' => $verified,
            'width' => $verified_width,
            'bar_class' => 'bg-emerald-500',
            'badge_class' => 'bg-emerald-100 text-emerald-700 dark:bg-emerald-900/40 dark:text-emerald-300',
        ),
    );
    ?>
    <div class="space-y-4">
        <div class="grid grid-cols-1 gap-4 xl:grid-cols-12">
            <div class="rounded-2xl border border-border bg-card p-5 shadow-sm xl:col-span-8">
                <div class="flex flex-col gap-4 md:flex-row md:items-start md:justify-between">
                    <div>
                        <p class="text-xs font-semibold uppercase tracking-[0.2em] text-muted-foreground"><?= lang('bugs') ?></p>
                        <h3 class="mt-1 text-lg font-semibold text-foreground"><?= lang('bugs') ?> Dashboard</h3>
                        <p class="mt-1 text-sm text-muted-foreground"><?= lang('last_weeks') . ' ' . lang('created') ?>: <span class="font-semibold text-foreground"><?= $total_terget ?></span></p>
                    </div>
                    <div class="inline-flex items-center rounded-full bg-slate-100 px-3 py-1 text-sm font-semibold text-slate-700 dark:bg-slate-800 dark:text-slate-200">
                        <?= $total_progress ?>% <?= lang('done') ?>
                    </div>
                </div>
                <div class="mt-5 grid grid-cols-1 gap-3 sm:grid-cols-3">
                    <div class="rounded-2xl border border-border bg-background/70 p-4">
                        <p class="text-xs uppercase tracking-wide text-muted-foreground"><?= lang('achievement') ?></p>
                        <p class="mt-2 text-2xl font-semibold text-foreground"><?= $tolal_goal ?></p>
                    </div>
                    <div class="rounded-2xl border border-border bg-background/70 p-4">
                        <p class="text-xs uppercase tracking-wide text-muted-foreground"><?= lang('completed') . ' ' . lang('achievements') ?></p>
                        <p class="mt-2 text-2xl font-semibold text-foreground"><?= $complete_achivement ?></p>
                    </div>
                    <div class="rounded-2xl border border-border bg-background/70 p-4">
                        <p class="text-xs uppercase tracking-wide text-muted-foreground"><?= lang('pending') . ' ' . lang('achievements') ?></p>
                        <p class="mt-2 text-2xl font-semibold text-foreground"><?= $pending_goal ?></p>
                    </div>
                </div>
                <div class="mt-5">
                    <div class="mb-2 flex items-center justify-between text-xs font-medium text-muted-foreground">
                        <span>Goal Progress</span>
                        <span><?= $complete_achivement ?> / <?= $tolal_goal ?></span>
                    </div>
                    <div class="h-3 overflow-hidden rounded-full bg-slate-100 dark:bg-slate-800">
                        <div class="h-full rounded-full bg-primary transition-all" style="width: <?= max(0, min(100, $total_progress)) ?>%;"></div>
                    </div>
                </div>
            </div>
            <div class="rounded-2xl border border-border bg-card p-5 shadow-sm xl:col-span-4">
                <div class="flex items-center justify-between">
                    <div>
                        <p class="text-xs font-semibold uppercase tracking-[0.2em] text-muted-foreground"><?= lang('resolved') ?></p>
                        <h3 class="mt-1 text-lg font-semibold text-foreground">Recent Activity</h3>
                    </div>
                    <span class="rounded-full bg-emerald-100 px-3 py-1 text-xs font-semibold text-emerald-700 dark:bg-emerald-900/40 dark:text-emerald-300"><?= array_sum($invoice_result) ?></span>
                </div>
                <div class="mt-4 grid grid-cols-4 gap-2 sm:grid-cols-8 xl:grid-cols-4">
                    <?php foreach ($invoice_result as $date => $v_invoice_result) { ?>
                        <div class="rounded-xl border border-border bg-background/70 p-3 text-center">
                            <p class="text-xs font-semibold text-muted-foreground"><?= date('d', safe_strtotime($date)) ?></p>
                            <p class="mt-2 text-lg font-semibold text-foreground"><?= $v_invoice_result ?></p>
                        </div>
                    <?php } ?>
                </div>
            </div>
        </div>

        <div class="grid grid-cols-1 gap-4 md:grid-cols-2 xl:grid-cols-5">
            <?php foreach ($status_cards as $status_card) { ?>
                <a href="#"
                   id="<?= $status_card['id'] ?>"
                   class="b_status block rounded-2xl border border-border bg-card p-4 shadow-sm transition hover:-translate-y-0.5 hover:border-primary/40 hover:shadow-md"
                   data-bug-status-card="1"
                   data-bug-filter-action="1"
                   data-bug-filter="<?= $status_card['id'] ?>"
                   data-bug-search=""
                   data-bug-filter-label="<?= html_escape($status_card['label']) ?>">
                    <div class="flex items-start justify-between gap-3">
                        <div>
                            <p class="text-sm font-semibold text-foreground"><?= $status_card['label'] ?></p>
                            <p class="mt-1 text-2xl font-semibold text-foreground"><?= $status_card['count'] ?></p>
                        </div>
                        <span class="rounded-full px-2.5 py-1 text-xs font-semibold <?= $status_card['badge_class'] ?>">
                            <?= $status_card['count'] ?> / <?= $total_bug_count ?>
                        </span>
                    </div>
                    <div class="mt-4 h-2 overflow-hidden rounded-full bg-slate-100 dark:bg-slate-800">
                        <div class="h-full rounded-full <?= $status_card['bar_class'] ?>" style="width: <?= max(0, min(100, $status_card['width'])) ?>%;"></div>
                    </div>
                    <div class="mt-2 text-xs text-muted-foreground"><?= round($status_card['width']) ?>% of <?= lang('bugs') ?></div>
                </a>
            <?php } ?>
        </div>
    </div>
<?php } ?>
