<?php

namespace App\Controllers\Cusen;

use App\Controllers\BaseController;
use CodeIgniter\API\ResponseTrait;

class BrowsePesan extends BaseController
{
    use ResponseTrait;

    public function __construct()
    {
        $this->PesanModel = model('App\Models\Cusen\PesanModel');
        $this->PesanChildModel = model('App\Models\Cusen\PesanChildModel');
    }

    private function setJsonHeader()
    {
        return $this->response->setHeader('Content-Type', 'application/json');
    }

    public function index()
    {
        //cek privilege dan link
        if (priv_menu('browse_pesan', 'hapus')) {
            $hapus = 1;
            $link_hapus = base_url('cusen/browse_pesan/delete_pesan');
        } else {
            $hapus = 0;
            $link_hapus = '';
        }

        return view('cusen/browse/pesan_list', [
            'hapus' => $hapus,
            'action' => $link_hapus,
        ]);
    }

    public function datagrid_pesan()
    {
        $this->setJsonHeader();

        $user_id = auth()->id();
        $id_kantor = $this->PesanModel->kantor_user($user_id);

        $page           = trim($this->request->getPost('page')) ? trim($this->request->getPost('page')) : 1;
        $rows           = trim($this->request->getPost('rows')) ? trim($this->request->getPost('rows')) : 10;
        $sort           = trim($this->request->getPost('sort'));
        $order          = trim($this->request->getPost('order'));
        $filterRules    = trim($this->request->getPost('filterRules'));
        $start          = ($page - 1) * $rows;

        if ($sort && $order) {
            $sortOrder = multi_sort($sort, $order);
        } else {
            $sortOrder = NULL;
        }

        $datas = $this->PesanModel->get_rowsx($rows, $start, $filterRules, $sortOrder, $id_kantor);
        foreach ($datas as $k => $v)
        {
            $datas[$k]->id = encrypt_url($v->id);
        }

        $res['total']  = $this->PesanModel->get_totalx($filterRules, $id_kantor);
        $res['rows']   = $datas;

        return $this->response->setJSON($res);
    }

    public function datagrid_rcv()
    {
        $this->setJsonHeader();

        $id_pesan = (!empty($this->request->getPost('id_pesan')) ? decrypt_url($this->request->getPost('id_pesan')) : $this->session->get('id_psn_bp'));
        $this->session->set('id_psn_bp',$id_pesan);

        $page           = trim($this->request->getPost('page')) ? trim($this->request->getPost('page')) : 1;
        $rows           = trim($this->request->getPost('rows')) ? trim($this->request->getPost('rows')) : 10;
        $sort           = trim($this->request->getPost('sort'));
        $order          = trim($this->request->getPost('order'));
        $filterRules    = trim($this->request->getPost('filterRules'));
        $start          = ($page - 1) * $rows;

        if ($sort && $order) {
            $sortOrder = multi_sort($sort, $order);
        } else {
            $sortOrder = NULL;
        }

        $datas = $this->PesanChildModel->get_rows2($id_pesan, $rows, $start, $filterRules, $sortOrder);

        $res['total']  = $this->PesanChildModel->get_total2($id_pesan, $filterRules);
        $res['rows']   = $datas;

        return $this->response->setJSON($res);
    }

    public function delete_pesan()
    {
        $this->setJsonHeader();

        //jika tdk memiliki akses utk hapus data
        if (!priv_menu('browse_pesan', 'hapus')) {
            return $this->fail(['message' => NO_AKSES], 403);
        }

        //collect data form
        $ids  = $this->request->getPost('ids');
        $del = array();
        foreach ($ids as $id) {
            $del[] = decrypt_url($id);
        }

        //jika gagal hapus
        if (!$this->PesanModel->delete($del)) {
            return $this->fail($this->PesanModel->errors(), 500);
        }

        return $this->respond(['message' => B_HAPUS], 201);
    }
}