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

class Link extends MY_Controller {

	public $data = array();

	public function __construct() {

		parent::__construct();

		if(!$this->session->userdata('IsLogin')) {
			redirect($this->globalUrl.'main');	
		}

		$this->load->library('form_validation');
		$this->load->model('link_model');

	}

	public function index()
	{

		$this->load->template('create_link', $this->data);
		
	}

	public function create() {

		// Set that this is NOT edit mode
		$this->data['edit_mode'] = false;

		if($this->input->post('addlink') == 'addlink') {

			// Do validation on client side 
			$this->form_validation->set_error_delimiters('<font color="red">', '</font>');
			$validation = $this->form_validation->run('create_link');

			
			if($validation) {
			
				$linkResult = $this->link_model->create_link(
					$this->input->post('domain'),
					$this->input->post('custom_name'),
					$this->input->post('remark'),
					$this->input->post('phone_number'),
					rawurlencode($this->input->post('prefilledtext')),
					$this->input->post('landing_page')
				);

				if($linkResult['success']) {
					redirect($this->globalUrl.'dashboard');
				} else {
					$this->data['error_insert'] = $linkResult['reason'];
				}
			}

		}

		$this->load->template('create_link', $this->data);

	}

	public function edit() {

		// Set that this is NOT edit mode
		$this->data['edit_mode'] = true;

		// Get Link ID from URL
		$linkId = $this->uri->segment('3');

		// Query for link details
		$linkResult = $this->link_model->get_link_details($linkId);

		// Pump to view
		if($linkResult) {
			$this->data['domain'] = $linkResult->Domain;
			$this->data['custom_name'] = $linkResult->Subdomain;
			$this->data['phone_number'] = $linkResult->PhoneNumber;
			$this->data['prefilledtext'] = rawurldecode($linkResult->Text);
			$this->data['remark'] = $linkResult->Remark;
			$this->data['landing_page'] = $linkResult->LandingPage;

		} else {
			redirect($this->globalUrl."errors/html/error_404.php");
		}
		

		if($this->input->post('addlink') == 'addlink') {

			// Do validation on client side 
			$this->form_validation->set_error_delimiters('<font color="red">', '</font>');
			$validation = $this->form_validation->run('create_link');

			
			if($validation) {
			
				$linkResult = $this->link_model->edit_link(
					$linkId,
					$this->input->post('domain'),
					$this->input->post('custom_name'),
					$this->input->post('remark'),
					$this->input->post('phone_number'),
					rawurlencode($this->input->post('prefilledtext')),
					$this->input->post('landing_page'));

				if($linkResult['success']) {
					redirect($this->globalUrl.'dashboard');
				} else {
					$this->data['error_insert'] = $linkResult['reason'];
				}
			}

		}

		$this->load->template('create_link', $this->data);
	}

	public function delete() {

		// Set that this is NOT edit mode
		$this->data['edit_mode'] = true;

		// Get Link ID from URL
		$linkId = $this->uri->segment('3');

		// Query for link details
		$linkResult = $this->link_model->delete_link($linkId);

		// Redirect to dashboard
		if($linkResult) {
	
			redirect($this->globalUrl.'dashboard');

		} else {
			redirect($this->globalUrl."errors/html/error_404.php");
		}
	}

}
