<?php

namespace App\Http\Services\Api;

use App\Models\SignupData;
use App\Models\TrackData;

use DB;
use GuzzleHttp\Client;

class SeminarSignUpService
{
    protected $signupDb;
    protected $trackDataDb;
    
    public function __construct()
    {
        $this->signupDb = new SignupData();
        $this->trackDataDb = new TrackData();
        
    }
    
    public function insertData(
        $firstName, $lastName, $companyName, $companyEmail, $backupEmail, $phoneNumber, $country, $trackNo, 
        $registeredSession, $lunchOptions, $typeOfIndustry, $typeOfJob, $jobTitle, $trackOfInterest, $areaOfInterest, 
        $howToKnowAboutTheEvent, $consentAcceptEmail, $consentPrivacyPolicy)
    {
        
        $this->signupDb
            ->insert([
                'firstName'     => $firstName,
                'lastName'      => $lastName,
                'companyName'   => $companyName,
                'companyEmail'  => $companyEmail,
                'backupEmail'   => $backupEmail,
                'phoneNumber'   => $phoneNumber,
                'country'       => $country,
                'trackNo'       => $trackNo,
                'registeredSession' => $registeredSession,
                'lunchOptions'      => $lunchOptions,
                'typeOfIndustry'    => $typeOfIndustry,
                'typeOfJob'     => $typeOfJob,
                'jobTitle'      => $jobTitle,
                'trackOfInterest'   => $trackOfInterest,
                'areaOfInterest'    => $areaOfInterest,
                'howToKnowAboutTheEvent'    => $howToKnowAboutTheEvent,
                'consentAcceptEmail'    => $consentAcceptEmail,
                'consentPrivacyPolicy'    => $consentPrivacyPolicy,
                'createDate'    => date("Y-m-d H:i:s"),
            ]);
        
    }
    
    public function overLimitOrNot($trackNo)
    {
        $nowCount = 0;

        $nowCount = $this->signupDb
            ->where('trackNo', '=', $trackNo)
            ->count();
        
        $limit = $this->trackDataDb
            ->select('trackLimit')
            ->where('trackNo', '=', $trackNo)
            ->first();
        
        if ($limit->trackLimit>$nowCount) {
            return true;
        } else {
            return false;
        }
    }
    
    public function duplicatedOrNot($trackNo, $companyEmail, $phoneNumber)
    {
        $cnt = 0;

        $cnt = $this->signupDb
            ->where('trackNo', '=', $trackNo)
            ->where('companyEmail', '=', $companyEmail)
            ->where('phoneNumber', '=', $phoneNumber)
            ->count();
        
        \Log::info($cnt);

        if ($cnt==0) {
            return true;
        } else {
            return false;
        }
    }
    
    public function getData()
    {
        
        $res = $this->trackDataDb
            ->select([
                'trackData.*',
                \DB::raw('trackLimit-count(signupData.id) as remain')
                
            ])
            ->leftJoin('signupData', 'trackData.trackNo', '=', 'signupData.trackNo')
            ->groupBy('trackData.trackNo')
            ->get();

        // 整理返回值並返回
        return $res;

    }

}