<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use App\Notification;
use App\NotificationType;
use App\ProductNotification;
use App\CategoryMaster;
use App\Product;
use App\ProductUnit;
use App\UnitMeasurement;
use App\ProductGrade;
use App\GradeMaster;
use App\CustomerType;
use Auth;
use App\Common;
use App\NotificationStatus;
use App\ProductStandard;

class NotificationController extends Controller
{
    public function __construct()
    {
        $this->middleware('auth');
    }
    
    public function index()
    {
    	 $notifications = Notification::all();
       
       return view('notification/index',compact('notifications'));
    
    }

    public function create()
    {
    	  $categories = CategoryMaster::orderBy('category')->get();
        $customerType = CustomerType::where('is_active','=','Y')->get();
        $offerType    = Common::where('masterkeyword','=','NOTIFICATION')->get();
        return view('notification/create',compact('categories','customerType','offerType'));
    
    }

    public function listProduct(Request $request)
    {
    	 $product = Product::where('category_id','=',$request->get('option'))
                         ->where('is_active','=','Y')->orderBy('name')->get();

    	 return $product;
    }

    public function listUnit(Request $request)
    {
    	 $unit = ProductUnit::join('unit_of_measurement','unit_of_measurement.id','=','product_unit.unit_id')
    	                   ->where('product_unit.product_id','=',$request->get('option'))
    	                   ->selectRaw('unit_of_measurement.*')->get();

    	 return $unit;                   
    }

    public function listGrade(Request $request)
    {
    	 $grade = ProductGrade::join('grade_master','grade_master.id','=','product_grade.grade_id')
                           ->where('product_grade.product_id','=',$request->get('option'))
    	                     ->where('grade_master.is_active','=','Y')
    	                     ->selectRaw('grade_master.*')->get();
    	 return $grade;                     
    }

    public function listPacking(Request $request)
    {
       $packing = ProductStandard::join('standard_master','standard_master.id','=','product_standard.standard_id')
                  ->where('product_standard.product_id','=',$request->get('option1'))
                  ->where('product_standard.grade_id','=',$request->get('option'))
                  ->where('standard_master.is_active','=','Y')
                  ->selectRaw('standard_master.*')->get();
       
       return $packing;                     
    }

    public function store(Request $request)
    {
      $valid_from   = date('Y-m-d', strtotime($request->get('validFrom')));
      $valid_to     = date('Y-m-d', strtotime($request->get('validTo')));
      $matter       = $request->get('message');

      if($request->get('notfn') == 'P')
      {
        $product = Product::find($request->get('product'))->name;
        $matter  = $product;

        if($request->get('unit') != 0)
        {
          $unit    = UnitMeasurement::find($request->get('unit'))->unit;
          $matter  = $matter.'/'.$unit;
        }

        if($request->get('grade') != 0)
        {
          $grade   = GradeMaster::find($request->get('grade'))->grade;
          $matter  = $matter.'/'.$grade;
        }
        
        
        
        $matter  = $matter.' '.$request->get('message');
      }

      $notification = Notification::create(array( 'notification_type'  => $request->get('notfn'),
            	                                    'matter'             => strtoupper($matter),
            	                                    'entered_on'         => date('Y-m-d'),
            	                                    'entered_by'         => Auth::user()->id,
            	                                    'valid_from'         => $valid_from,
            	                                    'valid_to'           => $valid_to,
            	                                    'offer_type'         => $request->get('offerType') ));
    	if($request->get('notfn') == 'P')
    	{
           
            ProductNotification::create(array( 'notification_id' => $notification->id,
                                            'message'        => $request->get('message'),
            	                               'product_id'      => $request->get('product'),
            	                               'offer_price'     => $request->get('price'),
                                             'price_type'      => $request->get('price_type'),
            	                               'grade_id'        => $request->get('grade'),
                                             'standard_id'     => $request->get('packing'),
            	                               'unit_id'         => $request->get('unit') ));
    	}

    	if(in_array('ALL',$request->get('apply')))
    	{
          
    		$customer_types = CustomerType::all();
             
            foreach($customer_types as $type)
            {
            	NotificationType::create(array('notification_id'   => $notification->id,
    			                           'customer_type_id'      => $type->id ));
            }
    		
    	}
    	else
    	{
           
    		$customer_types = $request->get('apply');
           
            foreach($customer_types as $type)
            {
            	NotificationType::create(array('notification_id'   => $notification->id,
    			                           'customer_type_id'      => $type ));
            }
    	}
    	
    	return redirect('notification/index')->with('exist','true');
    }

    public function edit($id)
    {
      $products = NULL;
      $units    = NULL;
      $grades   = NULL;
      $offer_price = NULL;
      $notCat      = NULL;
      $priceType   = NULL;
      $message = NULL;
      $categories = CategoryMaster::orderBy('category')->get();  
      $customerType = CustomerType::where('is_active','=','Y')->get();
      $offerType    = Common::where('masterkeyword','=','NOTIFICATION')->get();
      $notification = Notification::find($id);
      $productNot   = ProductNotification::where('notification_id','=',$id)->first();   
      $notTypes     = NotificationType::where('notification_id','=',$id)->pluck('customer_type_id')->toArray();

      if($productNot != NULL)
      {
        $notCat       = Product::where('id','=',$productNot->product_id)->first();
        $products     = Product::where('category_id','=',$notCat->category_id)
                               ->where('is_active','=','Y')->orderBy('name')->get();

        $units        = ProductUnit::join('unit_of_measurement','unit_of_measurement.id','=','product_unit.unit_id')
                           ->where('product_unit.product_id','=',$productNot->product_id) 
                           ->selectRaw('unit_of_measurement.*')->get();  
        $grades       = ProductGrade::join('grade_master','grade_master.id','=','product_grade.grade_id')
                             ->where('product_grade.product_id','=',$productNot->product_id)
                             ->where('grade_master.is_active','=','Y')
                             ->selectRaw('grade_master.*')->get();
        $standards =  ProductStandard::join('standard_master','standard_master.id','=','product_standard.standard_id')
                            ->where('product_standard.product_id','=',$productNot->product_id)
                            ->where('product_standard.grade_id','=',$productNot->grade_id)
                            ->where('standard_master.is_active','=','Y')
                            ->selectRaw('standard_master.*')->get();
        $offer_price  = $productNot->offer_price;    
        $priceType    = $productNot->price_type;   
        $message = $productNot->message;               
      }
      
      return view('notification/edit',compact('notification','productNot','notTypes','categories','customerType','offerType','products','units','grades','offer_price','notCat','priceType','message','standards'));
    }

    public function updateData(Request $request)
    {
        $valid_from   = date('Y-m-d', strtotime($request->get('validFrom')));
        $valid_to     = date('Y-m-d', strtotime($request->get('validTo')));
        $notList      = Notification::find($request->get('notification_id'))->notification_type;
        $matter       = $request->get('message');
        if($notList == 'P')
        {
           $product = Product::find($request->get('product'))->name;
           $matter  = $product;

           if($request->get('unit') != 0)
           {
              $unit    = UnitMeasurement::find($request->get('unit'))->unit;
              $matter  = $matter.'/'.$unit;
           }
           if($request->get('grade') != 0)
           {
              $grade   = GradeMaster::find($request->get('grade'))->grade;
              $matter  = $matter.'/'.$grade;
           }
           
           $matter  = $matter.' '.$request->get('message');
       }
        
        $notification = Notification::where('id','=',$request->get('notification_id'))
                                    ->update(array(
                                                    'matter'             => strtoupper($matter),
                                                    'entered_on'         => date('Y-m-d'),
                                                    'entered_by'         => Auth::user()->id,
                                                    'valid_from'         => $valid_from,
                                                    'valid_to'           => $valid_to,
                                                    'offer_type'         => $request->get('offerType') ));
        if($notList == 'P')
        {
            ProductNotification::where('notification_id','=',$request->get('notification_id'))
                               ->update(array( 'notification_id' => $request->get('notification_id'),
                                               'message'        => $request->get('message'),
                                               'product_id'      => $request->get('product'),
                                               'offer_price'     => $request->get('price'),
                                               'price_type'      => $request->get('price_type'),
                                               'grade_id'        => $request->get('grade'),
                                               'standard_id'     => $request->get('packing'),
                                               'unit_id'         => $request->get('unit') ));
        }

        NotificationType::where('notification_id','=',$request->get('notification_id'))->delete();

       if(in_array('ALL',$request->get('apply')))
        {
           $customer_types = CustomerType::where('is_active','=','Y')->get();
             
            foreach($customer_types as $type)
            {
                NotificationType::create(array('notification_id'   => $request->get('notification_id'),
                                           'customer_type_id'      => $type->id ));
            }
            
        }
        else
        {
            $customer_types = $request->get('apply');
           
            foreach($customer_types as $type)
            {
                NotificationType::create(array('notification_id'   => $request->get('notification_id'),
                                           'customer_type_id'      => $type ));
            }
        } 

        return redirect('notification/index')->with('exist','true');                          

    }

     public function showList()
    {
      $status = NULL;
      if(Auth::user()->customer_id != NULL)
      {
        $user = Auth::user()->customer->type->id;

        $notifications = Notification::join('notification_types','notification_types.notification_id','=',
                                    'notifications.id')
                                    ->where('notification_types.customer_type_id','=',$user)
                                    ->where('valid_from','<=',date('Y-m-d'))
                                    ->where('valid_to','>=',date('Y-m-d'))
                                    ->selectRaw('notifications.*')
                                    ->get();
                                    
        $status  = NotificationStatus::where('customer_id','=',Auth::user()->customer_id)
                                     ->pluck('notification_id')
                                     ->toArray();
                                      
      }
      else
      {
        $notifications = Notification::where('valid_from','<=',date('Y-m-d'))
                                    ->where('valid_to','>=',date('Y-m-d'))
                                    ->selectRaw('notifications.*')
                                    ->get();

      }
           
      return view('notification/show',compact('notifications','status'));
    }

    public function insertStatus($id)
    {
      NotificationStatus::create(array( 'notification_id'  => $id,
                                        'customer_id'      => Auth::user()->customer_id,
                                        'status'           => 'READ'));
      return redirect('notification/show');
    }
  
}
