<?php

namespace App\Controllers;

use App\Controllers\BaseController;
use App\Models\CustomerModel;
use App\Models\PengirimanModel;
use App\Models\PermintaanCustomModel;
use Config\Services;

class PengirimanController extends BaseController
{
    public $pengiriman, $customer, $permintaan, $db, $session;

    public function __construct()
    {
        $this->pengiriman = new PengirimanModel();
        $this->customer = new CustomerModel();
        $this->permintaan = new PermintaanCustomModel();
        $this->db = db_connect();
        $this->session = Services::session();
    }

    public function index()
    {
        $data['title'] = 'Pengiriman';
        $pengiriman = $this->pengiriman->findAll();
        $pengiriman = $this->db->query("SELECT p.*, c.nama, pc.no_order_custom, pc.tanggal_order FROM tabel_pengiriman p JOIN tabel_permintaan_custom pc ON pc.kode_permintaan_custom = p.kode_permintaan_custom JOIN tabel_customer c ON c.id_customer = p.id_customer")->getResult();
        $data['data'] = $pengiriman;
        $data['user_id'] = auth()->user()->user_id;

        return view('pengiriman/index', $data);
    }

    public function create()
    {
        $data['title'] = 'Tambah Pengiriman';
        $data['customer'] = $this->customer->orderBy('created_at', 'desc')->findAll();
        $data['pemesanan'] = $this->db->query("SELECT pc.*, c.nama FROM tabel_permintaan_custom pc JOIN tabel_customer c ON c.id_customer = pc.id_customer WHERE pc.status_pesanan = 'selesai' AND pc.kode_permintaan_custom NOT IN (SELECT p.kode_permintaan_custom FROM tabel_pengiriman p) ")->getResult();
        return view('pengiriman/create', $data);
    }

    public function detail($id)
    {

        $data['title'] = 'Detail Pengiriman';

        $pengiriman = $this->db->query("SELECT p.*, c.nama, pc.no_order_custom, pc.tanggal_order FROM tabel_pengiriman p JOIN tabel_permintaan_custom pc ON pc.kode_permintaan_custom = p.kode_permintaan_custom JOIN tabel_customer c ON c.id_customer = p.id_customer WHERE p.kode_pengiriman = '$id'")->getRow();

        $data['pengiriman'] = $pengiriman;

        return view('pengiriman/detail', $data);
    }

    public function store()
    {
        $post = $this->request->getPost();

        $permintaan = $this->permintaan->where('kode_permintaan_custom', $post['kode_permintaan_custom'])->first();

        $data = [
            'kode_permintaan_custom' => $post['kode_permintaan_custom'],
            'no_pengiriman' => $this->pengiriman->generatePengirimanNumber('KRM'),
            'id_customer' => $permintaan->id_customer,
            'created_by_role' => auth()->user()->role,
            'tanggal_pengiriman' => date('Y-m-d H:i:s', strtotime($post['tanggal_pengiriman'])),
            'status' => 'Belum Proses'
        ];

        $this->pengiriman->insert($data);
        return redirect()->to('pengiriman');
    }

    public function update($id)
    {
        $post = $this->request->getPost();

        $data = [
            'status' => $post['status'],
            'proses_at' => $post['status'] == 'Proses Pengiriman' ? date('Y-m-d H:i:s') : null,
            'sampai_at' => $post['status'] == 'Sampai' ? date('Y-m-d H:i:s') : null,
        ];

        $this->pengiriman->update($id, $data);
        return redirect()->to('pengiriman');
    }

    public function delete($id)
    {
        $this->pengiriman->delete($id);
        return redirect()->to('pengiriman');
    }
}
