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


class Banner extends CI_Controller
{

    public function __construct()
    {
        parent::__construct();
    }

    public $table = 'banners';

    public function view_list()
    {
        $datas = $this->banner->get_data_table(['remove' => '0']);
        // print_r($this->db->last_query());
        // exit;

        $final = [];
        $sno = $_POST['start'];
        foreach ($datas as $data) {
            $row = [];
            $row[] = ++$sno;
            $row[] = "<img src='".$data['banner']."' alt='banner image'>";
            if($data['active'] == '1'){

                $row[] = "<span style='cursor:pointer' onclick='changeStatus(". $data['id'] .")' class='badge badge-success'>Active</span>";
                
            }else{
                
                $row[] = "<span style='cursor:pointer' onclick='changeStatus(". $data['id'] .")' class='badge badge-danger'>De-Active</span>";
   
            }
            $row[] = $data['title'];
            $row[] = $data['paragraph'];
            $row[] = "<div class='btn-group' role='group' aria-label='Basic example'>
            <a type='button' href='".site_url()."/admin/banner/edit?id=".$data['id']."' class='btn btn-primary'><i class='fa-solid fa-pencil'></i></a>
            <a  href='".site_url()."/admin/banner/delete?id=".$data['id']."' type='button' class='btn btn-primary'><i class='fa-solid fa-trash'></i></a>
        </div>";

            $final[] = $row;
        }

        $output = array(
            "draw" => $_POST['draw'],
            "recordsTotal" => $this->banner->count_all_data(['remove' => '0']),
            "recordsFiltered" => $this->banner->count_filtered_data(['remove' => '0']),
            "data" => $final,
        );

        $this->output->set_content_type('application/json')->set_output(json_encode($output));

        // print_r($data);exit;

        // echo json_encode($final);
    }

    public function change_banner_status(){

        request_method('GET');

        $request = check_get_parameter([
            'id' => true
        ]);

        // get banner details 

        $details = $this->cm->search_single_where(['id' => $request['id']]);
        if(empty($details)){

            echo json_encode([
                'status' => '0',
                'message' => 'Invalid banner Id' 
            ]);exit;
            
        }else{

            if($details['active'] == '1'){
                $status = '0';
            }else{
                $status = '1';
            }

            $this->cm->update_where(['active' => $status], ['id' => $details['id']]);
            echo json_encode([
                'status' => '1',
                'message' => 'status updated successfuly.'
            ]);exit;
            
        }
        
    }
    

}
