<?php

namespace App\Controllers\Api;

use CodeIgniter\RESTful\ResourceController;
use CodeIgniter\API\ResponseTrait;
//use App\Models\BillingModel;
use App\Libraries\JWTCI4;


class Peda extends ResourceController
{
    use ResponseTrait;
    // get all billing
    public function index()
    {
        $d = array('resp_code' => '00', 'resp_desc' => 'PEDAMATENG');
        return $this->respond($d);

    }

    	function tunjuk()
	{
	    
		$request = \Config\Services::request();
        $db = \Config\Database::connect();
		$data = $request->getPost('data');
		$wilayah_id = $request->getPost('wilayah_id');
		$armada_id = $data['armada_id'];
		$q = "update tbl_armada set wilayah_id=:wilayah_id: where armada_id=:armada_id:";
		$db->query($q,['wilayah_id'=>$wilayah_id,'armada_id'=>$armada_id]);

		echo json_encode(array('success'=>true));

	}


    public function show($id = null)
    {
        $blarr = array('-','Januari','Februari','Maret','April','Mei','Juni','Juli','Agustus','September','Oktober','November','Desember');
        $db = \Config\Database::connect();

        $query = $db->query("select a.id_billing,a.tahun,a.bulan,a.status,a.tgl_penetapan,a.tgl_jth_tempo,a.nominal,a.denda,(a.nominal+a.denda) as tot_tagihan,b.pengelola_id,c.nik as pengelola_nik,c.nama as pengelola_nama,c.alamat as pengelola_alamat,c.wa as pengelola_wa,b.kontrak_no,date_format(b.kontrak_tgl,'%Y%m%d') as kontrak_tgl,date_format(b.kontrak_tglberlaku,'%Y%m%d') as kontrak_tglberlaku,b.kontrak_nilai,d.nptp,e.kec_nama as kecamatan,f.kel_nama as kelurahan,d.keterangan as titikparkir
        from billing a
        left join ep_lokasinilai b on a.id_lokasinilai=b.id
        left join ep_pengelola c on b.pengelola_id=c.id
        left join ep_lokasi d on b.lokasi_id = d.id
        left join ep_wilayah e on d.kec_kode=e.kec_kode and e.kel_kode='00'
        left join ep_wilayah f on d.kec_kode=f.kec_kode and d.kel_kode=f.kel_kode where a.id_billing =:id_billing:",['id_billing'=>$id]);

        $data = $query->getRowArray();
        $dt = array();
        
        if ($data) {
            if($data['status'] == '1') {
            $dt['resp_code'] = '02';
            $dt['resp_desc'] = 'Tagihan Telah Dibayar / Lunas';
            } else {
            $dt['resp_code'] = '00';
            $dt['resp_desc'] = 'Success';
            }
            //$dt['id_billing'] = KODEOPD . str_pad($data['id_billing'], 10, '0', STR_PAD_LEFT);
            $dt['id_billing'] = $data['id_billing'];
            $dt['kode_opd'] = KODEOPD;
            $dt['nama_opd'] = NAMAOPD;
            $dt['no_kontrak'] = $data['kontrak_no'];
            $dt['keterangan'] = 'Kontrak Pengelolaan Titik Parkir:'.$data['nptp'].' Tahun : '.$data['tahun'];
            $dt['tgl_penetapan'] = $data['tgl_penetapan'];
            $dt['tgl_jth_tempo'] = $data['tgl_jth_tempo'];
            $dt['kode_pengelola'] = $data['pengelola_id'];
            $dt['nama_pengelola'] = $data['pengelola_nama'];
            $dt['alamat_pengelola'] = $data['pengelola_alamat'];
            $dt['kode_blj_1']= $data['id_billing'];
            $dt['ket_blj_1']= 'Pembayaran Tagihan Bulanan, bulan:'.$blarr[$data['bulan']].' Tahun:'.$data['tahun'];
            $dt['amt_blj_1']= $data['tot_tagihan'];
            return $this->respond($dt);

        } else {
            $d = array('resp_code' => '01', 'resp_desc' => 'Transaksi Gagal / Data Tidak Ditemukan');
            return $this->respond($d);
        }
    }

    // update billing
    public function update($id = null)
    {
        $db = \Config\Database::connect();

        $q = "select id_billing,status from billing where id_billing=:id_billing:";
        $query = $db->query($q, ['id_billing' => $id]);
        $row = $query->getRow();
        if (!$row) {
            $response = ['resp_code' => '01', 'resp_desc' => 'Transaksi Gagal / Data tidak ditemukan'];
        } else {
            if ($row->status == '1') {
                $response = ['resp_code' => '02', 'resp_desc' => 'Tagihan Telah Dibayar/ Lunas'];
            } else {
                $q = "update billing set status=:status:,tgl_posting=:tgl_posting: where id_billing=:id_billing:";
                $db->db_debug = false;
                if (!$db->query($q, ['status' => '1', 'tgl_posting' => date('Ymd'), 'id_billing' => $id])) {
                    $error = $db->error();
                    $response = ['resp_code' => '99', 'resp_desc' => $error];
                } else {
                    $response = ['resp_code' => '00', 'resp_desc' => 'Transaksi Berhasil'];
                }
            }
        }
            return $this->respond($response);
    }

    // delete billing
    public function delete($id = null)
    {
        //$model = new BillingModel();
        //$data = $model->find($id);
        $data = 1;
        if ($data) {
            $model->delete($id);
            $response = [
                'status'   => 200,
                'error'    => null,
                'messages' => [
                    'success' => 'Data Deleted'
                ]
            ];

            return $this->respondDeleted($response);
        } else {
            return $this->failNotFound('No Data Found with id ' . $id);
        }
    }
}
