<?php

namespace App\Controllers\Admin;
use App\Controllers\BaseController;
// use App\Models\UserModel;
use App\Models\CustomerModel;
use App\Models\QuoteModel;
use App\Models\QuoteItemModel;

class Quote extends BaseController
{

    public function viewquotes()
    {
    $quoteModel = new QuoteModel();
    $data['quotes'] = $quoteModel->getAllQuotes();
    return view('admin/quotes/template-view-quotes',$data);
    }

    public function viewquote($id)
    {
       $quoteModel = new QuoteModel();
       $customerModel = new CustomerModel();
       $quoteItemModel = new QuoteItemModel();

        $quote = $quoteModel->find($id);
        $customer=$customerModel->find($quote['cid']);
        $quoteItems = $quoteItemModel->where('quotation_id',$quote['qid'])->findAll();
                         
    return view('admin/quotes/template-view-quotes',['quote'=>$quote,'customer'=>$customer,'quoteItems'=>$quoteItems]);
    }


    public function createquote() {

        $customerModel = new CustomerModel();

        $data['customers'] = $customerModel
                            ->select('cid, c_cname')
                            ->where('c_status', 'active')
                            ->orderBy('c_cname', 'ASC')
                            ->findAll();

        return view('admin/quotes/template-create-quote',$data);
    }


    public function savequote()
    {
        $quoteModel = new QuoteModel();
        $itemModel  = new QuoteItemModel();

        $header = [

            'quotation_no' =>
                'QN-' .str_pad($this->request->getPost('customername'),5,'0',STR_PAD_LEFT).'-'.date('Y-hi'),

            'cid' =>
                $this->request->getPost('customername'),

            'quotation_date' =>
                $this->request->getPost('quotationdate'),

            'validity_date' =>
                $this->request->getPost('quotationvalid'),

            'po_number' =>
                $this->request->getPost('purchaseorder'),

            'po_date' =>
                $this->request->getPost('purchaseorderdate'),

            'subtotal' =>
                $this->request->getPost('subtotal'),

            'discount' =>
                $this->request->getPost('discount'),

            'amount_after_discount' =>
                $this->request->getPost('afterdiscount'),

            'vat_amount' =>
                $this->request->getPost('displayvat'),

            'net_amount' =>
                $this->request->getPost('netamount'),

            'payment_terms' =>
                $this->request->getPost('paymentterms'),

            'delivery_terms' =>
                $this->request->getPost('contendeliveryterms'),

            'q_status' => 'Draft',

        ];
      
        $quoteModel->insert($header);
        
        $qid = $quoteModel->insertID();
        
        
        $items = json_decode(
            $this->request->getPost('quotation_items'),
            true
            );
            
        foreach($items as $item)
        {
            $itemModel->insert([

                'quotation_id' => $qid,

                'product' =>
                    $item['product_name'],

                'description' =>
                    $item['description'],

                'unit' =>
                    $item['unit'],

                'qty' =>
                    $item['qty'],

                'unit_price' =>
                    $item['unitPrice'],

                'amount' =>
                    $item['amount'],

                'vat_amount' =>
                    $item['vat']
            ]);
        }

        return redirect()->to('/admin/viewquote/'. $qid);
    }
}