<?php

namespace App\Http\Controllers\Admin;

use App\Http\Controllers\Controller;
use Illuminate\Http\Request;
use App\Models\ManageModule;
use App\Models\IamPrincipal;
use App\Models\ManageModuleLink;
use Illuminate\Support\Facades\Auth;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Log;
use Exception;
use Illuminate\Support\Facades\Mail;
use App\Mail\Add_Subadmin;
class ManageSubAdminController extends Controller
{
    public function index(){
        $sub_admins_module = ManageModule::latest()->get();
        $sub_admins_data = IamPrincipal::where('principal_type_xid', 2)->latest()->get();

        return view('Admin.pages.manage_users.manage_sub_admin.manage_subadmin',compact('sub_admins_data','sub_admins_module'));
    }

    public function create()
    {
        $sub_admins_module = ManageModule::latest()->get();

        return view('Admin.pages.manage_users.manage_sub_admin.create', compact('sub_admins_module'));
    }

    public function store_subadmin(Request $request)
    {


        /*
        Created By : shailesh Gupta
        Created at : 29 May 2024
        Use : To store sub admin form and email
        */
        try {

            // DB::beginTransaction();


            $sub_admin = new IamPrincipal();
            $sub_admin->first_name = $request->input('sub_admin_name');
            $sub_admin->last_name=$request->input('sub_admin_lname');
            $sub_admin->user_name = 'sub_admin';
            $sub_admin->principal_type_xid = 2;
            $sub_admin->principal_source_xid = Auth::guard('admin')->user()->principal_source_xid;
            $sub_admin->email_address = $request->input('sub_admin_email');
            $sub_admin->password = bcrypt($request->input('password'));
            $sub_admin->save();


            $moduleIds = $request->input('module_id');

            foreach ($moduleIds as $moduleId) {

                $sub_admin_permission = new ManageModuleLink;
                $sub_admin_permission->principal_xid = $sub_admin->id;
                $sub_admin_permission->manage_modules_xid = $moduleId;
                $sub_admin_permission->save();

            }

            $mailData =[
                'username'=>$request->input('sub_admin_name'),
                'password'=>$request->input('password'),
            ];



            // Mail::to($sub_admin->email_address)->send(new Add_Subadmin($mailData));
            $mail = Mail::to($request->input('sub_admin_email'))->send(new Add_Subadmin($mailData));
            // dd($mail);

            // DB::commit();
            return jsonResponseWithSuccessMessage(__('success.save_data'));
            // return response()->json(['status'=>200]);
            // return $voucher_data;
        } catch (Exception $e) {
            DB::rollBack();
            Log::error("restaurant Store Page Load Failed " . $e->getMessage());
            return jsonResponseWithErrorMessage(__('auth.something_went_wrong'), 500);
        }
    }
    public function edit($id)
    {
        // dd($id);
        $sub_admins_module = ManageModule::latest()->get();
        $edit_sub_admin = IamPrincipal::with(['moduleLinks' => function ($query) {
            $query->with('module');
        }])->find($id);
        return view('Admin.pages.manage_users.manage_sub_admin.edit', compact('edit_sub_admin', 'sub_admins_module'));
    }

    public function delete_sub_admin($id)
    {
        /*
            Created By : Megha
            Created at : 14 Feb 2024
            Use : To Delete Admin.
        */
        try {
            DB::beginTransaction();

            $passport = IamPrincipal::find($id);
            $passport->delete();

            DB::commit();

            return response()->json(['success' => true, 'status' => 200]);
        } catch (Exception $e) {
            DB::rollBack();
            Log::error("delete_passport function Load Failed " . $e->getMessage());
            return response()->json(['success' => false, 'status' => 500, 'message' => __('auth.something_went_wrong')]);
        }
    }

    public function update_subadmin(Request $request)
    {
        /*
        Created By : Megha
        Created at : 14 Feb 2024
        Use : To update sub admin form.
        */
        try {
            DB::beginTransaction();

            $sub_admin = IamPrincipal::find($request->sub_admin_id);
            $sub_admin->user_name = 'sub_admin';
            $sub_admin->first_name = $request->input('sub_admin_name');
            $sub_admin->last_name = $request->input('sub_admin_lname');
            $sub_admin->principal_type_xid = 2;
            $sub_admin->principal_source_xid = Auth::guard('admin')->user()->principal_source_xid;
            $sub_admin->email_address = $request->input('sub_admin_email');
            // $sub_admin->password = bcrypt($request->input('password'));
            $sub_admin->save();

            $moduleIds = $request->input('module_id');
            // dd($moduleIds);
            $update_module = ManageModuleLink::where('principal_xid', $sub_admin->id)->delete();

            foreach ($moduleIds as $moduleId) {
                $sub_admin_permission = new ManageModuleLink;
                $sub_admin_permission->principal_xid = $sub_admin->id;
                $sub_admin_permission->manage_modules_xid = $moduleId;
                $sub_admin_permission->save();
            }


            DB::commit();
            return jsonResponseWithSuccessMessage(__('success.save_data'));
            // return $voucher_data;
        } catch (Exception $e) {
            DB::rollBack();
            Log::error("restaurant Store Page Load Failed " . $e->getMessage());
            return jsonResponseWithErrorMessage(__('auth.something_went_wrong'), 500);
        }
    }

    public function change_admin_status(Request $request)
    {
        /*
        Created By : Megha
        Created at : 14 Feb 2024
        Use : To change passport status.
        */
        try {
            DB::beginTransaction();
            $status = IamPrincipal::find($request->passport_id);
            $status->is_active = $request->status;
            $status->save();
            DB::commit();

            return jsonResponseWithSuccessMessage(__('success.update_data'));
        } catch (Exception $e) {
            Log::error("Update Status function Load Failed " . $e->getMessage());
            return jsonResponseWithErrorMessage(__('auth.something_went_wrong'), 500);
        }
    }

    public function get_sub_admin_permission(Request $request)
    {
        $testing_admin_id = $request->id;
        $test = ManageModuleLink::where('principal_xid', $testing_admin_id)->get('manage_modules_xid')->toArray();
        return response()->json(['success' => true, 'data' => $test]);
    }


}
