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

class Colors extends MY_Controller {
	public function __construct(){
		parent::__construct();
		$this->load_global();
		$this->load->model('colors_model','colors');
	}

	public function add(){
		$this->permission_check('color_add');
		$data=$this->data;
		$data['page_title']=$this->lang->line('colors');
		$this->load->view('color', $data);
	}

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

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

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

      } 
      else {
        echo "Please Fill Compulsory(* marked) Fields.";
      }
    }
    //END
	public function s_all_color(){
		$query = $this->db->get('db_color')->result_array();
        $data['colorInfo']=$query;        
        echo json_encode($data);
	}

	public function new_color(){

		$this->form_validation->set_rules('color', 'Color', 'trim|required');

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

	public function itm_new_color(){
		$color=$this->input->post('color');
		$result=$this->colors->color_save($color);
		if ($result=="exist") {
            $output="exist";
        }elseif ($result=="success") {
            $output="success";
        }elseif ($result=="failed") {
            $output="failed";
        }
		echo json_encode($output);
	}

	public function update($id){
		$this->permission_check('colors_edit');
		$data=$this->data;
		$result=$this->colors->get_details($id,$data);
		$data=array_merge($data,$result);
		$data['page_title']=$this->lang->line('colors');
		$this->load->view('color', $data);
	}
	public function update_Color(){
		$this->form_validation->set_rules('color', 'Color', 'trim|required');
		$this->form_validation->set_rules('q_id', '', 'trim|required');

		if ($this->form_validation->run() == TRUE) {
			$result=$this->colors->update_Color();
			echo $result;
		} else {
			echo "Please Enter Color.";
		}
	}
	public function index(){
		$this->permission_check('colors_view');
		$data=$this->data;
		$data['page_title']=$this->lang->line('colors_list');
		$this->load->view('colors-list', $data);
	}

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

			 		if($i_color->status==1){ 
			 			$str= "<span onclick='update_status(".$i_color->id.",0)' id='span_".$i_color->id."'  class='label label-success' style='cursor:pointer'>Active </span>";}
					else{ 
						$str = "<span onclick='update_status(".$i_color->id.",1)' id='span_".$i_color->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('colors_edit'))
											$str2.='<li>
												<a title="Editd Record ?" href="'.base_url('colors/update/'.$i_color->id).'">
													<i class="fa fa-fw fa-edit text-blue"></i>Edit
												</a>
											</li>';

											if($this->permissions('colors_delete'))
											$str2.='<li>
												<a style="cursor:pointer" title="Delete Record ?" onclick="delete_color('.$i_color->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->colors->count_all(),
						"recordsFiltered" => $this->colors->count_filtered(),
						"data" => $data,
				);
                //print_r($output);
		//output to json format
		echo json_encode($output);
	}

	public function update_status(){
		$this->permission_check_with_msg('colors_edit');
		$id=$this->input->post('id');
		$status=$this->input->post('status');
		$result=$this->colors->update_status($id,$status);
		return $result;
	}
	
	public function delete_color(){
		$this->permission_check_with_msg('colors_delete');
		$id=$this->input->post('q_id');
		$result=$this->colors->delete_color($id);
		return $result;
	}
}

