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

class Book_A_Health_Checkup extends CI_Controller {

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

    public function index()
    {
        $data["extra_js"] = array("book_a_health_checkup");
        $data['controller'] = base_url("Book_A_Health_Checkup");
        $data['page_title'] = "Manage Book Health Checkups";
        $data['page_heading'] = "Manage Book Health Checkups";
        $data['main_content'] = 'book_a_health_checkup';

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

    public function confirm_checkup($id)
    {
        $this->category->update_status($id, [
            'status' => 1
        ]);
        echo json_encode(['status' => true]);
    }

    public function reject_checkup()
    {
        $id = $this->input->post('checkup_id');
        $remarks = $this->input->post('remarks');

        $this->category->update_status($id, [
            'status' => 2,
            'remarks' => $remarks
        ]);

        echo json_encode(['status' => true]);
    }

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

        foreach ($list as $row) {

       
            if ($row->status == 0) {

                $status = '
                <div class="dropdown">
                    <span class="badge bg-warning text-dark dropdown-toggle cursor-pointer"
                          data-bs-toggle="dropdown">
                        Pending
                    </span>

                    <ul class="dropdown-menu">
                        <li>
                            <a class="dropdown-item text-success"
                               href="javascript:void(0)"
                               onclick="confirm_checkup('.$row->id.')">
                                <i class="fa fa-circle-check me-1"></i> Confirm
                            </a>
                        </li>
                        <li>
                            <a class="dropdown-item text-danger"
                               href="javascript:void(0)"
                               onclick="reject_btn('.$row->id.')">
                                <i class="fa fa-circle-xmark me-1"></i> Reject
                            </a>
                        </li>
                    </ul>
                </div>';

            } elseif ($row->status == 1) {

                $status = '<span class="badge bg-success">Confirmed</span>';

            } else {

                $status = '<span class="badge bg-danger">Rejected</span>';
            }

         $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[] = [
    '<a href="javascript:void(0)"
        class="text-primary fw-bold"
        onclick="view_checkup('.$row->id.')">
        '.$no++.'
    </a>',
    $row->patient_name,
    $row->patient_mobile,
    $row->patient_email,
    date('d-m-Y', strtotime($row->appontment_date)),
    $row->appontment_time,
    $row->package_name,
    $status,
    $action
];


        }

        echo json_encode([
            "draw" => $_POST['draw'],
            "recordsTotal" => $this->category->count_all(),
            "recordsFiltered" => $this->category->count_filtered(),
            "data" => $data
        ]);
    }
public function view($id)
{
    $row = $this->category->get_by_id($id);

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