<?php

class Sendmessage extends CI_Controller {

	var $module = 'seo';
	public function __construct() {
		// $this->load does not exist until after you call this
		parent::__construct(); // Construct CI's core so that you can use it

		$this->load->database();
		$this->load->helper('language');
		$this->load->library('session');
		$this->load->model('Security', 'security_model');
		$this->load->model('Settings_Model', 'settings_model');
		$this->load->model('Language', 'language_model');
		//require_once(APPPATH.'controllers/admin/Security.php'); //include controller
		//$security = new Security();  //create object
		//$security->permission(); //call function

		//check user have login or not
		$this->security_model->admin($this->session->userdata('session_id'), $this->session->userdata('user_id'), $this->session->userdata('username'));
		//end check user have login or not
	}
	function index() {

		$session_id = $this->security_model->getcode();
		//$sess = $this->security_model->admin($session_id);
		//$property = $this->Property->get();

		//check user have login or not
		$this->security_model->admin($this->session->userdata('session_id'), $this->session->userdata('user_id'), $this->session->userdata('username'));
		//end check user have login or not

		$data['selectedmenu'] = "settings";
		$data['save'] = $this->input->get('save');

		//$data['sess'] = $sess;
		//$data['property'] = $property;
		// $data['module'] = $this->module;
		// $data['settings'] = $this->settings_Model->get();
		// $data['selectedmenu']="seo";
		// $this->load->view('admin/seo.php', $data);

		$this->load->library("form_validation");
		$config['upload_path'] = $_SERVER['DOCUMENT_ROOT'] . "/assets/images/uploads/";
		$config['allowed_types'] = 'gif|jpg|png|jpeg|ico';
		$this->load->library('upload', $config);
		$data['item'] = new stdClass();
		foreach ($this->settings_model->get() as $setting) {
			// echo "get setting";
			// var_dump($setting);

			$data['item']->{$setting->key} = $setting->value;
			$this->form_validation->set_rules('setting[' . $setting->key . ']', 'lang:settings_' . $setting->key, "trim");
		}
		// var_dump($data['item']);

		//echo $data['item']->title;
		// $this->form_validation->set_rules('logo', 'Logo', "callback_logo");
		// $this->form_validation->set_rules('favicon', 'Favicon', "callback_favicon");
		// $this->form_validation->set_rules('contactlogo', 'Favicon', "callback_contactlogo");
		// $this->form_validation->set_rules('wechatlogo', 'Favicon', "callback_wechatlogo");
		// $this->form_validation->set_rules('wechatpubliclogo', 'Favicon', "callback_wechatpubliclogo");

		//get chai api connect logo
		// $params = array('token' => 'xquu6n9m7tnyo2o5', 'url' => 'https://eu183.chat-api.com/instance217279');

		// $this->load->library('chatapi', $params);
		// // echo $this->chatapi->createUrl('getQRCode');

		// // header('Content-Type: image/png');
		// // $result = file_get_contents($url);
		// $result = file_get_contents($this->chatapi->getQRCode());
		// // $data['qrcode'] = $this->chatapi->getQRCode();
		// $data['qrcode'] = $result;
		//end
		$data['result'] = "";
		if ($this->form_validation->run() == false) {
			// $this->load->view("settings/manage", $data);
			$this->load->view('admin/sendmessage_view.php', $data);
		} else {
			// foreach ($this->input->post('setting') as $key => $value) {
			// 	$this->settings_model->key = $key;
			// 	$this->settings_model->value = $value;
			// 	$this->settings_model->save();
			// }
			$phone = $this->input->post('phone');
			$message = $this->input->post('message');
			$params = array('token' => '9fsic23hjstt6uli', 'url' => 'https://eu80.chat-api.com/instance218485');

			$this->load->library('chatapi', $params);

			$data['result'] = $this->chatapi->sendPhoneMessage($phone, $message);
			// die(
			// 	// ($this->chatapi->sendPhoneMessage('+85293692455', 'It works!') == true) ? 'Message sent' : 'Fail'
			// 	($this->chatapi->sendPhoneMessage($phone, $message) == true) ? 'Message sent' : 'Fail'
			// );
			// redirect("admin/settings");
			$this->load->view('admin/sendmessage_view', $data);

			// redirect("admin/sendmessage", $data);
		}

	}
	function logo($var) {

		if ($this->upload->do_upload('logo')) {
			$data = $this->upload->data();
			if ($data['file_name']) {
				$this->settings_model->key = 'logo';
				$this->settings_model->value = base_url() . 'assets/images/uploads/' . $data['file_name'];
				$this->settings_model->save();
			}
		}
		return true;
	}
	function favicon($var) {

		if ($this->upload->do_upload('favicon')) {
			$data = $this->upload->data();
			if ($data['file_name']) {
				$this->settings_model->key = 'favicon';
				$this->settings_model->value = base_url() . 'assets/images/uploads/' . $data['file_name'];
				$this->settings_model->save();
			}
		}
		return true;
	}
	function contactlogo($var) {

		if ($this->upload->do_upload('contactlogo')) {
			$data = $this->upload->data();
			if ($data['file_name']) {
				$this->settings_model->key = 'contactlogo';
				$this->settings_model->value = base_url() . 'assets/images/uploads/' . $data['file_name'];
				$this->settings_model->save();
			}
		}
		return true;
	}
	function wechatlogo($var) {

		if ($this->upload->do_upload('wechatlogo')) {
			$data = $this->upload->data();
			if ($data['file_name']) {
				$this->settings_model->key = 'wechatlogo';
				$this->settings_model->value = base_url() . 'assets/images/uploads/' . $data['file_name'];
				$this->settings_model->save();
			}
		}
		return true;
	}
	function wechatpubliclogo($var) {

		if ($this->upload->do_upload('wechatpubliclogo')) {
			$data = $this->upload->data();
			if ($data['file_name']) {
				$this->settings_model->key = 'wechatpubliclogo';
				$this->settings_model->value = base_url() . 'assets/images/uploads/' . $data['file_name'];
				$this->settings_model->save();
			}
		}
		return true;
	}

	function edit() {
		//not in use 2020-11-19
		$session_id = $this->security_model->getcode();
		//$this->security_model->admin($this->session->userdata('session_id'),$this->session->userdata('user_id'),$this->session->userdata('username'));
		//$property = $this->Property->get();

		echo $this->input->post('save');

		if ($this->input->post('save')) {
			echo "post save";
			$id = $this->input->post('id');

			$arr['title'] = $this->input->post('title');
			$arr['description'] = $this->input->post('description');
			$arr['keywords'] = $this->input->post('keywords');
			$arr['lang'] = $this->input->post('lang');

			if ($id == false) {
				$arr['entered'] = date('Y-m-d H:i:s');
				$id = $this->seo_model->insert($arr);
				header('location:' . base_url() . 'admin/seo/edit?save=1&id=' . $id);
				exit;
			} else {
				$this->seo_model->update($id, $arr);
				header('location:' . base_url() . 'admin/seo/edit?save=1&id=' . $id);
				exit;
			}
		}

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

		$data['languages'] = $this->language_model->get_all();
		//$data['sess'] = $sess;
		$data['sess'] = $session_id;
		//$data['property'] = $property;
		//$data['module'] = $this->module;
		$data['seo'] = $this->seo_model->get_id($id);
		$data['save'] = $this->input->get('save');
		$data['selectedmenu'] = "seo";
		$this->load->view('admin/seo_edit_view.php', $data);
		//end not in use 2020-11-19
	}

}

?>