<?php


/**
 *
 */
class Order 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'));
		}

		$query = "SELECT kategori.id, kategori.harga, kategori.nama,orders.username,orders.status,orders.file, orders.jumlah, orders.kategori_id, orders.jumlah * kategori.harga as price, orders.created_at FROM kategori, orders WHERE kategori.id = orders.kategori_id";
    $data['orders'] = $this->db->query($query)->result();

    $this->load->view('pages/admin/header');
		$this->load->view('pages/admin/daftar_order',$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_imunisasi');
		$this->load->view('pages/admin/footer');
	}

  public function proses_tambah(){

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

    // $kode_gejala = $this->input->post('kode_gejala');
    // $gejala = $this->input->post('gejala');

		// $data = array(
    //   'kode_gejala' => $kode_gejala,
		// 	'gejala' => $gejala
    // );

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

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

		// $cek_kode = $this->Admin_model->cek_kode_gejala($kode_gejala)->num_rows();
		// if ($cek_kode == 0) {
		//   $this->Admin_model->insert_gejala($data);
    //   redirect(base_url('admin/imunisasi'));
		// }
		// else {
		// 	$data['cek_kode'] = "ada";
		// 	$this->load->view('pages/admin/header');
		// 	$this->load->view('pages/admin/tambah_imunisasi',$data);
		// 	$this->load->view('pages/admin/footer');
		// }
  }

  public function update($id){

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

    // $where = array('id' => $id, );
    $this->db->set('status',$_POST['status']);
    $this->db->where('id',$id);
    $this->db->update('orders');
		
		redirect(base_url('admin/order'));
	}

  public function proses_edit(){

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

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

    // $kode_gejala = $this->input->post('kode_gejala');
    // $gejala = $this->input->post('gejala');

		// $data = array(
    //   'kode_gejala' => $kode_gejala,
		// 	'gejala' => $gejala
    // );

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

    unset($data['id']);
    
		$this->db->where('id',$id);
		$this->db->update('imunisasi',$data);
		redirect(base_url('admin/imunisasi'));

		// $cek_kode = $this->Admin_model->cek_kode_gejala($kode_gejala)->num_rows();
		// if ($cek_kode == 0) {
		//   $this->Admin_model->update_gejala($data,$id_gejala);
    //   redirect(base_url('admin/imunisasi'));
		// }
		// else {
		// 	if ($kode_gejala == $now_kode_gejala) {
    //     $this->Admin_model->update_gejala($data,$id_gejala);
    //     redirect(base_url('admin/imunisasi'));
    //   }else {
    //     $data['cek_kode'] = "ada";
    //     $where = array('id_gejala' => $id_gejala );

    // 		$data['gejala'] = $this->Admin_model->get_gejala($where)->result();
  	// 		$this->load->view('pages/admin/header');
  	// 		$this->load->view('pages/admin/edit_imunisasi',$data);
  	// 		$this->load->view('pages/admin/footer');
    //   }
		// }
  }

  public function cetak($id){

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

		$query = "SELECT A.*,B.nama,C.nama as keterangan,C.harga, A.jumlah * C.harga as price FROM orders A INNER JOIN tb_admin B ON A.username=B.username INNER JOIN kategori C ON A.kategori_id=C.id WHERE A.id=1 ORDER BY A.created_at DESC";

    $data['invoice'] = $this->db->query($query)->row();
    
    $this->load->view('pages/admin/cetakinvoice.php',$data);
	}

	public function wa($id) {
		$curl = curl_init();
		$token = "0sS0vrtsB38tNlIWyFfoVwTjCvYSPJr2KP1xzvV4PMiFWbl4GvwVqHLaMmH3j6YU";

		$query = "SELECT A.*,B.nama,B.wa,C.nama as keterangan FROM orders A INNER JOIN tb_admin B ON A.username=B.username INNER JOIN kategori C ON A.kategori_id=C.id WHERE A.id=$id ORDER BY A.created_at DESC";

		$order = $this->db->query($query)->row();

		if ($order) {
			$tgl = date('d-m-Y', strtotime($order->created_at));
			$harga = number_format($order->harga);
			$total_bayar = number_format($order->harga * $order->jumlah);

			if (in_array($order->status, ['Pending', 'Proses', 'Pembayaran Valid'])) {
				$message = "Nama : $order->nama\nKeterangan Request : $order->keterangan\nJumlah : $order->jumlah\nHarga : $harga\nTanggal Pemesanan : $tgl\nTotal Bayar : $total_bayar\nStatus : $order->status\n\nStatus akan selalu kami informasikan melalui WA.\n\n*CATATAN : Jika Status Selesai Maka Pemesanan foto dapat di ambil di PANORAMAFOTO*\n\nPembayaran dapat transfer ke Bank Riau Kepri 1453100071\n\nSalam Terima Kasih\n~PANORAMAFOTO";
			} else if ($order->status == 'Selesai') {
				$message = "Pemesana Photo\nNama : $order->nama\nKeterangan Request : $order->keterangan\nJumlah : $order->jumlah\n\nTelah Selesai, Anda dapat segera mengambil photonya pada PANORAMAFOTO.\n\nSalam Terima Kasih\n~PANORAMAFOTO";
			}

			// Kirim pesan melalui curl atau metode lain jika diperlukan
			// curl_setopt() dan curl_exec() dapat ditambahkan di sini jika diperlukan untuk mengirim $message melalui API
		}

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

		$data = [
			'phone' => "$order->wa",
			'message' => $message,
		];

		curl_setopt($curl, CURLOPT_HTTPHEADER,
		    array(
		        "Authorization: $token",
		    )
		);

		curl_setopt($curl, CURLOPT_CUSTOMREQUEST, "POST");
		curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
		curl_setopt($curl, CURLOPT_POSTFIELDS, http_build_query($data));
		curl_setopt($curl, CURLOPT_URL,  "https://pati.wablas.com/api/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/order'));
	}


	public function cetakall(){

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

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

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

  public function hapus(){

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

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

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

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

}
