<?php
defined('BASEPATH') OR exit('No direct script access allowed');
class {name} extends Admin_Controller {
	public function __construct(){
		parent::__construct();
		$this->load->model('Admin/admin_m');
		$this->load->model('Admin/channel_m');
	}
	public function index(){
		$data["subview"] 	= ADMIN_THEME_FILE."template/{name}/data";
		$this->load->view(ADMIN_THEME_FILE."_main_layout",$data);
	}
	public function List(){
		xhrCheck();
		$where  = array();
		$search = $this->input->post('search[value]',true);
		$start 	= $this->input->post('start',true);
		$length = $this->input->post('length',true);

		$draw 	= $this->input->post('draw',true);

		if($search){
			$this->db->like('name',$search);
		}
		$total 	= $this->my_model->row_total('{db_name}',$where);
		$data 	= array();
		if($search){
			$this->db->like('name',$search);
		}
		$this->db->limit(10);
		$result = $this->my_model->get('{db_name}',$where,"result",array($length,$start),array('id', 'desc'));
		if(!empty($result)){
			foreach($result as $row){
				$gender = $row->gender == 2 ? 'Erkek' : 'Kadın';
				$type = '<span class="badge badge-dark">'.$row->name.'</span>';
				
				$data[] = array(
					"id"  			=> $row->id,
					"name" 			=> $row->name,
					"image" 		=> '<img width="50" height="65" src="'.base_url("uploads/cast/original/$row->image").'" />',
					"alias" 		=> $row->alias,
					"type"			=> $type,
					"gender"		=> '<button type="button" class="btn btn-dark">'.$gender.'</button>',
          "action" 		=> '<div class="btn-group">
			                    	<button type="button" class="btn btn-primary dropdown-toggle" data-toggle="dropdown" aria-expanded="false"><i class="icon-cog5 mr-2"></i> İşlemler</button>
			                    	<div class="dropdown-menu dropdown-menu-right" x-placement="bottom-end" >
															<a href="javascript:;" data-type="cast/delete" data-delete="'.$row->id.'" class="dropdown-item"><i class="icon-trash"></i> Sil</a> 
															<div class="dropdown-divider"></div>
															<a href="'.base_url("panel/cast/edit/$row->id").'" class="dropdown-item"><i class="icon-pencil"></i> Düzenle</a>
														</div>
													</div>'
				);
			}
		}
		$json 	= array(
      "draw" 						=>  $draw,
      "recordsTotal" 		=>  $total,
      "recordsFiltered" =>  $total,
    );
    $json["data"] = $data;
    echo json_encode($json);
	}
	public function Add(){
		$message = "";
		$rules = "required";
		$this->form_validation->set_rules("name","{name} Adı",$rules);
		$this->form_validation->set_rules("alias","{name} Link",$rules);
		if($this->form_validation->run()){
			$name 	= $this->input->post('name',true);
			$alias 	= $this->input->post('alias',true);
			$gender	= $this->input->post('gender',true);
			$files 	= !empty($_FILES["file"]) ? $_FILES["file"] : "";
			if($files){
				$imageName = sef_link($name)."-".rand(0,9999);
				$imageName = $this->admin_m->createImg($imageName,$files,"{name}");
			}
			$data 		= array(
				"name"	=> $name,
				"alias"	=> $alias,
				"gender"=> $gender,
				"image"	=> $imageName ?? null,
				"guid"	=> $name,
			);
			$add 			= $this->my_model->insert('cast',$data);
			if($add){
				$message = hata("success","{name} başarıyla eklendi.");
			}else {
				$message = hata("error","{name} eklenirken beklenmedik bir sorun oluştu.");
			}
		}
		$data["message"] 	= $message;
		$data["error"] 	 	= error();
		$data["subview"] 	= ADMIN_THEME_FILE."template/{name}/add";
		$this->load->view(ADMIN_THEME_FILE."_main_layout",$data);
	}
	public function Edit(){
		$id 		 = $this->uri->segment(4);
		$get 		 = $this->my_model->get('cast',array('id' => $id),"row");
		if(!$id || !$get){
			show_404();
		}
		$message = "";
		$rules = "required";
		$this->form_validation->set_rules("name","{name} Adı",$rules);
		$this->form_validation->set_rules("alias","{name} Link",$rules);
		if($this->form_validation->run()){
			$name 	= $this->input->post('name',true);
			$alias 	= $this->input->post('alias',true);
			$gender	= $this->input->post('gender',true);
			$files 			= !empty($_FILES["file"]) ? $_FILES["file"] : "";
			if(!empty($files["tmp_name"])){
				$names 		= sef_link($name)."-".rand(0,9999);
				$imageName = $this->admin_m->createImg($names,$files,"{name}");
				if(!empty($imageName)){
					$this->my_model->delete_image("{name}",$get->image);
				}
			}else {
				$imageName = $get->image;
			}
			$data 		= array(
				"name"	=> $name,
				"alias"	=> $alias,
				"gender"=> $gender,
				"image"	=> $imageName,
				"guid"	=> $name,
			);
			$update 			= $this->my_model->update('{db_name}',$data,array('id' => $id));
			if($update){
				$message = hata("success","Kanal başarıyla düzenlendi.");
			}else {
				$message = hata("error","Kanal eklenirken beklenmedik bir sorun oluştu.");
			}
		}
		$data["get"] 			= $get;
		$data["message"] 	= $message;
		$data["error"] 	 	= error();
		$data["types"]		= $this->my_model->get('category_type',null,"result");
		$data["subview"] 	= ADMIN_THEME_FILE."template/{name}/edit";
		$this->load->view(ADMIN_THEME_FILE."_main_layout",$data);
	}
	public function Delete(){
		$id = $this->input->post('id',TRUE);
		if($id){
			$result = $this->my_model->get('{db_name}',array('id' => $id),"row");
			if($result){
				$delete = $this->my_model->delete('{db_name}',array('id' => $id));
				$this->my_model->delete_image('{db_name}',$result->image);
				if($delete){
					$json["success"] = "Kanal başarıyla silindi.";
				}else {
					$json["error"] = "Silme işlemi yapılırken beklenmedik bir sorun oluştu.";
				}
			}else {
				$json["error"] = "Aradığınız oyuncu bulunamadı.";
			}
		}else {
			$json["error"] = "ID Bilgisi alınamadı";
		}
		echo json_encode($json);
	}
	public function Update(){
		xhrCheck();
		$this->load->library('tmdb');
		$type = $this->input->post('type',true);
		$lang = $this->input->post('lang',true);
		if($type && $lang){
			$category = json_decode($this->tmdb->getGenres($lang,$type));
			$type_id 	= $type == 'movie' ? 2 : 1;
		
    
			$json["success"] = 1;
			$json["message"] = "{name} bilgileri çekildi";
		}else {
			$json["error"] = 1;
			$json["message"] = "Beklenmedik bir sorun oluştu.";
		}
		echo json_encode($json);
	}
	public function Active(){
		xhrCheck();
		$id 			= $this->input->post('id',TRUE);
		$checked 	= $this->input->post('checked',TRUE);
		if($id){
			$pin = $checked ? 'aktif' : 'pasif';
			$update = $this->my_model->update('{db_name}',array("active" => $checked),array('id' => $id));
			if($update){
				$json["success"] = true;
				$json["message"] = "{name} $pin olarak düzenlendi";
			}else {
				$json["error"] = true;
				$json["message"] = "Beklenmedik bir sorun oluştu.";
			}
		}else {
			$json["error"] = true;
			$json["message"] = "Kategori bulunamadı.";
		}
		echo json_encode($json);
	}
}
