<?php
defined('BASEPATH') OR exit('No direct script access allowed');


class ReceiptGenAlgorithm extends CI_Controller {


  /**
    * Get All Data from this method.
    *
    * @return Response
   */
   
   public function index()
   {
	$this->load->view('welcome_message');
   }


   /**
    * Get Download PDF File
    *
    * @return Response
   */
   function receipt(){


	$this->load->library('pdf');


    // $data['name'] = 'Pat Levin';
    // $data['amount'] = '$3988';
    // $data['description'] = '$100';
    // $data['balance'] = '$3230';
    // $data['payment_method'] = 'Wire Transfer';
    // $data['item'] = 'Pat and Family Safari';
    // $data['date'] = '20th/06/2023';
    
    $data['name'] = isset($_POST['fullname']) ? $_POST['fullname'] : '';
    $data['amount'] = isset($_POST['amount']) ? $_POST['amount'] : '';
     $data['link'] = isset($_POST['link']) ? $_POST['link'] : '';
    $data['description'] = isset($_POST['description']) ? $_POST['description'] : '';
    $data['balance'] = isset($_POST['balance']) ? $_POST['balance'] : '';
    $data['payment_method'] = isset($_POST['payment_method']) ? $_POST['payment_method'] : '';
    $data['item'] = isset($_POST['package']) ? $_POST['package'] : '';
if (isset($_POST['date'])) {
    $date = DateTime::createFromFormat('D M d Y H:i:s e+', $_POST['date']);
    $data['date'] = $date !== false ? $date->format('d/m/Y') : '';
} else {
    $data['date'] = '';
}



     $lastReceipt = $this->db->order_by('id', 'desc')->get('receipts')->row();
    if ($lastReceipt) {
        $lastReceiptNo = $lastReceipt->receipt_no;
        $newReceiptNo = str_pad(intval($lastReceiptNo) + 1, strlen($lastReceiptNo), '0', STR_PAD_LEFT);
    } else {
        $newReceiptNo = '00145';
    }
    
    $data['receipt_no'] = $newReceiptNo;
    $data['date_added'] = time();
   
    $this->db->insert('receipts',$data);
    
    

    // $this->load->view('receipt', $data);
        $this->load->library('pdf');
        $html =  $this->load->view('test', $data,true);
        $this->pdf->createPDF($html, $data['name'], false);
   }
   
   function receipts(){
       $receipts = $this->db->order_by('id','desc')->get('receipts')->result_array();
       
       
          return $this->output
            ->set_content_type('application/json')
            ->set_status_header(200)
            ->set_output(json_encode(array(
                'success' => true,
                'message' => "FETCHED_SUCCESSFULLY",
                'receipts'=>$receipts
            )));
    
   }
   
   function download_receipt($id){

    $receipt = $this->db->get_where('receipts',array("id"=>$id))->row();
    $data['name'] = $receipt->name;
    $data['amount'] = $receipt->amount;
    $data['receipt_no'] = $receipt->receipt_no;
    $data['link'] = $receipt->link;
    $data['description'] = $receipt->description;
    $data['balance'] = $receipt->balance;
    $data['payment_method'] = $receipt->payment_method;
    $data['item'] = $receipt->item;
    $data['date'] = $receipt->date;

    $this->load->library('pdf');
    $html =  $this->load->view('test', $data,true);
    $this->pdf->createPDF($html, $data['name'], false);
    
   }
}