<?php

namespace App\Http\Controllers\CRMAgency;
use DB;
use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use App\CRMLeadSourceModel;
use App\Fun\Fun;
use Yajra\DataTables\DataTables;

class LeadSourceAgentController extends Controller
{
    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function index()
    {
        return view('CRMAgent.leadsource.index');

    }

    /**
     * Show the form for creating a new resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function create()
    {
        return view('CRMAgent.leadsource.create');
    }

    /**
     * Store a newly created resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @return \Illuminate\Http\Response
     */
    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-agency.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('CRMAgent.leadsource.edit',compact('leadsourcemodel','leadsource'));


    }

    /**
     * Update the specified resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    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-agency.index')
            ->with('success','Lead Sources update successfully');
    }

    /**
     * Remove the specified resource from storage.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    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";
                    }
                    return
                        '<a href="' . route("lead-source-agency.edit", $product->id) . '" title="Edit"><span class="button_info">' . $edit . '</span></a>
                        <button type="button" class="button_danger delete_products" data-toggle="modal"  data-id="' . $product->id . '" > ' . $delete . ' </button>';
                })
                ->addIndexColumn()
                ->rawColumns(['action' => 'action'])
                ->make(true);
        }
    }
}
