<div class="invoice-modern">

<?= message_box('success') ?>
<?= message_box('error') ?>
<?php
$mdate = date('Y-m-d');
$last_7_days = date('Y-m-d', safe_strtotime('today - 7 days'));
$all_goal_tracking = $this->invoice_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->invoice_model->get_progress($v_goal_track, true);
        if ($v_goal_track->goal_type_id == 7) {
            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->invoice_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->invoice_model->send_goal_mail('goal_not_achieve', $v_goal_track);
                        }
                    }
                }
            }
            $all_goal += $v_goal_track->achievement;
            $complete_achivement += $goal_achieve['achievement'];
        }
    }
}
// 30 days before

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

$terget_achievement = $this->db->where(array('goal_type_id' => 7, '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->invoice_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;
    }
    ?>
    <div class="rounded-2xl border border-border bg-card p-4 shadow-sm dark:border-slate-800 dark:bg-slate-900/60">
        <div class="grid grid-cols-1 gap-4 lg:grid-cols-12">
            <div class="lg:col-span-8">
                <div class="grid grid-cols-1 gap-4 sm:grid-cols-2 xl:grid-cols-4">
                    <div class="border-b border-border pb-3 sm:border-b-0 sm:border-r sm:pr-4 dark:border-slate-800">
                        <p class="text-xs font-semibold uppercase tracking-wide text-muted-foreground"><?= lang('target_achievement') ?></p>
                        <p class="mt-1 text-lg font-semibold text-foreground"><?= display_money($tolal_goal, $curency->symbol) ?></p>
                    </div>

                    <div class="border-b border-border pb-3 sm:border-b-0 sm:border-r sm:pr-4 dark:border-slate-800">
                        <p class="text-xs font-semibold uppercase tracking-wide text-muted-foreground"><?= lang('last_weeks') . ' ' . lang('created') ?></p>
                        <p class="mt-1 text-lg font-semibold text-foreground"><?= display_money($total_terget, $curency->symbol); ?></p>
                    </div>

                    <div class="border-b border-border pb-3 sm:border-b-0 sm:border-r sm:pr-4 dark:border-slate-800">
                        <p class="text-xs font-semibold uppercase tracking-wide text-muted-foreground"><?= lang('completed') . ' ' . lang('achievements') ?></p>
                        <p class="mt-1 text-lg font-semibold text-foreground"><?= display_money($complete_achivement, $curency->symbol); ?></p>
                        <div class="mt-2">
                            <div data-sparkline="" data-bar-color="#3b82f6" data-height="50" data-bar-width="6"
                                 data-bar-spacing="5" data-chart-range-min="0" values="<?php
                            if (!empty($invoice_result)) {
                                foreach ($invoice_result as $amount) {
                                    echo $amount . ',';
                                }
                            }
                            ?>">
                            </div>
                            <p class="mt-1 text-[10px] text-muted-foreground">
                                <?php
                                if (!empty($invoice_result)) {
                                    foreach ($invoice_result as $date => $amount) {
                                        echo date('d', safe_strtotime($date)) . ' ';
                                    }
                                }
                                ?>
                            </p>
                        </div>
                    </div>

                    <div>
                        <p class="text-xs font-semibold uppercase tracking-wide text-muted-foreground"><?= lang('pending') . ' ' . lang('achievements') ?></p>
                        <p class="mt-1 text-lg font-semibold text-foreground"><?= display_money($pending_goal, $curency->symbol); ?></p>
                    </div>
                </div>
            </div>

            <div class="lg:col-span-4">
                <div class="flex items-center justify-between rounded-xl border border-dashed border-border px-4 py-3 dark:border-slate-800">
                    <div>
                        <p class="text-xs font-semibold uppercase tracking-wide text-muted-foreground"><?= lang('done') ?></p>
                        <p class="mt-1 text-sm font-semibold text-foreground"><?= $total_progress ?>%</p>
                    </div>
                    <div class="inline">
                        <div class="easypiechart"
                             data-percent="<?= $total_progress ?>"
                             data-line-width="6" data-track-Color="#e5e7eb"
                             data-bar-color="#6b7280" data-rotate="270" data-scale-Color="false"
                             data-size="56" data-animate="2000">
                            <span class="text-xs font-semibold text-foreground"><?= $total_progress ?>%</span>
                            <span class="easypie-text text-xs text-muted-foreground"><?= lang('done') ?></span>
                        </div>
                    </div>
                </div>
            </div>
        </div>
    </div>
<?php } ?>
</div>
