<?php

namespace App\Http\Controllers;

use App\Models;
use Illuminate\Http\Request;
use Session;
use DB;


class MarketerReportController extends Controller
{
    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function index(Request $request)
    {
        $market_reports = Models\MarketReport::where('market_reports.id', '>', 0);
        $payment_type = [1 => 'Prepaid', 2 => 'Postpaid'];

        $columns = [
            'service' => 'Service',
            'email' => 'Email',
            'discussion' => 'Discussion',
            'contact_person' => 'Contact Person',
            'payment_status' => 'Payment Status',
            'sam_comment' => 'Sam Comment',
            'status' => 'status',
            'client' => 'Client',
            'auth_user_id' => 'User',
            'payment_type' => 'Payment Type',
        ];

        if (!empty($request->filter) && !empty($request->keyword)) {
            $market_reports = $this->resolveAppQuery($market_reports, $request);
        }

        $market_reports = $market_reports->orderBy('market_reports.created_at', 'DESC')->paginate(20);

        $data = [
            'page_title' => 'Click Apps',
            'market_reports' => $market_reports,
            'columns' => $columns,
            'payment_type' => $payment_type
        ];

        return view('marketer_report.index', $data);
    }

    /**
     * Show the form for creating a new resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function create()
    {
        $clients = Models\Client::pluck('name', 'id');
        $service = Models\Service::pluck('name', 'name');
        $payment_type = Models\PaymentType::pluck('name', 'id');
        $payment_status = ['paid' => 'Paid', 'unpaid' => 'Unpaid'];

        $data = [
            'page_title' => 'Marketing Report',
            'service' => $service,
            'clients' => $clients,
            'payment_type' => $payment_type,
            'payment_status' => $payment_status
        ];

        return view('marketer_report.create', $data);
    }

    /**
     * Store a newly created resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @return \Illuminate\Http\Response
     */
    public function store(Request $request)
    {
        $request->validate($this->mkrtRrptValidations());
        $reportData = $request->only($this->mkrtRrptWhiteListParms());
        $reportData['auth_user_id'] = session('current_user.id');

        DB::transaction(function () use ($reportData) {
            Models\MarketReport::create($reportData);
        });

        Session::flash('success_message', 'Report successfully added');
        return redirect(url('marketreport'));
    }

    /**
     * Display the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function show($id)
    {
        $market_report = Models\MarketReport::with('client_info', 'auth_user_info')->where('id', $id)->first();
        $data = [
          'page_title' => 'Report Details',
          'market_report' => $market_report
        ];
        return view('marketer_report.show', $data);
    }

    /**
     * Show the form for editing the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function edit($id)
    {
        $market_report = Models\MarketReport::find($id);
        if (!$market_report) {
            Session::flash('error_message', "Record with ID: {$id} not found");
            return redirect(url('marketreport'));
        }
        $clients = Models\Client::pluck('name', 'id');
        $service = Models\Service::pluck('name', 'name');
        $payment_type = Models\PaymentType::pluck('name', 'id');
        $payment_status = ['paid' => 'Paid', 'unpaid' => 'Unpaid'];

        $data = [
            'page_title' => 'Edit Meeting Report',
            'market_report' => $market_report,
            'service' => $service,
            'clients' => $clients,
            'payment_type' => $payment_type,
            'payment_status' => $payment_status
        ];

        return view('marketer_report.edit', $data);
    }

    /**
     * Update the specified resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function update(Request $request, $id)
    {
        $market_report =  Models\MarketReport::find($id);
        if (!$market_report) {
            Session::flash('error_message', "Record with ID: {$id} not found");
            return redirect(url('marketreport'));
        }
        $request->validate($this->mkrtRrptValidations());
        $reportData = $request->only($this->mkrtRrptWhiteListParms());
        $reportData['auth_user_id'] = session('current_user.id');

        DB::transaction(function () use ($reportData, $market_report) {
            $market_report->update($reportData);
        });

        Session::flash('success_message', 'Market report updated successfully');
        return redirect(url('marketreport'));
    }

    /**
     * Remove the specified resource from storage.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function destroy($id)
    {
        Models\MarketReport::destroy($id);
        if (request()->ajax()) {
            $res = ['code' => 1];
            return response()->json($res);
        }
        Session::flash('success_message', 'Market report successfully deleted!');
        return redirect(url('clickapps'));
    }

    public function mkrtRrptValidations()
    {
        return [
            'client' => 'required',
            'service' => 'required',
            'contact_person' => 'required',
            'email' => 'required',
            'discussion' => 'required',
            'payment_type' => 'required',
            'current_balance' => 'required|numeric',
            'payment_status' => 'required',
            'next_follow_up_date' => 'required',
            'last_follow_up_date' => 'required'
        ];
    }

    public function mkrtRrptWhiteListParms()
    {
        return [
            'client',
            'service',
            'contact_person',
            'email',
            'discussion',
            'payment_type',
            'current_balance',
            'payment_status',
            'next_follow_up_date',
            'last_follow_up_date',
        ];
    }

    function resolveAppQuery($clients, $request)
    {
        switch ($request->filter) {
            case 'service':
                return $clients
                    ->where('service', 'like', "%$request->keyword%");
                break;
            case 'email':
                return $clients
                    ->where('email', 'like', "%$request->keyword%");
                break;
            case 'discussion':
                return $clients
                    ->where('discussion', 'like', "%$request->keyword%");
                break;
            case 'contact_person':
                return $clients
                    ->where('contact_person', 'like', "%$request->keyword%");
                break;
            case 'payment_status':
                return $clients
                    ->where('payment_status', 'like', "%$request->keyword%");
                break;
            case 'sam_comment':
                return $clients
                    ->where('sam_comment', 'like', "%$request->keyword%");
                break;
            case 'status':
                return $clients
                    ->where('status', 'like', "%$request->keyword%");
                break;
                case 'client':
                    return $clients
                        ->join('clients', 'clients.id', '=', 'market_reports.client')
                        ->where('clients.name', 'like', "%$request->keyword%");
                    break;
                    case 'auth_user_id':
                        return $clients
                            ->join('auth_users', 'auth_users.id', '=', 'market_reports.auth_user_id')
                            ->where('auth_users.name', 'like', "%$request->keyword%");
                        break;
                        case 'payment_type':
                            return $clients
                                ->join('payment_type', 'payment_type.id', '=', 'market_reports.payment_type')
                                ->where('payment_type.name', 'like', "%$request->keyword%");
                            break;
            default:
                # code...
                break;
        }
    }
}


// payment_type