<?php

namespace App\Http\Controllers;


use Illuminate\Http\Request;
use Spatie\Permission\Models\Role;
use Spatie\Permission\Models\Permission;
use Illuminate\Support\Facades\Storage;
use App\Models\User;
use App\Imports\ImportUser;
use App\Exports\ExportUser;
use App\Http\Requests\UserRequest;
use Maatwebsite\Excel\Facades\Excel;
use Auth, DB, Mail, Validator, File, DataTables, Exception;

class AddVendorController extends Controller{
    /** construct */
        public function __construct(){
            $this->middleware('permission:addvendor-create', ['only' => ['create']]);
            $this->middleware('permission:addvendor-edit', ['only' => ['edit']]);
            $this->middleware('permission:addvendor-view', ['only' => ['view']]);
            $this->middleware('permission:addvendor-delete', ['only' => ['delete']]);
        }
    /** construct */

    /** index */
    public function index(Request $request)
    {
        if(auth()->user()->can('addvendor-view')){

            $user = DB::table('users')->where(['status' => 'active'])->get();
            if (sizeof($user)) {
                return response()->json(['status' => 200, 'message' => 'Data Found.', 'Data' => $user]);
            } else {
                return response()->json(['status' => 404, 'message' => 'No Data Found.']);
            }
        }else{
            return response()->json(['status' => 401, 'message' => 'Not Authorized.']);
        }
    }
/** index */


/** insert */
    public function insert(Request $request)
    {
        if(auth()->user()->can('addvendor-create')){
            $rules = [
                'name_of_company' => 'required',
                
                'email' => 'required|email|unique:users,email'
            ];

            $validator = Validator::make($request->all(), $rules);

            if ($validator->fails())
                return response()->json(['status' => 422, 'message' => $validator->errors()]);

            $password = $request->password ?? 'Abcd@1234';
            $crud = [
               
                'email' => $request->email,
                'role' => $request->role,
                'contact_number' => $request->contact_number,
                'name_of_company' => $request->name_of_company,
                'pan_no'=>$request->pan_no,
                 'branch' => $request->branch,
                'status' => 'active',
                'password' => bcrypt($password),
                'created_at' => date('Y-m-d H:i:s'),
                'created_by' => auth()->user()->id,
                'updated_at' => date('Y-m-d H:i:s'),
                'updated_by' => auth()->user()->id
            ];

            $user = User::create($crud);
            $user->assignRole($request->role);

            if ($user) {
                return response()->json(['status' => 200, 'message' => 'Record inserted successfully']);
            } else {
                return response()->json(['status' => 201, 'message' => 'Faild to insert Record!']);
            }
        }else{
            return response()->json(['status' => 401, 'message' => 'Not Authorized.']);
        }
    }
/** insert */

/** view */
    public function view(Request $request)
    {
        if(auth()->user()->can('addvendor-view')){   
            $data = User::where(['id' => $request->id])->first();

            if (sizeof($data))
                return response()->json(['status' => 200, 'message' => 'Data found', 'data' => $data]);
            else
                return response()->json(['status' => 404, 'message' => 'No data found']);
        }else{
            return response()->json(['status' => 401, 'message' => 'Not Authorized.']);
        }
    }
/** view */

/** update */
    public function update(Request $request)
    {
        if(auth()->user()->can('addvendor-edit')){ 
            $rules = [
                
                'name_of_company' => 'required',
                'email' => 'required|email|unique:users,email,' . $request->id
            ];

            $validator = Validator::make($request->all(), $rules);

            if ($validator->fails())
                return response()->json(['status' => 422, 'message' => $validator->errors()]);

            $crud = [
                'name_of_company' => ucfirst($request->name_of_company) ?? '',
                'email' => $request->email ?? '',
                'role' => $request->role ?? '',
                'contact_number' => $request->contact_number ?? '',
                'pan_no' => $request->pan_no ?? '',
                'branch' => $request->branch ?? null,
                'updated_at' => date('Y-m-d H:i:s'),
                'updated_by' => auth('sanctum')->user()->id
            ];

            if (isset($request->password) && !empty($request->password))
                $crud['password'] = bcrypt($request->password);

            $update = User::where(['id' => $request->id])->update($crud);
            if ($update)
                return response()->json(['status' => 200, 'message' => 'Record updated successfully']);
            else
                return response()->json(['status' => 404, 'message' => 'Faild to update record']);
        }else{
            return response()->json(['status' => 401, 'message' => 'Not Authorized.']);
        }
    }
/** update */


/** change-status */
    public function change_status(Request $request)
    {
        if(auth()->user()->can('addvendor-delete')){ 
            $rules = [
                'id' => 'required',
                'status' => 'required'
            ];

            $validator = Validator::make($request->all(), $rules);

            if ($validator->fails())
                return response()->json(['status' => 422, 'message' => $validator->errors()]);

            $data = User::where(['id' => $request->id])->first();

            if (!empty($data)) {
                $update = User::where(['id' => $request->id])->update(['status' => $request->status, 'updated_at' => date('Y-m-d H:i:s'), 'updated_by' => auth('sanctum')->user()->id]);
                if ($update) {
                    return response()->json(['status' => 200, 'message' => 'Record status change successfully']);
                } else {
                    return response()->json(['status' => 201, 'message' => 'Faild to update status']);
                }
            } else {
                return response()->json(['status' => 201, 'message' => 'Somthing went wrong !']);
            }
        }else{
            return response()->json(['status' => 401, 'message' => 'Not Authorized.']);
        }
    }
/** change-status */
    
    
}
