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

class Save_Estimate extends CI_Controller {
    
    public function __construct()
    {
        parent::__construct();
        $this->load->model('Get_An_Estimate_Model', 'estimate');
        $this->load->library('upload'); // Upload library load karo
        header("Access-Control-Allow-Origin: *");
        header("Access-Control-Allow-Methods: POST");
        header("Access-Control-Allow-Headers: Content-Type");
    }
    
    public function index()
    {
        if ($_SERVER['REQUEST_METHOD'] !== 'POST') {
            echo json_encode([
                'status' => 'error',
                'message' => 'Invalid request method'
            ]);
            return;
        }

        $data = [
            'patient_name'   => $this->input->post('patient_name'),
            'patient_mobile' => $this->input->post('patient_mobile'),
            'patient_email'  => $this->input->post('patient_email'),
            'patient_age'    => $this->input->post('patient_age'),
            'patient_gender' => $this->input->post('patient_gender'),
            'decription'     => $this->input->post('decription'),
            'status'         => $this->input->post('status') ?: 0,
            'publish'        => $this->input->post('publish') ?: 1,
        ];

        // Upload configuration
        $upload_path = './uploads/';
        
        // Folder check karo, nahi hai to banao
        if (!is_dir($upload_path)) {
            mkdir($upload_path, 0777, true);
        }

        // Latest Report upload
        if (!empty($_FILES['latest_report']['name'])) {
            $config['upload_path']   = $upload_path;
            $config['allowed_types'] = 'pdf|jpg|jpeg|png|doc|docx';
            $config['max_size']      = 10240;
            $config['encrypt_name']  = true; // Unique name generate karega
            
            $this->upload->initialize($config);
            
            if ($this->upload->do_upload('latest_report')) {
                $upload_data = $this->upload->data();
                $data['latest_report'] = $upload_data['file_name']; // Encrypted filename save hogi
            }
        }

        // Last Medical Prescription upload
        if (!empty($_FILES['last_medical_prescription']['name'])) {
            $config['upload_path']   = $upload_path;
            $config['allowed_types'] = 'pdf|jpg|jpeg|png|doc|docx';
            $config['max_size']      = 10240;
            $config['encrypt_name']  = true;
            
            $this->upload->initialize($config);
            
            if ($this->upload->do_upload('last_medical_prescription')) {
                $upload_data = $this->upload->data();
                $data['last_medical_prescription'] = $upload_data['file_name'];
            }
        }

        $insert_id = $this->estimate->save($data);
        
        if ($insert_id) {
            echo json_encode([
                'status' => 'success',
                'message' => 'Data saved successfully',
                'id' => $insert_id
            ]);
        } else {
            echo json_encode([
                'status' => 'error',
                'message' => 'Failed to save data'
            ]);
        }
    }
}