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

class Get_An_Estimate extends CI_Controller {

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

    public function index()
    {
        $data["extra_js"] = array("get_an_estimate");
        $data['controller'] = base_url('get-an-estimate');
        $data['page_title'] = "Manage Estimates";
        $data['page_heading'] = "Manage Estimates";
        $data['main_content'] = 'get_an_estimate';

        $this->load->view('main_content', $data);
    }

    public function category_active($id)
    {
        $this->estimate->update(['id' => $id], ['status' => 1]);
        echo json_encode('sucess');
    }

    public function category_deactive($id)
    {
        $this->estimate->update(['id' => $id], ['status' => 0]);
        echo json_encode('sucess');
    }

    public function ajax_list()
    {
        $list = $this->estimate->get_datatables();
        $data = [];
        $no = $_POST['start'] + 1;

        foreach ($list as $row) {
            
            // Status Badge with Dropdown
            if ($row->status == 1) {
                $status = '
                <div class="dropdown">
                    <span class="badge bg-success dropdown-toggle cursor-pointer" 
                          data-bs-toggle="dropdown" style="cursor:pointer;">
                        Active
                    </span>
                    <ul class="dropdown-menu">
                        <li>
                            <a class="dropdown-item text-danger" 
                               href="javascript:void(0)" 
                               onclick="category_deactive('.$row->id.')">
                                <i class="fa fa-ban me-1"></i> Inactive
                            </a>
                        </li>
                    </ul>
                </div>';
            } else {
                $status = '
                <div class="dropdown">
                    <span class="badge bg-danger dropdown-toggle cursor-pointer" 
                          data-bs-toggle="dropdown" style="cursor:pointer;">
                        Inactive
                    </span>
                    <ul class="dropdown-menu">
                        <li>
                            <a class="dropdown-item text-success" 
                               href="javascript:void(0)" 
                               onclick="category_active('.$row->id.')">
                                <i class="fa fa-check me-1"></i> Active
                            </a>
                        </li>
                    </ul>
                </div>';
            }

            // File Links
            $latest_report = !empty($row->latest_report) 
                ? '<a href="'.base_url('uploads/'.$row->latest_report).'" target="_blank" class="btn btn-sm btn-info">
                      <i class="fa fa-file"></i>
                   </a>' 
                : '-';

            $prescription = !empty($row->last_medical_prescription) 
                ? '<a href="'.base_url('uploads/'.$row->last_medical_prescription).'" target="_blank" class="btn btn-sm btn-info">
                      <i class="fa fa-file"></i>
                   </a>' 
                : '-';

           
      $action = '
<a type="button"
   class="delete_btn btn btn-sm btn-icon"
   href="javascript:void(0)"
   title="Delete Record"
   data-id="'.$row->id.'">
   <svg xmlns="http://www.w3.org/2000/svg"
        width="24" height="24"
        viewBox="0 0 24 24"
        class="eva eva-trash-2-outline">
        <g data-name="Layer 2">
            <g data-name="trash-2">
                <rect width="24" height="24" opacity="0"></rect>
                <path d="M21 6h-5V4.33A2.42 2.42 0 0 0 13.5 2h-3A2.42 2.42 0 0 0 8 4.33V6H3a1 1 0 0 0 0 2h1v11a3 3 0 0 0 3 3h10a3 3 0 0 0 3-3V8h1a1 1 0 0 0 0-2zM10 4.33c0-.16.21-.33.5-.33h3c.29 0 .5.17.5.33V6h-4zM18 19a1 1 0 0 1-1 1H7a1 1 0 0 1-1-1V8h12z"></path>
                <path d="M9 17a1 1 0 0 0 1-1v-4a1 1 0 0 0-2 0v4a1 1 0 0 0 1 1z"></path>
                <path d="M15 17a1 1 0 0 0 1-1v-4a1 1 0 0 0-2 0v4a1 1 0 0 0 1 1z"></path>
            </g>
        </g>
   </svg>
</a>
';


            $data[] = [
                $no++,
                $row->patient_name,
                $row->patient_mobile,
                $row->patient_email,
                $latest_report,
                $prescription,
                $status,
                $row->patient_age,
                ucfirst($row->patient_gender),
                $row->decription,
                $action
            ];
        }

        echo json_encode([
            "draw" => $_POST['draw'],
            "recordsTotal" => $this->estimate->count_all(),
            "recordsFiltered" => $this->estimate->count_filtered(),
            "data" => $data
        ]);
    }

    public function ajax_delete($id)
    {
        $this->estimate->delete_data(['id' => $id], ['publish' => 0]);
        echo json_encode(['status' => 'ok']);
    }
}