<?php

namespace App\Http\Services\Backend\DataManagement;

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

class TrackManagementService
{
    
    // 相關私有 model 調用器宣告
    private $signupDb;
    public function __construct()
    {
        // 建構 model 調用器
        $this->trackDataDb = new TrackData();

    }
    public function getList(&$cnt = 0, $orderColumn, $orderDir, $start, $length, $searchValue, $keyword)
    {
        // 調用資料庫(或者其他業務邏輯)
        $res = $this->trackDataDb
            ->select('*')
            ->where('trackNo', 'like', '%'.$keyword.'%')
            ->orWhere('trackTitle', 'like', '%'.$keyword.'%');
            
        // 取總筆數
        $cnt = $res->count();
        // 排序
        $res = $res
            ->orderByRaw((int)$orderColumn . ' ' . $orderDir);
        // 分頁
        $res = $res
            ->skip($start)->take($length);
        // 實際取資料
        $result = $res
            ->get()
            ->toArray();
        
        // 整理返回值並返回
        return $result;
    }

    public function getReferral()
    {
        $res=$this->referralDB->select(["id","name","referral"])->get();
        if(!empty($res))
        {
            $res=$res->toArray();
        }
        return $res;
    }
    public function getTrackDataById($id)
    {
        $res=$this->trackDataDb
            ->select('*')
            ->where("id",$id)
            ->first();

        if(!empty($res))
        {
            $res=$res->toArray();
        }
        return $res;
    }
    public function getReferralByReferral($referral)
    {
        return $this->referralDB->where('referral', $referral)->first();
    }
    public function modifyReferral($id,$name,$referral,$richmenuId,$orlCode=0)
    {
        $res=$this->referralDB->where("id",$id)->update(["name"=>$name,"richmenuId"=>$richmenuId,"orlCode"=>$orlCode]);
        return $res;
    }
    public function modifyReferralPicsee($id,$picseeCode,$picseeUrl,$qRCode)
    {
        $res=$this->referralDB->where("id",$id)->update(["picseeCode"=>$picseeCode,"shortUrl"=>$picseeUrl,"qRCode"=>$qRCode]);
        return $res;
    }
    public function modifyPicseeCntAndTime($id,$picseeCnt,$last_call)
    {
        $res=$this->referralDB->where("id",$id)->update(["picseeCnt"=>$picseeCnt,"last_call"=>$last_call]);
        return $res;
    }
    
    public function insert($trackNo, $trackTitle, $trackLimit)
    {
        
        $this->trackDataDb
            ->insert([
                'trackNo'       => $trackNo,
                'trackTitle'    => $trackTitle,
                'trackLimit'    => $trackLimit,
                'createDate'    => date("Y-m-d H:i:s"),
            ]);

        return ;
    }
    
    public function store($id, $trackNo, $trackTitle, $trackLimit)
    {
        $this->trackDataDb
                ->where('id', $id)
                ->update([
                    'trackNo'       => $trackNo,
                    'trackTitle'    => $trackTitle,
                    'trackLimit'    => $trackLimit,
                    'updateDate'    => date("Y-m-d H:i:s"),
                ]);
                
        return $id;
    }

    public function deleteDataById($id)
    {
        // 取得參數
        // 調用資料庫(或者其他業務邏輯)
        $res = $this->trackDataDb
            ->where('id', $id)
            ->delete();
        
        // 整理返回值並返回
        return $res;
    }

    public function getExportList($keyword)
    {
        $res = $this->signupDb
            ->select('*')
            // 過濾搜尋條件
            ->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('registeredSession', $keyword)
            // ->orWhere('lunchOptions', $keyword)
            ->orWhere('typeOfIndustry', $keyword)
            ->orWhere('typeOfJob', $keyword)
            ->orWhere('jobTitle', $keyword)
            ->get()
            ->toArray();
        
        // 整理返回值並返回
        return $res;
    }

}