<?php

namespace Wave\Http\Controllers\CMS;

use App\Http\Controllers\Controller;
use App\Models\CompanySubscription;
use App\Models\Subscription;
use Illuminate\Http\Request;
use Yajra\DataTables\Facades\DataTables;

class SubscriptionController extends Controller
{
    /**
     * Create a new controller instance.
     *
     * @return void
     */
    public function __construct()
    {
        $this->middleware('auth');
    }

    public function index()
    {
        return view('theme::cms.subscriptions.index');
    }

    public function table(Request $request, $status = 'active')
    {
        $companySubscriptions = CompanySubscription::query()
            ->leftJoin('companies', 'company_subscriptions.company_id', '=', 'companies.id')
            ->leftJoin('subscriptions', 'company_subscriptions.subscription_id', '=', 'subscriptions.id')
            ->select(
                'company_subscriptions.id',
                'companies.name_english as company_name_english',
                'companies.name_chinese as company_name_chinese',
                'company_subscriptions.created_at',
                'subscriptions.service_type',
                'company_subscriptions.expiration_at',
                'subscriptions.name_english',
                'subscriptions.name_chinese',
                'company_subscriptions.status',
            )
            ->when($request->company_name, function ($query) use ($request) {
                $query->where(function($query2) use ($request){
                    $query2->orWhere('companies.name_english', 'like', '%' . $request->company_name . '%');
                    $query2->orWhere('companies.name_chinese', 'like', '%' . $request->company_name . '%');
                });
            })
            ->when($request->service_type, function ($query) use ($request) {
                $query->where('subscriptions.service_type', $request->service_type);
            })
            ->when($request->subscription_id, function ($query) use ($request) {
                $query->where('subscriptions.id', $request->subscription_id);
            });
        
        if ($status == 'active') {
            $companySubscriptions = $companySubscriptions->where('company_subscriptions.status', true);
        }
        else if ($status == 'expired') {
            $companySubscriptions = $companySubscriptions->where('company_subscriptions.status', false);
        }

        return Datatables::of($companySubscriptions)
            ->escapeColumns(['id'])
            ->addColumn('service_type', function ($companySubscription) {
                if ($companySubscription->service_type == 'bookkeeping') {
                    return __('Bookkeeping');
                }
                else if ($companySubscription->service_type == 'company_secretary') {
                    return __('Company Secretary');
                }
                else if ($companySubscription->service_type == 'incorporation') {
                    return __('Incorporation');
                }
            })
            ->addColumn('subscription_period', function ($companySubscription) {
                if ($companySubscription->expiration_at) {
                    return date('Y.m.d', strtotime($companySubscription->created_at)) . '-' . date('Y.m.d', strtotime($companySubscription->expiration_at));
                }
                
                return '-';
            })
            ->addColumn('created_at', function ($companySubscription) {
                return date('Ymd', strtotime($companySubscription->created_at));
            })
            ->addColumn('status', function ($companySubscription) {
                return '<span class="success item-list-note">Active</span>';
            })
            ->addColumn('invoice', function ($company) { // Temporary
                return '<a href=""><img class="max-25" src="' . asset('themes/tailwind/images/download.svg') . '"></a>';
            })
            ->make(true);
    }
    
    public function getSubscriptions($service_type)
    {
        $subscriptions = Subscription::where('service_type', $service_type);
        
        if (strtolower(app()->getLocale()) == 'zh_hk') {
            $subscriptions->orderBy('name_chinese');
        }
        else {
            $subscriptions->orderBy('name_english');
        }

        return response()->json([
            'success' => true,
            'subscriptions' => $subscriptions->get(),
        ]);
    }
}
