<?php

namespace App\Http\Controllers\people;

use App\Http\Controllers\Controller;
use App\Http\Controllers\Helpers\HelperController;
use App\Http\Controllers\UsersController;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Hash;
use Illuminate\Support\Facades\Validator;

class PlantAccountantController extends Controller
{
    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function index()
    {

        return view('people.plant_accountant');
    }

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

    /**
     * Store a newly created resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @return \Illuminate\Http\Response
     */
    public function store(Request $request)
    {
        try {
            //validate
            $validator = Validator::make($request->all(), [
                'name' => 'required',
                'email' => 'required|email|unique:users,email',
                'phone' => 'required|unique:users,phone',
            ]);
            if ($validator->fails()) {
                $result = [
                    'status' => 400,
                    'errors' => $validator->errors()
                ];

                return response()->json($result, 400);
            }
            // check if active user exists
            UsersController::validations($request->notation, $request->plant_code, $request->company_code);
            $name = $request->name;
            $email = $request->email;
            $phone = $request->phone;
            $password = Hash::make('Password');
            $role = $request->role;
            $notation = $request->notation;
            $plant_code = $request->plant_code;
            $company_code = $request->company_code;
            HelperController::save_plant_level($name, $email, $phone, $password, $role, $plant_code, $notation, $company_code);
            $res = [
                'status' => 200,
                'message' => 'User created successfully'
            ];
            return response()->json($res, 200);
        } catch (\Throwable $th) {
            $res = [
                'status' => 500,
                'message' => $th->getMessage()
            ];
            return response()->json($res, 500);
        }
    }

    /**
     * Display the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function show($id)
    {
        //
    }

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

    /**
     * Update the specified resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function update(Request $request, $id)
    {
        //
    }

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