<?php
namespace App\Http\Controllers\v2019;

use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use App\Http\Controllers\Auth\authentication;
use Illuminate\Support\Facades\DB;
use Illuminate\Http\JsonResponse;
use App\Http\Controllers\Auth\User;

use Session;

class customizeNewsletterController extends Controller
{
    public $appType = 'SM' ;
	public function __construct(Request $request) {
        if($request->has('debug') && $request->input('debug') == true) {
            $this->debug = true;			
        }
    }
	
	
	public function getNews(Request $request)
    {
		$result = array();
		if($request->has('postIds') && $request->input('postIds')!=''){
			$postIds = $request->input('postIds');
			if($request->has('show_image') && $request->input('show_image')!=''){
                $show_image = $request->input('show_image');
			}
			if($request->has('show_image') && $request->input('show_image')!=''){
                $show_image = $request->input('show_image');
			}
			$section_type = 'commodity_wise';
			if($request->has('section_type') && $request->input('section_type')!=''){
                $section_type = $request->input('section_type');
			}
			
			 	
        if($section_type=='region_wise'){
                $query1 = DB::table('tbl_news_post as m')
                    ->select('m.post_id as itemId', 'm.post_title as title', 'm.post_content as description', DB::raw("CONCAT(m.post_date, ' ', m.post_time) as datePublished"), 'm.href', 'm.medium_image as imageUrl', 'tbl_region_commodity_master.itemName as category_data')
                    ->leftJoin('tbl_region_commodity_master', function($join){
                        $join->on('tbl_region_commodity_master.id', '=', 'm.regionId')
                            ->where('tbl_region_commodity_master.itemType', '=', 'region');
                    })
                    ->whereIn('post_id', $post_ids)
                    ->union(DB::table('intel as m')
                        ->select('m.itemId', 'm.title', 'm.description', 'm.datePublished', 'm.href', 'm.imageUrl', 'tbl_region_commodity_master.itemName as category_data')
                        ->leftJoin('tbl_region_commodity_master', function($join){
                            $join->on('tbl_region_commodity_master.id', '=', 'm.regionId')
                                ->where('tbl_region_commodity_master.itemType', '=', 'region');
                        })
                        ->whereIn('post_id', $post_ids)
                    );

                // Order by the order of post_ids provided
                $query1->orderByRaw(DB::raw("FIELD(post_id, " . implode(',', $post_ids) . ")"));

                $query1 = $query1->get();
          }else{
           $query1 = DB::table('tbl_news_post as m')
                    ->select('m.post_id as itemId', 'm.post_title as title', 'm.post_content as description', DB::raw("CONCAT(m.post_date, ' ', m.post_time) as datePublished"), 'm.href', 'm.medium_image as imageUrl', 'tbl_region_commodity_master.itemName as category_data')
                    ->leftJoin('tbl_region_commodity_master', function($join){
                        $join->on('tbl_region_commodity_master.id', '=', 'm.commodityId')
                            ->where('tbl_region_commodity_master.itemType', '=', 'commodity');
                    })
                    ->whereIn('post_id', $post_ids)
                    ->union(DB::table('intel as m')
                        ->select('m.itemId', 'm.title', 'm.description', 'm.datePublished', 'm.href', 'm.imageUrl', 'tbl_region_commodity_master.itemName as category_data')
                        ->leftJoin('tbl_region_commodity_master', function($join){
                            $join->on('tbl_region_commodity_master.id', '=', 'm.commodityId')
                                ->where('tbl_region_commodity_master.itemType', '=', 'commodity');
                        })
                        ->whereIn('post_id', $post_ids)
                    );
                  // Order by the order of post_ids provided
                $query1->orderByRaw(DB::raw("FIELD(post_id, " . implode(',', $post_ids) . ")"));

                $query1 = $query1->get();
          }
            
            $intel_insighs_list = [];
            foreach ($query1 as $row1) {
                $itemName = $row1->category_data;
                /*$intel_insighs_list[$itemName][$row1->itemId]['post_title'] = strlen(stripslashes($row1->title)) > 120 ? substr(stripslashes($row1->title), 0, 120) . '...' : stripslashes($row1->title);
                $intel_insighs_list[$itemName][$row1->itemId]['post_date'] = $row1->datePublished;
                $intel_insighs_list[$itemName][$row1->itemId]['href'] = $row1->href;*/
                
                
                $intel_insighs_list[$itemName][$row1->itemId]['post_title']=strlen(stripslashes($row1->title))>120?substr(stripslashes($row1->title),0,120).'...':stripslashes($row1->title);
                $intel_insighs_list[$itemName][$row1->itemId]['post_content']=$row1->description;
                $intel_insighs_list[$itemName][$row1->itemId]['medium_image']=$row1->imageUrl;                            
                $intel_insighs_list[$itemName][$row1->itemId]['category']=$row1->category_data;
                $intel_insighs_list[$itemName][$row1->itemId]['post_date']=$row1->datePublished;
                $intel_insighs_list[$itemName][$row1->itemId]['href']= $href =  $row1->href;
            }

			$code = 1001;
			$message = "Success";
		}else{
			$code = 2001;
			$message = "No news selected to show.";
		}
		
		
		return new JsonResponse(array("status"=>array("code" => $code, "message" => $message), "data"=> $intel_insighs_list));		
    }
}
