<?php

namespace App\Controllers;

use CodeIgniter\Controller;
use CodeIgniter\HTTP\CLIRequest;
use CodeIgniter\HTTP\IncomingRequest;
use CodeIgniter\HTTP\RequestInterface;
use CodeIgniter\HTTP\ResponseInterface;
use Psr\Log\LoggerInterface;
use App\Libraries\Log\Log;

/**
 * Class BaseController
 *
 * BaseController provides a convenient place for loading components
 * and performing functions that are needed by all your controllers.
 * Extend this class in any new controllers:
 *     class Home extends BaseController
 *
 * For security be sure to declare any new methods as protected or private.
 */
abstract class BaseController extends Controller
{
    /**
     * Instance of the main Request object.
     *
     * @var CLIRequest|IncomingRequest
     */
    protected $request;

    /**
     * An array of helpers to be loaded automatically upon
     * class instantiation. These helpers will be available
     * to all other controllers that extend BaseController.
     *
     * @var array
     */
    protected $helpers = ['Common'];

    /**
     * Be sure to declare properties for any property fetch you initialized.
     * The creation of dynamic property is deprecated in PHP 8.2.
     */
    // protected $session;

    /**
     * Constructor.
     */
    protected $_model = null;
    protected $_className = '';
    protected $_defines = array();
    protected $_viewPath = '';
    protected $_viewDatas = array();
    protected $_session = null;
    public function initController(RequestInterface $request, ResponseInterface $response, LoggerInterface $logger)
    {
        // Do Not Edit This Line
        parent::initController($request, $response, $logger);

        // Preload any models, libraries, etc, here.

        // E.g.: $this->session = \Config\Services::session();
        $this->_session = \Config\Services::session();
        $this->_viewDatas = [
            'layout' => LAYOUTS['empty'],
            'title' => ''
        ];
    }

    //Field별 Form Option용
    protected function getFieldFormOption(string $field): array
    {
        switch ($field) {
            default:
                $temps = lang($this->_className . '.' . strtoupper($field));
                if (!is_array($temps)) {
                    throw new \Exception(__FUNCTION__ . "에서 {$field}의 데이터가 array가 아닙니다.\n" . var_export($temps, true));
                }
                return array_merge(
                    [DEFAULTS['EMPTY'] => lang($this->_className . '.label.' . $field) . ' 선택'],
                    lang($this->_className . '.' . strtoupper($field))
                );
                break;
        }
    }
    //Field별 Form Option용
    final protected function getFieldFormOptions(array $fieldFilters): array
    {
        $fieldFormOptions = array();
        foreach ($fieldFilters as $field) {
            if (is_array($field)) {
                throw new \Exception(__FUNCTION__ . "에서 field가 array 입니다.\n" . var_export($fieldFilters, true));
            }
            $fieldFormOptions[$field] = $this->getFieldFormOption($field);
        }
        return $fieldFormOptions;
    }
    //Field별 Form Rule용
    final protected function getFieldRules(array $fields, array $fieldRules): array
    {
        $tempRules = $this->_model->getValidationRules(['only' => $fields]);
        foreach ($fields as $field) {
            if (is_array($field)) {
                throw new \Exception(__FUNCTION__ . "에서 field가 array 입니다.\n" . var_export($fieldRules, true));
            }
            if (array_key_exists($field, $fieldRules)) {
                $tempRules[$field] = $fieldRules[$field];
            }
        }
        return $tempRules;
    }

    //Insert관련
    protected function insert_init()
    {
        $this->_viewDatas['fields'] = $this->_defines['insert']['fields'];;
        $this->_viewDatas['fieldRules'] = $this->getFieldRules($this->_viewDatas['fields'], $this->_defines['insert']['fieldRules']);
    }
    protected function insert_form_init()
    {
        $this->_viewDatas['fieldFilters'] = $this->_defines['insert']['fieldFilters'];
        $this->_viewDatas['fieldFormOptions'] = $this->getFieldFormOptions($this->_viewDatas['fieldFilters']);
        $this->_viewDatas['forms'] = ['attributes' => ['method' => "post",], 'hiddens' => []];
        helper(['form']);
    }
    protected function insert_form_process()
    {
    }
    final public function insert_form()
    {
        try {
            $this->insert_init();
            $this->insert_form_init();
            $this->insert_form_process();
            return view($this->_viewPath . '/insert', $this->_viewDatas);
        } catch (\Exception $e) {
            return alert_CommonHelper($e->getMessage(), 'back');
        }
    }

    protected function insert_validate()
    {
        //변경된 값 적용
        $this->_viewDatas['fieldDatas'] = array();
        foreach ($this->_viewDatas['fields'] as $field) {
            $this->_viewDatas['fieldDatas'][$field] = rtrim($this->request->getVar($field));
            Log::add("info", "{$field} : {$this->_viewDatas['fieldDatas'][$field]}");
        }
        //변경할 값 확인
        if (!$this->validate($this->_viewDatas['fieldRules'])) {
            throw new \Exception("{$this->_viewDatas['title']}의 검증 오류발생\n" . implode("\n", $this->validator->getErrors()));
        }
    }
    protected function insert_process()
    {
        return $this->_model->create($this->_viewDatas['fieldDatas']);
    }
    protected function insert_procedure()
    {
        $message = "";
        try {
            $this->insert_init();
            $this->insert_validate();
            $this->insert_process();
            $message = __FUNCTION__ . " 완료하였습니다.";
            Log::save("{$this->_viewDatas['title']} {$message}");
            return alert_CommonHelper($message, $this->_session->get(SESSION_NAMES['RETURN_URL']));
        } catch (\Exception $e) {
            $message = __FUNCTION__ . " 실패하였습니다.";
            Log::add("warning", $e->getMessage());
            Log::add("warning", var_export($this->_viewDatas['fieldDatas'], true));
            Log::save("{$this->_viewDatas['title']} {$message}", false);
            return redirect()->back()->withInput()->with("error", $message . "<br>\n{$e->getMessage()}");
        }
    }

    //Update관련
    protected function update_init()
    {
        $this->_viewDatas['fields'] = $this->_defines['update']['fields'];;
        $this->_viewDatas['fieldRules'] = $this->getFieldRules($this->_viewDatas['fields'], $this->_defines['update']['fieldRules']);
    }
    protected function update_form_init()
    {
        $this->_viewDatas['fieldFilters'] = $this->_defines['update']['fieldFilters'];
        $this->_viewDatas['fieldFormOptions'] = $this->getFieldFormOptions($this->_viewDatas['fieldFilters']);
        $this->_viewDatas['forms'] = ['attributes' => ['method' => "post",], 'hiddens' => []];
        helper(['form']);
    }
    protected function update_form_process($entity)
    {
        return $entity;
    }
    final public function update_form($uid)
    {
        try {
            $entity = $this->_model->getEntity($uid);
            $this->update_init();
            $this->update_form_init();
            $this->_viewDatas['entity'] = $this->update_form_process($entity);
            return view($this->_viewPath . '/update', $this->_viewDatas);
        } catch (\Exception $e) {
            return alert_CommonHelper($e->getMessage(), 'back');
        }
    }
    protected function update_validate($entity)
    {
        //변경된 값 적용
        $this->_viewDatas['fieldDatas'] = array();
        foreach ($this->_viewDatas['fields'] as $field) {
            $this->_viewDatas['fieldDatas'][$field] = rtrim($this->request->getVar($field));
            if ($entity->$field != $this->_viewDatas['fieldDatas'][$field]) {
                // 기존값을 DB에서 수정전까지 유지하기위해서
                // $entity->$field = $this->_viewDatas['fieldDatas'][$field];
                //암호는 보안상 log에 남지 않게하기 위함
                Log::add(
                    $field == 'passwd' ? "debug" : "info",
                    "{$field} : {$entity->$field} => {$this->_viewDatas['fieldDatas'][$field]}"
                );
            }
        }
        //변경할 값 확인
        if (!$this->validate($this->_viewDatas['fieldRules'])) {
            throw new \Exception("{$this->_viewDatas['title']}의 검증 오류발생\n" . implode("\n", $this->validator->getErrors()));
        }
        return $entity;
    }
    protected function update_process($entity)
    {
        return $this->_model->modify($entity, $this->_viewDatas['fieldDatas']);
    }
    protected function update_procedure($uid)
    {
        $message = "";
        try {
            $entity = $this->_model->getEntity($uid);
            $this->update_init();
            $entity = $this->update_validate($entity);
            $entity = $this->update_process($entity);
            $message = "{$entity->getTitle()} " . __FUNCTION__ . " 완료하였습니다.";
            Log::save("{$this->_viewDatas['title']} {$message}");
            return alert_CommonHelper($message, $this->_session->get(SESSION_NAMES['RETURN_URL']));
        } catch (\Exception $e) {
            $message = __FUNCTION__ . " 실패하였습니다.";
            Log::add("warning", $e->getMessage());
            Log::add("warning", var_export($this->_viewDatas['fieldDatas'], true));
            Log::save("{$this->_viewDatas['title']} {$message}", false);
            return redirect()->back()->withInput()->with("error", $message . "<br>\n{$e->getMessage()}");
        }
    }

    //Toggle 관련
    protected function toggle_init($field)
    {
        $this->_viewDatas['fields'] = [$field];
        $this->_viewDatas['fieldRules'] = $this->getFieldRules($this->_viewDatas['fields'], array());
    }
    protected function toggle_validate($entity)
    {
        return $this->update_validate($entity);
    }
    protected function toggle_process($entity)
    {
        return $this->update_process($entity);
    }
    protected function toggle_procedure($uid, string $field)
    {
        $message = "";
        try {
            $entity = $this->_model->getEntity($uid);
            $this->toggle_init($field);
            $entity = $this->toggle_validate($entity);
            $entity = $this->toggle_process($entity);
            $message = "{$entity->getTitle()} " . __FUNCTION__ . " 완료하였습니다.";
            Log::save("{$this->_viewDatas['title']} {$message}");
            return alert_CommonHelper($message, $this->_session->get(SESSION_NAMES['RETURN_URL']));
        } catch (\Exception $e) {
            $message = __FUNCTION__ . " 실패하였습니다.";
            Log::add("warning", $e->getMessage());
            Log::add("warning", var_export($this->_viewDatas['fieldDatas'], true));
            Log::save("{$this->_viewDatas['title']} {$message}", false);
            return alert_CommonHelper($message . "<br>\n{$e->getMessage()}", 'back');
        }
    }
    //Batchjob 관련
    protected function batchjob_init()
    {
        //fields 해당하는 field중 선택된 값이 있는경우만 fields로 정의
        $fields = array();
        foreach ($this->_defines['index']['batchjobFilters'] as $field) {
            if ($this->request->getVar($field)) {
                array_push($fields, $field);
            }
        }
        if (!is_array($fields) || count($fields) === 0) {
            throw new \Exception($this->_viewDatas['title'] . '에서 변경할 항목(field)이 선택되지 않았습니다.');
        }
        //Fields,FielRules재정의
        $this->_viewDatas['fields'] = $fields;
        $this->_viewDatas['fieldRules'] = $this->getFieldRules($this->_viewDatas['fields'], array());
    }
    protected function batchjob_validate($entity)
    {
        return $this->update_validate($entity);
    }
    protected function batchjob_process($entity)
    {
        return $this->update_process($entity);
    }
    protected function batchjob_procedure()
    {
        $uids = $this->request->getVar('batchjob_uids');
        if (is_null($uids) || !is_array($uids) || !count($uids)) {
            return alert_CommonHelper($this->_viewDatas['title'] . '에서 변경할 항목(uid)이 선택되지 않았습니다.', 'back');
        }
        $message = "";
        try {
            $this->batchjob_init();
            $entitys = array();
            foreach ($uids as $uid) {
                $entity = $this->_model->getEntity($uid);
                try {
                    $entity = $this->batchjob_validate($entity);
                    array_push($entitys, $this->batchjob_process($entity));
                } catch (\Exception $e) {
                    Log::add("warning", "{$entity->getTitle()}는 다음과 같은 이유로 수정되지 않았습니다.\n<br>" . $e->getMessage());
                }
            }
            $message = "총: " . count($entitys) . "개의 수정(Batchjob)을 완료하였습니다.";
            Log::save("{$this->_viewDatas['title']} {$message}");
            return alert_CommonHelper($message, $this->_session->get(SESSION_NAMES['RETURN_URL']));
        } catch (\Exception $e) {
            $message = "총: " . count($uids) . "개의 수정(Batchjob)을 실패하였습니다.";
            Log::add("warning", $e->getMessage());
            Log::add("warning", var_export($this->_viewDatas['fieldDatas'], true));
            Log::save("{$this->_viewDatas['title']} {$message}", false);
            return alert_CommonHelper($message . "<br>\n{$e->getMessage()}", 'back');
        }
    }

    //Delete 관련
    protected function delete_process($entity)
    {
        if (!$this->_model->delete($entity->getPrimaryKey())) {
            Log::add("error", __FUNCTION__ . "에서 호출:" . $this->_model->getLastQuery());
            Log::add("error", implode("\n", $this->_model->errors()));
            throw new \Exception(__FUNCTION__ . " 오류 발생.\n" . var_export($this->_model->errors(), true));
        }
        return $entity;
    }
    protected function delete_procedure($uid)
    {
        $message = "";
        try {
            $entity = $this->_model->getEntity($uid);
            $this->delete_process($entity);
            $message = "{$entity->getTitle()} " . __FUNCTION__ . " 완료하였습니다.";
            Log::save("{$this->_viewDatas['title']} {$message}");
            return alert_CommonHelper($message, $this->_session->get(SESSION_NAMES['RETURN_URL']));
        } catch (\Exception $e) {
            $message = __FUNCTION__ . " 실패하였습니다.";
            Log::add("warning", $e->getMessage());
            Log::save("{$this->_viewDatas['title']} {$message}", false);
            return alert_CommonHelper($message . "<br>\n{$e->getMessage()}", 'back');
        }
    }

    //View 관련
    protected function view_init()
    {
        $this->_viewDatas['fields'] = $this->_defines['view']['fields'];
        $this->_viewDatas['fieldFilters'] = $this->_defines['view']['fieldFilters'];
        $this->_viewDatas['fieldRules'] = $this->getFieldRules($this->_viewDatas['fields'], $this->_defines['view']['fieldRules']);
        helper(['form']);
        $this->_viewDatas['fieldFormOptions'] = $this->getFieldFormOptions($this->_viewDatas['fieldFilters']);
        $this->_viewDatas['forms'] = ['attributes' => ['method' => "post",], 'hiddens' => []];
    }
    protected function view_process($entity)
    {
        return $entity;
    }
    protected function view_procedure($uid)
    {
        try {
            $entity = $this->_model->getEntity($uid);
            // dd($entity);
            $this->view_init();
            $this->_viewDatas['entity'] =  $this->view_process($entity);
            return view($this->_viewPath . '/view', $this->_viewDatas);
        } catch (\Exception $e) {
            return alert_CommonHelper($e->getMessage(), 'back');
        }
    }

    //Index 관련
    protected function index_init()
    {
        $this->_viewDatas['fields'] = $this->_defines['index']['fields'];
        $this->_viewDatas['fieldFilters'] = $this->_defines['index']['fieldFilters'];
        $this->_viewDatas['batchjobFilters'] = $this->_defines['index']['batchjobFilters'];
        helper(['form']);
        $this->_viewDatas['fieldFormOptions'] = $this->getFieldFormOptions($this->_viewDatas['fieldFilters']);
        $this->_viewDatas['forms'] = ['attributes' => ['method' => "post",], 'hiddens' => []];
        $this->_session->set(SESSION_NAMES['RETURN_URL'], current_url() . '?' . $this->request->getUri()->getQuery());

        foreach ($this->_viewDatas['fieldFilters'] as $field) {
            $this->_viewDatas[$field] = $this->request->getVar($field) ? $this->request->getVar($field) : DEFAULTS['EMPTY'];
        }
        $this->_viewDatas['word'] = $this->request->getVar('word') ? $this->request->getVar('word') : '';
        $this->_viewDatas['start'] = $this->request->getVar('start') ? $this->request->getVar('start') : '';
        $this->_viewDatas['end'] = $this->request->getVar('end') ? $this->request->getVar('end') : '';
        $this->_viewDatas['order_field'] = $this->request->getVar('order_field') ? $this->request->getVar('order_field') : 'uid';
        $this->_viewDatas['order_value'] = $this->request->getVar('order_value') ? $this->request->getVar('order_value') : 'DESC';
        $this->_viewDatas['page'] = $this->request->getVar('page') ? $this->request->getVar('page') : 1;
        $this->_viewDatas['per_page'] = $this->request->getVar('per_page') ? $this->request->getVar('per_page') : DEFAULTS['PERPAGE'];
        $this->_viewDatas['uri'] = $this->request->getUri();
    }
    //index 모델 전처리
    protected function index_setCondition()
    {
        foreach ($this->_viewDatas['fieldFilters'] as $field) {
            $value = $this->request->getVar($field) ? $this->request->getVar($field) : false;
            if ($value) {
                $this->_model->where($field, $value);
            }
        }
        $word = $this->request->getVar('word') ? $this->request->getVar('word') : '';
        if (isset($word) && $word !== '') {
            $this->_model->setIndexWordFilter($word);
        }
        $start = $this->request->getVar('start') ? $this->request->getVar('start') : '';
        $end = $this->request->getVar('end') ? $this->request->getVar('end') : '';
        if (isset($start) && $start !== '' && isset($end) && $end !== '') {
            $this->_model->setIndexDateFilter($start, $end);
        }
    }
    protected function index_getRows(int $page = 0, int $per_page = 0): array
    {
        //Totalcount 처리
        $this->index_setCondition();
        $this->_viewDatas['total_count'] = $this->_model->countAllResults();
        //Rows 처리
        $this->index_setCondition();
        //OrderBy
        $order_field = $this->request->getVar('order_field') ? $this->request->getVar('order_field') : 'uid';
        $order_value = $this->request->getVar('order_value') ? $this->request->getVar('order_value') : 'DESC';
        $this->_model->setIndexOrderBy($order_field, $order_value);
        //Limit
        $rows = $per_page ? $this->_model->findAll($per_page, $page * $per_page - $per_page) : $this->_model->findAll();
        // log_message("debug", __METHOD__ . "에서 호출[{$per_page}:{$page}=>{$page}*{$per_page}-{$per_page}]\n" . $this->_model->getLastQuery());
        return $rows;
    }
    private function index_getPagination($pager_group = 'default', int $segment = 0, $template = 'bootstrap_full'): string
    {
        // 1.Views/Pagers/에 bootstrap_full.php,bootstrap_simple.php 생성
        // 2.app/Config/Pager.php/$templates에 'bootstrap_full => 'Pagers\bootstrap_full',
        // 'bootstrap_simple' => 'Pagers\bootstrap_simple', 추가
        $pager = \Config\Services::pager();
        // $this->_model->paginate($this->_viewDatas['per_page'], $pager_group, $this->_viewDatas['page'], $segment);
        $pager->makeLinks(
            $this->_viewDatas['page'],
            $this->_viewDatas['per_page'],
            $this->_viewDatas['total_count'],
            $template,
            $segment,
            $pager_group
        );
        $this->_viewDatas['page'] = $pager->getCurrentPage($pager_group);
        $this->_viewDatas['total_page'] = $pager->getPageCount($pager_group);
        return $pager->links($pager_group, $template);
    }
    protected function index_process()
    {
        //모델 처리
        $this->_viewDatas['rows'] = $this->index_getRows((int)$this->_viewDatas['page'], (int)$this->_viewDatas['per_page']);
        //줄수 처리용
        $this->_viewDatas['pageOptions'] = array("" => "줄수선택");
        for ($i = 10; $i <= $this->_viewDatas['total_count'] + $this->_viewDatas['per_page']; $i += 10) {
            $this->_viewDatas['pageOptions'][$i] = $i;
        }
        //pagenation 처리
        $this->_viewDatas['pagination'] = $this->index_getPagination();
    }
    protected function index_procedure()
    {
        try {
            $this->index_init();
            $this->index_process();
            return view($this->_viewPath . '/index', $this->_viewDatas);
        } catch (\Exception $e) {
            return alert_CommonHelper($e->getMessage(), 'back');
        }
    }

    //Excel 관련
    protected function excel_init()
    {
        $this->_viewDatas['fields'] = $this->_defines['excel']['fields'];
        $this->_viewDatas['fieldFilters'] = $this->_defines['excel']['fieldFilters'];
        $this->_viewDatas['fieldFormOptions'] = $this->getFieldFormOptions($this->_viewDatas['fieldFilters']);
    }
    private function excel_getSpreadSheet()
    {
        //Excepl 초기화
        $spreadsheet = new \PhpOffice\PhpSpreadsheet\Spreadsheet();
        $sheet = $spreadsheet->getActiveSheet();
        //Header용
        $column = 'A';
        foreach ($this->_viewDatas['fields'] as $field) {
            $sheet->setCellValue($column++ . '1', lang($this->_className . '.label.' . $field));
        }
        //본문용
        $line = 2;
        foreach ($this->index_getRows() as $row) {
            $column = 'A';
            foreach ($this->_viewDatas['fields'] as $field) {
                // echo "\n<BR>".var_export($this->_fieldFilters,true)."\n<BR>".var_export($fieldFormOptions,true);exit;
                $value = in_array($field, $this->_viewDatas['fieldFilters']) ? $this->_viewDatas['fieldFormOptions'][$field][$row[$field]] : $row[$field];
                $sheet->setCellValue($column . $line, $value);
                $column++;
            }
            $line++;
        }
        return $spreadsheet;
    }
    protected function excel_process()
    {
        $fileName = date('Y-m-d_Hm') . '.xlsx';
        //파일저장 참고:https://teserre.tistory.com/19
        $writer = \PhpOffice\PhpSpreadsheet\IOFactory::createWriter($this->excel_getSpreadSheet(), 'Xlsx');
        //파일저장용
        // $writer->save(Excel_FilePath . '/' . $fileName);
        //Download시
        header("Content-Type: application/vnd.ms-excel");
        header(sprintf("Content-Disposition: attachment; filename=%s", urlencode($fileName)));
        return $writer->save('php://output');
    }
    protected function excel_procedure()
    {
        try {
            $this->excel_init();
            return $this->excel_process();
        } catch (\Exception $e) {
            return alert_CommonHelper($e->getMessage(), 'back');
        }
    }
}
