<?php

namespace App\Http\Controllers;

use App\Models\Log;
use App\Models\Indexing;
use App\Models\EBM;
use App\Models\Journal;
use App\Models\Manuscript;
use App\Models\Country;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\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 EBMRoleController extends BenthamController
{
    public function EditorRole(Request $request, $page = "", $id = "")
    {
        if ($page == 'list') {
            $id = $request->get('editor_id');
            $editor_roles = EBM::GetEbmRolebyEditorID($id);

            return View::make('admin.editorial_board_member.ebm_role.ebm_role_list', with([
                'editor_roles' => $editor_roles
            ]));
        }

        if ($page == 'add') {
            $param['editor_id'] = $request->get('editor_id');
            $param['name'] = $request->get('name');

            $journal_list = EBM::GetJournalList();
            $role_list = EBM::GetAllEbmRole();
            $region_list = EBM::GetAllRegions();

            return View::make('admin.editorial_board_member.ebm_role.ebm_role_add', with([
                'journal_list' => $journal_list, 'role_list' => $role_list, 'param' => $param, 'region_list' => $region_list
            ]));
        }

        if ($page == 'store') {

            $req = array();

            if (isset($request->cv_name)) {
                $req['cv_name'] = 'mimes:pdf,doc,docx';
            }
            if (isset($request->accept_email)) {
                $req['accept_email'] = 'mimes:pdf,doc,docx';
            }
            if (isset($request->agreement_form)) {
                $req['agreement_form'] = 'mimes:pdf,doc,docx';
            }
            if (isset($request->eic_approval)) {
                $req['eic_approval'] = 'mimes:pdf,doc,docx';
            }
            if (isset($request->director_approval)) {
                $req['director_approval'] = 'mimes:pdf,doc,docx';
            }

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

            if ($validator->fails()) {

                return Redirect::back()->withErrors($validator);
            } else {

                //----------------------Upload CV-------------------------------
                $cv_name_type = "";
                $CV_Name = "";
                $cv_path = 'ebm_cv/';
                $cv_file_path = config('constants.admin.ebm_root_path') . $cv_path;

                if ($request->hasFile('cv_name')) {
                    $cv_name = $request->file('cv_name');
                    $cv_name_type = $cv_name->getClientMimeType();
                    $extension = $cv_name->getClientOriginalExtension();
                    $CV_Name = $request->editor_id . '.' . $extension;

                    if (!is_dir($cv_file_path)) {
                        mkdir($cv_file_path, 0755, true);
                    }

                    $cv_name->move($cv_file_path, $CV_Name);
                }

                //----------------------Upload Acceptance Email-------------------------------
                $accept_email_type = "";
                $Accept_Email = "";
                $acceptance_email = 'ebm_acceptance_email/';
                $acceptance_email_file_path = config('constants.admin.ebm_root_path') . $acceptance_email;

                if ($request->hasFile('accept_email')) {
                    $accept_email = $request->file('accept_email');
                    $accept_email_type = $accept_email->getClientMimeType();
                    $extension = $accept_email->getClientOriginalExtension();
                    $Accept_Email = $request->editor_id . '.' . $extension;

                    if (!is_dir($acceptance_email_file_path)) {
                        mkdir($acceptance_email_file_path, 0755, true);
                    }

                    $accept_email->move($acceptance_email_file_path, $Accept_Email);
                }

                //----------------------Upload Agreement Form-------------------------------
                $agreement_form_type = "";
                $Agreement = "";
                $agreement_form = 'agreement_form/';
                $agreement_form_file_path = config('constants.admin.ebm_root_path') . $agreement_form;

                if ($request->hasFile('agreement_form')) {
                    $agreement = $request->file('agreement_form');
                    $agreement_form_type = $agreement->getClientMimeType();
                    $extension = $agreement->getClientOriginalExtension();
                    $Agreement = $request->editor_id . '.' . $extension;

                    if (!is_dir($agreement_form_file_path)) {
                        mkdir($agreement_form_file_path, 0755, true);
                    }

                    $agreement->move($agreement_form_file_path, $Agreement);
                }

                //----------------------EiC's Approval-------------------------------
                $eic_approval_form_type = "";
                $EIC_Approval = "";

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

                if ($request->hasFile('eic_approval')) {
                    $EIC_approval = $request->file('eic_approval');
                    $eic_approval_form_type = $EIC_approval->getClientMimeType();
                    $extension = $EIC_approval->getClientOriginalExtension();
                    $EIC_Approval = $request->editor_id . '.' . $extension;

                    if (!is_dir($eic_approval_file_path)) {
                        mkdir($eic_approval_file_path, 0755, true);
                    }

                    $EIC_approval->move($eic_approval_file_path, $EIC_Approval);
                }

                //----------------------Dr.Sahab Approval-------------------------------
                $director_approval_name_type = "";
                $Director_Approval = "";
                $director_approval = 'director_approval/';
                $director_approval_file_path = config('constants.admin.ebm_root_path') . $director_approval;

                if ($request->hasFile('director_approval')) {
                    $director = $request->file('director_approval');
                    $director_approval_name_type = $director->getClientMimeType();
                    $extension = $director->getClientOriginalExtension();
                    $Director_Approval = $request->editor_id . '.' . $extension;

                    if (!is_dir($director_approval_file_path)) {
                        mkdir($director_approval_file_path, 0755, true);
                    }

                    $director->move($director_approval_file_path, $Director_Approval);
                }

                //------------------------------Region---------------------------------
                if ($request->role_id == 4 && !empty($request->region_id)) {
                    $region_id = $request->region_id;
                } else {
                    $region_id = "";
                }

                //------------------------------Section---------------------------------
                if (!empty($request->section)) {
                    $section = $request->section;
                } else {
                    $section = "";
                }

                $data = array(
                    'journal_id'                       => $request->journal_id,
                    'editor_id'                        => $request->editor_id,
                    'ebm_role_id'                      => $request->role_id,
                    'region_id'                        => $region_id,
                    'section'                          => $section,
                    'conditional_ebm'                  => $request->c_ebm,
                    'executive_ge'                     => $request->executive_ge,
                    'section_region'                   => $request->section_region,
                    'acceptance_date'                  => $request->acceptance_date,
                    'acceptance_email_text'            => $request->acceptance_email,
                    'resign_date'                      => $request->resign_date,
                    'resign_email_text'                => $request->resign_email,
                    'ebm_status'                       => $request->status,
                    'source'                           => $request->source,
                    'cv_content_type'                  => $cv_name_type,
                    'cv_name'                          => $CV_Name,
                    'email_content_type'               => $accept_email_type,
                    'email_accp_name'                  => $Accept_Email,
                    'email_accp_created_on'            => time(),
                    'agreement_form_content_type'      => $agreement_form_type,
                    'agreement_form_name'              => $Agreement,
                    'eic_approval'                     => $EIC_Approval,
                    'eic_approval_content_type'        => $eic_approval_form_type,
                    'director_approval_content_type'   => $director_approval_name_type,
                    'director_approval'                => $Director_Approval,
                    'added_by'                         => Auth::user()->id,
                    'created_on'                       => time(),
                );

                $ebm_role_add = EBM::AddEditorRole($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);

                return redirect()->route('editorial_board_member.index')->with('success', 'Editor role was added.');
            }
        }

        if ($page == 'edit') {

            $editor = EBM::GetEBMRolebyID($id);
            $journal_list = EBM::GetJournalList();
            $role_list = Manuscript::GetAllEbmRole();
            $region_list = EBM::GetAllRegions();

            return View::make('admin.editorial_board_member.ebm_role.ebm_role_edit', with([
                'journal_list' => $journal_list, 'role_list' => $role_list, 'editor' => $editor, 'region_list' => $region_list
            ]));
        }

        if ($page == 'toggle-status') {
            $id = $request->get('editor_id');

            if ($request->get('toggle') == "active") {
                $editor_status = EBM::ChangeEditorStatus($id, 'A');
            }
            if ($request->get('toggle') == "block") {
                $editor_status = EBM::ChangeEditorStatus($id, 'B');
            }

            return redirect()->route('editorial_board_member.index')->with('success', 'Editor status changed.');
        }

        if ($page == 'update') {
            // dd($request);

            $req = array();

            if (isset($request->cv_name)) {
                $req['cv_name'] = 'mimes:pdf,doc,docx';
            }
            if (isset($request->accept_email)) {
                $req['accept_email'] = 'mimes:pdf,doc,docx';
            }
            if (isset($request->agreement_form)) {
                $req['agreement_form'] = 'mimes:pdf,doc,docx';
            }
            if (isset($request->eic_approval)) {
                $req['eic_approval'] = 'mimes:pdf,doc,docx';
            }
            if (isset($request->director_approval)) {
                $req['director_approval'] = 'mimes:pdf,doc,docx';
            }

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

            if ($validator->fails()) {

                return Redirect::back()->withErrors($validator);
            } else {

                //------------------------------Region---------------------------------
                if ($request->role_id == 4 && !empty($request->region_id)) {
                    $region_id = $request->region_id;
                } else {
                    $region_id = "";
                }

                //------------------------------Section---------------------------------
                if (!empty($request->section)) {
                    $section = $request->section;
                } else {
                    $section = "";
                }

                //---------------------Update Other Column----------------------
                $update_editor_role['journal_id']             = $request->journal_id;
                $update_editor_role['editor_id']              = $request->editor_id;
                $update_editor_role['ebm_role_id']            = $request->role_id;
                $update_editor_role['region_id']              = $region_id;
                $update_editor_role['section']                = $section;
                $update_editor_role['conditional_ebm']        = $request->c_ebm;
                $update_editor_role['executive_ge']           = $request->executive_ge;
                $update_editor_role['section_region']         = $request->section_region;
                $update_editor_role['acceptance_date']        = $request->acceptance_date;
                $update_editor_role['acceptance_email_text']  = $request->acceptance_email;
                $update_editor_role['resign_date']            = $request->resign_date;
                $update_editor_role['resign_email_text']      = $request->resign_email;
                $update_editor_role['ebm_status']             = $request->status;
                $update_editor_role['source']                 = $request->source;

                //----------------------Upload CV-------------------------------
                $cv_path = 'ebm_cv/';
                $cv_file_path = config('constants.admin.ebm_root_path') . $cv_path;

                if ($request->hasFile('cv_name')) {
                    $CV_Name = $request->file('cv_name');
                    $update_editor_role['cv_content_type'] = $CV_Name->getClientMimeType();
                    $extension = $CV_Name->getClientOriginalExtension();
                    $update_editor_role['cv_name'] = $request->editor_id . '.' . $extension;

                    if (!is_dir($cv_file_path)) {
                        mkdir($cv_file_path, 0755, true);
                    }

                    $CV_Name->move($cv_file_path, $update_editor_role['cv_name']);
                    $update_editor_role['cv_updated_on'] = now();
                }

                //----------------------Upload Acceptance Email-------------------------------
                $acceptance_email = 'ebm_acceptance_email/';
                $acceptance_email_file_path = config('constants.admin.ebm_root_path') . $acceptance_email;

                if ($request->hasFile('accept_email')) {
                    $accept_email = $request->file('accept_email');
                    $update_editor_role['email_content_type'] = $accept_email->getClientMimeType();
                    $extension = $accept_email->getClientOriginalExtension();
                    $update_editor_role['email_accp_name'] = $request->editor_id . '.' . $extension;

                    if (!is_dir($acceptance_email_file_path)) {
                        mkdir($acceptance_email_file_path, 0755, true);
                    }

                    $accept_email->move($acceptance_email_file_path, $update_editor_role['email_accp_name']);
                    $update_editor_role['email_accp_created_on'] = now();
                }

                //----------------------Upload Agreement Form-------------------------------
                $agreement_form = 'agreement_form/';
                $agreement_form_file_path = config('constants.admin.ebm_root_path') . $agreement_form;

                if ($request->hasFile('agreement_form')) {
                    $agreement = $request->file('agreement_form');
                    $update_editor_role['agreement_form_content_type'] = $agreement->getClientMimeType();
                    $extension = $agreement->getClientOriginalExtension();
                    $update_editor_role['agreement_form_name'] = $request->editor_id . '.' . $extension;

                    if (!is_dir($agreement_form_file_path)) {
                        mkdir($agreement_form_file_path, 0755, true);
                    }

                    $agreement->move($agreement_form_file_path, $update_editor_role['agreement_form_name']);
                    $update_editor_role['agreement_form_updated_on'] = now();
                }

                //----------------------EiC's Approval-------------------------------
                $eic_approval = 'eic_approval/';
                $eic_approval_file_path = config('constants.admin.ebm_root_path') . $eic_approval;

                if ($request->hasFile('eic_approval')) {
                    $EIC_approval = $request->file('eic_approval');
                    $update_editor_role['eic_approval_content_type'] = $EIC_approval->getClientMimeType();
                    $extension = $EIC_approval->getClientOriginalExtension();
                    $update_editor_role['eic_approval'] = $request->editor_id . '.' . $extension;

                    if (!is_dir($eic_approval_file_path)) {
                        mkdir($eic_approval_file_path, 0755, true);
                    }

                    $EIC_approval->move($eic_approval_file_path, $update_editor_role['eic_approval']);
                    $update_editor_role['eic_approval_updated_on'] = time();
                }

                //----------------------Dr.Sahab Approval-------------------------------
                $director_approval = 'director_approval/';
                $director_approval_file_path = config('constants.admin.ebm_root_path') . $director_approval;

                if ($request->hasFile('director_approval')) {
                    $director = $request->file('director_approval');
                    $update_editor_role['director_approval_content_type'] = $director->getClientMimeType();
                    $extension = $director->getClientOriginalExtension();
                    $update_editor_role['director_approval'] = $request->editor_id . '.' . $extension;

                    if (!is_dir($director_approval_file_path)) {
                        mkdir($director_approval_file_path, 0755, true);
                    }

                    $director->move($director_approval_file_path, $update_editor_role['director_approval']);
                    $update_editor_role['director_approval_updated_on'] = time();
                }

                $update_editor_role['updated_by'] = Auth::user()->id;
                $update_editor_role['updated_on'] = now();

                $ebm_role_update = EBM::UpdateEditorRole($update_editor_role, $request->ebm_id);
                $ebm_role_history = EBM::InsertEBMRoleHistory($request->ebm_id, "U");

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

                return redirect()->route('editorial_board_member.index')->with('success', 'Editor role was updated.');
            }
        }

        if ($page == 'delete') {
            $ebm_role_history = EBM::InsertEBMRoleHistory($id, "D");
            $data = EBM::GetEBMRolebyID($id);
            $ebm_role_delete = EBM::DeleteEBMRolebyID($id);

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

            return redirect()->route('editorial_board_member.index')->with('success', 'Editor role was deleted.');
        }

        if ($page == 'download_cv') {

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

            $headers = array(
                'Content-Type: application/pdf',
            );

            return Response::download($cv_file_path, $id, $headers);
        }

        if ($page == 'download_email_accp') {

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

            $headers = array(
                'Content-Type: application/pdf',
            );

            return Response::download($cv_file_path, $id, $headers);
        }

        if ($page == 'download_eic_approval') {

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

            $headers = array(
                'Content-Type: application/pdf',
            );

            return Response::download($cv_file_path, $id, $headers);
        }

        if ($page == 'download_director_approval') {

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

            $headers = array(
                'Content-Type: application/pdf',
            );

            return Response::download($cv_file_path, $id, $headers);
        }

        if ($page == 'download_agreement_form') {

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

            $headers = array(
                'Content-Type: application/pdf',
            );

            return Response::download($cv_file_path, $id, $headers);
        }
    }
}
