<?php
namespace App\Http\Controllers\Api\v1;

use Illuminate\Support\Facades\DB;

/**
 * SubscriptionPlanController Class Doc Comment
 *
 * @category Class
 * @package  Api
 * @author   Ketan Patel - ketan.patel@brainvire.com
 *
*/
class ManagerPlanController extends Controller
{
    public function __construct()
    {
     //$this->middleware('oauth', ['except' => ['index']]);
     //$this->middleware('auth:api', ['except' => ['index']]);
    }
    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function index()
    {
        $subscriptions = DB::table('manager_plans as mp')
          ->select(
                'u.name as manager_name',
                'u.email',
                'u.mobile',
                'sp.validity',
                'sp.name as plan_name',
                'sp.price',
                'mp.activated_at',
                'mp.expire_on',
                'mp.final_price',
                'mp.promo_discount',
                'mp.status'
          )
          ->leftJoin('subscription_plans as sp', 'sp.id', '=', 'mp.plan_id')
          ->leftJoin('users as u', 'u.id', '=', 'mp.manager_id')
          ->where('mp.status', '!=', 'deleted')
          ->orderBy('mp.id', 'desc')
          ->get();
        
        $helperObj = new \App\CommonHelper();
        $dateformat = env("DATE_FORMAT");
        //return 
        if (!empty($subscriptions)) {
            foreach($subscriptions as $key => $val){
                $subscriptions[$key]->activated_at = $helperObj->convertDateByFormat($val->activated_at, $dateformat);
                $subscriptions[$key]->expire_on = $helperObj->convertDateByFormat($val->expire_on, $dateformat);
            }
        }
        
        return response()->json($subscriptions);
    }
}
