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

class Platform extends MY_Controller {
	public function __construct(){
		parent::__construct();
		$this->load_global();
		$this->load->model('platform_model','platform');
	}

	public function add(){
		$this->permission_check('platform_add');
		$data=$this->data;
		$data['page_title']='Platform';
		$this->load->view('platform', $data);
	}

	//ITS FROM POP UP MODAL
    public function add_size_modal(){

        // $reponse = array(
        //     'csrfName' => $this->security->get_csrf_token_name(),
        //     'csrfHash' => $this->security->get_csrf_hash()
        // );

      $this->form_validation->set_rules('size', 'Size', 'trim|required');
      if ($this->form_validation->run() == TRUE) {
      	
        $result=$this->sizes->verify_and_save();
        //fetch latest item details
        $res=array();
        $query=$this->db->query("select id,size from db_size order by id desc limit 1");
        $res['id']=$query->row()->id;
        $res['size']=$query->row()->size;
        $res['result']=$result;
        
        echo json_encode($res);

      } 
      else {
        echo "Please Fill Compulsory(* marked) Fields.";
      }
    }
    //END

	public function new_platform(){

		$this->form_validation->set_rules('platform', 'Size', 'trim|required');

		if ($this->form_validation->run() == TRUE) {
			
			$result=$this->sizes->verify_and_save();
			echo $result;
		} else {
			echo "Please Enter Platform.";
		}
	}

	public function sw_new_platform(){
		$platform=$this->input->post('platform');
		$result=$this->platform->platform_save($platform);
		if ($result=="exist") {
            $output="exist";
        }elseif ($result=="success") {
            $output="success";
        }elseif ($result=="failed") {
            $output="failed";
        }
		echo json_encode($output);
	}

	public function s_all_platform(){
		$query = $this->db->get('db_platform')->result_array();
        $data['platformInfo']=$query;        
        echo json_encode($data);
	}

	public function update($id){
		$this->permission_check('size_edit');
		$data=$this->data;
		$result=$this->sizes->get_details($id,$data);
		$data=array_merge($data,$result);
		$data['page_title']=$this->lang->line('sizes');
		$this->load->view('size', $data);
	}
	public function update_Size(){
		$this->form_validation->set_rules('size', 'Size', 'trim|required');
		$this->form_validation->set_rules('q_id', '', 'trim|required');

		if ($this->form_validation->run() == TRUE) {
			$result=$this->sizes->update_Size();
			echo $result;
		} else {
			echo "Please Enter Size.";
		}
	}
	public function index(){
		$this->permission_check('size_view');
		$data=$this->data;
		$data['page_title']=$this->lang->line('sizes_list');
		$this->load->view('size-list', $data);
	}

	public function ajax_list()
	{
		$list = $this->sizes->get_datatables();		
		$data = array();
		$no = $_POST['start'];
		foreach ($list as $i_size) {
			$no++;
			$row = array();
			$row[] = $i_size->size;

			 		if($i_size->status==1){ 
			 			$str= "<span onclick='update_status(".$i_size->id.",0)' id='span_".$i_size->id."'  class='label label-success' style='cursor:pointer'>Active </span>";}
					else{ 
						$str = "<span onclick='update_status(".$i_size->id.",1)' id='span_".$i_size->id."'  class='label label-danger' style='cursor:pointer'> Inactive </span>";
					}
			$row[] = $str;			
			         $str2 = '<div class="btn-group" title="View Account">
										<a class="btn btn-primary btn-o dropdown-toggle" data-toggle="dropdown" href="#">
											Action <span class="caret"></span>
										</a>
										<ul role="menu" class="dropdown-menu dropdown-light pull-right">';

											if($this->permissions('size_edit'))
											$str2.='<li>
												<a title="Editd Record ?" href="'.base_url('size/update/'.$i_size->id).'">
													<i class="fa fa-fw fa-edit text-blue"></i>Edit
												</a>
											</li>';

											if($this->permissions('size_delete'))
											$str2.='<li>
												<a style="cursor:pointer" title="Delete Record ?" onclick="delete_size('.$i_size->id.')">
													<i class="fa fa-fw fa-trash text-red"></i>Delete
												</a>
											</li>
											
										</ul>
									</div>';			
			$row[] = $str2;

			$data[] = $row;
		}

		$output = array(
						"draw" => $_POST['draw'],
						"recordsTotal" => $this->sizes->count_all(),
						"recordsFiltered" => $this->sizes->count_filtered(),
						"data" => $data,
				);
                //print_r($output);
		//output to json format
		echo json_encode($output);
	}

	public function update_status(){
		$this->permission_check_with_msg('size_edit');
		$id=$this->input->post('id');
		$status=$this->input->post('status');
		$result=$this->sizes->update_status($id,$status);
		return $result;
	}
	public function delete_size(){
		$this->permission_check_with_msg('size_delete');
		$id=$this->input->post('q_id');
		$result=$this->sizes->delete_size($id);
		return $result;
	}
}

