<?php
$mdate = date('Y-m-d');
$last_7_days = date('Y-m-d', safe_strtotime('today - 7 days'));
$all_goal_tracking = $this->transactions_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->transactions_model->get_progress($v_goal_track, true);
        if ($v_goal_track->goal_type_id == 1) {
            if ($v_goal_track->end_date <= $mdate) {
                if ($v_goal_track->email_send == 'no') {
                    if ($v_goal_track->achievement <= $goal_achieve['achievement']) {
                        if ($v_goal_track->notify_goal_achive == 'on') {
                            $this->transactions_model->send_goal_mail('goal_achieve', $v_goal_track);
                        }
                    } else {
                        if ($v_goal_track->notify_goal_not_achive == 'on') {
                            $this->transactions_model->send_goal_mail('goal_not_achive', $v_goal_track);
                        }
                    }
                }
            }
            $all_goal += $v_goal_track->achievement;
            $complete_achivement += $goal_achieve['achievement'];
        }
        if ($v_goal_track->goal_type_id == 2) {
            if ($v_goal_track->end_date <= $mdate) {
                if ($v_goal_track->email_send == 'no') {
                    if ($v_goal_track->achievement <= $goal_achieve['achievement']) {
                        if ($v_goal_track->notify_goal_achive == 'on') {
                            $this->transactions_model->send_goal_mail('goal_achieve', $v_goal_track);
                        }
                    } else {
                        if ($v_goal_track->notify_goal_not_achive == 'on') {
                            $this->transactions_model->send_goal_mail('goal_not_achive', $v_goal_track);
                        }
                    }
                }
            }
            $bank_goal += $v_goal_track->achievement;
            $complete_achivement += $goal_achieve['achievement'];
        }
    }
}

$this_7_days_deposit = isset($daily_series) ? $daily_series : array();

$this_7_days_all = $this->db->where(array('goal_type_id' => 1, 'start_date >=' => $last_7_days, 'end_date <=' => $mdate))->get('tbl_goal_tracking')->result();
$this_7_days_bank = $this->db->where(array('goal_type_id' => 2, 'start_date >=' => $last_7_days, 'end_date <=' => $mdate))->get('tbl_goal_tracking')->result();

$this_7_days_all = !empty($this_7_days_all) ? $this_7_days_all : array();
$this_7_days_bank = !empty($this_7_days_bank) ? $this_7_days_bank : array();

$terget_achievement = array_merge($this_7_days_all, $this_7_days_bank);
$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->transactions_model->check_by(array('code' => config_item('default_currency')), 'tbl_currencies');

$pending_goal = ($tolal_goal < $complete_achivement) ? 0 : ($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;
}
?>

<?php if ($this->session->userdata('user_type') == 1) { ?>
    <div class="rounded-2xl border border-border bg-card p-4 shadow-sm">
        <div class="grid gap-4 md:grid-cols-3">
            <div class="rounded-xl border border-border bg-background p-4">
                <p class="text-xs font-semibold uppercase tracking-wide text-muted-foreground"><?= lang('achievement'); ?></p>
                <div class="mt-2 text-lg font-semibold text-foreground"><?= display_money($tolal_goal, $curency->symbol); ?></div>
                <p class="mt-1 text-xs text-muted-foreground"><?= lang('last_weeks') . ' ' . lang('created'); ?>: <?= display_money($total_terget, $curency->symbol); ?></p>
            </div>
            <div class="rounded-xl border border-border bg-background p-4">
                <div class="flex items-start justify-between gap-3">
                    <div>
                        <p class="text-xs font-semibold uppercase tracking-wide text-muted-foreground"><?= lang('completed') . ' ' . lang('achievements'); ?></p>
                        <div class="mt-2 text-lg font-semibold text-foreground"><?= display_money($complete_achivement, $curency->symbol); ?></div>
                    </div>
                    <div class="text-right">
                        <div data-sparkline="" data-bar-color="#23b7e5" data-height="50" data-bar-width="7" data-bar-spacing="5" data-chart-range-min="0" values="<?php
                            if (!empty($this_7_days_deposit)) {
                                foreach ($this_7_days_deposit as $amount) {
                                    echo $amount . ',';
                                }
                            }
                        ?>"></div>
                        <p class="mt-1 text-[10px] text-muted-foreground">
                            <?php
                            if (!empty($this_7_days_deposit)) {
                                foreach ($this_7_days_deposit as $date => $amount) {
                                    echo date('d', safe_strtotime($date)) . ' ';
                                }
                            }
                            ?>
                        </p>
                    </div>
                </div>
            </div>
            <div class="rounded-xl border border-border bg-background p-4">
                <div class="flex items-center justify-between gap-3">
                    <div>
                        <p class="text-xs font-semibold uppercase tracking-wide text-muted-foreground"><?= lang('pending') . ' ' . lang('achievements'); ?></p>
                        <div class="mt-2 text-lg font-semibold text-foreground"><?= display_money($pending_goal, $curency->symbol); ?></div>
                    </div>
                    <div class="text-center">
                        <div class="easypiechart text-success" data-percent="<?= $total_progress ?>" data-line-width="5" data-track-Color="#f0f0f0" data-bar-color="#<?php
                            if ($total_progress == 100) {
                                echo '8ec165';
                            } elseif ($total_progress >= 40 && $total_progress <= 50) {
                                echo '5d9cec';
                            } elseif ($total_progress >= 51 && $total_progress <= 99) {
                                echo '7266ba';
                            } else {
                                echo 'fb6b5b';
                            }
                        ?>" data-rotate="270" data-scale-Color="false" data-size="48" data-animate="2000">
                            <span class="small"><?= $total_progress ?>%</span>
                            <span class="easypie-text"><strong><?= lang('done'); ?></strong></span>
                        </div>
                    </div>
                </div>
            </div>
        </div>
    </div>
<?php } ?>
