<?php

class StranasNewHelpController extends BaseController {
    public function index() {
        $this->checkIsAuthenticated();
        $getParams = $this->request->get();

        $getParams['limit'] = $this->getDefaultLimit($getParams);
        $getParams['offset'] = array_key_exists('offset', $getParams) ? $getParams['offset'] : 0;

        $params = $this->validate(
            $getParams,
            [
                'limit' => ['validators' => ['digit']],
                'offset' => ['validators' => ['digit']],
                'keyword' => ['validators' => []]
            ]
        );

        $condition = 'deleted_at is null';

        $data = StranasNewHelp::find([
            'conditions' => $condition,
            'limit' => $params['limit'],
            'offset' => $params['offset'],
            'order' => 'label'
        ]);

        $total_record = StranasNewHelp::count();

        return new PaginatedResponse($data, $total_record, $params['limit'], $params['offset']);
    }
    // C
    public function create() {
        $this->checkScopes(['stranas.admin']);

        $rawBody = $this->request->getJsonRawBody(true);
        $params = $this->validate( $rawBody, [ 
            'label' => ['validators' => ['required']], 
            'description' => ['validators' => ['required']],
        ] );

        $tbl = new StranasNewHelp();
        $tbl->label = $params['label'];
        $tbl->description = $params['description'];
        $now = date('Y-m-d H:i:s');
        $tbl->created_at = $now;
        $tbl->created_by = $this->shared->user->um_id;
        $status = $tbl->save();

        if (!$status) {
            $error = $tbl->getMessages();
            throw new ValidationException($error->getMessage());
        } else {
            return new CollectionPaginatedResponse($tbl);
        }
    }
    // R
    public function detail($id) {
        $tbl = StranasNewHelp::findFirst($id);
        if (empty($tbl)) {
            throw new CustomErrorException(BaseResponse::INVALID_REQUEST, 'Data tidak ditemukan');
        }
        $result = $tbl;
        return new CollectionPaginatedResponse([$result]);
    }
    // U
    public function update($id) {
        $this->checkScopes(['stranas.admin']);
        $tbl = StranasNewHelp::findFirst($id);

        $rawBody = $this->request->getJsonRawBody(true);
        $params = $this->validate( $rawBody, [ 
            'label' => ['validators' => ['required']], 
            'description' => ['validators' => ['required']],
        ]);

        // validasi
        $tbl->label = $params['label'];
        $tbl->description = $params['description'];

        $now = date('Y-m-d H:i:s');
        $tbl->updated_at = $now;
        $tbl->updated_by = $this->shared->user->um_id;
        $status = $tbl->save();

        if (!$status) {
            $error = $tbl->getMessages();
            throw new ValidationException($error->getMessage());
        } else {
            return new CollectionPaginatedResponse([$tbl]);
        }
    }
    // D
    public function delete($id) {
        // $this->checkScopes(['stranas.admin']);

        $tbl = StranasNewHelp::findFirst($id);
        $now = date('Y-m-d H:i:s');
        $tbl->deleted_at = $now;
        $tbl->deleted_by = $this->shared->user->um_id;
        $status = $tbl->save();

        if (!$status) {
            $error = $tbl->getMessages();
            throw new ValidationException($error->getMessage());
        } else {
            return new CollectionPaginatedResponse([$tbl]);
        }
    }

}
