*/
class FormController extends Controller
{
    /**
     * Create a new controller instance.
     * Constructor to check.
     *
     * @return void
     */
    public function __construct(TicketWorkflowController $TicketWorkflowController)
    {
        $this->middleware('board');
        // creating a TicketController instance
        $this->TicketWorkflowController = $TicketWorkflowController;
    }
    /**
     * getform.
     *
     * @param type Help_topic $topic
     *
     * @return type
     */
    public function getForm(Help_topic $topic, CountryCode $code)
    {
        if (\Config::get('database.install') == '%0%') {
            return \Redirect::route('licence');
        }
        $settings = CommonSettings::select('status')->where('option_name', '=', 'send_otp')->first();
        $email_mandatory = CommonSettings::select('status')->where('option_name', '=', 'email_mandatory')->first();
        if (!\Auth::check() && ($settings->status == 1 || $settings->status == '1')) {
            return redirect('auth/login')->with(['login_require' => 'Please login to your account for submitting a ticket', 'referer' => 'form']);
        }
        $location = GeoIP::getLocation();
        $phonecode = $code->where('iso', '=', $location->iso_code)->first();
        if (System::first()->status == 1) {
            $topics = $topic->get();
            $codes = $code->get();
            if ($phonecode->phonecode) {
                $phonecode = $phonecode->phonecode;
            } else {
                $phonecode = '';
            }
            return view('themes.default1.client.helpdesk.form', compact('topics', 'codes', 'email_mandatory'))->with('phonecode', $phonecode);
        } else {
            return \Redirect::route('home');
        }
    }
    /**
     * This Function to post the form for the ticket.
     *
     * @param type Form_name    $name
     * @param type Form_details $details
     *
     * @return type string
     */
    public function postForm($id, Help_topic $topic)
    {
        if ($id != 0) {
            $helptopic = $topic->where('id', '=', $id)->first();
            $custom_form = $helptopic->custom_form;
            $values = Fields::where('forms_id', '=', $custom_form)->get();
            if (!$values) {
            }
            if ($values) {
                foreach ($values as $form_data) {
                    if ($form_data->type == 'select') {
                        $form_fields = explode(',', $form_data->value);
                        $var = '';
                        foreach ($form_fields as $form_field) {
                            $var .= '';
                        }
                        echo '
';
                    } elseif ($form_data->type == 'radio') {
                        $type2 = $form_data->value;
                        $vals = explode(',', $type2);
                        echo '
';
                        foreach ($vals as $val) {
                            echo ' '.$form_data->value.'       ';
                        }
                        echo '
';
                    } elseif ($form_data->type == 'textarea') {
                        $type3 = $form_data->value;
                        echo '
';
                    } elseif ($form_data->type == 'checkbox') {
                        $type4 = $form_data->value;
                        $checks = explode(',', $type4);
                        echo '
';
                        foreach ($checks as $check) {
                            echo '  '.$check;
                        }
                    } else {
                        echo '
';
                    }
                }
                echo '
';
            }
        } else {
            return;
        }
    }
    /**
     * Posted form.
     *
     * @param type Request $request
     * @param type User    $user
     */
    public function postedForm(User $user, ClientRequest $request, Ticket $ticket_settings, Ticket_source $ticket_source, Ticket_attachments $ta, CountryCode $code)
    {
        $form_extras = $request->except('Name', 'Phone', 'Email', 'Subject', 'Details', 'helptopic', '_wysihtml5_mode', '_token', 'mobile', 'Code');
        $name = $request->input('Name');
        $phone = $request->input('Phone');
        if ($request->input('Email')) {
            if ($request->input('Email')) {
                $email = $request->input('Email');
            } else {
                $email = null;
            }
        } else {
            $email = null;
        }
        $subject = $request->input('Subject');
        $details = $request->input('Details');
        $phonecode = $request->input('Code');
        if ($request->input('mobile')) {
            $mobile_number = $request->input('mobile');
        } else {
            $mobile_number = null;
        }
        $status = $ticket_settings->first()->status;
        $helptopic = $request->input('helptopic');
        $helpTopicObj = Help_topic::where('id', '=', $helptopic);
        if ($helpTopicObj->exists() && ($helpTopicObj->value('status') == 1)) {
            $department = $helpTopicObj->value('department');
        } else {
            $defaultHelpTopicID = Ticket::where('id', '=', '1')->first()->help_topic;
            $department = Help_topic::where('id', '=', $defaultHelpTopicID)->value('department');
        }
        $sla = $ticket_settings->first()->sla;
         // $priority = $ticket_settings->first()->priority;
         $default_priority = Ticket_Priority::where('is_default', '=', 1)->first();
        $user_priority = CommonSettings::where('id', '=', 6)->first();
        if (!($request->input('priority'))) {
            $priority = $default_priority->priority_id;
        } else {
            $priority = $request->input('priority');
        }
        $source = $ticket_source->where('name', '=', 'web')->first()->id;
        $attachments = $request->file('attachment');
        $collaborator = null;
        $assignto = null;
        $auto_response = 0;
        $team_assign = null;
        if ($phone != null || $mobile_number != null) {
            $location = GeoIP::getLocation();
            $geoipcode = $code->where('iso', '=', $location->iso_code)->first();
            if ($phonecode == null) {
                $data = [
                    'fails'              => Lang::get('lang.country-code-required-error'),
                    'phonecode'          => $geoipcode->phonecode,
                    'country_code_error' => 1,
                ];
                return Redirect::back()->with($data)->withInput($request->except('password'));
            } else {
                $code = CountryCode::select('phonecode')->where('phonecode', '=', $phonecode)->get();
                if (!count($code)) {
                    $data = [
                        'fails'              => Lang::get('lang.incorrect-country-code-error'),
                        'phonecode'          => $geoipcode->phonecode,
                        'country_code_error' => 1,
                    ];
                    return Redirect::back()->with($data)->withInput($request->except('password'));
                }
            }
        }
        $result = $this->TicketWorkflowController->workflow($email, $name, $subject, $details, $phone, $phonecode, $mobile_number, $helptopic, $sla, $priority, $source, $collaborator, $department, $assignto, $team_assign, $status, $form_extras, $auto_response);
        // dd($result);
        if ($result[1] == 1) {
            $ticketId = Tickets::where('ticket_number', '=', $result[0])->first();
            $thread = Ticket_Thread::where('ticket_id', '=', $ticketId->id)->first();
            if ($attachments != null) {
                foreach ($attachments as $attachment) {
                    if ($attachment != null) {
                        $name = $attachment->getClientOriginalName();
                        $type = $attachment->getClientOriginalExtension();
                        $size = $attachment->getSize();
                        $data = file_get_contents($attachment->getRealPath());
                        $attachPath = $attachment->getRealPath();
                        $ta->create(['thread_id' => $thread->id, 'name' => $name, 'size' => $size, 'type' => $type, 'file' => $data, 'poster' => 'ATTACHMENT']);
                    }
                }
            }
            // dd($result);
            return Redirect::back()->with('success', Lang::get('lang.Ticket-has-been-created-successfully-your-ticket-number-is').' '.$result[0].'. '.Lang::get('lang.Please-save-this-for-future-reference'));
        } else {
            return Redirect::back()->withInput($request->except('password'))->with('fails', Lang::get('lang.failed-to-create-user-tcket-as-mobile-has-been-taken'));
        }
//        dd($result);
    }
    /**
     * reply.
     *
     * @param type $value
     *
     * @return type view
     */
    public function post_ticket_reply($id, Request $request)
    {
        try {
            if ($comment != null) {
                $tickets = Tickets::where('id', '=', $id)->first();
                $thread = Ticket_Thread::where('ticket_id', '=', $tickets->id)->first();
                $subject = $thread->title.'[#'.$tickets->ticket_number.']';
                $body = $request->input('comment');
                $user_cred = User::where('id', '=', $tickets->user_id)->first();
                $fromaddress = $user_cred->email;
                $fromname = $user_cred->user_name;
                $phone = '';
                $phonecode = '';
                $mobile_number = '';
                $helptopic = $tickets->help_topic_id;
                $sla = $tickets->sla;
                $priority = $tickets->priority_id;
                $source = $tickets->source;
                $collaborator = '';
                $dept = $tickets->dept_id;
                $assign = $tickets->assigned_to;
                $form_data = null;
                $team_assign = null;
                $ticket_status = null;
                $auto_response = 0;
                $this->TicketWorkflowController->workflow($fromaddress, $fromname, $subject, $body, $phone, $phonecode, $mobile_number, $helptopic, $sla, $priority, $source, $collaborator, $dept, $assign, $team_assign, $ticket_status, $form_data, $auto_response);
                return \Redirect::back()->with('success1', Lang::get('lang.successfully_replied'));
            } else {
                return \Redirect::back()->with('fails1', Lang::get('lang.please_fill_some_data'));
            }
        } catch (Exception $e) {
            return \Redirect::back()->with('fails1', $e->getMessage());
        }
    }
    public function getCustomForm(Request $request)
    {
        $html = '';
        $helptopic_id = $request->input('helptopic');
        $helptopics = new Help_topic();
        $helptopic = $helptopics->find($helptopic_id);
        if (!$helptopic) {
            throw new Exception('We can not find your request');
        }
        $custom_form = $helptopic->custom_form;
        if ($custom_form) {
            $fields = new Fields();
            $forms = new \App\Model\helpdesk\Form\Forms();
            $form_controller = new \App\Http\Controllers\Admin\helpdesk\FormController($fields, $forms);
            $html = $form_controller->renderForm($custom_form);
        }
        return $html;
    }
}