<?php
defined('BASEPATH') OR exit('No direct script access allowed');
class Book_An_Appointment extends CI_Controller {

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

    public function index()
    {
        $data["extra_js"] = array("book_an_appointment");
        $data['controller'] = base_url('book_an_appointment');
        $data['page_title'] = "Manage Book Appointments";
        $data['page_heading'] = "Manage Book Appointments";
        $data['main_content'] = 'book_an_appointment';

        $this->load->view('main_content', $data);
    }
    public function confirm_appointment($id)
{
    
    
    $data = array();
        $sql = "update ". TBL_BOOK_AN_APPINTMENT ." set status='1' where id ='".$id."'";       
        $res = $this->db->query($sql);
        $data[] = "sucess";
        die(json_encode($data));
        
        
}

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

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

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

public function get_details($id)
{
    $row = $this->category->get_by_id($id);

    if ($row) {
        echo json_encode([
            'status' => true,
            'data' => $row
        ]);
    } else {
        echo json_encode([
            'status' => false
        ]);
    }
}

 public function ajax_delete($id)
    {
        $get_id = $id; 
        // $sql_query = $this->db->query("select id, image, publish from ". TBL_BOOK_AN_APPINTMENT ." where id='$get_id' and publish='1'")->row(); 
        // if (isset($sql_query))
        // {   
        //     $image =  $sql_query->image;
        //     if(!empty($sql_query->image))
        //     {
        //         $image_file_path = SLIDER_PATH . "/" . $image; 
        //         unlink($image_file_path);
        //     }
        // }
        $response = array("status" => "ok", "heading" => "Deleted", "message" => "Record Delete Successfully.", "tbc_name" => "");
        $this->category->delete_by_id($get_id);  
        echo json_encode($response);  
        die;
    }

    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_ap('.$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_details('.$row->id.')">
    '.$no++.'
</a>',

        $row->patient_name,
        $row->patient_mobile,
        $row->patient_email,
        $row->patient_age,
        date('d-m-Y', strtotime($row->appontment_date)),
        $row->appontment_time,
        $row->doctor_name,
        $status,
        $action
    ];
}



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