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

class Save_health_checkup extends CI_Controller {

    public function __construct()
    {
        parent::__construct();
        $this->load->model('Book_A_Health_Checkup_Model');
    }

    public function index()
    {
        header("Access-Control-Allow-Origin: https://synergy.jeetinfo.co.in/");
        header("Access-Control-Allow-Methods: POST, OPTIONS");
        header("Access-Control-Allow-Headers: Content-Type");
        header("Content-Type: application/json");

        if ($_SERVER['REQUEST_METHOD'] === 'OPTIONS') {
            exit;
        }

        if ($this->input->method(TRUE) !== 'POST') {
            echo json_encode([
                'status' => 'error',
                'message' => 'Invalid request method'
            ]);
            return;
        }

        $input = json_decode(file_get_contents("php://input"), true);

        if (!$input) {
            echo json_encode([
                'status' => 'error',
                'message' => 'Invalid JSON'
            ]);
            return;
        }

        $data = [
            'patient_name'     => $input['patient_name'],
            'patient_mobile'   => $input['patient_mobile'],
            'patient_email'    => $input['patient_email'],
            'appontment_date'  => $input['appontment_date'],
            'appontment_time'  => $input['appontment_time'],
            'package_name'     => $input['package_name'],
            'decription'       => $input['decription'] ?? null,
            'status'           => '1',
            'publish'          => '1',
            'date_time'        => date('Y-m-d H:i:s')
        ];

        $insert_id = $this->Book_A_Health_Checkup_Model->save($data);

        if ($insert_id) {
            echo json_encode([
                'status' => 'success',
                'message' => 'Health checkup booked successfully',
                'id' => $insert_id
            ]);
        } else {
            echo json_encode([
                'status' => 'error',
                'message' => 'Failed to save data'
            ]);
        }
    }
}
