<?php

namespace App\Http\Controllers;

use App\Models\Activity;
use App\Models\User;
use App\Models\Recipients;
use App\Models\Prize;
use App\Models\CheckIn;
use App\Http\Requests\StoreActivityRequest;
use App\Http\Requests\UpdateActivityRequest;
use App\Http\Controllers\Controller;
use Illuminate\Http\Request;
use PhpOffice\PhpSpreadsheet\Spreadsheet;
use PhpOffice\PhpSpreadsheet\Reader\IReader;
use PhpOffice\PhpSpreadsheet\Reader\Csv;
use PhpOffice\PhpSpreadsheet\Reader\Xlsx;
use PhpOffice\PhpSpreadsheet\Reader\Xls;
use PhpOffice\PhpSpreadsheet\IOFactory;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\File;


class ActivityController extends Controller
{
    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function index()
    {
        //
        $activities = Activity::all();
        return $activities;
    }

    public function get_activity_by_id(Request $request)
    {
        return Activity::where('id', $request->id)->first();
    }

    /**
     * Show the form for creating a new resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function create()
    {
        //
    }

    /**
     * Store a newly created resource in storage.
     *
     * @param  \App\Http\Requests\StoreActivityRequest  $request
     * @return \Illuminate\Http\Response
     */
    public function store(StoreActivityRequest $request)
    {
        //
        $activity = new Activity;
        $activity->activity_name = $request->activity_name;
        $activity->date = $request->date;
        $activity->place = $request->place;
        $activity->user_id = $request->user_id;
        $activity->deadline = $request->deadline;
        $activity->save();
    }

    /**
     * Display the specified resource.
     *
     * @param  \App\Models\Activity  $activity
     * @return \Illuminate\Http\Response
     */
    public function show(Request $request, Activity $activity)
    {
        //
        $activity = Activity::where('id', '=', $request->activity_id)->first();
        return $activity;
    }

    /**
     * Show the form for editing the specified resource.
     *
     * @param  \App\Models\Activity  $activity
     * @return \Illuminate\Http\Response
     */
    public function edit(Activity $activity)
    {
        //
    }

    /**
     * Update the specified resource in storage.
     *
     * @param  \App\Http\Requests\UpdateActivityRequest  $request
     * @param  \App\Models\Activity  $activity
     * @return \Illuminate\Http\Response
     */
    public function update(UpdateActivityRequest $request, Activity $activity)
    {
        //
        Activity::where('id', '=', $request->activity_id)->update([
            'activity_name' => $request->activity_name,
            'date' => $request->date,
            'place' => $request->place,
            'user_id' => $request->user_id,
            'deadline' => $request->deadline,
        ]);
    }

    /**
     * Remove the specified resource from storage.
     *
     * @param  \App\Models\Activity  $activity
     * @return \Illuminate\Http\Response
     */
    public function destroy(Activity $activity)
    {
        //
    }




    //這邊有兩種insert，要怎麼處理StoreRequest?
    public function uploadFile(Request $request)
    {
        $option = $request->option;
        $activity_id = $request->activity_id;
        if ($activity_id == 'undefined') {
            $activity_id = Activity::orderBy('id', 'DESC')->first()->id;
        }

        $upload_path = public_path('upload');
        $file_name = $request->file->getClientOriginalName();
        $file_path = $upload_path . '/' . $file_name;
        if (File::exists($file_path)) {
            File::delete($file_path);
        }
        $request->file->move($upload_path, $file_name);

        $extension = pathinfo($file_path, PATHINFO_EXTENSION);
        if ('csv' == $extension) {
            $reader = new \PhpOffice\PhpSpreadsheet\Reader\Csv();
        } else if ('xls' == $extension) {
            $reader = new \PhpOffice\PhpSpreadsheet\Reader\Xls();
        } else if ('xlsx' == $extension) {
            $reader = new \PhpOffice\PhpSpreadsheet\Reader\Xlsx();
        }
        $reader->setReadDataOnly(true);
        $reader->setReadEmptyCells(false);
        $inputFileName = public_path('upload') . '\\' . $file_name;
        $spreadsheet = $reader->load($inputFileName);
        $worksheet = $spreadsheet->getActiveSheet();
        $highestRow = $worksheet->getHighestDataRow();
        date_default_timezone_set('Asia/Taipei');
        if ($option == 'prize') {
            if ($worksheet->getCell([1, 1])->getValue() == '獎品') {
                if(DB::table('prizes')->where('activity_id', '=', $activity_id)->exists()){
                    DB::table('prizes')->where('activity_id', '=', $activity_id)->delete();
                }
                for ($row = 2; $row <= $highestRow; $row++) {
                    DB::table('prizes')->insert([
                        'activity_id' => $activity_id,
                        'name' => $worksheet->getCell([1, $row])->getValue(),
                        'count' => $worksheet->getCell([2, $row])->getValue(),
                        'provider' => $worksheet->getCell([3, $row])->getValue(),
                        'created_at' => now(),
                        'updated_at' => now(),
                    ]);
                }
                $isSuccess = true;
            } else {
                $isSuccess = false;
            }
        } else if ($option == 'guest') {
            if ($worksheet->getCell([1, 1])->getValue() == '來賓編號') {
                if(DB::table('check_ins')->where('activity_id', '=', $activity_id)->where('region', '=', '來賓')->exists()){
                    DB::table('check_ins')->where('activity_id', '=', $activity_id)->where('region', '=', '來賓')->delete();
                }
                for ($row = 2; $row <= $highestRow; $row++) {
                    DB::table('check_ins')->insert([
                        'user_id' => $worksheet->getCell([1, $row])->getValue(),
                        'name' => $worksheet->getCell([2, $row])->getValue(),
                        'department_id' => 'guest',
                        'region' => '來賓',
                        'is_checked_in' => false,
                        'is_awarded' => false,
                        'activity_id' => $activity_id,
                        'created_at' => now(),
                        'updated_at' => now(),
                    ]);
                }
                $isSuccess = true;
            } else {
                $isSuccess = false;
            }
        }

        $response = [
            'isSuccess' => $isSuccess,
        ];

        return response($response, 201);
    }
}
