<?php


namespace App\Controllers;


use CodeIgniter\API\ResponseTrait;
use CodeIgniter\HTTP\RequestInterface;


class Chiefsoft extends BaseController
{
    use ResponseTrait;

    protected $db;
    public $con_name = 'default';

  



    private function apiData() {
        $this->db = \Config\Database::connect($this->con_name);
        $data = array();

        try {
            $mysql = "SELECT  id, post_title, post_content,post_date,comment_count FROM wp_posts WHERE post_type='post' AND post_status = 'publish' ORDER BY post_date DESC LIMIT 9";
            $mysql = "SELECT p1.id AS id,  p1.*, wm2.meta_value FROM  wp_posts p1 LEFT JOIN   wp_postmeta wm1
            ON (wm1.post_id = p1.id AND wm1.meta_value IS NOT NULL AND wm1.meta_key = '_thumbnail_id' )    
            LEFT JOIN 
            wp_postmeta wm2
            ON (wm1.meta_value = wm2.post_id AND wm2.meta_key = '_wp_attached_file' AND wm2.meta_value IS NOT NULL   )
            WHERE
            p1.post_status='publish'
            AND p1.post_type='post'
            ORDER BY p1.post_date DESC LIMIT 9";


            $query = $this->db->query($mysql);

            $row = $query->result_array();
           
            $data["blog_array"] = $row;
        } catch (Exception $ex) {
            
        }

        // featured
        try {
            $mysql = "SELECT  id, post_title, post_content,post_date,comment_count 
			          FROM wp_posts WHERE post_type='post' AND post_status = 'publish' AND id = 263";
            $query = $this->db->query($mysql);

            $rowF = $query->result_array();
            // print_r( $rowF );
            // $r = $this->db->query($mysql);
            $data["blog_featured"] = $rowF[0];
        } catch (Exception $ex) {
            
        }

       // $this->renderSitePage('blog', $data);
       // return $this->response->setJson($data);
    }

    public function blogdata(){
      //  return $this->response->setJson([]);
        $this->db = \Config\Database::connect($this->con_name);
        $data = array();

        try {
            $mysql = "SELECT  id, post_title, post_content,post_date,comment_count FROM wp_posts WHERE post_type='post' AND post_status = 'publish' ORDER BY post_date DESC LIMIT 9";
            $mysql = "SELECT p1.id AS id,  p1.*, wm2.meta_value FROM  wp_posts p1 LEFT JOIN   wp_postmeta wm1
            ON (wm1.post_id = p1.id AND wm1.meta_value IS NOT NULL AND wm1.meta_key = '_thumbnail_id' )    
            LEFT JOIN 
            wp_postmeta wm2
            ON (wm1.meta_value = wm2.post_id AND wm2.meta_key = '_wp_attached_file' AND wm2.meta_value IS NOT NULL   )
            WHERE
            p1.post_status='publish'
            AND p1.post_type='post'
            ORDER BY p1.post_date DESC LIMIT 9";


            $query = $this->db->query($mysql);

            $row = $query->result_array();
           
            $data["blog_array"] = $row;
        } catch (Exception $ex) {
            
        }
        return $this->response->setJson([]);
        // featured
        try {
            $mysql = "SELECT  id, post_title, post_content,post_date,comment_count 
			          FROM wp_posts WHERE post_type='post' AND post_status = 'publish' AND id = 263";
            $query = $this->db->query($mysql);

            $rowF = $query->result_array();
            // print_r( $rowF );
            // $r = $this->db->query($mysql);
            $data["blog_featured"] = $rowF[0];
        } catch (Exception $ex) {
            
        }

        return $this->response->setJson($data);
    }

    public function contact(){
        
    }


    public function index()
    {
        $rawData = $this->apiData();
        $res1= $rawData[0]['payload'];
        return $this->response->setJson($res1);
    }

}