<?php

namespace App\Http\Controllers;

use App\Models\Log;
use App\Models\EBM;
use Illuminate\Http\Request;
use Illuminate\Http\Response;
use Illuminate\Support\Facades\Crypt;
use Illuminate\Support\Facades\Redirect;
use Illuminate\Support\Facades\Session;
use Illuminate\Support\Facades\Validator;
use Illuminate\Support\Facades\View;
use Auth;
use DB;
use File;

class EBMVerificationController extends BenthamController
{

    public function index(Request $request, $page = "", $id = "")
    {
        $pending_editors_list = "";
        $approve_editors_list = "";
        $reject_editors_list  = "";
        $param['paginate']    = 10;
        $param['email']       = trim($request->get('email'));

        if ($page == 'download_cv') {

            $cv_path = 'ebm_cv/' . $id;
            $cv_file_path = config('constants.admin.ebm_root_path') . $cv_path;

            return response()->file($cv_file_path);
        } elseif ($page == 'download_email_accp') {

            $email_accp_path = 'ebm_acceptance_email/' . $id;
            $email_accp_file_path = config('constants.admin.ebm_root_path') . $email_accp_path;

            return response()->file($email_accp_file_path);
        } elseif ($page == 'download_eic_approval') {

            $eic_approval_path = 'eic_approval/' . $id;
            $eic_approval_file_path = config('constants.admin.ebm_root_path') . $eic_approval_path;

            return response()->file($eic_approval_file_path);
        } elseif ($page == 'download_director_approval') {

            $director_approval_path = 'director_approval/' . $id;
            $director_approval_file_path = config('constants.admin.ebm_root_path') . $director_approval_path;

            return response()->file($director_approval_file_path);
        } elseif ($page == 'download_agreement_form') {

            $agreement_form_path = 'agreement_form/' . $id;
            $agreement_file_path = config('constants.admin.ebm_root_path') . $agreement_form_path;

            return response()->file($agreement_file_path);
        } elseif ($page == "approve_list") {
            $param['verification_status'] = 'approve';
            $approve_editors_list = EBM::GetApproveorRejectListofEBM($param);
        } elseif ($page == "reject_list") {
            $param['verification_status'] = 'reject';
            $reject_editors_list = EBM::GetApproveorRejectListofEBM($param);
        } else {
            $page = "pending_list";
            $param['verification_status'] = 'pending';
            $pending_editors_list = EBM::GetVerificationEditorsList($param);
            // dd($pending_editors_list);
        }

        return view('admin.ebm_verification.verification_list', ['pending_editors_list' => $pending_editors_list, 'approve_editors_list' => $approve_editors_list, 'reject_editors_list' => $reject_editors_list, 'page' => $page, 'param' => $param]);
    }

    public function EditorVerification(Request $request)
    {
        $verification_status = $request->get('status');

        $verification_id = $request->get('verificationId');
        $editor_id = $request->get('editorId');
        $editor_comment = $request->get('editorcomment');

        $ebm_id = $request->get('ebmeditorId');
        $ebmverificationId = $request->get('ebmverificationId');
        $ebm_comment = $request->get('ebmcomment');

        if ($request->get('dataType') == 'Delete Editor' || $request->get('dataType') == 'Editor Status Active' || $request->get('dataType') == 'Editor Status Block' || $request->get('dataType') == 'Edit Editor') {

            $data = array(
                'verification_status'   => $verification_status,
                'updated_on'            => now(),
                'updated_by'            => Auth::user()->id,
                'editor_comment'        => $editor_comment,
            );

            $verification_update = EBM::UpdateEditorVerification($verification_id, $data);
        }

        if ($request->get('dataType') == 'Add Editor Role' || $request->get('dataType') == 'Delete Editor Role' || $request->get('dataType') == 'Edit Editor Role') {

            $data = array(
                'ebm_verification_status'   => $verification_status,
                'updated_on'                => now(),
                'updated_by'                => Auth::user()->id,
                'ebm_comment'               => $ebm_comment,

            );

            $ebm_verification_update = EBM::UpdateEbmVerification($ebmverificationId, $data);
        }

        if ($request->get('dataType') == 'Delete Editor') {

            if ($verification_status == 'approve') {

                $data = EBM::GetEditorbyID($editor_id);
                $arrayData = (array) $data;

                $editor_delete = EBM::AddEditorHistory($editor_id, "D");

                $editor_delete_history = EBM::DeleteEditor($editor_id);

                $add_log = array(
                    'content_id'      => $editor_id,
                    'content_type_id' => config('constants.log_content_types.EditorialBoardMember'),
                    'action'          => config('constants.log_action_types.delete'),
                    'log_data'        => json_encode($arrayData),
                    'created_dated'   => time(),
                    'created_by'      => Auth::user()->id,
                    'ip'              => ip2long($request->ip()),
                );
                $log = Log::AddLog($add_log);

                return redirect()->route('ebm_verification')->with('success', 'Editor has been delete.');
            } else {
                return redirect()->route('ebm_verification')->with('success', 'Request to delete editor has been rejected.');
            }
        } elseif ($request->get('dataType') == 'Editor Status Active' || $request->get('dataType') == 'Editor Status Block') {

            if ($verification_status == 'approve') {

                if ($request->get('dataType') == 'Editor Status Active') {
                    $editor_status = EBM::ChangeEditorStatus($editor_id, 'A');
                } elseif ($request->get('dataType') == 'Editor Status Block') {
                    $editor_status = EBM::ChangeEditorStatus($editor_id, 'B');
                }

                return redirect()->route('ebm_verification')->with('success', 'Editor status has been change.');
            } else {
                return redirect()->route('ebm_verification')->with('success', 'Request to change editor status has been rejected.');
            }
        } elseif ($request->get('dataType') == 'Edit Editor') {

            if ($verification_status == 'approve') {

                $editor_changes = $request->get('editorChanges');
                $editor_changes = strpos($editor_changes, "email") !== false ? $editor_changes : $editor_changes . ",email";

                $editor = EBM::GetVerificationEditorbyID($verification_id, $editor_changes);
                $arrayData = (array) $editor;
                $arrayData['updated_on'] = now();
                $arrayData['updated_by'] = Auth::user()->id;

                $update_editor = EBM::UpdateEditor($arrayData, $editor_id);

                $update_editor_history = EBM::AddEditorHistory($editor_id, "U");

                $add_log = array(
                    'content_id'      => $editor_id,
                    'content_type_id' => config('constants.log_content_types.EditorialBoardMember'),
                    'action'          => config('constants.log_action_types.update'),
                    'log_data'        => json_encode($arrayData),
                    'created_dated'   => time(),
                    'created_by'      => Auth::user()->id,
                    'ip'              => ip2long($request->ip()),
                );
                $log = Log::AddLog($add_log);

                $reject_status = EBM::UpdateEditorRejectStatus($editor->email, $request->get('dataType'));

                return redirect()->route('ebm_verification')->with('success', 'Editor has been updated.');
            } else {

                return redirect()->route('ebm_verification')->with('success', 'Request for changes in editor has been rejected.');
            }
        } elseif ($request->get('dataType') == 'editor_changes_modal') {

            $editorChanges = $request->get('editorChanges');

            $editorChanges = str_replace(',profile_pic_type', '', $editorChanges);

            $data1 = EBM::GetEditorbyID($editor_id, $editorChanges);
            $data2 = EBM::GetVerificationEditorbyID($verification_id, $editorChanges);

            $editor_changes = '<div class="modal-header border-bottom">
            <h2 class="modal-title fw-bolder text-center">Editor Changes</h2>
            </div>
            <div class="modal-body p-0 p-l-20 p-r-20">';

            foreach ($data1 as $fieldName => $fieldValue1) {
                $fieldValue2 = $data2->$fieldName ?? null;

                $fieldNameDisplay = ucfirst(str_replace('_', ' ', $fieldName));

                if (empty($fieldValue1)) {
                    $fieldValue1Display = 'N/A';
                } else {
                    $fieldValue1Display = $fieldValue1;
                }

                if (empty($fieldValue2)) {
                    $fieldValue2Display = 'N/A';
                } else {
                    $fieldValue2Display = $fieldValue2;
                }

                if ($fieldName === 'profile_pic') {
                    if ($fieldValue1Display != 'N/A') {
                        $imagePath1 = DisplayImage(config("constants.admin.ebm_root_path") . 'ebm_pic/' . $fieldValue1Display);
                        $fieldValue1Display = '<div class="fileinput-new"><img src="' . $imagePath1 . '" class="img-responsive thumbnail" style="height: 150px;"></div>';
                    }

                    if ($fieldValue2Display != 'N/A') {
                        $imagePath2 = DisplayImage(config("constants.admin.ebm_root_path") . 'ebm_pic/' . $fieldValue2Display);
                        $fieldValue2Display = '<div class="fileinput-new"><img src="' . $imagePath2 . '" class="img-responsive thumbnail" style="height: 150px;"></div>';
                    }
                }

                $editor_changes .= '<h3 class="m-0 m-t-5"><span class="label d-block label-default">' . $fieldNameDisplay . '</span></h3>
                <div class="row m-b-10 m-t-10">
                <div class="col-md-6" style="border-right:1px solid #333"><strong class="fw-bold text-large" style="background-color: #ffd8d8;">Current Value:</strong><br> ' . $fieldValue1Display . '</div>
                <div class="col-md-6"><strong class="fw-bold text-large" style="background-color: #fff4c5;">Request to Change:</strong><br> ' . $fieldValue2Display . '</div>
                </div>';
            }

            $editor_changes .= '</div>
            <div class="modal-footer">
                <button type="button" class="btn btn-light" data-dismiss="modal">
                    Close
                </button>
            </div>';

            return $editor_changes;
        } elseif ($request->get('dataType') == 'ebm_role_list') {

            $ebm_role = EBM::GetEbmRolebyEditorID($editor_id, 'active');
            $ebm_new_role = EBM::GetVerificationEbmRolebyID($ebmverificationId);

            $ebm_role_list = '<div class="modal-header border-bottom">';

            if (!empty($ebm_new_role)) {
                $ebm_role_list .= '<h3 class="modal-title fw-bolder text-center bg-success">Editor Previous Role List</h3>';
            } else {
                $ebm_role_list .= '<h3 class="modal-title fw-bolder text-center bg-success">Editor Role List</h3></div>';
            }

            $ebm_role_list .= '<div class="modal-body p-0 p-l-20 p-r-20">';

            if (!$ebm_role->isEmpty()) {
                foreach ($ebm_role as $ebm_roles) {

                    $ebm_role_delete = DB::table('tbl_ebm_verification as e')->select('e.*')->where('e.ebm_id', '=', $ebm_roles->ebm_id)->where('e.ebm_verification_status', '=', 'pending')->where('e.ebm_verification_comment', '=', 'Delete Editor Role')->first();

                    $ebm_role_list .= '<div class="row border-bottom"><div class="col-md-2" style="border-right:1px solid #333;">
                                <h4 class="fw-bold text-large"><strong>Journal Code</strong></h4>
                            </div>';

                    if (empty($ebm_role_delete)) {
                        $ebm_role_list .= '<div class="col-md-10">' . $ebm_roles->subtitle . '</div></div>';
                    } else {
                        $ebm_role_list .= '<div class="col-md-10">' . $ebm_roles->subtitle . '<strong class="fw-bold text-large" style="background-color: #ffd8d8;"> (Delete Request)</strong></div></div>';
                    }

                    if (!empty($ebm_role_delete->role_delete_comment)) {
                        $ebm_role_list .= '<div class="row border-bottom"><div class="col-md-2" style="border-right:1px solid #333;">
                        <h4 class="fw-bold text-large"><strong>Comment</strong></h4>
                        </div>
                        <div class="col-md-10">' . $ebm_role_delete->role_delete_comment . '</div></div>';
                    }

                    $ebm_role_list .= '<div class="row border-bottom"><div class="col-md-2" style="border-right:1px solid #333;">
                                <h4 class="fw-bold text-large"><strong>Role</strong></h4>
                            </div>';

                    $ebm_role_list .= '<div class="col-md-10">' . $ebm_roles->nicename . '</div></div>';

                    $ebm_role_list .= '<div class="row border-bottom"><div class="col-md-2" style="border-right:1px solid #333;">
                            <h4 class="fw-bold text-large"><strong>Attachements</strong></h4>
                        </div>';

                    $ebm_role_list .= '<div class="col-md-10">';
                    if (!empty($ebm_roles->email_accp_name)) {
                        $ebm_role_list .= '<a href="' . route("ebm_verification", ["page" => "download_email_accp", "id" => $ebm_roles->email_accp_name]) . '" class="btn btn-info btn-sm" title="Download Acceptance Email" style="background-color:#009688; margin-right:10px;" target="_blank">
                            <span class="glyphicon glyphicon-download-alt" aria-hidden="true"></span> Acceptance Email
                            </a>';
                    }
                    if (!empty($ebm_roles->eic_approval)) {
                        $ebm_role_list .= '<a href="' . route("ebm_verification", ["page" => "download_eic_approval", "id" => $ebm_roles->eic_approval]) . '" class="btn btn-info btn-sm" title="Download EIC Approval" style="background-color:#009688; margin-right:10px;" target="_blank">
                            <span class="glyphicon glyphicon-download-alt" aria-hidden="true"></span> EIC Approval
                            </a>';
                    }
                    if (!empty($ebm_roles->director_approval)) {
                        $ebm_role_list .= '<a href="' . route("ebm_verification", ["page" => "download_director_approval", "id" => $ebm_roles->director_approval]) . '" class="btn btn-info btn-sm" title="Download Dr.Sahab Approval" style="background-color:#009688; margin-right:10px;" target="_blank">
                            <span class="glyphicon glyphicon-download-alt" aria-hidden="true"></span> Dr.Sahab Approval
                            </a>';
                    }
                    if (!empty($ebm_roles->cv_name)) {
                        $ebm_role_list .= '<a href="' . route("ebm_verification", ["page" => "download_cv", "id" => $ebm_roles->cv_name]) . '" class="btn btn-info btn-sm" title="Download CV" style="background-color:#009688; margin-right:10px;" target="_blank">
                            <span class="glyphicon glyphicon-download-alt" aria-hidden="true"></span> Download CV
                            </a>';
                    }
                    if (!empty($ebm_roles->agreement_form_name)) {
                        $ebm_role_list .= '<a href="' . route("ebm_verification", ["page" => "download_agreement_form", "id" => $ebm_roles->agreement_form_name]) . '" class="btn btn-info btn-sm" title="Download Agreement Form" style="background-color:#009688; margin-right:10px;" target="_blank">
                            <span class="glyphicon glyphicon-download-alt" aria-hidden="true"></span> Agreement Form
                            </a>';
                    }
                    $ebm_role_list .= '</div></div>';
                }
            } else {
                $ebm_role_list .= '<div class="col-md-12 p-t-10 p-b-10 text-center border-bottom">No records found</div>';
            }

            if (!empty($ebm_new_role)) {
                $ebm_role_list .= '<div class="col-md-12 border-bottom bg-info p-t-10 m-t-20 m-b-5">
                <h3 class="text-center m-0 p-b-5">Editor New Role (Request to Add)</h3>
                    </div>
                <div class="col-md-2 border-bottom" style="border-right:1px solid #333">
                        <h4 class="fw-bold text-large"><strong>Journal</strong></h4>
                </div>
                <div class="col-md-4 border-bottom" style="border-right:1px solid #333">
                        <h4 class="fw-bold text-large"><strong>Role</strong></h4>
                </div>
                <div class="col-md-6 border-bottom">
                        <h4 class="fw-bold text-large"><strong>Section</strong></h4>
                </div>
                <div class="col-md-2 border-bottom m-b-10" style="border-right:1px solid #333">
                        <div class="fw-bold text-large p-t-10 p-b-10">' . $ebm_new_role->subtitle . '</div>
                </div>
                <div class="col-md-4 border-bottom m-b-10" style="border-right:1px solid #333">
                        <div class="fw-bold text-large p-t-10 p-b-10">' . $ebm_new_role->nicename . '</div>
                </div>
                <div class="col-md-6 border-bottom m-b-10">';
                if(!empty($ebm_new_role->section)){
                    $ebm_role_list .= '<div class="fw-bold text-large p-t-10 p-b-10">' . $ebm_new_role->section . '</div>';
                }else{
                    $ebm_role_list .= '<div class="fw-bold text-large p-t-10 p-b-10">Empty</div>';
                }
                $ebm_role_list .= '</div>
                <div class="col-md-12 p-b-20 m-t-10 border-bottom"><strong>Attachments: </strong>';
                if (!empty($ebm_new_role->cv_name)) {
                    $ebm_role_list .= '
                    <a href="' . route("ebm_verification", ["page" => "download_cv", "id" => $ebm_new_role->cv_name]) . '" class="btn btn-info btn-sm" title="Download cv" style="background-color:#009688;" target="_blank">
                    <span class="glyphicon glyphicon-download-alt" aria-hidden="true"></span>Download CV
                    </a>
                ';
                }
                if (!empty($ebm_new_role->email_accp_name)) {
                    $ebm_role_list .= '
                    <a href="' . route("ebm_verification", ["page" => "download_email_accp", "id" => $ebm_new_role->email_accp_name]) . '" class="btn btn-info btn-sm" title="Download Acceptance Email" style="background-color:#009688;" target="_blank">
                    <span class="glyphicon glyphicon-download-alt" aria-hidden="true"></span>Acceptance Email
                    </a>
                ';
                }
                if (!empty($ebm_new_role->eic_approval)) {
                    $ebm_role_list .= '
                    <a href="' . route("ebm_verification", ["page" => "download_eic_approval", "id" => $ebm_new_role->eic_approval]) . '" class="btn btn-info btn-sm" title="Download EIC Approval" style="background-color:#009688;" target="_blank"><span class="	glyphicon glyphicon-download-alt" aria-hidden="true"></span> EIC Approval
                    </a>
                ';
                }
                if (!empty($ebm_new_role->director_approval)) {
                    $ebm_role_list .= '
                    <a href="' . route("ebm_verification", ["page" => "download_director_approval", "id" => $ebm_new_role->director_approval]) . '" class="btn btn-info btn-sm" title="Download Dr.Sahab Approval" style="background-color:#009688;" target="_blank"><span class="	glyphicon glyphicon-download-alt" aria-hidden="true"></span> Dr.Sahab Approval
                    </a>
                ';
                }
                if (!empty($ebm_new_role->agreement_form_name)) {
                    $ebm_role_list .= '
                    <a href="' . route("ebm_verification", ["page" => "download_agreement_form", "id" => $ebm_new_role->agreement_form_name]) . '" class="btn btn-info btn-sm" title=" Download Agreement Form" style="background-color:#009688;" target="_blank"><span class="	glyphicon glyphicon-download-alt" aria-hidden="true"></span> Agreement Form
                    </a>
                ';
                }
            }

            $ebm_role_list .= '</div></div>';

            $ebm_role_list .= '<div class="modal-footer border-top">
                    <button type="button" class="btn btn-light m-t-5" data-dismiss="modal">
                        Close
                    </button>
                </div>';

            return $ebm_role_list;
        } elseif ($request->get('dataType') == 'Add Editor Role') {

            if ($verification_status == 'approve') {

                $ebm_update = EBM::GetEbmVerificationEditorbyID($ebmverificationId);
                $arrayData = (array) $ebm_update;
                $arrayData['updated_on'] = now();
                $arrayData['updated_by'] = Auth::user()->id;

                $data['ebm_id'] = $ebm_role_add = EBM::AddEditorRole($arrayData);

                $ebm_verification_update = EBM::UpdateEbmVerification($ebmverificationId, $data);

                $ebm_role_history = EBM::InsertEBMRoleHistory($ebm_role_add, "A");

                $add_log = array(
                    'content_id'      => $ebm_role_add,
                    'content_type_id' => config('constants.log_content_types.EditorialBoardMember'),
                    'action'          => config('constants.log_action_types.insert'),
                    'log_data'        => json_encode($data),
                    'created_dated'   => time(),
                    'created_by'      => Auth::user()->id,
                    'ip'              => ip2long($request->ip()),
                );
                $log = Log::AddLog($add_log);

                $reject_status = EBM::UpdateEditorRoleRejectStatus($ebm_update->editor_id, $request->get('dataType'));

                return redirect()->route('ebm_verification')->with('success', 'Editor role has been added.');
            } else {
                return redirect()->route('ebm_verification')->with('success', 'Request to add editor role has been rejected.');
            }
        } elseif ($request->get('dataType') == 'Delete Editor Role') {

            if ($verification_status == 'approve') {

                $ebm = EBM::GetEbmVerificationEditorbyID($ebmverificationId);
                $arrayData = (array) $ebm;

                $ebm_role_history = EBM::InsertEBMRoleHistory($ebm->ebm_id, "D");

                $ebm_role_delete = EBM::DeleteEBMRolebyID($ebm->ebm_id);

                $add_log = array(
                    'content_id'      => $ebm->ebm_id,
                    'content_type_id' => config('constants.log_content_types.EditorialBoardMember'),
                    'action'          => config('constants.log_action_types.delete'),
                    'log_data'        => json_encode($arrayData),
                    'created_dated'   => time(),
                    'created_by'      => Auth::user()->id,
                    'ip'              => ip2long($request->ip()),
                );
                $log = Log::AddLog($add_log);

                return redirect()->route('ebm_verification')->with('success', 'Editor has been delete.');
            } else {
                return redirect()->route('ebm_verification')->with('success', 'Request to delete editor has been rejected.');
            }
        } elseif ($request->get('dataType') == 'Edit Editor Role') {

            if ($verification_status == 'approve') {

                $ebm_changes = rtrim($request->get('ebmChanges'), ',');
                $ebm_changes = strpos($ebm_changes, "editor_id") !== false ? $ebm_changes : $ebm_changes . ",editor_id";

                $ebm = EBM::GetEbmVerificationEditorbyID($ebmverificationId, $ebm_changes);
                $arrayData = (array) $ebm;
                $arrayData['updated_on'] = now();
                $arrayData['updated_by'] = Auth::user()->id;

                $ebm_role_update = EBM::UpdateEditorRole($arrayData, $ebm_id);
                $ebm_role_history = EBM::InsertEBMRoleHistory($ebm_id, "U");

                $add_log = array(
                    'content_id'      => $ebm_id,
                    'content_type_id' => config('constants.log_content_types.EditorialBoardMember'),
                    'action'          => config('constants.log_action_types.update'),
                    'log_data'        => json_encode($arrayData),
                    'created_dated'   => time(),
                    'created_by'      => Auth::user()->id,
                    'ip'              => ip2long($request->ip()),
                );
                $log = Log::AddLog($add_log);

                $reject_status = EBM::UpdateEditorRoleRejectStatus($ebm->editor_id, $request->get('dataType'));

                return redirect()->route('ebm_verification')->with('success', 'Editor role changes has been updated.');
            } else {

                return redirect()->route('ebm_verification')->with('success', 'Request for changes in editor role has been rejected.');
            }
        } elseif ($request->get('dataType') == 'ebm_changes_modal') {

            $ebmChanges = $request->get('ebmChanges');

            $toRemove = array('director_approval_content_type,', 'director_approval_updated_on,', 'eic_approval_content_type,', 'eic_approval_updated_on,', 'agreement_form_content_type,', 'agreement_form_updated_on,', 'email_content_type,', 'email_accp_created_on,', 'cv_content_type,', 'cv_updated_on,');

            $ebmChanges = str_replace($toRemove, '', $ebmChanges);
            $ebmChanges = ltrim($ebmChanges, ',');
            $ebmChanges = rtrim($ebmChanges, ',');

            $ebmChangesArray = explode(',', $ebmChanges);

            $data1 = EBM::GetEBMbyID($ebm_id, $ebmChanges);
            $data2 = EBM::GetEbmVerificationEditorbyID($ebmverificationId, $ebmChanges);
            $data3 = EBM::GetEBMListbyEBMId($ebm_id);

            $ebm_changes = '<div class="modal-header border-bottom">
            <h2 class="modal-title fw-bolder text-center">EBM Changes</h2>
            </div>
            <div class="modal-body p-0 p-l-20 p-r-20">';

            $ebm_changes .= '<div class="modal-body p-0 p-l-20 p-r-20">
                <div class="row border-bottom">
                    <div class="col-md-6" style="border-right:1px solid #333;">
                        <h4 class="fw-bold text-large text-center border-bottom p-b-10"><strong>Journal Code</strong></h4>
                        <h5 class="fw-bold text-large text-center">' . $data3->subtitle . '</h5>
                    </div>
                    <div class="col-md-6">
                        <h4 class="fw-bold text-large text-center border-bottom p-b-10"><strong>Role</strong></h4>
                        <h5 class="fw-bold text-large text-center">' . $data3->nicename . '</h5>
                    </div>
                </div>';

            foreach ($data1 as $fieldName => $fieldValue1) {
                $fieldValue2 = $data2->$fieldName ?? null;

                $fieldNameDisplay = ucfirst(str_replace('_', ' ', $fieldName));

                if (empty($fieldValue1)) {
                    $fieldValue1Display = 'N/A';
                } else {
                    $fieldValue1Display = $fieldValue1;
                }

                if (empty($fieldValue2)) {
                    $fieldValue2Display = 'N/A';
                } else {
                    $fieldValue2Display = $fieldValue2;
                }

                if ($fieldName === 'agreement_form_name') {
                    if ($fieldValue1Display != 'N/A') {
                        $fieldValue1Display = '<a href="' . route("ebm_verification", ["page" => "download_agreement_form", "id" => $fieldValue1]) . '" class="btn btn-info btn-md" title=" Download Agreement Form" style="background-color:#009688;"><span class="glyphicon glyphicon-download-alt" aria-hidden="true"></span> Download
                        </a>';
                    }

                    if ($fieldValue2Display != 'N/A') {
                        $fieldValue2Display = '<a href="' . route("ebm_verification", ["page" => "download_agreement_form", "id" => $fieldValue2]) . '" class="btn btn-info btn-md" title=" Download Agreement Form" style="background-color:#009688;"><span class="glyphicon glyphicon-download-alt" aria-hidden="true"></span> Download
                        </a>';
                    }
                } elseif ($fieldName === 'email_accp_name') {
                    if ($fieldValue1Display != 'N/A') {
                        $fieldValue1Display = '<a href="' . route("ebm_verification", ["page" => "download_email_accp", "id" => $fieldValue1]) . '" class="btn btn-info btn-md" title=" Download Agreement Form" style="background-color:#009688;"><span class="glyphicon glyphicon-download-alt" aria-hidden="true"></span> Download
                        </a>';
                    }

                    if ($fieldValue2Display != 'N/A') {
                        $fieldValue2Display = '<a href="' . route("ebm_verification", ["page" => "download_email_accp", "id" => $fieldValue2]) . '" class="btn btn-info btn-md" title=" Download Agreement Form" style="background-color:#009688;"><span class="glyphicon glyphicon-download-alt" aria-hidden="true"></span> Download
                        </a>';
                    }
                } elseif ($fieldName === 'eic_approval') {
                    if ($fieldValue1Display != 'N/A') {
                        $fieldValue1Display = '<a href="' . route("ebm_verification", ["page" => "download_eic_approval", "id" => $fieldValue1]) . '" class="btn btn-info btn-md" title=" Download Agreement Form" style="background-color:#009688;"><span class="glyphicon glyphicon-download-alt" aria-hidden="true"></span> Download
                        </a>';
                    }

                    if ($fieldValue2Display != 'N/A') {
                        $fieldValue2Display = '<a href="' . route("ebm_verification", ["page" => "download_eic_approval", "id" => $fieldValue2]) . '" class="btn btn-info btn-md" title=" Download Agreement Form" style="background-color:#009688;"><span class="glyphicon glyphicon-download-alt" aria-hidden="true"></span> Download
                        </a>';
                    }
                } elseif ($fieldName === 'director_approval') {
                    if ($fieldValue1Display != 'N/A') {
                        $fieldValue1Display = '<a href="' . route("ebm_verification", ["page" => "download_director_approval", "id" => $fieldValue1]) . '" class="btn btn-info btn-md" title=" Download Agreement Form" style="background-color:#009688;"><span class="glyphicon glyphicon-download-alt" aria-hidden="true"></span> Download
                        </a>';
                    }

                    if ($fieldValue2Display != 'N/A') {
                        $fieldValue2Display = '<a href="' . route("ebm_verification", ["page" => "download_director_approval", "id" => $fieldValue2]) . '" class="btn btn-info btn-md" title=" Download Agreement Form" style="background-color:#009688;"><span class="glyphicon glyphicon-download-alt" aria-hidden="true"></span> Download
                        </a>';
                    }
                } elseif ($fieldName === 'cv_name') {
                    if ($fieldValue1Display != 'N/A') {
                        $fieldValue1Display = '<a href="' . route("ebm_verification", ["page" => "download_cv", "id" => $fieldValue1]) . '" class="btn btn-info btn-md" title=" Download Agreement Form" style="background-color:#009688;"><span class="glyphicon glyphicon-download-alt" aria-hidden="true"></span> Download
                        </a>';
                    }

                    if ($fieldValue2Display != 'N/A') {
                        $fieldValue2Display = '<a href="' . route("ebm_verification", ["page" => "download_cv", "id" => $fieldValue2]) . '" class="btn btn-info btn-md" title=" Download Agreement Form" style="background-color:#009688;"><span class="glyphicon glyphicon-download-alt" aria-hidden="true"></span> Download
                        </a>';
                    }
                } elseif ($fieldName === 'ebm_role_id') {
                    if ($fieldValue1Display != 'N/A') {
                        $role =  DB::table('tbl_ebm_role')->select('*')->where('role_id', $fieldValue1)->first();
                        $fieldValue1Display =  $role->nicename;
                    }

                    if ($fieldValue2Display != 'N/A') {
                        $role =  DB::table('tbl_ebm_role')->select('*')->where('role_id', $fieldValue2)->first();
                        $fieldValue2Display =  $role->nicename;
                    }
                } elseif ($fieldName === 'journal_id') {
                    if ($fieldValue1Display != 'N/A') {
                        $role =  DB::table('journal')->select('*')->where('journal_id', $fieldValue1)->first();
                        $fieldValue1Display =  $role->subtitle;
                    }

                    if ($fieldValue2Display != 'N/A') {
                        $role =  DB::table('journal')->select('*')->where('journal_id', $fieldValue2)->first();
                        $fieldValue2Display =  $role->subtitle;
                    }
                } elseif ($fieldName === 'region_id') {
                    if ($fieldValue1Display != 'N/A') {
                        $region =  DB::table('tbl_regions')->select('*')->where('region_id', $fieldValue1)->first();
                        $fieldValue1Display =  $region->name;
                    }

                    if ($fieldValue2Display != 'N/A') {
                        $region =  DB::table('tbl_regions')->select('*')->where('region_id', $fieldValue2)->first();
                        $fieldValue2Display =  $region->name;
                    }
                }

                $ebm_changes .= '<h3 class="m-0 m-t-5"><span class="label d-block label-default">' . $fieldNameDisplay . '</span></h3>
                <div class="row m-b-10 m-t-10">
                <div class="col-md-6" style="border-right:1px solid #333; white-space: normal; word-wrap: break-word;"><strong class="fw-bold text-large" style="background-color: #ffd8d8;">Current Value:</strong><br>' . $fieldValue1Display . '</div>
                <div class="col-md-6" style="white-space: normal; word-wrap: break-word;"><strong class="fw-bold text-large" style="background-color: #fff4c5;">Request to Change:</strong><br>' . $fieldValue2Display . '</div>
                </div>';
            }

            if (!in_array('resign_email_text', $ebmChangesArray) && !empty($data3->resign_email_text) && in_array('ebm_status', $ebmChangesArray) && ($fieldValue2Display == 'resign' || $fieldValue2Display == 'terminate')) {
                $ebm_changes .= '<h3 class="m-0 m-t-5"><span class="label d-block label-default">Resign Email Text</span></h3>
                <div class="row m-b-10 m-t-10">
                <div class="col-md-12" style="white-space: normal; word-wrap: break-word;">' . $data3->resign_email_text . '</div>
                </div>';
            }

            $ebm_changes .= '</div>
            <div class="modal-footer">
                <button type="button" class="btn btn-light" data-dismiss="modal">
                    Close
                </button>
            </div>';

            return $ebm_changes;
        } elseif ($request->get('dataType') == 'view_logs') {

            $param['editor_id'] = $editor_id;
            $log_list = EBM::GetApproveorRejectListofEBM($param);

            return view('admin.ebm_verification.logs_list', ['log_list' => $log_list]);
        }
    }
}
