<?php

namespace App\Controllers\Admin;

use App\Models\UserModel;
use CodeIgniter\HTTP\RequestInterface;
use CodeIgniter\HTTP\ResponseInterface;
use Psr\Log\LoggerInterface;

class UserController extends \App\Controllers\Admin\AdminController
{
    public function initController(RequestInterface $request, ResponseInterface $response, LoggerInterface $logger)
    {
        parent::initController($request, $response, $logger);
        $this->_className .= '/User';
        $this->_model = new UserModel();
        $this->_defines = [
            'insert' => [
                'fields' => ['id', 'passwd', 'name', 'email', 'role', 'status'],
                'fieldFilters'  => ['role', 'status'],
                'fieldRules' => [
                    'id'                => 'required|min_length[4]|max_length[20]|is_unique[user.id]',
                    'passwd'            => 'required|trim|min_length[4]|max_length[150]',
                    'name'              => 'required|min_length[2]|max_length[20]',
                    'email'             => 'required|valid_email',
                    'role'              => 'required|in_list[member,manager,cloudflare,director,master]',
                    'status'            => 'required|in_list[use,unuse]',
                ]
            ],
            'update' => [
                'fields' => ['passwd', 'name',  'email', 'role', 'status'],
                'fieldFilters'  => ['role', 'status'],
                'fieldRules' => [
                    'passwd'            => 'required|trim|min_length[4]|max_length[150]',
                    'name'              => 'required|min_length[2]|max_length[20]',
                    'email'             => 'required|valid_email',
                    'role'              => 'required|in_list[member,manager,cloudflare,director,master]',
                    'status'            => 'required|in_list[use,unuse]',
                ]
            ],
            'view' => [
                'fields' => ['id', 'name', 'email', 'role', 'status', 'updated_at', 'created_at'],
                'fieldFilters'  => ['role', 'status'],
                'fieldRules' => [],
            ],
            'index' => [
                'fields' => ['id', 'name', 'email', 'role', 'status', 'created_at'],
                'fieldFilters'  => ['role', 'status'],
                'batchjobFilters' => ['role', 'status'],
            ],
            'excel' => [
                'fields' => ['id', 'name', 'email', 'role', 'status', 'created_at'],
                'fieldFilters'  => ['role', 'status'],
            ],
        ];
        helper($this->_className);
        $this->_viewPath = strtolower($this->_className);
        $this->_viewDatas['title'] = lang($this->_className . '.title');
        $this->_viewDatas['className'] = $this->_className;
    }

    //Insert관련
    protected function insert_process()
    {
        //암호값 hash작업
        $this->_viewDatas['fieldDatas']['passwd'] = password_hash($this->_viewDatas['fieldDatas']['passwd'], PASSWORD_DEFAULT);
        return parent::insert_process();
    }
    //Update관련
    protected function update_process($entity)
    {
        //암호값 hash작업
        $entity->passwd = password_hash($entity->passwd, PASSWORD_DEFAULT);
        return parent::update_process($entity);
    }
}
