<?php

namespace App\Http\Controllers\CRM;

use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use App\Model\crm\LeadSource;
use DataTables, Validator;
use Auth;

class LeadSourceController extends Controller
{
    public function index(Request $request)
    {
        if($request->ajax()){
            $data = LeadSource::orderBy('id','desc')
                            ->where(function ($q) use ($request) {
                                if($request->name){
                                    $q->where('source_name', 'like', '%' . $request->name . '%');
                                }
                                if($request->note){
                                    $q->where('note', 'like', '%' . $request->note . '%');
                                }
                            })
                            ->paginate($request->record , ['*'], 'page', str_replace(',', '', $request->page));

            return DataTables::of($data->items())
                    ->with([
                        "activePage" => $request->page,
                        "records" => $request->record,
                        "recordsTotal" => $data->total(),
                        "recordsFiltered" => $data->total(),
                        'totalPage' => $data->lastPage()
                    ])
                    ->addColumn('action', function($action){
                            return '<i href="javascript:;" data-href = "' . route("prospect-source.edit", $action->id) . '"  class="fa fa-pencil btn btn-sm btn-warning getHtmlForm" title="Edit"></i>' .
                                ' <i href="javascript:;" data-url = "' . route("prospect-source.destroy", $action->id) . '" class="fa fa-trash btn btn-sm btn-danger" title="Delete"></i>';
                    })
                    ->addIndexColumn()
                    ->rawColumns(['action'])
                    ->make(true);
        }
        return view('crm.lead_source.index');
    }

    public function create()
    {
        return view('crm.lead_source.create');
    }

    public function store(Request $request)
    {
       return $this->update($request,null);
    }
    public function edit($id)
    {
        $leadSource  = LeadSource::find($id);
        return view('crm.lead_source.edit',compact('leadSource'));
    }
    public function update(Request $request, $id)
    {
        $data = $this->validate($request, [
            'source_name' => 'required|max:25|string|unique:nso007_crm_lead_source,source_name,'.$id,
            'note' => 'nullable|string|max:255',
        ]);
        $master  = LeadSource::updateOrCreate(['id'=>$id],$data );
        if ($request['input_name']) {
            return response()->json(['value' => $master->id, 'text' => $master->source_name, 'input_name' => $request['input_name']]);
        }
        return response()->json(['status' => 'ok','reload'=>1]);
    }
    public function destroy($id)
    {
        LeadSource::find($id)->delete();
        return response()->json(['', 200]);
    }
}
