<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use App\Models;
use Session;

class SenderIdController extends Controller
{
    public function index(){
        $senderid_arr = new Models\SenderId;
        // $senderid_arr = $senderid_arr->with('modified_by_info', 'network_info', 'created_by_info')->orderBy('senderid', 'ASC')->paginate(15);
        // dd($senderid_arr);
        $data = [
            'page_title' => 'Sender IDs',
            // 'senderid_arr' => $senderid_arr,
            'current_user' => session('current_user')
        ];
        return view('senderid.index', $data);
    }
    public function getSenderIdsJson(Request $request)
    {
        #$this->log_query();
        $senderid_arr = \DB::table('sender_ids')
            ->join('staff_members AS staffcreate', 'staffcreate.id', '=', 'sender_ids.created_by')
            ->join('staff_members AS staffmodify', 'staffmodify.id', '=', 'sender_ids.last_modified_by')
            ->join('network_operators', 'network_operators.id', '=', 'sender_ids.network_id')
            ->join('clients', 'clients.id', '=', 'sender_ids.client_id')
            ->select('sender_ids.id', 'clients.name AS clientName', 'network_operators.name AS networkName', 'network_operators.country', 'sender_ids.senderid', 'sender_ids.status', 'sender_ids.network_id', 'network_operators.country','sender_ids.remarks', 'staffcreate.name As createdBy', 'staffmodify.name AS modifiedBy')
            ->orderBy('sender_ids.senderid', 'ASC')
            ->paginate(15);

        if($request->has('keyword')){
            $keyword = $request->keyword;
            $senderid_arr = \DB::table('sender_ids')
            ->join('staff_members AS staffcreate', 'staffcreate.id', '=', 'sender_ids.created_by')
            ->join('staff_members AS staffmodify', 'staffmodify.id', '=', 'sender_ids.last_modified_by')
            ->join('network_operators', 'network_operators.id', '=', 'sender_ids.network_id')
            ->join('clients', 'clients.id', '=', 'sender_ids.client_id')
            ->select('sender_ids.id', 'clients.name AS clientName', 'network_operators.name AS networkName', 'network_operators.country', 'sender_ids.senderid', 'sender_ids.status', 'sender_ids.network_id', 'network_operators.country','sender_ids.remarks', 'staffcreate.name As createdBy', 'staffmodify.name AS modifiedBy')
            ->whereRaw("sender_ids.senderid LIKE '%$keyword%' OR sender_ids.status LIKE '%$keyword%' OR network_operators.name LIKE '%$keyword%' OR network_operators.country LIKE '%$keyword%' OR staffcreate.name LIKE '%$keyword%' OR staffmodify.name LIKE '%$keyword%' OR clients.name LIKE '%$keyword%'")
            ->orderBy('sender_ids.senderid', 'ASC')
            ->paginate(15);
        }     
        return response()->json($senderid_arr);
    }
    public function create()
    {
        #$network_arr = Models\NetworkOps::pluck('name','country');
        
        $networks = \DB::table('network_operators')->Select(\DB::raw('id, concat(name, " (", country, ")") AS network'))->orderBy('network')->get()->toArray();
        $network_arr = array_pluck($networks, 'network', 'id');
        // dd($network_arr);
        #$network_arr = array_combine($network_arr, $network_arr);
        

        $clients = Models\Client::pluck('name', 'id');
        $staffmembers = Models\StaffMember::pluck('name', 'id');
        $status = ['Pending' => 'Pending', 'Inactive' => 'Inactive', 'Approved' => 'Approved'];
        
        $data = [
            'page_title' => 'Create Sender ID',
            'network_arr' => $network_arr,
            'status' => $status,
            'clients' => $clients,
            'staffmembers' => $staffmembers
        ];

        return view('senderid.create', $data);
    }
    /**
     * Store a newly created resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @return \Illuminate\Http\Response
     */
    public function store(Request $request)
    {
        $request->validate([
            'senderid' => 'required|max:11',
            'network_id' => 'required',
            'status' => 'required',
            'remarks' => 'sometimes',
        ]);
        $senderid_arr = $request->except('_token');
        $senderid_arr['created_by'] = session('current_user.id');
        $senderid_arr['last_modified_by'] = session('current_user.id');
        $senderid_arr['remarks'] = $request->remarks ?? '';
        

        $result = Models\SenderId::create($senderid_arr);
        
        
        #save services this has been moved to the main client table
        //$retval = $this->store_services($request->services, $result->id);
        
        Session::flash('success_message', 'Sender ID successfully added');
        return redirect(url('senderids'));
    }
    public function edit($id){
        $senderid = Models\SenderId::findOrFail($id);
        //dd($senderid);

        $networks = \DB::table('network_operators')->Select(\DB::raw('id, concat(name, " (", country, ")") AS network'))->orderBy('network')->get()->toArray();
        $network_arr = array_pluck($networks, 'network', 'id');
        

        $clients = Models\Client::pluck('name', 'id');
        $staffmembers = Models\StaffMember::pluck('name', 'id');
        $status = ['Pending' => 'Pending', 'Inactive' => 'Inactive', 'Approved' => 'Approved'];
        

        $data = [
            'page_title' => 'Create Sender ID',
            'network_arr' => $network_arr,
            'status' => $status,
            'clients' => $clients,
            'senderid' => $senderid,
            'staffmembers' => $staffmembers
        ];

        return view('senderid.edit', $data);
    }
    public function update(Request $request, $id)
    {

        $request->validate([
            'senderid' => 'required|max:11',
            'network_id' => 'required',
            'status' => 'required',
            'remarks' => 'sometimes',
        ]);
        
        $senderid = Models\SenderId::findOrFail($id);
        
        $senderid->senderid = $request->senderid;
        $senderid->client_id = $request->client_id;
        $senderid->status = $request->status;
        $senderid->network_id = $request->network_id;
        $senderid->remarks = $request->remarks;

        $senderid->save();

        Session::flash('success_message', 'Sender ID successfully Updated');
        return redirect(url('senderids'));
    }
}
