<?php

namespace App\Controllers\Admin\Cloudflare\API;

use App\Models\Cloudflare\API\AccountModel;
use CodeIgniter\HTTP\RequestInterface;
use CodeIgniter\HTTP\ResponseInterface;
use Psr\Log\LoggerInterface;
use App\Libraries\Log\Log;

class AccountController extends APIController
{
    private $_auth_uids = null;
    public function initController(RequestInterface $request, ResponseInterface $response, LoggerInterface $logger)
    {
        parent::initController($request, $response, $logger);
        $this->_className .= '/Account';
        $this->_model = new AccountModel();
        $this->_defines = [
            'index' => [
                'fields' => ['auth_uid', 'title', 'type', 'status', 'updated_at', 'created_at'],
                'fieldFilters'  => ['auth_uid', 'type', 'status'],
                'batchjobFilters' => [],
            ],
            'excel' => [
                'fields' => ['auth_uid', 'title', 'type', 'status', 'updated_at', 'created_at'],
                'fieldFilters'  => ['auth_uid', 'type', 'status'],
            ],
        ];
        helper($this->_className);
        $this->_viewPath = strtolower($this->_className);
        $this->_viewDatas['title'] = lang($this->_className . '.title');
        $this->_viewDatas['className'] = $this->_className;
    }

    //Field별 Form Option용
    protected function getFieldFormOption(string $field): array
    {
        switch ($field) {
            case 'auth_uid':
                if (is_null($this->_auth_uids)) {
                    //모든 필요한 FormOption등 조기화작업 필요
                    $this->_auth_uids = [DEFAULT_EMPTY => lang($this->_className . '.label.' . $field) . ' 선택'];
                    foreach ($this->getAuthModel()->orderBy('id', 'asc')->findAll() as $auth) {
                        $this->_auth_uids[$auth['uid']] = $auth['id'];
                    }
                }
                return $this->_auth_uids;
                break;
            default:
                return parent::getFieldFormOption($field);
                break;
        }
    }

    ////Action 모음
    //Insert관련
    // final public function insert()
    // {
    //     return $this->insert_procedure();
    // }
    //Update관련
    // final public function update($uid)
    // {
    //     return $this->update_procedure($uid);
    // }
    //Toggle관련
    // final public function toggle($uid, string $field)
    // {
    //     return $this->toggle_procedure($uid, $field);
    // }
    //Batchjob 관련
    // final public function batchjob()
    // {
    //     return $this->batchjob_procedure();
    // }
    //Delete 관련
    // final public function delete($uid)
    // {
    //     return $this->delete_procedure($uid);
    // }
    //View 관련
    // final public function view($uid)
    // {
    //     return $this->view_procedure($uid);
    // }
    //Index 관련
    final public function index()
    {
        return $this->index_procedure();
    }
    //Excel 관련
    final public function excel()
    {
        return $this->excel_procedure();
    }
    ////API Action
    //Reload관련
    protected function reload_process($entity)
    {
        //Zone Reload
        $zoneApi = new \App\Libraries\Cloudflare\API\Zone($entity, false);
        $zoneApi->reload();

        return $entity;
    }
    final public function reload($uid)
    {
        return $this->reload_procedure($uid);
    }
}
