<?php

namespace App\Controllers\Pesaing;

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

class Datapesaing extends BaseController
{
    use ResponseTrait;

    public function __construct()
    {
        $this->DatapesaingModel = model('App\Models\Pesaing\DatapesaingModel');
    }

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

    public function index()
    {
        //function($modul, $menu) --------
        $links = genlinks('pesaing', 'datapesaing');

        $link_show = base_url('pesaing/datapesaing/show/');

        $rekam = $links['rekam']['status'];
        $link_rekam = base_url('pesaing/datapesaing/new');

        $ubah = $links['ubah']['status'];
        $link_ubah = base_url('pesaing/datapesaing/edit/');

        $hapus = $links['hapus']['status'];
        $link_hapus = base_url('pesaing/datapesaing/delete');

        return view('pesaing/dpesaing/pesaing_list', [
            'rekam' => $rekam,
            'link_show' => $link_show,
            'link_rekam' => $link_rekam,
            'ubah' => $ubah,
            'link_ubah' => $link_ubah,
            'hapus' => $hapus,
            'link_hapus' => $link_hapus,
        ]);
    }

    public function datagrid()
    {
        $this->setJsonHeader();
        
        $user_id = auth()->id();
        $id_kantor = $this->DatapesaingModel->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 = multi_sort('id', 'ASC');
        }

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

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

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

    public function show($id)
    {
        $row = $this->DatapesaingModel->find_by_id(decrypt_url($id));

        if (!$row) {
            exit('Data tidak ditemukan');
        }

        // Ambil data SWOT pesaing
        $swot_data = $this->DatapesaingModel->get_swot_pesaing(decrypt_url($id));

        $map_api = getenv('GOOGLE_MAPS_API_KEY');
        return view('pesaing/dpesaing/pesaing_show', [
            'back' => base_url('pesaing/datapesaing'),
            'id' => encrypt_url($row->id),
            'nama_pesaing' => $row->nama_pesaing,
            'alamat' => $row->alamat,
            'lati' => $row->lati,
            'longi' => $row->longi,
            'phone' => $row->phone,
            'website' => $row->website,
            'rating' => $row->rating,
            'nama_user' => $row->username,
            'wkt_rekam' => $row->wkt_rek,
            'kantor_lati' => $row->kantor_lati,
            'kantor_longi' => $row->kantor_longi,
            'swot_data' => $swot_data,
            'map_api' => $map_api,
        ]);
    }

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

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

        $ids  = $this->request->getPost('ids');

        $del = array();
        foreach ($ids as $id) {
            $del[] = decrypt_url($id);
        }

        if (!$this->DatapesaingModel->delete($del)) {
            return $this->fail($this->DatapesaingModel->errors(), 500);
        } else {
            return $this->respond(['message' => count($del).lang('App.delete_success')]);
        }
    }
}
