<?php

namespace App\Http\Controllers\In_Charge;

use App\Http\Controllers\Controller;
use App\Models\Common;
use App\Models\Department;
use App\Models\In_Charge;
use Illuminate\Http\Request;
use Exception;
use Illuminate\Support\Facades\Validator;
use Illuminate\Validation\Rule;

class ProfileController extends Controller
{
    private $folder = "in_charge";
    private $folder_department = "department";
    public $common;
    public function __construct()
    {
        $this->common = new Common;
    }

    public function index(Request $request)
    {
        try {

            $params['data'] = [];
            $Provider = InCharge_Data();
            $data = In_Charge::where('id', $Provider['id'])->with('department')->first();
        
            $this->common->imageNameToUrl(array($data), 'image', $this->folder);
            if ($data['department'] != null) {
                $data['department']['department_icon'] = $this->common->getImage($this->folder_department, $data['department']['department_icon']);
            }
            $params['data'] = $data;
            return view('incharge.profile.index', $params);
        } catch (Exception $e) {
            return response()->json(array('status' => 400, 'errors' => $e->getMessage()));
        }
    }
    public function update($id, Request $request)
    {
        try {
            $validator = Validator::make($request->all(), [
                'full_name' => 'required|min:2',
                'email' => 'required|email|unique:tbl_in_charge,email,' . $id,
                'country_code' => 'required',
                'country_name' => 'required',
                'mobile_number' => [
                    'required',
                    'numeric',
                    Rule::unique('tbl_in_charge')->where(function ($query) use ($request, $id) {
                        return $query->where('country_code', $request->country_code)
                            ->where('mobile_number', $request->mobile_number)
                            ->where('id', '!=', $id);
                    }),
                ],
            ]);
            if ($validator->fails()) {
                $errs = $validator->errors()->all();
                return response()->json(['status' => 400, 'errors' => $errs]);
            }
            
            $requestData = $request->all();
            if (isset($requestData['image'])) {
                $files = $requestData['image'];
                $requestData['image'] = $this->common->saveImage($files, $this->folder, 'in_charge_');

                $this->common->deleteImageToFolder($this->folder, basename($requestData['old_image']));
            }

            $department = Department::where('id', $request['department_id'])->first();
            if (isset($department['id'])) {

                $department['department_color'] = $request['department_color'];
                
                if (isset($request['department_icon'])) {
                    $files = $request['department_icon'];
                    $department['department_icon'] = $this->common->saveImage($files, $this->folder_department, 'dep_');

                    $this->common->deleteImageToFolder($this->folder_department, basename($request['old_department_icon']));
                }
                $department->save();
            }

            unset($requestData['old_image'], $requestData['department_icon'] ,$requestData['old_department_icon'], $requestData['name'], $requestData['department_color']);

            $provider_data = In_Charge::updateOrCreate(['id' => $requestData['id']], $requestData);
            if (isset($provider_data->id)) {

                return response()->json(array('status' => 200, 'success' => __('label.success_edit_in_charge')));
            } else {
                return response()->json(array('status' => 400, 'errors' => __('label.error_edit_in_charge')));
            }
        } catch (Exception $e) {
            return response()->json(array('status' => 400, 'errors' => $e->getMessage()));
        }
    }
}
