<?php

namespace App\DataTables;

use App\Country;
use App\User;
use Yajra\DataTables\Services\DataTable;

class CountryNetworkDataTable extends DataTable
{

    protected $country_id;

    public function dataTable($query)
    {
       return datatables()
            ->eloquent($query)
            ->addColumn('action', function ($row) {
                return '<div class="btn-group">' .
                '<a style="text-decoration:none" class="btn btn-primary actionBtn" href="' . route('admin.setting.countries.networks.edit', ['country_id'=>$row->country_id,'id'=>$row->id]).'">Edit</a>' .
                '<a style="text-decoration:none" class="btn btn-warning actionBtn" href="' . route('admin.setting.countries.networks.delete', ['country_id'=>$row->country_id,'id'=>$row->id]) . '"' .
                'onclick="return confirm(\'Do you want to delete this network?\')">Delete</a>' .
                '</div>';
            });

    }

    public function with_country($country_id) {
        $this->country_id = $country_id;
        return $this;
    }

    /**
     * Get the query object to be processed by dataTables.
     *
     * @return \Illuminate\Database\Eloquent\Builder|\Illuminate\Database\Query\Builder|\Illuminate\Support\Collection
     */
    public function query()
    {
        $query = Country::findOrFail($this->country_id)->networks()->orderBy('nice_name');

        return $this->applyScopes($query);
    }

    /**
     * Optional method if you want to use html builder.
     *
     * @return \Yajra\Datatables\Html\Builder
     */
    public function html()
    {
        return $this->builder()
                    ->columns($this->getColumns())
                    ->ajax('')
                    ->addAction(['width' => '150px'])
                    ->parameters($this->getBuilderParameters());
    }

    /**
     * Get columns.
     *
     * @return array
     */
    protected function getColumns()
    {
        return [
            'nice_name' => ['title'=> 'Network Name'],
            'full_name' => ['title'=> 'Registered Name'],
            'code',
            'sms_price',
            'prefix' => ['title'=> 'Prefixes']
        ];
    }

    /**
     * Get filename for export.
     *
     * @return string
     */
    protected function filename()
    {
        return 'countrynetworkdatatables_' . time();
    }
}
