<?php


/**
 *
 */
class Jadwal extends CI_Controller
{

  function __construct()
  {
    parent::__construct();
    $this->load->helper('form');
    $this->load->library('form_validation');
    $this->load->library('upload');
    $this->load->helper('url_helper');
    $this->load->helper('text');
    $this->load->helper('date');
    $this->load->library('pagination');
    $this->load->model('Admin_model');
  }

  public function index(){

		if($this->session->userdata('status') != "login"){
			redirect(base_url('admin/login'));
		}

    $data['jadwal'] = $this->db->get('jadwal')->result();

    $this->load->view('pages/admin/header');
		$this->load->view('pages/admin/daftar_jadwal',$data);
		$this->load->view('pages/admin/footer');
	}

  public function tambah(){

		if($this->session->userdata('status') != "login"){
			redirect(base_url('admin/login'));
		}

    $this->load->view('pages/admin/header');
		$this->load->view('pages/admin/tambah_jadwal');
		$this->load->view('pages/admin/footer');
	}

  public function proses_tambah(){

    if($this->session->userdata('status') != "login"){
			redirect(base_url('admin/login'));
		}

		$data = $this->input->post();

		$this->db->insert('jadwal',$data);
    redirect(base_url('admin/jadwal'));

  }

  public function edit(){

    if($this->session->userdata('status') != "login"){
			redirect(base_url('admin/login'));
		}

		$id = $this->uri->segment(4);

    $where = array('id' => $id, );

		$data['jadwal'] = $this->db->get_where('jadwal',$where)->result();

		$this->load->view('pages/admin/header');
		$this->load->view('pages/admin/edit_jadwal',$data);
		$this->load->view('pages/admin/footer');
	}

  public function proses_edit(){

    if($this->session->userdata('status') != "login"){
			redirect(base_url('admin/login'));
		}

    $id = $this->input->post('id');

		$data = $this->input->post();

		unset($data['id']);

		$this->db->where('id',$id);
		$this->db->update('jadwal',$data);

    redirect(base_url('admin/jadwal'));
  }

  public function hapus(){

    if($this->session->userdata('status') != "login"){
			redirect(base_url('admin/login'));
		}

		$id_rule = $this->uri->segment(4);

		$this->db->where('id',$id);
    $this->db->delete('jadwal');

		redirect(base_url('admin/jadwal'));
	}

	public function wa($id) {
		$curl = curl_init();
		$token = "bT0SCfwHjMVHfccVVF2nCfVK783DX1rlEyRjJYvW3Rfj2YD35oyNn8j5Fts3a8bV";
		$random = true;

		$jadwal = $this->db->get_where('jadwal',['id'=>$id])->row();

		$tgl = date('d-m-Y',strtotime($jadwal->tgl));
		$harga = number_format($jadwal->harga);

		$message = "Nama Pendaftaran : $jadwal->nama_pendaftar\nNomor Pendaftaran :$jadwal->nomor_pendaftar\nJam : $jadwal->jam\n\nJadwal Imunisasi $jadwal->jenis Pada Tanggal $tgl Dengan Harga $harga";

		$user = $this->db->get_where('tb_admin',['role'=>'user'])->result();
		$arr = [];
		foreach ($user as $key => $item) {
			$arr[] = [
				'phone' => $item->wa,
				'message' => $message
			];
		}

		$payload = [
		    "data" => $arr
		];

		curl_setopt($curl, CURLOPT_HTTPHEADER,
		    array(
		        "Authorization: $token",
		        "Content-Type: application/json"
		    )
		);

		curl_setopt($curl, CURLOPT_CUSTOMREQUEST, "POST");
		curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
		curl_setopt($curl, CURLOPT_POSTFIELDS, json_encode($payload) );
		curl_setopt($curl, CURLOPT_URL,  "https://pati.wablas.com/api/v2/send-message");
		curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, 0);
		curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, 0);
		$result = curl_exec($curl);
		curl_close($curl);
		echo "<pre>";
		print_r($result);

		redirect(base_url('admin/jadwal'));
	}

}
