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

class ReferralCompleted extends MX_Controller {

    public function __construct() {
        parent::__construct();
        $this->load->helper('url'); 
        $this->load->model('ReferralCompleted_model');
    }
     public function index()
{
    // This line fetches the data from your database model
    $data['referrals'] = $this->ReferralCompleted_model->getReferrals(); 
    
    $data['referralNoMax'] = $this->ReferralCompleted_model->getMaxReferralNo(); 
    $data['nextReferralNumber'] = $this->ReferralCompleted_model->getNextReferralNumber();
      $data['latestReferralNumber'] = $this->ReferralCompleted_model->getLatestReferralNumber();
     $data['companies'] = $this->ReferralCompleted_model->getCompanies(); 
    $data['view'] = 'listReferralCompleted'; 
    
    // This sends the data to your 'master' or 'addReferral' view
    $this->load->view('master', $data);
}////////
   public function deleteReferral() {
    $id = $this->input->post('id');
    
    if (!empty($id)) {
        $result = $this->ReferralCompleted_model->deleteReferral($id);
        if ($result) {
            echo json_encode(array('status' => true, 'message' => 'Referral Deleted Successfully'));
        } else {
            echo json_encode(array('status' => false, 'message' => 'Failed to delete referral'));
        }
    } else {
        echo json_encode(array('status' => false, 'message' => 'Invalid ID'));
    }
}
   // public function index()
   // {
        
      //  $data['Referral'] = $this->ReferralCompleted_model->getReferrals(); //var_dump($data); die;
       // $data['referralNoMax'] = $this->ReferralCompleted_model->getMaxReferralNo(); // Ensure this variable is passed to the view
       // $data['view']  = 'addReferral';
       // $this->load->view('addReferral');
    //}
 public function saveReferral()
{
    error_reporting(E_ALL);
    ini_set('display_errors', 1);

    $post = $this->input->post();
    $referralId = !empty($post['referralId']) ? (int)$post['referralId'] : 0;

    // Directly decode the cookie in PHP
    $userId = 0;
    if (!empty($_COOKIE['user_data'])) {
        $json = urldecode($_COOKIE['user_data']);
        $cookieData = json_decode($json, true);
        
        // Loop through the decoded cookie data to find any key containing 'id' or 'user'
        if (is_array($cookieData)) {
            foreach ($cookieData as $key => $val) {
                if (stripos($key, 'id') !== false || stripos($key, 'user') !== false) {
                    if (!empty($val)) {
                        $userId = (int)$val;
                        break;
                    }
                }
            }
        }
    }

    $data = array(
        'referralName'   => $post['referralName'],
        'referralquaNo'  => $post['referralquaNo'],
        'referralDate'   => $post['referralDate'],
        'referralPhone1' => $post['referralPhone1'],
        'medicalType'    => $post['medicalType'],
        'companyName'    => $post['companyName'],
        'notes'          => $post['notes'],
        'userId'         => $userId  // Automatically populated from the PHP cookie
    );

    if ($referralId > 0) {
        $data['referralNumber'] = $post['referralNumber']; 
        $this->ReferralCompleted_model->updateReferral($referralId, $data);

        echo json_encode(array(
            'status' => true,
            'message' => 'Referral Updated Successfully'
        ));

    } else {
        $data['referralNumber'] = $this->ReferralCompleted_model->getNextReferralNumber();
        $data['status'] = 1;
        $this->ReferralCompleted_model->insertReferral($data);

        echo json_encode(array(
            'status' => true,
            'message' => 'Referral Added Successfully'
        ));
    }
}
  //public function saveReferral() {
   // $post = $this->input->post();
   // $referralId = !empty($post['referralId']) ? (int)$post['referralId'] : 0;

   // $data = [
      //  'referralName'    => $post['referralName'],
      //  'referralNo'      => $post['referralNo'],
      //  'referralPhone1'  => $post['referralPhone1'],
     //   'medicalType'     => $post['medicalType'],  // Added to match form
     //   'otherDetails'    => $post['otherDetails'],
    //    'notes'           => $post['notes']         // Added to match form
   // ];

   // if ($referralId > 0) {
      //  $this->ReferralCompleted_model->updateReferral($referralId, $data);
       // echo json_encode(['status' => true, 'message' => 'Referral Updated Successfully']);
  //  } else {
      //  $this->ReferralCompleted_model->insertReferral($data);
       // echo json_encode(['status' => true, 'message' => 'Referral Added Successfully']);
  //  }
//}

    public function getReferral() {
        $id = $this->input->post('id');
        echo json_encode($this->ReferralCompleted_model->getReferralById($id));
    }
}
