<?php

namespace App\Http\Controllers\crm;

use App\Models\CRMLeadModel;
use App\Models\CRMTaskModel;
use App\Fun\Fun;
use App\User;
use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use Illuminate\Support\Facades\Auth;
use Yajra\DataTables\DataTables;

class TaskController extends Controller
{
    public function index()
    {
        //
    }

    public function create($lead_id)
    {
        $u_id = Auth::user()->id;
        $lead_name = CRMLeadModel::find($lead_id);
        $agent = User::selectRaw('CONCAT(users.user_firstname," ",users.user_lastname) as name,id')
            ->where("id",Auth::user()->id)
            ->pluck('name', 'id');
        return view('CRM.task.create', compact('lead_name','agent'));
    }

    public function store(Request $request)
    {
        $this->validate($request, [
            'lead_id' => 'required',
            'task_type' => 'required',
            'subject' => 'required',
            'agent_id' => 'required',
            'admin_date' => 'required',
            'admin_hour' => 'required',
            'admin_minutes' => 'required',
            'admin_shift' => 'required',
        ]);
        if($request->task_type == "Appointment" || $request->task_type == "Meeting"){
            $this->validate($request, [
                'where' => 'required'
            ]);
        }
        $call_log = new CRMTaskModel();
        $call_log->lead_id = $request->lead_id;
        $call_log->agent_id = $request->agent_id;
        $call_log->task_type = $request->task_type;
        $call_log->call_duration = $request->call_duration;
        $call_log->subject = $request->subject;
        $call_log->where = $request->where;
        $call_log->date = date('Y-m-d', strtotime($request->admin_date));
        $call_log->hour = sprintf('%02d', $request->admin_hour);
        $call_log->minutes = sprintf('%02d', $request->admin_minutes);
        $call_log->shift = $request->admin_shift;
        $call_log->note = $request->admin_note;
        $call_log->save();
        if($request->save_type=="save_con"){
            return redirect()->route('agent-task.create',$request->lead_id)->with('success', 'Task create successfully...');
        }else{
            if ($call_log->task_type == 'Appointment') {
                return redirect()->route('appointment.index')->with('success', 'Appointment log create successfully...');
            }
            if ($call_log->task_type == 'Meeting') {
                return redirect()->route('meeting.index')->with('success', 'Appointment log create successfully...');
            }
            if ($call_log->task_type == 'Call_log') {
                return redirect()->route('call-log.index')->with('success', 'Appointment log create successfully...');
            }
        }
    }

    public function show($id)
    {
        //
    }

    public function edit($id)
    {
        $task = CRMTaskModel::find($id);
        $lead_name = CRMLeadModel::find($task->lead_id);
        $agent = User::selectRaw('CONCAT(users.user_firstname," ",users.user_lastname) as name,id')
            ->where("id",Auth::user()->id)
            ->pluck('name', 'id'); 
        return view('CRM.task.edit', compact('lead_name', 'task','agent'));
    }

    public function update(Request $request, $id)
    {
        $this->validate($request, [
            'lead_id' => 'required',
            'task_type' => 'required',
            'subject' => 'required',
            'date' => 'required',
            'hour' => 'required',
            'minutes' => 'required',
            'shift' => 'required',
        ]);
        $call_log = CRMTaskModel::find($id);
        $call_log->lead_id = $request->lead_id;
        $call_log->task_type = $request->task_type;
        $call_log->call_duration = $request->call_duration;
        $call_log->subject = $request->subject;
        $call_log->where = $request->where;
        $call_log->date = date('Y-m-d', strtotime($request->date));
        $call_log->hour = sprintf('%02d', $request->hour);
        $call_log->minutes = sprintf('%02d', $request->minutes);
        $call_log->shift = $request->shift;
        $call_log->note = $request->note;
        $call_log->save();
        if ($call_log->task_type == 'Appointment') {
            return redirect()->route('appointment.index')->with('success', 'Appointment update successfully...');
        }
        if ($call_log->task_type == 'Meeting') {
            return redirect()->route('meeting.index')->with('success', 'Meeting update successfully...');
        }
        if ($call_log->task_type == 'Call_log') {
            return redirect()->route('call-log.index')->with('success', 'Call log update successfully...');
        }
    }

    public function destroy($id)
    {
        //
    }

    public function task_report()
    {
        $agent = User:: selectRaw('CONCAT(users.user_firstname," ",users.user_lastname) as name,id')
            ->where(function ($q) {
            $q->where('users.id', auth::user()->id);
        })
            ->pluck('name', 'id');
        return view('CRM.report.task-report', compact('agent'));
    }

    public function datatable_task_report(Request $request)
    {
        $agent_s = isset($_GET['agent_id']) ? $_GET['agent_id'] : '0';
        $status_s = isset($_GET['status']) ? $_GET['status'] : '';
        $close_deal_s = isset($_GET['close_deal']) ? $_GET['close_deal'] : '';
        $date_s = $_GET['date'] ? date('Y-m-d', strtotime($_GET['date'])) : '0';

        $data = CRMTaskModel::selectRaw('nso007_crm_task.id as task_id,nso007_crm_task.date,
            CONCAT(users.user_firstname," ",users.user_lastname) as agent_name,nso007_crm_task.lead_id,
            nso007_crm_task.subject,nso007_crm_task.hour,
            nso007_crm_task.minutes,nso007_crm_task.shift,nso007_crm_task.note,nso007_crm_task.complete_status,nso007_crm_task.convert_status,nso007_crm_task.task_type')
            ->join('users', 'users.id', 'nso007_crm_task.agent_id')
            ->where(function ($q) {
                $q->where('users.id', auth::user()->id);
            })
            ->where(function ($q) use ($agent_s) {
                if ($agent_s != 0) {
                    $q->where('nso007_crm_task.agent_id', $agent_s);
                }
            })
            ->where(function ($q) use ($status_s) {
                if ($status_s != '') {
                    $q->where('nso007_crm_task.complete_status', $status_s);
                }
            })
            ->where(function ($q) use ($close_deal_s) {
                if ($close_deal_s != '') {
                    $q->where('nso007_crm_task.convert_status', $close_deal_s);
                }
            })
            ->where(function ($q) use ($date_s) {
                if ($date_s != 0) {
                    $q->where('nso007_crm_task.date', $date_s);
                }
            })
            ->where('nso007_crm_task.delete_status', 0)->get();
        if ($request->isMethod('get')) {
            return DataTables::of($data)
                ->editColumn('date', function ($data) {
                    return date('d-m-Y', strtotime($data->date));
                })
                ->editColumn('hour', function ($task) {
                    return $task->hour . ':' . $task->minutes . ' ' . $task->shift;
                })
                ->editColumn('task_type', function ($task) {
                    return str_replace('_', ' ', $task->task_type);
                })
                ->addColumn('lead_name', function ($task) {
                    $lead = CRMLeadModel::find($task->lead_id);
                    return $lead->family_name . ' ' . $lead->name;
                })
                ->addColumn('contact', function ($task) {
                    $lead = CRMLeadModel::find($task->lead_id);
                    return $lead->contact;
                })
                ->editColumn('convert_status', function ($task) {
                    $check_language = Fun::lang();
                    if ($check_language == 'km') {
                        $fail = 'បរាជ័យ';
                        $pending = 'រង់ចាំ';
                        $success = 'ជោគជ័យ';
                    } else {
                        $fail = 'Failed';
                        $pending = 'Pending';
                        $success = 'Successful';
                    }
                    return $task->convert_status == 1 ? '<span class="label label-success">' . $success . '</span>' : ($task->convert_status == 2 ? '<span class="label label-danger">' . $fail . '</span>' : '<span class="label label-warning">' . $pending . '</span>');
                })
                ->editColumn('complete_status', function ($task) {
                    $check_language = Fun::lang();
                    if ($check_language == 'km') {
                        $pending = 'រង់ចាំ';
                        $complete = 'រួចរាល់';
                    } else {
                        $pending = 'Pending';
                        $complete = 'Complete';
                    }
                    return $task->complete_status == 1 ? '<span class="label label-primary">' . $complete . '</span>' : '<span class="label label-warning">' . $pending . '</span>';
                })
                ->addIndexColumn()
                ->rawColumns(['convert_status' => 'convert_status', 'complete_status' => 'complete_status', 'lead_name' => 'lead_name', 'contact' => 'contact'])
                ->make(true);
        }

    }

    public function view_task_report()
    {
        $agent_s = isset($_GET['agent_id']) ? $_GET['agent_id'] : '0';
        $status_s = isset($_GET['status']) ? $_GET['status'] : '';
        $close_deal_s = isset($_GET['close_deal']) ? $_GET['close_deal'] : '';
        $date_s = $_GET['date'] ? date('Y-m-d', strtotime($_GET['date'])) : '0';
        $u_id = Auth::user()->id;
        $data = CRMTaskModel::selectRaw('nso007_crm_task.id as task_id,nso007_crm_task.date,
            CONCAT(users.user_firstname," ",users.user_lastname) as agent_name,nso007_crm_task.lead_id,
            nso007_crm_task.subject,nso007_crm_task.hour,
            nso007_crm_task.minutes,nso007_crm_task.shift,nso007_crm_task.note,nso007_crm_task.complete_status,nso007_crm_task.convert_status,nso007_crm_task.task_type')
            ->join('users', 'users.id', 'nso007_crm_task.agent_id')
            ->where(function ($q) use ($u_id) {
                $q->where('users.user_status', 2);
            })
            ->where(function ($q) use ($agent_s) {
                if ($agent_s != 0) {
                    $q->where('nso007_crm_task.agent_id', $agent_s);
                }
            })
            ->where(function ($q) use ($status_s) {
                if ($status_s != '') {
                    $q->where('nso007_crm_task.complete_status', $status_s);
                }
            })
            ->where(function ($q) use ($close_deal_s) {
                if ($close_deal_s != '') {
                    $q->where('nso007_crm_task.convert_status', $close_deal_s);
                }
            })
            ->where(function ($q) use ($date_s) {
                if ($date_s != 0) {
                    $q->where('nso007_crm_task.date', $date_s);
                }
            })
            ->where('nso007_crm_task.delete_status', 0)->get();
        return view('CRM.report.show', compact('data'));
    }
}
