<?php

namespace App\DataTables\Admin;

use App\Helper\Util;
use App\Models\Invoice;
use Yajra\DataTables\Services\DataTable;
use Yajra\DataTables\EloquentDataTable;

/**
 * Class InvoiceDataTable
 * @package App\DataTables\Admin
 */
class InvoiceDataTable extends DataTable
{
    /**
     * Build DataTable class.
     *
     * @param mixed $query Results from query() method.
     * @return \Yajra\DataTables\DataTableAbstract
     */
      public $user_id    = null;
      public $is_team_lead    = null;
      public $team_id    = null;
      
    public function dataTable($query)
    {
         $query     = $query->with(['user', 'leads', 'websites']);
        $dataTable = new EloquentDataTable($query);
        $dataTable->editColumn('status', function (Invoice $model) {
            if ($model->status == 0) {
                return "<span class='label label-info '>Pending</span>";
            } else {
                return '<span class="label label-success">Paid</span>';
            }

        });
        $dataTable->editColumn('amount', function (Invoice $model) {
            return $model->amount . '(' . $model->currency . ')';
        });
        $dataTable->editColumn('websites.name', function (Invoice $model) {
            return $model->websites->name ?? "";
        });

        $dataTable->editColumn('user.name', function (Invoice $model) {
            return '<a href="' . url('/admin/users/' . $model->user_id ?? "") . '" style="font-weight:bold">' . $model->user->name . '</a>';
        });
        $dataTable->editColumn('leads.id', function (Invoice $model) {
            return '<a href="' . url('/admin/leads/' . $model->lead_id ?? "") . '" style="font-weight:bold">' . $model->lead_id . '</a>';
        });
         $dataTable->editColumn('comments', function (Invoice $model) {

            return strip_tags($model->comments);

        });

        $dataTable->rawColumns(['status', 'user.name', 'leads.id', 'action']);
        return $dataTable->addColumn('action', 'admin.invoices.datatables_actions');
    }

    /**
     * Get query source of dataTable.
     *
     * @param \App\Models\Invoice $model
     * @return \Illuminate\Database\Eloquent\Builder
     */
    public function query(Invoice $model)
    {
          if (\Entrust::hasRole('admin') || \Entrust::hasRole('super-admin')) {
               return $model->newQuery();
          }
          $model = $model->newQuery();
        if ($this->is_team_lead != null && $this->is_team_lead >= 0) {
             
            $model = $model->where('team_id', $this->team_id)->orwhere('team_id', NULL);
        }else{
           
             if ($this->user_id != null && $this->user_id >= 0) {

            $model = $model->where('user_id', $this->user_id);
        }
        }
        
       
//         if ($this->website_id != null || $this->website_id >= 0) {
// //            dd('i am working');
//             $model = $model->where('website_id', $this->website_id);
//         }
        return $model;
    }

    /**
     * Optional method if you want to use html builder.
     *
     * @return \Yajra\DataTables\Html\Builder
     */
    public function html()
    {
        $buttons = [];
        if (\Entrust::can('invoices.create') || \Entrust::hasRole('super-admin')) {
            $buttons = ['create'];
        }
        $buttons = array_merge($buttons, [
            // 'export',
            // 'print',
            'reset',
            'reload',
        ]);
        return $this->builder()
            ->columns($this->getColumns())
            ->minifiedAjax()
            ->addAction(['width' => '80px', 'printable' => false])
            ->parameters(array_merge(Util::getDataTableParams(), [
                'dom'     => 'Blfrtip',
                'order'   => [[0, 'desc']],
                'buttons' => $buttons,
            ]));
    }

    /**
     * Get columns.
     *
     * @return array
     */
    protected function getColumns()
    {
        return [
            'id'         => ['title' => 'Invoice Code'],
            'leads.id'    => ['title' => 'Lead Code'],
            'user.name'    => ['title' => 'User Name'],
            'websites.name' => ['title' => 'Website Name '],
            'amount',
//            'currency',
            'comments',
            'status'=> ['searchable' => false ],

        ];
    }

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