<?php

namespace App\Http\Services\Backend\DataManagement;

use App\Http\Services\ConstDef\GeneralConst;
use App\Models\SignupData;
use DB;
use Log;
use PhpOffice\PhpSpreadsheet\IOFactory;
use PhpOffice\PhpSpreadsheet\Spreadsheet;

class SignupManagementService
{
    
    // 相關私有 model 調用器宣告
    private $signupDb;
    public function __construct()
    {
        // 建構 model 調用器
        $this->signupDb=new SignupData();
    }
    public function getList(&$cnt = 0, $orderColumn, $orderDir, $start, $length, $searchValue, $keyword, $trackNo, $createDateStart, $createDateFinal)
    {
        // 調用資料庫(或者其他業務邏輯)
        $res = $this->signupDb
            ->select('*');
            
        if ($keyword) {

            $res = $res->where('firstName', $keyword)
                ->orWhere('lastName', $keyword)
                ->orWhere('companyName', $keyword)
                ->orWhere('companyEmail', $keyword)
                ->orWhere('backupEmail', $keyword)
                ->orWhere('phoneNumber', $keyword);
                // ->orWhere('country', $keyword)
                // ->orWhere('trackNo', $keyword)
                // ->orWhere('typeOfIndustry', $keyword)
                // ->orWhere('typeOfJob', $keyword)
                // ->orWhere('jobTitle', $keyword);
        }

        if ($trackNo) {
            \Log::info($trackNo);
            $res = $res->where('trackNo','LIKE', $trackNo.'%');
        }
        if ($createDateStart && $createDateFinal) {
            
            $res = $res->where('createDate', '>', $createDateStart)
                ->where('createDate', '<', $createDateFinal);
        }
        
                        
        // 取總筆數
        $cnt = $res->count();
        // 排序
        $res = $res
            ->orderByRaw((int)$orderColumn . ' ' . $orderDir);
        // 分頁
        $res = $res
            ->skip($start)->take($length);
        // 實際取資料
        $result = $res
            ->get()
            ->toArray();
        
        // 整理返回值並返回
        return $result;
    }

    public function getExportList($keyword, $trackNo, $createDateStart, $createDateFinal)
    {
        \Log::info('keyword: '.$keyword);
        \Log::info('trackNo: '.$trackNo);
        \Log::info('createDateStart: '.$createDateStart);
        \Log::info('createDateFinal: '.$createDateFinal);

        $res = $this->signupDb
            ->select('*');
            
        if ($trackNo!="") {
            
            \Log::info('trackNo123');
            $res = $res->where('trackNo','LIKE', $trackNo.'%');
        }
        
        if ($keyword!=""){
            
            $res = $res->where('firstName', $keyword)
                ->orWhere('lastName', $keyword)
                ->orWhere('companyName', $keyword)
                ->orWhere('companyEmail', $keyword)
                ->orWhere('backupEmail', $keyword)
                ->orWhere('phoneNumber', $keyword);
                // ->orWhere('country', $keyword)
                // ->orWhere('trackNo', $keyword)
                // ->orWhere('typeOfIndustry', $keyword)
                // ->orWhere('typeOfJob', $keyword)
                // ->orWhere('jobTitle', $keyword);
        }
        
        // if ($createDateStart!="" && $createDateFinal!="") {
            // \Log::info('date123');
            $res = $res->where('createDate', '>', $createDateStart)
                        ->where('createDate', '<', $createDateFinal);
        // }
        
        $res = $this->signupDb
            ->get()
            ->toArray();
        \Log::info(print_r($res,true));
        // 整理返回值並返回
        return $res;
    }

}