<?php
namespace App\Controllers\results;

use App\Controllers\BaseController;
use App\Models\results\SummaryModel;

class Summary extends BaseController
{
    private $summaryModel;

    private $schDateGb = '';	// 일자구분
    private $sdate = '';		// 시작일자
    private $edate = '';		// 종료일자

    public function __construct()
    {
        $this->summaryModel = new SummaryModel();
    }

    public function index()
    {
        $this->schDateGb = $this->request->getPost('schDateGb') ?? '1';
        $this->sdate = $this->request->getPost('sdate') ?? date('Y-m-d', strtotime('first day of this month'));
        $this->edate = $this->request->getPost('edate') ?? date('Y-m-d', strtotime('last day of this month'));
    }


    public function lists(): string
    {

        $this->schDateGb = $this->request->getPost('schDateGb') ?? '1';

        $this->sdate = $this->request->getPost('sdate');
        if (empty($this->sdate)) {
            $this->sdate = date('Y-m-01'); // 이번달 1일
        }

        $this->edate = $this->request->getPost('edate');
        if (empty($this->edate)) {
            $this->edate = date('Y-m-t');  // 이번달 말일
        }

        $data = [
            'schDateGb' => $this->schDateGb,
            'sdate' => $this->sdate,
            'edate' => $this->edate
        ];

        $res = $this->summaryModel->st_s01($data);
        $res2 = $this->summaryModel->st_s01_2($data);
        $totalAmount = 0;

        if (!empty($res)) {
            foreach ($res as $row) {
                foreach ($row as $key => $value) {
                    if (empty($value))
                        $value = 0;

                    switch ($key) {
                        case 'next_visit_cnt':
                            $res['cost'][$key] = 8000;
                            $res['amt'][$key] = $res['cost'][$key] * intval($value);
                            $totalAmount += $res['amt'][$key];
                            break;

                        case 'next_shoot_cnt':
                            $res['cost'][$key] = 10000;
                            $res['amt'][$key] = $res['cost'][$key] * intval($value);
                            $totalAmount += $res['amt'][$key];
                            break;

                        case 'delay_confirm_cnt':
                        case 'fail_confirm_cnt':
                        case 'confirm_cnt':
                            $res['cost'][$key] = 13000;
                            $res['amt'][$key] = $res['cost'][$key] * intval($value);
                            $totalAmount += $res['amt'][$key];
                            break;

                        default:
                            $res['cost'][$key] = 0;
                            $res['amt'][$key] = $res['cost'][$key] * intval($value);
                            $totalAmount += $res['amt'][$key];
                            break;
                    }
                }
            }
        }

        $this->data['schDateGb'] = $this->schDateGb;
        $this->data['sdate'] = $this->sdate;
        $this->data['edate'] = $this->edate;
        $this->data['st_list'] = $res;
        $this->data['st_agent'] = $res2;
        $this->data['totalAmount'] = $totalAmount;

        return view("pages/results/summary/stats_s01", $this->data);
    }
}