<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use Illuminate\Support\Arr;
use App\Http\Requests;
use Carbon\Carbon;
use App\Models;
use Session;

class ShortCodesController extends Controller{

    public function indexTab(){
        $data = [
            'page_title' => 'Short Codes',
            'type' => 'SMS',
            'current_user' => session('current_user')
        ];
        return view('shortcodes.index', $data);
    }
    public function create(){
        $country_networks = \DB::table('network_operators')->Select(\DB::raw('id, concat(name, " (", country, ")") AS network'))->orderBy('network')->pluck('network', 'network');
        $client_arr = Models\Client::pluck('name', 'id');
        $data = [
            'page_title' => 'New Short Code',
            'country_network_arr' => $country_networks,
            'client_arr' => $client_arr,
            'current_user' => session('current_user')
        ];
        return view('shortcodes.create', $data);
    }

    public function smsindex(){
        // $codes_data = Models\ClientShortCode::groupBy('code_type')->get();
        $country_networks = \DB::table('network_operators')->Select(\DB::raw('id, concat(name, " (", country, ")") AS network'))->orderBy('network')->pluck('network', 'network');
        $client_arr = Models\Client::orderBy('name', 'ASC')->pluck('name', 'id');
        $codes_data = \DB::table('client_short_codes')
                 ->select('code_type', \DB::raw('count(*) as total'))
                 ->groupBy('code_type')
                 ->get();
        $networks_raw = [
            'AirtelTigo GH' => 'AirtelTigo GH',
            'MTN GH' => 'MTN GH',
            'Airtel MW' => 'Airtel MW',
            'Airtel Zambia' => 'Airtel Zambia',
            'TNM MW' => 'TNM MW',
            'Airtel MW' => 'Airtel MW',
            'Safaricom Kenya' => 'Safaricom Kenya',
            'Airtel Kenya' => 'Airtel Kenya',
            'Telkom Kenya' => 'Telkom Kenya',
            'Orange Kenya' => 'Orange Kenya'
        ];
        sort($networks_raw);
        $data = [
            'page_title' => 'Short Codes',
            'type' => 'SMS',
            'codes_data' => $codes_data,
            'networks_raw' => array_combine($networks_raw, $networks_raw),
            'country_network_arr' => $country_networks,
            'client_arr' => $client_arr,
            'current_user' => session('current_user')
        ];
        return view('shortcodes.smsindex', $data);
    }
    public function getShortCodesJson(Request $request){
        //$this->log_query();
        //$codes_data = Models\ClientShortCode::with('client_info', 'update_info')->where('code_type', 'sms')->orderBy('id', 'DESC')->get();

        $shortcode_arr = \DB::table('client_short_codes')
            ->join('auth_users', 'auth_users.id', '=', 'client_short_codes.last_updated_by')
            ->join('clients', 'clients.id', '=', 'client_short_codes.client_id')
            ->select('client_short_codes.id', 'client_short_codes.name', 'client_short_codes.shortcode',  'clients.name AS clientName', 'client_short_codes.network', 'client_short_codes.toll_free', 'client_short_codes.monthly_fee', 'client_short_codes.status', 'client_short_codes.remarks', 'client_short_codes.launch_date', 'client_short_codes.expiry_date', 'client_short_codes.last_updated_by', 'auth_users.name AS modifiedBy')
            ->where('client_short_codes.code_type', 'sms')
            ->orderBy('client_short_codes.created_at', 'DESC')
            ->paginate(15);

        if($request->has('keyword')){
            $keyword = $request->keyword;
            $code_type = 'sms';
            $shortcode_arr = \DB::table('client_short_codes')
            ->join('auth_users', 'auth_users.id', '=', 'client_short_codes.last_updated_by')
            ->join('clients', 'clients.id', '=', 'client_short_codes.client_id')
            ->select('client_short_codes.id', 'client_short_codes.name', 'client_short_codes.shortcode', 'clients.name AS clientName', 'client_short_codes.network', 'client_short_codes.toll_free', 'client_short_codes.monthly_fee', 'client_short_codes.status', 'client_short_codes.remarks', 'client_short_codes.launch_date', 'client_short_codes.expiry_date', 'client_short_codes.last_updated_by', 'auth_users.name AS modifiedBy')
            ->whereRaw("code_type = '$code_type' AND (client_short_codes.name LIKE '%$keyword%' OR client_short_codes.shortcode LIKE '%$keyword%' OR client_short_codes.status LIKE '%$keyword%' OR client_short_codes.network LIKE '%$keyword%' OR client_short_codes.toll_free LIKE '%$keyword%' OR client_short_codes.monthly_fee LIKE '%$keyword%' OR client_short_codes.launch_date LIKE '%$keyword%' OR client_short_codes.expiry_date LIKE '%$keyword%' OR clients.name LIKE '%$keyword%' OR auth_users.name LIKE '%$keyword%')")
            // ->where('code_type', $code_type)
            ->orderBy('client_short_codes.created_at', 'DESC')
            ->paginate(15);
        }
        return response()->json($shortcode_arr);
    }

    public function ussdindex(){
        // $codes_data = Models\ClientShortCode::groupBy('code_type')->get();
        $country_networks = \DB::table('network_operators')->Select(\DB::raw('id, concat(name, " (", country, ")") AS network'))->orderBy('network')->pluck('network', 'network');
        $client_arr = Models\Client::orderBy('name', 'ASC')->pluck('name', 'id');
        $codes_data = \DB::table('client_short_codes')
                 ->select('code_type', \DB::raw('count(*) as total'))
                 ->groupBy('code_type')
                 ->get();
        $data = [
            'page_title' => 'Short Codes',
            'type' => 'USSD',
            'codes_data' => $codes_data,
            'country_network_arr' => $country_networks,
            'client_arr' => $client_arr,
            'current_user' => session('current_user')
        ];
        
        return view('shortcodes.ussdindex', $data);
    }
    public function getUssdShortCodesJson(Request $request){
        #$this->log_query();
        //$codes_data = Models\ClientShortCode::with('client_info', 'update_info')->where('code_type', 'sms')->orderBy('id', 'DESC')->get();

        $shortcode_arr = \DB::table('client_short_codes')
            ->join('auth_users', 'auth_users.id', '=', 'client_short_codes.last_updated_by')
            ->join('clients', 'clients.id', '=', 'client_short_codes.client_id')
            ->select('client_short_codes.id', 'client_short_codes.name', 'client_short_codes.shortcode',  'clients.name AS clientName', 'client_short_codes.network', 'client_short_codes.toll_free', 'client_short_codes.monthly_fee', 'client_short_codes.status', 'client_short_codes.remarks', 'client_short_codes.launch_date', 'client_short_codes.expiry_date', 'client_short_codes.last_updated_by', 'auth_users.name AS modifiedBy')
            ->where('client_short_codes.code_type', 'ussd')
            ->orderBy('client_short_codes.created_at', 'DESC')
            ->paginate(15);

        if($request->has('keyword')){
            $keyword = $request->keyword;
            $code_type = 'ussd';
            $shortcode_arr = \DB::table('client_short_codes')
            ->join('auth_users', 'auth_users.id', '=', 'client_short_codes.last_updated_by')
            ->join('clients', 'clients.id', '=', 'client_short_codes.client_id')
            ->select('client_short_codes.id', 'client_short_codes.name', 'client_short_codes.shortcode', 'clients.name AS clientName', 'client_short_codes.network', 'client_short_codes.toll_free', 'client_short_codes.monthly_fee', 'client_short_codes.status', 'client_short_codes.remarks', 'client_short_codes.launch_date', 'client_short_codes.expiry_date', 'client_short_codes.last_updated_by', 'auth_users.name AS modifiedBy')
            ->whereRaw("code_type = '$code_type' AND (client_short_codes.name LIKE '%$keyword%' OR client_short_codes.shortcode LIKE '%$keyword%' OR client_short_codes.status LIKE '%$keyword%' OR client_short_codes.network LIKE '%$keyword%' OR client_short_codes.toll_free LIKE '%$keyword%' OR client_short_codes.monthly_fee LIKE '%$keyword%' OR client_short_codes.launch_date LIKE '%$keyword%' OR client_short_codes.expiry_date LIKE '%$keyword%' OR clients.name LIKE '%$keyword%' OR auth_users.name LIKE '%$keyword%')")
            // ->where('code_type', $code_type)
            ->orderBy('client_short_codes.created_at', 'DESC')
            ->paginate(15);
        }
        return response()->json($shortcode_arr);
    }


    public function voiceindex(){
        // $codes_data = Models\ClientShortCode::groupBy('code_type')->get();
        $country_networks = \DB::table('network_operators')->Select(\DB::raw('id, concat(name, " (", country, ")") AS network'))->orderBy('network')->pluck('network', 'network');
        $client_arr = Models\Client::orderBy('name', 'ASC')->pluck('name', 'id');
        $codes_data = \DB::table('client_short_codes')
                 ->select('code_type', \DB::raw('count(*) as total'))
                 ->groupBy('code_type')
                 ->get();
        $data = [
            'page_title' => 'Short Codes',
            'type' => 'Voice',
            'codes_data' => $codes_data,
            'country_network_arr' => $country_networks,
            'client_arr' => $client_arr,
            'current_user' => session('current_user')
        ];
        return view('shortcodes.voiceindex', $data);
    }
    public function getVoiceShortCodesJson(Request $request){
        #$this->log_query();
        //$codes_data = Models\ClientShortCode::with('client_info', 'update_info')->where('code_type', 'sms')->orderBy('id', 'DESC')->get();

        $shortcode_arr = \DB::table('client_short_codes')
            ->join('auth_users', 'auth_users.id', '=', 'client_short_codes.last_updated_by')
            ->join('clients', 'clients.id', '=', 'client_short_codes.client_id')
            ->select('client_short_codes.id', 'client_short_codes.name', 'client_short_codes.shortcode',  'clients.name AS clientName', 'client_short_codes.network', 'client_short_codes.toll_free', 'client_short_codes.monthly_fee','client_short_codes.status', 'client_short_codes.remarks', 'client_short_codes.launch_date', 'client_short_codes.expiry_date', 'client_short_codes.last_updated_by', 'auth_users.name AS modifiedBy')
            ->where('client_short_codes.code_type', 'voice')
            ->orderBy('client_short_codes.created_at', 'DESC')
            ->paginate(15);

        if($request->has('keyword')){
            $keyword = $request->keyword;
            $code_type = 'voice';
            $shortcode_arr = \DB::table('client_short_codes')
            ->join('auth_users', 'auth_users.id', '=', 'client_short_codes.last_updated_by')
            ->join('clients', 'clients.id', '=', 'client_short_codes.client_id')
            ->select('client_short_codes.id', 'client_short_codes.name', 'client_short_codes.shortcode', 'clients.name AS clientName', 'client_short_codes.network', 'client_short_codes.toll_free', 'client_short_codes.monthly_fee', 'client_short_codes.status', 'client_short_codes.remarks', 'client_short_codes.launch_date', 'client_short_codes.expiry_date', 'client_short_codes.last_updated_by', 'auth_users.name AS modifiedBy')
            ->whereRaw("code_type = '$code_type' AND (client_short_codes.name LIKE '%$keyword%' OR client_short_codes.shortcode LIKE '%$keyword%' OR client_short_codes.status LIKE '%$keyword%' OR client_short_codes.network LIKE '%$keyword%' OR client_short_codes.toll_free LIKE '%$keyword%' OR client_short_codes.monthly_fee LIKE '%$keyword%' OR client_short_codes.launch_date LIKE '%$keyword%' OR client_short_codes.expiry_date LIKE '%$keyword%' OR clients.name LIKE '%$keyword%' OR auth_users.name LIKE '%$keyword%')")
            // ->where('code_type', $code_type)
            ->orderBy('client_short_codes.created_at', 'DESC')
            ->paginate(15);
        }
        return response()->json($shortcode_arr);
    }





}
