<?php

namespace App\Http\Controllers\Ajax;

use App\AuctionObject;
use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use Illuminate\Support\Facades\DB;
use Yajra\DataTables\Facades\DataTables;

class AuctionObjectsErrorOverviewController extends Controller
{
    public function getData(Request $request) {

        $filterStatus = $request->status;
        $filterLanguage = $request->lang;

        $data = DB::table('t_auction_object_error as ao')
            ->leftJoin('t_language as l', 'l.id', '=', 'ao.t_language_id')
            //->leftJoin('t_medium_category as m', 'm.id', '=', 'ao.t_medium_category_id')
            ->leftJoin('t_source_type as st', 'st.id', '=', 'ao.t_source_type_id')
            ->leftJoin('t_artist as a', 'a.id', '=', 'ao.t_artist_id')
            ->select(
                'ao.id',
                'ao.crawl_id',
                'ao.status',
                'ao.object_id',
                'ao.source_type',
                'ao.t_source_type_id',
                'ao.t_artist_id',
                'ao.t_language_id',
                //'ao.t_medium_category_id',
                'ao.lot_no',
                'ao.auction_no',
                'ao.auction_name',
                'ao.auction_loc',
                'ao.auction_date',
                //DB::raw("DATE_FORMAT(ao.auction_date, '%d.%m.%Y') as auction_date"),
                'ao.artist_firstname',
                'ao.artist_lastname',
                'ao.artist_born_loc',
                DB::raw("DATE_FORMAT(ao.artist_born_date, '%Y') as artist_born_date"),
                DB::raw("DATE_FORMAT(ao.artist_died_date, '%Y') as artist_died_date"),
                'ao.artist_died_loc',
                'ao.object_title',
                'ao.object_medium',
                'ao.object_date_pre',
                'ao.object_date',
                'ao.object_date_post',
                'ao.object_height',
                'ao.object_width',
                'ao.object_depth',
                'ao.object_size_unit',
                'ao.object_est_from',
                'ao.object_est_to',
                'ao.object_est_curr',
                'ao.t_currency_est_id',
                'ao.object_sold',
                'ao.object_result_curr',
                'ao.t_currency_result_id',
                'ao.object_result',
                'ao.object_url',
                'ao.object_image_url',
                'ao.object_indicators',
                DB::raw("DATE_FORMAT(ao.crawled_at, '%d.%m.%Y %H:%i') as crawled_at"),
                DB::raw("DATE_FORMAT(ao.created_at, '%d.%m.%Y %H:%i') as created_at"),
                'ao.hash_value',
                'ao.editor',
                'ao.lastchange',
                'l.language as language_name',
                'l.lang_code as lang_code',
                //'m.name as medium_category',
                'st.name as source_type_name',
                DB::raw("CONCAT(a.firstname_de, ' ', a.lastname_de) artist_fullname_de")
            );

        if(!is_null($filterStatus)) {
            $data->where('ao.status', $filterStatus);
        }

        if(!is_null($filterLanguage)) {
            $data->where('ao.t_language_id', $filterLanguage);
        }

        return Datatables::of($data)->make(true);
    }
}
