<?php

namespace App\Controllers;

use App\Models\OrderModel;

class Orders extends BaseController
{
    public function create()
    {
        helper('form');
        $data['title'] = 'Order Layanan';
        return view('templates/header', $data)
            . view('orders/create')
            . view('templates/footer');
    }

    public function store()
    {
        helper('form');
        $model = new OrderModel();

        if (! $this->validate([
            'customer_name'  => 'required|min_length[3]|max_length[100]',
            'customer_email' => 'required|valid_email|max_length[100]',
            'customer_phone' => 'required|min_length[10]|max_length[20]',
            'service_type'   => 'required',
            'description'    => 'permit_empty|max_length[1000]',
        ])) {
            $data['title'] = 'Order Layanan';
            return view('templates/header', $data)
                . view('orders/create', ['validation' => $this->validator])
                . view('templates/footer');
        }

        $model->save([
            'customer_name'  => $this->request->getPost('customer_name'),
            'customer_email' => $this->request->getPost('customer_email'),
            'customer_phone' => $this->request->getPost('customer_phone'),
            'service_type'   => $this->request->getPost('service_type'),
            'description'    => $this->request->getPost('description'),
        ]);

        return redirect()->to('/order/success');
    }

    public function success()
    {
        $data['title'] = 'Order Berhasil';
        return view('templates/header', $data)
            . view('orders/success')
            . view('templates/footer');
    }
}
