<?php

class StranasNewAbbrController 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 = StranasNewAbbr::find([
            'conditions' => $condition,
            'limit' => $params['limit'],
            'offset' => $params['offset']
        ]);

        $total_record = StranasNewAbbr::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, [ 
            'id_instansi' => ['validators' => ['required', 'numeric']], 
            'singkatan' => ['validators' => ['required']]
        ] );

        $tbl = new StranasNewAbbr();
        $tbl->id_instansi = $params['id_instansi'];
        $tbl->singkatan = $params['singkatan'];
        $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) {
            $errors = $tbl->getMessages();

            $error_messages = [];
            foreach ($errors as $key => $error) {
                $error_messages[] = ["field" => $error->getField(), "message" => $error->getMessage()];
            }
            throw new ValidationException($error_messages);
        } else {
		return new CollectionPaginatedResponse([$tbl]);
	}
    }
    // R
    public function detail($id) {
        $tbl = StranasNewAbbr::findFirst($id);
        
        $result = $tbl;
        return new CollectionPaginatedResponse([$result]);
    }
    // U
    public function update($id) {
        $this->checkScopes(['stranas.admin']);
        $tbl = StranasNewAbbr::findFirst($id);

        
        $rawBody = $this->request->getJsonRawBody(true);

        $params = $this->validate( $rawBody, [ 
            'id_instansi' => ['validators' => ['required', 'numeric']], 
            'singkatan' => ['validators' => ['required']]
        ]);

        // validasi
        $tbl->id_instansi = $params['id_instansi'];
        $tbl->singkatan = $params['singkatan'];
        $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) {
            $errors = $tbl->getMessages();
            $error_messages = [];

            foreach ($errors as $key => $error) {
                $error_messages[] = ["field" => $error->getField(), "message" => $error->getMessage()];
            }

            throw new ValidationException($error_messages);
        } else {
            return new CollectionPaginatedResponse([$tbl]);
        }
    }
    // D
    public function delete($id) {
        $this->checkScopes(['stranas.admin']);

        $tbl = StranasNewAbbr::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) {
            $errors = $tbl->getMessages();
            $error_messages = [];

            foreach ($errors as $key => $error) {
                $error_messages[] = ["field" => $error->getField(), "message" => $error->getMessage()];
            }

            throw new ValidationException($error_messages);
        } else {
            return new CollectionPaginatedResponse([$tbl]);
        }
    }
    
}
