<?php

namespace App\Http\Controllers\crm;
use DB;
use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use App\Models\CRMLeadSourceModel;
use App\Fun\Fun;
use Yajra\DataTables\DataTables;
use Gate;

class LeadSourceController extends Controller
{
    function __construct(){
        $this->middleware("permission:Lead-Source-Create",['only'=>['create','store']]);
        $this->middleware("permission:Lead-Source-Edit",['only'=>['edit','update']]);
        $this->middleware("permission:Lead-Source-Delete",['only'=>['destroy']]);
        $this->middleware("permission:Lead-Source-List",['only'=>['index']]);
    }
    public function index()
    {
        return view('CRM.leadsource.index');

    }

    public function create()
    {
        return view('CRM.leadsource.create');
    }

    public function store(Request $request)
    {
        $this->validate($request, [
            'source_name'      => 'required',
        ]);
        $leadsource = new CRMLeadSourceModel;
        $leadsource->source_name = $request->source_name;
        $leadsource->note = $request->note;
        $leadsource->save();
        return redirect()->route('lead-source.index')
            ->with('success','Lead Sources created successfully');

    }
    public function show($id)
    {

    }
    public function edit($id)
    {
        $leadsourcemodel = CRMLeadSourceModel::pluck('source_name','id')
            ->prepend('Please Select', '');
        $leadsource = CRMLeadSourceModel::find($id);
        return view('CRM.leadsource.edit',compact('leadsourcemodel','leadsource'));


    }
    public function update(Request $request, $id)
    {
        $this->validate($request, [
            'source_name'      => 'required',
        ]);
        $leadsource =  CRMLeadSourceModel ::find($id);
        $leadsource->source_name = $request->source_name;
        $leadsource->note = $request->note;
        $leadsource->save();
        return redirect()->route('lead-source.index')
            ->with('success','Lead Sources update successfully');
    }
    public function destroy($id)
    {
        DB::table('nso007_crm_lead_source')->where('id',$id)->delete();
        return response()->json([
            'success' => 'Record deleted successfully!'
        ]);
}
    public function datable_lead_source(Request $request)
    {

        $data = CRMLeadSourceModel::get();
        if ($request->isMethod('get')) {

            return DataTables::of($data)

                ->addColumn('action', function ($product) {
                    $check_language = Fun::lang();
                    if ($check_language == 'km') {
                        $delete = 'លុប';
                        $edit = "កែសម្រួល";
                    } else {
                        $delete = 'Delete';
                        $edit = "Edit";
                    }
                    $action="";
                    if(Gate::check('Lead-Source-Edit'))
                    $action.='<a href="' . route("lead-source.edit", $product->id) . '" title="Edit"><span class=" button_warning">' . $edit . '</span></a>';
                    if(Gate::check('Lead-Source-Delete'))
                        $action.='
                            <a href="#" type="button" class="button_danger delete_products" data-toggle="modal"  data-id="' . $product->id . '" > ' . $delete . ' </a>';
                    return $action;
                })
                ->addIndexColumn()
                ->rawColumns(['action' => 'action'])
                ->make(true);
        }
    }
}
