<?php

namespace App\Controllers;

use App\Controllers\BaseController;

use App\Entities\FormOptionEntity;
use App\Libraries\LogCollector;
use App\Services\MyLogService;
use CodeIgniter\HTTP\DownloadResponse;

use CodeIgniter\HTTP\RedirectResponse;
use CodeIgniter\HTTP\RequestInterface;
use CodeIgniter\HTTP\ResponseInterface;
use CodeIgniter\Validation\Validation;
use PhpOffice\PhpSpreadsheet\IOFactory;

use PhpOffice\PhpSpreadsheet\Reader\Html;
use PhpOffice\PhpSpreadsheet\Writer\Pdf\Mpdf;
use Psr\Log\LoggerInterface;

abstract class CommonController extends BaseController
{
    private $_db;
    private $_myAuth = null;
    private ?MyLogService $_myLogService = null;
    private $_viewDatas = [];
    private $_control = [];
    abstract public function getService(): mixed;
    abstract function getHelper(): mixed;
    public function initController(RequestInterface $request, ResponseInterface $response, LoggerInterface $logger)
    {
        parent::initController($request, $response, $logger);
        $this->isLoggedIn = false;
        $this->uri    = $request->getUri();
        if ($this->getMyAuth()->isLoggedIn()) {
            $this->isLoggedIn = true;
            $this->myAuthName = $this->getMyAuth()->getNameByAuthInfo();
            $this->myAuthUID = $this->getMyAuth()->getUIDByAuthInfo();
        }
        $this->_db = \Config\Database::connect();
    }
    final public function __get($name)
    {
        if (!array_key_exists($name, $this->_viewDatas)) {
            return null;
        }
        return $this->_viewDatas[$name];
    }
    final public function __set($name, $value): void
    {
        $this->_viewDatas[$name] = $value;
    }
    final protected function getMyAuth(): mixed
    {
        if (!$this->_myAuth) {
            $this->_myAuth = service('myauth');
        }
        return $this->_myAuth;
    }
    final protected function getControlDatas(?string $key = null): mixed
    {
        if (!$key) {
            return $this->_control;
        }
        return array_key_exists($key, $this->_control) ? $this->_control[$key] : null;
    }
    private function setControlDatas(string $key, mixed $values): void
    {
        if (!array_key_exists($key, $this->_control)) {
            $this->_control[$key] = [];
        }
        $this->_control[$key] = $values;
    }
    final protected function getViewDatas(): array
    {
        return $this->_viewDatas;
    }
    final protected function getMyLogService(): mixed
    {
        if (!$this->_myLogService) {
            $this->_myLogService = new MyLogService();
        }
        return $this->_myLogService;
    }
    //Index,FieldForm관련
    final protected function setAction(string $action): void
    {
        $this->_control['action'] = $action;
    }
    final protected function getAction(): string
    {
        if (!array_key_exists('action', $this->_control)) {
            throw new \Exception("action이 정의되지 않았습니다.");
        }
        return $this->_control['action'];
    }
    final protected function initAction(array $actionFields): void
    {
        //Fields 정의
        $this->setControlDatas('actionFields', array_key_exists('fields', $actionFields) ? $actionFields['fields'] : []);
        //Filters 정의
        $this->setControlDatas('actionFilters', array_key_exists('filters', $actionFields) ? $actionFields['filters'] : []);
        //Field Rule정의
        foreach ($this->getControlDatas('actionFields') as $field) {
            $this->setFormFielRule($field, $this->getFormFieldRule_process($this->getAction(), $field));
        }
        //Form용 Options정의
        foreach ($this->getControlDatas('actionFilters') as $field) {
            $options = [];
            foreach ($this->getFormFieldOption_process($field) as $entity) {
                $options[$entity->getPK()] = $entity;
            }
            $this->setFormFieldOptions($field, $options);
        }
        //일괄작업용 Fields정의
        $this->setControlDatas(
            'batchjob_fields',
            array_key_exists('batchjob_fields', $actionFields) ? $actionFields['batchjob_fields'] : $this->getService()->getBatchjobFields()
        );
        //일괄작업용 버튼정의
        $this->setControlDatas(
            'batchjob_buttions',
            array_key_exists('batchjob_buttions', $actionFields) ? $actionFields['batchjob_buttions'] : $this->getService()->getBatchjobButtons()
        );
    }
    //FormRules정의
    final protected function setFormFielRule($field, string $rule)
    {
        //기존 Filter Rules 가져와서 field에 해당하는 rule이 없으면 field를 filter_rules 전체 적용
        $allRules = $this->getControlDatas('field_rules') ?? [];
        $allRules[$field] = $rule;
        $this->setControlDatas('field_rules', $allRules);
    }
    //FormOptions정의
    final protected function setFormFieldOptions($field, array $options)
    {
        //기존 Filter Options 가져와서 field에 해당하는 option이 없으면 field를 key로 배열추가 후 다시 filter_options 전체 적용
        $allOptions = $this->getControlDatas('filter_optons') ?? [];
        $allOptions[$field] = $options;
        $this->setControlDatas('filter_optons', $allOptions);
    }
    //전체 FormDatas 전달값받기
    final protected function getFormDatas(array $fields, array $requestDatas, array $formDatas = []): array
    {
        foreach ($fields as $field) {
            //Post값이 null이면 formDatas에서 해당하는 default값이 있는지 확인후 넣고,없으면 최종적으로 null
            $datas = $requestDatas[$field] ?? null;
            if ($datas !== null) {
                $formDatas[$field] = $datas;
            } else {
                $formDatas = $this->getFormData_process($field, $requestDatas, $formDatas);
            }
        }
        return $formDatas;
    }
    final protected function setFieldDefaultValue(string $field, mixed $default): void
    {
        if (array_key_exists('field_default_values', $this->_control)) {
            $this->_control['field_default_values'] = [];
        }
        $this->_control['field_default_values'][$field] = $default;
    }
    //FormDatas 검증
    final protected function doValidation(array $formDatas): void
    {
        //변경할 값 확인 : Upload된 파일 검증시 $this->request->getPOST()보다 먼처 체크필요
        $validation = service('validation');
        foreach ($this->getControlDatas('field_rules') as $field => $rule) {
            $validation = $this->doValidation_process($validation, $field, $rule);
        }
        if (!$validation->run($formDatas)) {
            throw new \Exception("{$this->getService()->getClassName()} 작업 데이터 검증 오류발생\n" . implode(
                "\n",
                $validation->getErrors()
            ));
        }
        // return $validation->getValidated();
    }
    //생성 기본기능
    final public function create_form(): RedirectResponse|string
    {
        try {
            //각 Field 초기화
            $this->setAction(__FUNCTION__);
            //각 Field 초기화
            $this->initAction($this->getService()->getFormFields());
            $this->create_form_process();
            //actionFilters에 해당하는 값이 있을 경우 정의
            helper(['form']);
            $this->forms = ['attributes' => ['method' => "post",], 'hiddens' => []];
            return $this->getResultSuccess_process();
        } catch (\Exception $e) {
            return $this->getResultFail_process($e->getMessage());
        }
    }
    final public function create(): RedirectResponse|string
    {
        $this->_db->transStart();
        try {
            //각 Field 초기화
            $this->setAction(__FUNCTION__);
            //각 Field 초기화
            $this->initAction($this->getService()->getFormFields());
            //전달값받기
            $formDatas = $this->getFormDatas($this->getControlDatas('actionFields'), $this->request->getPost());
            $formDatas = $this->create_validate_process($formDatas);
            $this->entity = $this->create_process($formDatas);
            $this->_db->transCommit();
            return $this->getResultSuccess_process();
        } catch (\Exception $e) {
            $this->_db->transRollback();
            return $this->getResultFail_process($e->getMessage());
        }
    }
    //수정 기본기능
    final public function modify_form(mixed $uid): RedirectResponse|string
    {
        try {
            //각 Field 초기화
            $this->setAction(__FUNCTION__);
            //각 Field 초기화
            $this->initAction($this->getService()->getFormFields());
            //기존 Entity 가져오기
            $entity = $this->getService()->getEntity($uid);
            if (!$entity) {
                throw new \Exception("{$uid}에 대한 정보를 찾을수 없습니다.");
            }
            $this->entity = $this->modify_form_process($entity);
            helper(['form']);
            $this->forms = ['attributes' => ['method' => "post",], 'hiddens' => []];
            return $this->getResultSuccess_process();
        } catch (\Exception $e) {
            return $this->getResultFail_process($e->getMessage());
        }
    }
    final public function modify(int $uid): RedirectResponse|string
    {
        //Transaction Start
        $this->_db->transStart();
        try {
            //각 Field 초기화
            $this->setAction(__FUNCTION__);
            //각 Field 초기화
            $this->initAction($this->getService()->getFormFields());
            //기존 Entity 가져오기
            $entity = $this->getService()->getEntity($uid);
            if (!$entity) {
                throw new \Exception("{$uid}에 대한 정보를 찾을수 없습니다.");
            }
            //전달값받기
            $formDatas = $this->getFormDatas($this->getControlDatas('actionFields'), $this->request->getPost());
            $formDatas = $this->modify_validate_process($formDatas);
            $this->entity = $this->modify_process($entity, $formDatas);
            $this->_db->transCommit();
            return $this->getResultSuccess_process();
        } catch (\Exception $e) {
            $this->_db->transRollback();
            return $this->getResultFail_process($e->getMessage());
        }
    }
    //단일필드작업기능
    final public function toggle(mixed $uid, string $field): RedirectResponse|string
    {
        //Transaction Start
        $this->_db->transStart();
        try {
            //각 Field 초기화
            $this->setAction(__FUNCTION__);
            //각 Field 초기화:조건항목 Field는 한개만 존재하므로 Field와 Rule을 정의
            $this->initAction(['fields' => [$field], 'filters' => [$field]]);
            //기존 Entity 가져오기
            $entity = $this->getService()->getEntity($uid);
            if (!$entity) {
                throw new \Exception("{$uid}에 대한 정보를 찾을수 없습니다.");
            }
            //전달값받기
            $formDatas = $this->getFormDatas($this->getControlDatas('actionFields'), $this->request->getVar());
            $formDatas = $this->toggle_validate_process($formDatas);
            // echo var_dump($this->getControlDatas());
            // dd($formDatas);
            $this->entity = $this->toggle_process($entity,  $formDatas);
            $this->_db->transCommit();
            return $this->getResultSuccess_process();
        } catch (\Exception $e) {
            $this->_db->transRollback();
            return $this->getResultFail_process($e->getMessage());
        }
    }
    //일괄처리작업기능
    protected function batchjob_pre_process(): array
    {
        $selectedFields = [];
        //getBatchJobFields를 이용해서 선택된 Field 와 값정의
        $formDatas = [];
        foreach ($this->getService()->getBatchjobFields() as $field) {
            $value = $this->request->getPost($field);
            if ($value) {
                $selectedFields[] = $field;
                $formDatas[$field] = $value;
            }
        }
        if (!count($selectedFields)) {
            throw new \Exception("변경할 조건항목을 선택하셔야합니다.");
        }
        //변경할 UIDS 정의
        $uids = $this->request->getPost('batchjob_uids[]');
        if (!is_array($uids) || !count($uids)) {
            throw new \Exception("적용할 리스트을 선택하셔야합니다.");
        }
        return [$selectedFields, $formDatas, $uids];
    }
    final public function batchjob(): RedirectResponse|string
    {
        //Transaction Start
        $this->_db->transStart();
        try {
            //각 Field 초기화
            $this->setAction(__FUNCTION__);
            list($selectedFields, $formDatas, $uids) = $this->batchjob_pre_process();
            //각 Field 초기화:조건항목 Field와 Rule을 정의
            $this->initAction(['fields' => $selectedFields, 'filters' => $selectedFields]);
            //전달값받기
            $formDatas = $this->getFormDatas($this->getControlDatas('actionFields'), $this->request->getPost());
            $formDatas = $this->batchjob_validate_process($formDatas);
            $entities = [];
            foreach ($uids as $uid) {
                //기존 Entity 가져오기
                $entity = $this->getService()->getEntity($uid);
                if (!$entity) {
                    throw new \Exception("{$uid}에 대한 정보를 찾을수 없습니다.");
                }
                $entities[] = $this->batchjob_process($entity, $formDatas);
            }
            $this->entities = $entities;
            $this->_db->transCommit();
            LogCollector::debug(sprintf("%s에서 총 %s개중 %s개 일괄작업을 완료하였습니다.", __METHOD__, count($uids), count($this->entities)));
            return $this->getResultSuccess_process();
        } catch (\Exception $e) {
            $this->_db->transRollback();
            return $this->getResultFail_process($e->getMessage());
        }
    }
    final public function delete(mixed $uid): RedirectResponse|string
    {
        //Transaction Start
        $this->_db->transStart();
        try {
            //Action 정의:삭제는 다른 초기화 필요없음
            $this->setAction(__FUNCTION__);
            //기존 Entity 가져오기
            $entity = $this->getService()->getEntity($uid);
            if (!$entity) {
                throw new \Exception("{$uid}에 대한 정보를 찾을수 없습니다.");
            }
            $this->delete_process($entity);
            $this->_db->transCommit();
            return $this->getResultSuccess_process();
        } catch (\Exception $e) {
            $this->_db->transRollback();
            return $this->getResultFail_process($e->getMessage());
        }
    }
    //일괄삭제
    final public function batchjob_delete(): RedirectResponse|string
    {
        //Transaction Start
        $this->_db->transStart();
        try {
            //Action 정의:삭제는 다른 초기화 필요없음
            $this->setAction(__FUNCTION__);
            //변경할 UIDS
            $uids = $this->request->getPost('batchjob_uids[]');
            if (!is_array($uids) || !count($uids)) {
                throw new \Exception("적용할 리스트를 선택하셔야합니다.");
            }
            $entities = [];
            foreach ($uids as $uid) {
                //기존 Entity 가져오기
                $entity = $this->getService()->getEntity($uid);
                if (!$entity) {
                    throw new \Exception("{$uid}에 대한 정보를 찾을수 없습니다.");
                }
                $entities[] = $this->batchjob_delete_process($entity);
            }
            $this->entities = $entities;
            $this->_db->transCommit();
            LogCollector::debug(sprintf("%s에서 총 %s개중 %s개 일괄삭제를 완료하였습니다.", __METHOD__, count($uids), count($this->entities)));
            return $this->getResultSuccess_process();
        } catch (\Exception $e) {
            $this->_db->transRollback();
            return $this->getResultFail_process($e->getMessage());
        }
    }
    final public function view(string $uid): RedirectResponse|string
    {
        try {
            //Action 정의
            //각 Field 초기화
            $this->setAction(__FUNCTION__);
            //각 Field 초기화
            $this->initAction($this->getService()->getViewFields());
            //기존 Entity 가져오기
            $entity = $this->getService()->getEntity($uid);
            if (!$entity) {
                throw new \Exception("{$uid}에 대한 정보를 찾을수 없습니다.");
            }
            $this->entity = $this->view_process($entity);
            helper(['form']);
            $this->forms = ['attributes' => ['method' => "post",], 'hiddens' => []];
            return $this->getResultSuccess_process();
        } catch (\Exception $e) {
            return $this->getResultFail_process($e->getMessage());
        }
    }
    //OUPUT Document 관련
    private function download_document(string $document_type, mixed $loaded_data): array
    {
        $full_path = WRITEPATH . DIRECTORY_SEPARATOR . "excel";
        switch ($document_type) {
            case 'excel':
                $file_name = sprintf("%s_%s.xlsx", $this->getService()->getClassName(), date('Y-m-d_Hm'));
                $writer = IOFactory::createWriter($loaded_data, 'Xlsx');
                $writer->save($full_path . DIRECTORY_SEPARATOR . $file_name);
                break;
            case 'pdf':
                $file_name = sprintf("%s_%s.pdf", $this->getService()->getClassName(), date('Y-m-d_Hm'));
                $writer = new Mpdf($loaded_data);
                $writer->save($full_path . DIRECTORY_SEPARATOR . $file_name);
                break;
        }
        return array($full_path, $file_name);
    }
    // Download
    final public function download(string $output_type, mixed $uid = false): DownloadResponse|RedirectResponse|string
    {
        try {
            //각 Field 초기화
            $this->setAction(__FUNCTION__);
            //각 Field 초기화
            $this->initAction($this->getService()->getIndexFields());
            //URL처리
            // $this->uri = $this->request->getUri();
            switch ($output_type) {
                case 'excel':
                case 'pdf':
                    helper(['form']);
                    $this->index_process();
                    $html = $this->getResultSuccess_process();
                    //data loading
                    $reader = new Html();
                    $loaded_data = $reader->loadFromString($html);
                    list($full_path, $file_name) = $this->download_document($output_type, $loaded_data);
                    $full_path .= DIRECTORY_SEPARATOR . $file_name;
                    break;
                default:
                    if (!$uid) {
                        throw new \Exception("{$output_type}은 반드시 uid의 값이 필요합니다.");
                    }
                    $entity = $this->getService()->getEntity($uid);
                    if (!$entity) {
                        throw new \Exception("{$uid}에 대한 정보를 찾을수 없습니다.");
                    }
                    $this->entity = $entity;
                    list($file_name, $uploaded_filename) = $entity->getDownlaodFile();
                    $full_path = WRITEPATH . DIRECTORY_SEPARATOR . "uploads" . DIRECTORY_SEPARATOR . $uploaded_filename;
                    break;
            }
            return $this->response->download($full_path, null)->setFileName($file_name);
        } catch (\Exception $e) {
            return $this->getResultFail_process($e->getMessage());
        }
    }
    //공통 기본 기능

    //추가 개별 처리 기능
    //FieldForm관련용
    protected function getFormFieldOption_process(string $field, array $options = []): array
    {
        switch ($field) {
            default:
                $options = $this->getService()->getFormFieldOption($field, $options);
                break;
        }
        if (!is_array($options)) {
            throw new \Exception(__FUNCTION__ . "에서 {$field}의 options 값이 array가 아닙니다.\n" . var_export($options, true));
        }
        return $options;
    }
    protected function getFormFieldRule_process(string $action, string $field): string
    {
        if (is_array($field)) {
            throw new \Exception(__FUNCTION__ . "=> field가 array 입니다.\n" . var_export($field, true));
        }
        switch ($field) {
            default:
                $rule = $this->getService()->getFormFieldRule($action, $field);
                break;
        }
        return $rule;
    }
    //FormData Field별 전달값 처리
    protected function getFormData_process(string $field, array $requestDatas, array $formDatas): array
    {
        return $formDatas;
    }
    protected function doValidation_process(Validation $validation, string $field, string $rule): Validation
    {
        switch ($field) {
            default:
                $validation->setRule($field, $field, $rule);
                break;
        }
        return $validation;
    }
    //Process Result처리
    protected function getResultFail_process(string $message = MESSAGES["FAILED"]): RedirectResponse
    {
        LogCollector::debug($message);
        $this->getMyLogService()->save($this->getService()->getClassName(), $this->getAction(), $message, $this->getMyAuth()->getUIDByAuthInfo());
        if ($this->request->getMethod() === 'POST') {
            return redirect()->back()->withInput()->with('error', $message);
        }
        return redirect()->to($this->getMyAuth()->popPreviousUrl())->with('error', $message);
    }
    protected function getResultSuccess_process(string $message = MESSAGES["SUCCESS"], ?string $actionTemplate = null): RedirectResponse|string
    {
        helper(['form']);
        switch ($this->getControlDatas('action')) {
            case 'create':
            case 'modify':
                $this->getMyLogService()->save($this->getService()->getClassName(), $this->getAction(), $message, $this->getMyAuth()->getUIDByAuthInfo());
                $result = $this->view($this->entity->getPK());
                break;
            case 'create_form':
            case 'modify_form':
            case 'login_form':
            case 'view':
            case 'index':
            case 'download':
                $this->control = $this->getControlDatas();
                $this->getHelper()->setViewDatas($this->getViewDatas());
                $actionTemplate = $this->request->getVar('ActionTemplate') ?? $actionTemplate;
                if ($actionTemplate) {
                    $view_file = $this->view_path . $actionTemplate . DIRECTORY_SEPARATOR . $this->getAction();
                } else {
                    $view_file = $this->view_path . $this->getAction();
                }
                $result = view($view_file, ['viewDatas' => $this->getViewDatas()]);
                break;
            default:
                $result = redirect()->to($this->getMyAuth()->popPreviousUrl())->with('error', $message);
                break;
        }
        return $result;
    }
    //Index,FieldForm관련
    //생성관련
    protected function create_form_process(): void {}
    protected function create_validate_process(array $formDatas): array
    {
        $this->doValidation($formDatas);
        return $formDatas;
    }
    protected function create_process(array $formDatas): mixed
    {
        return $this->getService()->create($formDatas);
    }
    //수정관련
    protected function modify_form_process(mixed $entity): mixed
    {
        return $entity;
    }
    protected function modify_validate_process(array $formDatas): array
    {
        $this->doValidation($formDatas);
        return $formDatas;
    }
    protected function modify_process(mixed $entity, array $formDatas): mixed
    {
        return $this->getService()->modify($entity, $formDatas);
    }
    //단일필드작업기능
    protected function toggle_validate_process(array $formDatas): array
    {
        $this->doValidation($formDatas);
        return $formDatas;
    }
    protected function toggle_process(mixed $entity, array $formDatas): mixed
    {
        return $this->getService()->modify($entity, $formDatas);
    }
    //일괄처리작업기능
    protected function batchjob_validate_process(array $formDatas): array
    {
        $this->doValidation($formDatas);
        return $formDatas;
    }
    protected function batchjob_process($entity, array $formDatas): mixed
    {
        return $this->getService()->modify($entity, $formDatas);
    }
    //삭제관련
    protected function delete_process(mixed $entity): mixed
    {
        return $this->getService()->delete($entity);
    }
    //일괄삭제관련
    protected function batchjob_delete_process(mixed $entity, array $entities = []): mixed
    {
        return $this->getService()->delete($entity);
    }
    //View 관련
    protected function view_process(mixed $entity): mixed
    {
        return $entity;
    }
    //리스트관련
    //조건절 처리
    //Filter Field별 전달값 처리
    protected function index_condition_filterfield_process(string $field, $filter_values = []): array
    {
        switch ($field) {
            default:
                $filter_value = $this->request->getVar($field);
                if ($filter_value !== null && $filter_value !== '') {
                    $this->getService()->index_condition_filterField($field, $filter_value);
                    $filter_values[$field] = $filter_value;
                }
                break;
        }
        return $filter_values;
    }
    protected function index_condition_process(): void
    {
        //Paramter로 전달된 Filter값을 정의용
        $filter_values = [];
        foreach ($this->getControlDatas('actionFilters') as $field) {
            $filter_values = $this->index_condition_filterfield_process($field, $filter_values);
        }
        $this->setControlDatas('filter_values', $filter_values);
        //검색어조건절 처리
        $this->word = $this->request->getVar('word');
        if ($this->word !== null && $this->word !== '') {
            $this->getService()->index_condition_filterWord($this->word);
        }
        //날자검색
        $this->start  = $this->request->getVar('start');
        $this->end  = $this->request->getVar('end');
        if ($this->start !== null && $this->start !== '' && $this->end !== null && $this->end !== '') {
            $this->getService()->index_condition_filterDate($this->start, $this->end);
        }
    }
    //PageNation 처리
    protected function index_pagenation_process($pager_group = 'default', int $segment = 0, $template = 'bootstrap_full')
    {
        //Page, Per_page필요부분
        $this->page = (int) $this->request->getVar('page') ?: 1;
        $this->per_page = (int) $this->request->getVar('per_page') ?: intval(DEFAULT_LIST_PERPAGE ?? 20);
        // 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 = service("pager");
        $pager->makeLinks($this->page, $this->per_page, $this->total_count, $template, $segment, $pager_group);
        $this->page = $pager->getCurrentPage($pager_group);
        $this->total_page = $pager->getPageCount($pager_group);
        return $pager->links($pager_group, $template);
    }
    //Page출력 처리
    protected function index_pageOptions_process(): array
    {
        $page_options = ["" => "줄수선택"];
        for ($i = $this->per_page; $i <= $this->total_count; $i += $this->per_page) {
            $page_options[$i] = $i;
        }
        $page_options[$this->total_count] = $this->total_count;
        return $page_options;
    }
    //Entities처리
    protected function index_process(array $entities = []): array
    {
        foreach ($this->getService()->getEntities() as $entity) {
            $entities[] = $entity;
        }
        return $entities;
    }
    public function index(): RedirectResponse|string
    {
        try {
            //각 Field 초기화
            $this->setAction(__FUNCTION__);
            //각 Field 초기화
            $this->initAction($this->getService()->getIndexFields());
            helper(['form']);
            //Return Url정의
            $this->getMyAuth()->pushCurrentUrl($this->request->getUri()->getPath() . ($this->request->getUri()->getQuery() ? "?" . $this->request->getUri()->getQuery() : ""));
            //조건절 처리
            $this->index_condition_process();
            //TotalCount (SoftDelete적용이 되려면 countAllResults를 사용해야함)
            $this->total_count = $this->getService()->getTotalCount();
            //Pagination 처리
            $this->pagination = $this->index_pagenation_process();
            //줄수 처리용
            $this->page_options = $this->index_pageOptions_process();
            //조건절 처리
            //OrcerBy , Limit 처리
            $this->order_field = $this->request->getVar('order_field');
            $this->order_value = $this->request->getVar('order_value');
            $this->getService()->setOrderBy($this->order_field, $this->order_value);
            $this->getService()->setLimit($this->per_page);
            $this->getService()->setOffset(($this->page - 1) * $this->per_page);
            $this->index_condition_process();
            $this->entities = $this->index_process();
            return $this->getResultSuccess_process();
        } catch (\Exception $e) {
            return $e->getMessage();
            // return $this->getResultFail_process($e->getMessage());
        }
    }
}
