<?php
if ($this->input->post('goal_month', TRUE)) { // if input year
    $data['goal_month'] = $this->input->post('goal_month', TRUE);
} else { // else current year
    $data['goal_month'] = date('Y-m'); // get current year
}
$goal_report = $this->admin_model->get_goal_report($data['goal_month']);
//echo '<pre>'; print_r($goal_report); exit();
/*
$end_date = date('Y-m-d');

$start_date = date('Y-m-d', strtotime("$end_date - 1 year"));

//echo $start_date; exit;


$goal_report_2 = $this->admin_model->get_goal_report_by_month_2($start_date, $end_date);

echo '<pre>'; print_r($goal_report_2); exit();*/

?>


<?php
$mdate = date('Y-m-d');
$last_7_days = date('Y-m-d', strtotime('today - 7 days'));
$all_goal_tracking = $this->estimates_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->estimates_model->get_progress($v_goal_track, true);
        if ($v_goal_track->goal_type_id == 6) {
            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->estimates_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->estimates_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', strtotime('today - ' . $iDay . 'days'));
    $where = array('date_saved >=' => $date . " 00:00:00", 'date_saved <=' => $date . " 23:59:59");
    $invoice_result[$date] = count(array($this->db->where($where)->get('tbl_estimates')->result()));
}

$terget_achievement = $this->db->where(array('goal_type_id' => 6, '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->estimates_model->check_by(array('code' => config_item('default_currency')), 'tbl_currencies');

//$tolal_goal = $tolal_goal;  //Target Achievement

//$total_terget            //Last Weeks Created

//Completed Achievemen

//Pending Achievement

if ($this->session->userdata('user_type') == 1) {
    $margin = 'margin-bottom:15px';
    $h_s = config_item('estimate_state') === 'none' ? 'none' : 'block';
    ?>
    <div id="state_report" class="space-y-4" style="display: <?= $h_s ?>">
        <div class="overflow-hidden rounded-2xl border border-slate-200 bg-white shadow-sm dark:border-slate-700 dark:bg-slate-900" style="<?= $margin ?>">
            <div class="grid gap-3 bg-gradient-to-r from-sky-50 via-indigo-50 to-slate-50 p-4 dark:from-slate-800 dark:via-slate-800 dark:to-slate-900 md:grid-cols-3">
                <div class="rounded-xl border border-sky-200/80 bg-white/90 p-4 backdrop-blur dark:border-slate-700 dark:bg-slate-900/80">
                    <p class="text-xs font-semibold uppercase tracking-wide text-slate-500 dark:text-slate-400"><?= lang('achievement') ?></p>
                    <p class="mt-2 text-3xl font-bold text-slate-900 dark:text-slate-100"><?= ($tolal_goal) ?></p>
                    <p class="mt-1 text-xs text-slate-500 dark:text-slate-400"><?= lang('last_weeks') . ' ' . lang('created') ?>: <span class="font-semibold text-sky-700 dark:text-sky-300"><?= ($total_terget) ?></span></p>
                </div>

                <div class="rounded-xl border border-indigo-200/80 bg-white/90 p-4 backdrop-blur dark:border-slate-700 dark:bg-slate-900/80">
                    <p class="text-xs font-semibold uppercase tracking-wide text-slate-500 dark:text-slate-400"><?= lang('completed') . ' ' . lang('achievements') ?></p>
                    <p class="mt-2 text-3xl font-bold text-slate-900 dark:text-slate-100"><?= ($complete_achivement) ?></p>
                    <div class="mt-3">
                        <div data-sparkline="" data-bar-color="#23b7e5" data-height="54" data-bar-width="7"
                             data-bar-spacing="4" data-chart-range-min="0" values="<?php
                        if (!empty($invoice_result)) {
                            foreach ($invoice_result as $v_invoice_result) {
                                echo $v_invoice_result . ',';
                            }
                        }
                        ?>">
                        </div>
                        <p class="mt-1 text-[11px] text-slate-500 dark:text-slate-400">
                            <?php
                            if (!empty($invoice_result)) {
                                foreach ($invoice_result as $date => $v_invoice_result) {
                                    echo date('d', strtotime($date)) . ' ';
                                }
                            }
                            ?>
                        </p>
                    </div>
                </div>

                <div class="rounded-xl border border-emerald-200/80 bg-white/90 p-4 backdrop-blur dark:border-slate-700 dark:bg-slate-900/80">
                    <?php
                    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;
                    }
                    ?>
                    <p class="text-xs font-semibold uppercase tracking-wide text-slate-500 dark:text-slate-400"><?= lang('pending') . ' ' . lang('achievements') ?></p>
                    <p class="mt-2 text-3xl font-bold text-slate-900 dark:text-slate-100"><?= ($pending_goal) ?></p>
                    <div class="mt-3 text-center">
                        <div class="inline-block">
                            <div class="easypiechart text-emerald-600 dark:text-emerald-400"
                                 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="50"
                                 data-animate="2000">
                                <span class="text-xs"><?= $total_progress ?>%</span>
                                <span class="easypie-text"><strong><?= lang('done') ?></strong></span>
                            </div>
                        </div>
                    </div>
                </div>
            </div>
        </div>
        <?php
        ?>
        <div class="grid gap-3 md:grid-cols-2 xl:grid-cols-5">
            <?php
            $currency = $this->db->where(array('code' => config_item('default_currency')))->get('tbl_currencies')->row();
            $expired = 0;
            $draft = 0;
            $total_draft = 0;
            $total_sent = 0;
            $total_declined = 0;
            $total_accepted = 0;
            $total_expired = 0;
            $sent = 0;
            $declined = 0;
            $accepted = 0;
            $pending = 0;
            $cancelled = 0;
            $all_estimates = $this->estimates_model->get_permission('tbl_estimates');
            if (!empty($all_estimates)) {
                $all_estimates = array_reverse($all_estimates);
                foreach ($all_estimates as $v_invoice) {
                    if (strtotime($v_invoice->due_date) < strtotime(date('Y-m-d')) && $v_invoice->status == ('pending') || strtotime($v_invoice->due_date) < strtotime(date('Y-m-d')) && $v_invoice->status == ('draft')) {
                        $total_expired += $this->estimates_model->estimate_calculation('total', $v_invoice->estimates_id);
                        $expired += count(array($v_invoice->estimates_id));;
                    }
                    if ($v_invoice->status == ('draft')) {
                        $draft += count(array($v_invoice->estimates_id));
                        $total_draft += $this->estimates_model->estimate_calculation('total', $v_invoice->estimates_id);
                    }
                    if ($v_invoice->status == ('sent')) {
                        $sent += count(array($v_invoice->estimates_id));
                        $total_sent += $this->estimates_model->estimate_calculation('total', $v_invoice->estimates_id);
                    }
                    if ($v_invoice->status == ('declined')) {
                        $declined += count(array($v_invoice->estimates_id));
                        $total_declined += $this->estimates_model->estimate_calculation('total', $v_invoice->estimates_id);
                    }
                    if ($v_invoice->status == ('accepted')) {
                        $accepted += count(array($v_invoice->estimates_id));
                        $total_accepted += $this->estimates_model->estimate_calculation('total', $v_invoice->estimates_id);
                    }
                    if ($v_invoice->status == ('pending')) {
                        $pending += count(array($v_invoice->estimates_id));
                    }
                    if ($v_invoice->status == ('cancelled')) {
                        $cancelled += count(array($v_invoice->estimates_id));
                    }
                }
            }
            ?>

            <div class="rounded-xl border border-slate-200 bg-white p-4 shadow-sm dark:border-slate-700 dark:bg-slate-900">
                <p class="text-xs uppercase tracking-wide text-slate-500 dark:text-slate-400"><?= lang('draft') ?></p>
                <h3 class="mt-2 text-2xl font-semibold text-slate-900 dark:text-slate-100"><?= display_money($total_draft, $currency->symbol) ?></h3>
            </div>
            <div class="rounded-xl border border-slate-200 bg-white p-4 shadow-sm dark:border-slate-700 dark:bg-slate-900">
                <p class="text-xs uppercase tracking-wide text-blue-600 dark:text-blue-400"><?= lang('sent') ?></p>
                <h3 class="mt-2 text-2xl font-semibold text-slate-900 dark:text-slate-100"><?= display_money($total_sent, $currency->symbol) ?></h3>
            </div>
            <div class="rounded-xl border border-slate-200 bg-white p-4 shadow-sm dark:border-slate-700 dark:bg-slate-900">
                <p class="text-xs uppercase tracking-wide text-rose-600 dark:text-rose-400"><?= lang('expired') ?></p>
                <h3 class="mt-2 text-2xl font-semibold text-slate-900 dark:text-slate-100"><?= display_money($total_expired, $currency->symbol) ?></h3>
            </div>
            <div class="rounded-xl border border-slate-200 bg-white p-4 shadow-sm dark:border-slate-700 dark:bg-slate-900">
                <p class="text-xs uppercase tracking-wide text-amber-600 dark:text-amber-400"><?= lang('declined') ?></p>
                <h3 class="mt-2 text-2xl font-semibold text-slate-900 dark:text-slate-100"><?= display_money($total_declined, $currency->symbol) ?></h3>
            </div>
            <div class="rounded-xl border border-slate-200 bg-white p-4 shadow-sm dark:border-slate-700 dark:bg-slate-900">
                <p class="text-xs uppercase tracking-wide text-emerald-600 dark:text-emerald-400"><?= lang('accepted') ?></p>
                <h3 class="mt-2 text-2xl font-semibold text-slate-900 dark:text-slate-100"><?= display_money($total_accepted, $currency->symbol) ?></h3>
            </div>
        </div>
        <?php if (!empty($all_estimates)) { ?>
            <div class="grid gap-3 md:grid-cols-2 xl:grid-cols-5">
                <div class="rounded-xl border border-slate-200 bg-white p-4 shadow-sm transition hover:shadow-md dark:border-slate-700 dark:bg-slate-900">
                    <div class="flex items-center justify-between">
                        <a class="filter_by_type text-sm font-semibold text-violet-600 dark:text-violet-400" search-type="<?= lang('draft') ?>" id="draft" href="#"><?= lang('draft') ?></a>
                        <span class="rounded-full bg-violet-100 px-2 py-0.5 text-xs font-medium text-violet-700 dark:bg-violet-900/40 dark:text-violet-300"><?= $draft ?> / <?= count(array($all_estimates)) ?></span>
                    </div>
                    <div class="mt-3 h-2 overflow-hidden rounded-full bg-slate-200 dark:bg-slate-700">
                        <div class="h-full rounded-full bg-violet-500" data-toggle="tooltip" data-original-title="<?= ($draft / count(array($all_estimates))) * 100 ?>%" style="width: <?= ($draft / count(array($all_estimates))) * 100 ?>%"></div>
                    </div>
                </div>
                <div class="rounded-xl border border-slate-200 bg-white p-4 shadow-sm transition hover:shadow-md dark:border-slate-700 dark:bg-slate-900">
                    <div class="flex items-center justify-between">
                        <a class="filter_by_type text-sm font-semibold text-blue-600 dark:text-blue-400" search-type="<?= lang('sent') ?>" id="sent" href="#"><?= lang('sent') ?></a>
                        <span class="rounded-full bg-blue-100 px-2 py-0.5 text-xs font-medium text-blue-700 dark:bg-blue-900/40 dark:text-blue-300"><?= $sent ?> / <?= count(array($all_estimates)) ?></span>
                    </div>
                    <div class="mt-3 h-2 overflow-hidden rounded-full bg-slate-200 dark:bg-slate-700">
                        <div class="h-full rounded-full bg-blue-500" data-toggle="tooltip" data-original-title="<?= ($sent / count(array($all_estimates))) * 100 ?>%" style="width: <?= ($sent / count(array($all_estimates))) * 100 ?>%"></div>
                    </div>
                </div>
                <div class="rounded-xl border border-slate-200 bg-white p-4 shadow-sm transition hover:shadow-md dark:border-slate-700 dark:bg-slate-900">
                    <div class="flex items-center justify-between">
                        <a class="filter_by_type text-sm font-semibold text-rose-600 dark:text-rose-400" search-type="<?= lang('expired') ?>" id="expired" href="#"><?= lang('expired') ?></a>
                        <span class="rounded-full bg-rose-100 px-2 py-0.5 text-xs font-medium text-rose-700 dark:bg-rose-900/40 dark:text-rose-300"><?= $expired ?> / <?= count(array($all_estimates)) ?></span>
                    </div>
                    <div class="mt-3 h-2 overflow-hidden rounded-full bg-slate-200 dark:bg-slate-700">
                        <div class="h-full rounded-full bg-rose-500" data-toggle="tooltip" data-original-title="<?= ($expired / count(array($all_estimates))) * 100 ?>%" style="width: <?= ($expired / count(array($all_estimates))) * 100 ?>%"></div>
                    </div>
                </div>
                <div class="rounded-xl border border-slate-200 bg-white p-4 shadow-sm transition hover:shadow-md dark:border-slate-700 dark:bg-slate-900">
                    <div class="flex items-center justify-between">
                        <a class="filter_by_type text-sm font-semibold text-amber-600 dark:text-amber-400" search-type="<?= lang('declined') ?>" id="declined" href="#"><?= lang('declined') ?></a>
                        <span class="rounded-full bg-amber-100 px-2 py-0.5 text-xs font-medium text-amber-700 dark:bg-amber-900/40 dark:text-amber-300"><?= $declined ?> / <?= count(array($all_estimates)) ?></span>
                    </div>
                    <div class="mt-3 h-2 overflow-hidden rounded-full bg-slate-200 dark:bg-slate-700">
                        <div class="h-full rounded-full bg-amber-500" data-toggle="tooltip" data-original-title="<?= ($declined / count(array($all_estimates))) * 100 ?>%" style="width: <?= ($declined / count(array($all_estimates))) * 100 ?>%"></div>
                    </div>
                </div>
                <div class="rounded-xl border border-slate-200 bg-white p-4 shadow-sm transition hover:shadow-md dark:border-slate-700 dark:bg-slate-900">
                    <div class="flex items-center justify-between">
                        <a class="filter_by_type text-sm font-semibold text-emerald-600 dark:text-emerald-400" search-type="<?= lang('accepted') ?>" id="accepted" href="#"><?= lang('accepted') ?></a>
                        <span class="rounded-full bg-emerald-100 px-2 py-0.5 text-xs font-medium text-emerald-700 dark:bg-emerald-900/40 dark:text-emerald-300"><?= $accepted ?> / <?= count(array($all_estimates)) ?></span>
                    </div>
                    <div class="mt-3 h-2 overflow-hidden rounded-full bg-slate-200 dark:bg-slate-700">
                        <div class="h-full rounded-full bg-emerald-500" data-toggle="tooltip" data-original-title="<?= ($accepted / count(array($all_estimates))) * 100 ?>%" style="width: <?= ($accepted / count(array($all_estimates))) * 100 ?>%"></div>
                    </div>
                </div>
            </div>
        <?php } ?>
    </div>
<?php } ?>
<script type="text/javascript">

    $('.filter_by_type').on('click', function () {
        $('.filter_by_type').removeClass('active');
        $('#showed_result').html($(this).attr('search-type'));
        $(this).addClass('active');
        var filter_by = $(this).attr('id');
        if (filter_by) {
            filter_by = filter_by;
        } else {
            filter_by = '';
        }
        table_url(base_url + "admin/estimates/estimatesList/" + filter_by);
    });
</script>
