<?php

namespace App\Http\Controllers\In_Charge;

use App\Http\Controllers\Controller;
use App\Models\Common;
use App\Models\Department_Working_Time;
use App\Models\In_Charge;
use App\Models\Provider;
use App\Models\Provider_Working_Time;
use Illuminate\Http\Request;
use Exception;

class WorkingTimeController extends Controller
{
    public $common;
    public function __construct()
    {
        $this->common = new Common;
    }

    public function index(Request $request)
    {
        try {
            $params['data'] = [];
            $incharge = InCharge_Data();
            if (isset($incharge) && $incharge != null) {

                $params['incharge_id'] = $incharge['id'];
                $params['work_time'] = Department_Working_Time::where('department_id', $incharge['department_id'])->get();

                return view('incharge.working_time.index', $params);
            } else {
                return redirect()->route('incharge.login')->with('success', __('label.logout_successfully'));
            }
        } catch (Exception $e) {
            return response()->json(array('status' => 400, 'errors' => $e->getMessage()));
        }
    }
    public function update(Request $request)
    {
        try {
             
            $incharge = InCharge_Data();
            // Working Time
            foreach (range(1, 7) as $day) {
                $data = [
                    'start_time' => $request->input("start_time_{$day}") ?? '00:00',
                    'end_time' => $request->input("end_time_{$day}") ?? '00:00',
                    'status' => $request->input("day_{$day}") ?? 0,
                ];

                Department_Working_Time::updateOrCreate(
                    [
                        'department_id' => $incharge['department_id'],
                        'week_day' => $day,
                    ],
                    $data
                );
            }
            return response()->json(array('status' => 200, 'success' => __('label.success_edit_working_time')));
        } catch (Exception $e) {
            return response()->json(array('status' => 400, 'errors' => $e->getMessage()));
        }
    }
}
