<?php

namespace App\Http\Controllers\crm;

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

class CallLogController extends Controller
{
    public function index()
    {
        $u_id = Auth::user()->id;
        $agent = User::selectRaw('CONCAT(users.user_firstname," ",users.user_lastname) as name,id')
            ->where("id",Auth::user()->id)
            ->pluck('name', 'id');
        $lead = CRMLeadModel:: selectRaw('nso007_crm_leads.id,CONCAT(nso007_crm_leads.family_name," ",nso007_crm_leads.name) as lead_name,nso007_crm_leads.agent_id')
            ->join('users', 'users.id', 'nso007_crm_leads.agent_id')
            ->where('users.id', Auth::user()->id)
            ->pluck('lead_name','nso007_crm_leads.id');
            
        return view('CRM.call-log.index',compact('agent', 'lead'));
    }

    public function create($lead_id)
    {
        $u_id = Auth::user()->id;
        $agent = User::selectRaw('CONCAT(users.user_firstname," ",users.user_lastname) as name,id')
            ->where(function ($q) use ($u_id) {
            $q->where('users.user_status', 2);
        })

            ->pluck('name', 'id');
        $lead_name = CRMLeadModel::find($lead_id);
        return view('CRM.call-log.create', compact('agent', 'lead_name'));
    }

    public function store(Request $request)
    {

    }

    public function show($id)
    {
        $meeting = CRMTaskModel::find($id);
        $lead = CRMLeadModel::find($meeting->lead_id);
        return view('CRM.call-log.show', compact('meeting', 'lead'));
    }

    public function edit($id)
    {
        $meeting = CRMTaskModel::find($id);
        $lead = CRMLeadModel::find($meeting->lead_id);
        
        return view('CRM.call-log.edit', compact('meeting', 'lead'));
    }

    public function update(Request $request, $id)
    {
        $outcome = $request->out_come;
        $result_percentage = $request->result_percentage;
        $result = $request->result;
        $meeting = CRMTaskModel::find($id);
        $meeting->out_come = $outcome;
        $meeting->result_percentage = $result_percentage;
        $meeting->complete_status = 1;
        $meeting->call_type = $request->call_type;
        $meeting->call_duration = $request->call_duration;
        $meeting->result = $result;
        if ($meeting->update()) {
            return redirect()->route('call-log.index')->with('success', 'Task Completed...');
        }
        return redirect()->route('call-log.index')->with('danger', 'Task Failed...');
    }


    public function destroy($id)
    {
        //
    }

    public function datable_lead_call_log(Request $request)
    {
        $agent_s = isset($_GET['agent_id'])?$_GET['agent_id']:'0';
        $date_s = $_GET['date']?date('Y-m-d',strtotime($_GET['date'])):'0';
        $data = CRMTaskModel::selectRaw('nso007_crm_task.subject
        ,nso007_crm_task.date,nso007_crm_task.hour,
        nso007_crm_task.minutes,nso007_crm_task.shift,
        nso007_crm_task.note,nso007_crm_task.complete_status,
        nso007_crm_task.id,
        nso007_crm_task.lead_id,nso007_crm_task.agent_id
        ,nso007_crm_task.convert_status,CONCAT(users.user_firstname," ",users.user_lastname)  as agent_name')
            ->join('users', 'users.id', 'nso007_crm_task.agent_id')
            ->where([['task_type', 'Call_log'], ['delete_status', 0]])
            ->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 ($date_s) {
                if($date_s!=0){
                    $q->where('nso007_crm_task.date', $date_s);
                }
            })
            ->get();
        if ($request->isMethod('get')) {
            return DataTables::of($data)
                ->editColumn('agent_id', function ($data) {
                    $user = User::find($data->agent_id);
                    return $user->name;
                })
                ->editColumn('note', function ($data) {
                    return $data->note ? $data->note : 'N/A';
                })
                ->editColumn('lead_id', function ($data) {
                    $lead = CRMLeadModel::find($data->lead_id);
                    return $lead->family_name.' '.$lead->name;
                })
                ->editColumn('hour', function ($data) {
                    return $data->hour . ':' . $data->minutes . ' ' . $data->shift;
                })
                ->editColumn('date', function ($data) {
                    return date('d-m-Y', strtotime($data->date));
                })
                ->editColumn('complete_status', function ($data) {
                    $check_language = Fun::lang();
                    $lb = '';
                    if($data->convert_status==1){
                        $lb = ' label-success ';
                    }
                    if($data->convert_status==2){
                        $lb = ' label-danger ';
                    }
                    if($data->convert_status==0 && $data->complete_status == 0){
                        $lb = ' label-warning ';
                    }
                    if($data->convert_status==0 && $data->complete_status == 1){
                        $lb = ' label-primary ';
                    }
                    if ($data->complete_status == 0) {
                        if ($check_language == 'km') {
                            $st = 'រងចាំ';
                        } else {
                            $st = 'Pending';
                        }
                        $status = '<label class="label'.$lb.'">' . $st . '</label>';
                    } else {
                        if ($check_language == 'km') {
                            $st = 'រួចរាល់';
                        } else {
                            $st = 'Completed';
                        }
                        $status = '<label class="label'.$lb.'">' . $st . '</label>';
                    }

                    return $status;
                })
                ->addColumn('action', function ($data) {
                    $check_language = Fun::lang();

                    if ($check_language == 'km') {
                        $delete = 'លុប';
                        $edit = "កែសម្រួល";
                        $comp = "រួចរាល់";
                        $view = 'បង្ហាញ';
                        $act = 'សកម្មភាព';
                    } else {
                        $delete = 'Delete';
                        $edit = "Edit";
                        $comp = "Complete";
                        $view = 'View';
                        $act = 'Action';
                    }
                    $del = '';
                    if($data->convert_status==0){
                        if ($data->complete_status == 0) {
                            $del = '<li><a href="' . route("agent-task.edit", $data->id) . '" title="Edit"><span class="">' . $edit . '</span></a></li>
                        <li><a class="cursor delete_products" data-toggle="modal" 
                        data-id="' . $data->id . '" > ' . $delete . ' </a></li>';
                            if(Auth::user()->id == $data->agent_id){
                                $del.=' <li><a href="' . route('meeting.edit', $data->id) . '" class="">' . $comp . '</a></li>';
                            }
                        }
                    }
                    return '
                          <ul>
                           <li class="dropdown">
                            <a href="#" class="dropdown-toggle btn btn-xs btn-default" data-toggle="dropdown">' . $act . ' <span class="caret"></span></a>
                            <ul class="dropdown-menu" role="menu">
                              <li><a href="' . route('call-log.show', $data->id) . '">' . $view . '</a></li>
                              ' . $del . '
                            </ul>
                          </li>
                          </ul>';

                })
                ->addIndexColumn()
                ->rawColumns(['action' => 'action', 'complete_status' => 'complete_status'])
                ->make(true);
        }
    }

    public function delete_call_log(Request $request)
    {
        $st = CRMTaskModel::find($request->id);
        $st->delete_status = 1;
        $st->update();
    }
}
