<?php

namespace Datacake\Core\Admin\Crud;

use Datacake\Core\Admin\Crud\BaseCrud;
use Datacake\Core\ORM\WP\Post;
use Datacake\Core\Models\IspRedirect;
use Datacake\Core\Models\IspCity;
use Datacake\Core\Models\IspRedirectCity;

class RedirectCrud extends BaseCrud {
	public $parent_page = 'isp_redir';
    public $columns = [
		'id'=>'id',
		'from_page_id'=>'De Pagina',
		'to_page_id'=>'Para Pagina',
		
	];
	public $extraColumns = [
		'city'=>'Cidades',
	];
	public $sortableColumns = [
		'id'=>array( 'id', true ),
		'from_page_id'=>array('from_page_id', true),
        'to_page_id'=>array('to_page_id', true),
    ];
	public $info = [
		'singular' => 'Redirecionamento', //singular name of the listed records
		'plural' => 'Redirecionamentos', //plural name of the listed records
		'ajax' => true,
	];

    public function __construct($args = array()) {
		$this->entity = new IspRedirect();
		
		parent::__construct($args);
	}

	public function menu_items($role, $plugin_name){
		add_submenu_page("isp", 
			__('Redirecionamentos', $plugin_name), 
			__('Redirecionamentos', $plugin_name), 
			$role, $this->parent_page, [$this, 'list']
		);
		add_submenu_page("isp", 
			__('Novo Redirecionamento', $plugin_name), 
			__('Novo Redirecionamento', $plugin_name), 
			$role, $this->parent_page.'_edit', [$this, 'edit']
		);
		add_action($this->parent_page.'_delete', [$this, 'remove']);
	}

	public function column_from_page_id($item){
		$postEntity = new Post();
		if($item['from_page_id']){
			$post = $postEntity->select('ID','post_title')->where('ID',$item['from_page_id'])->first();
			if(!$post){
				return "<b>PAGINA REMOVIDA, REMOVER/EDITAR REDIRECT</b>";
			}
			return "#".$post->ID." - ".$post->post_title;
		}else{
			return 'Não escolhido';
		}

		
	}
	public function column_to_page_id($item){
		$postEntity = new Post();
		if($item['to_page_id']){
			$post = $postEntity->select('ID','post_title')->where('ID',$item['to_page_id'])->first();
			if(!$post){
				return "<b>PAGINA REMOVIDA, REMOVER/EDITAR REDIRECT</b>";
			}
			return "#".$post->ID." - ".$post->post_title;
		}else{
			return 'Não escolhido';
		}
	}

	public function column_city($item){
		$entity = new IspRedirect();
		$redir = $entity->where('ID',$item['id'])->first();
		$cities = $redir->cities()->get();
		$city_list = "<ul>";
		foreach ($cities as $key => $city){
			$city_list .= "<li>".$city['name']."</li>";
		}
		$city_list .= "</ul>";
		return $city_list;
	}


	public function edit_action($data) {
		$item = parent::edit_action($data);
		if($data['cities']){
			$item->cities()->detach();
			$item->cities()->attach($data['cities']);
		}
		//print_r($data);
    }
	public function edit_html($entity =null) {
		$postEntity = new Post();
		$posts = $postEntity->select('ID','post_title','post_name')->where('post_type','page')->orderBy('post_title','ASC')->get();

		$cityEntity = new IspCity();
		$cities = $cityEntity->select('id','name','code')->orderBy('name','ASC')->get();
		
		?>
		<h2><?php echo $entity ? "Editando Redicionamento" : "Novo Redirecionamento" ?></h2>
		<form method="post">
			<input type="hidden" name="id" value="<?php echo $entity ? $entity->id : ""?>">
			<table class="form-table" role="presentation">
				<tbody>
					<tr>
						<th scope="row">
							<label>De Pagina</label>
						</th>
						<td>
							<select type="text" name="from_page_id" id="from_page_id" required>
								<option>-- SELECIONE --</option>
								<?php foreach ($posts as $key => $post): ?>
									<?php $selected = ($entity && $entity->from_page_id == $post->ID ? "selected='selected'" : "") ?>
									<option value='<?php echo $post->ID; ?>'  <?php echo $selected;?> ><?php echo $post->post_title." - (".$post->post_name.")" ?></option>
								<?php endforeach; ?>
							</select>
						</td>
					</tr>
					<tr>
						<th scope="row">
							<label>Para Pagina</label>
						</th>
						<td>
							<select type="text" name="to_page_id" id="to_page_id" required>
								<option>-- SELECIONE --</option>
								<?php foreach ($posts as $key => $post): ?>
									<?php $selected = ($entity && $entity->to_page_id == $post->ID ? "selected='selected'" : "") ?>
									<option value='<?php echo $post->ID; ?>'  <?php echo $selected;?> ><?php echo $post->post_title." - (".$post->post_name.")" ?></option>
								<?php endforeach; ?>
							</select>
						</td>
					</tr>
					<tr>
						<th scope="row">
							<label>Cidades</label>
						</th>
						<td>
							<?php foreach ($cities as $key => $city): ?>
								<?php $checked = ""; ?>
								<?php 
									if($entity){
										$cityCheck = $entity->cities()->find($city['id']);
										if($cityCheck){
											$checked ='checked="checked"';
										}
									}
								?>
								
								<p>
									<input type="checkbox" <?php echo $checked ?>  name="cities[<?php echo $city['id'] ?>]" id="cities[<?php echo $city['id'] ?>]" value="<?php echo $city['id'] ?>">
									<label for="cities[<?php echo $city['id'] ?>]"><?php echo $city['name']." (".$city['code'].")" ?></label>
								</p>
							<?php endforeach; ?>
						</td>
					</tr>
				</tbody>
			</table>
			<?php
				echo submit_button("Salvar");
			?>
		</form>
		
		<?php
	}
}