<?php

namespace App\Http\Controllers\Agencies;
use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use App\User;
use Auth;
use App\ClientModel;
use App\SaleStockModel;
use App\ItemModel;
use DB;
use DataTables;
use Illuminate\Support\Facades\Crypt;

class ReportClientBookingController extends Controller
{
    public function index(){
        return view('agencies.report-client-booking.index');
    }
    public function datatable_get_report_client_booked(){
        $log_id = Auth::user()->id;
        dd($log_id);
        $client = ClientModel::leftJoin('nso007_sale_stock as sale', 'sale.sale_clientid', '=', 'nso007_client.id')
                                ->leftJoin('nso007_sale_detail_stock as detail', 'detail.saledetail_saleid', '=', 'sale.id')
                                ->leftJoin('nso007_item_stock as item', 'item.id', '=', 'detail.saledetail_itemid')
                                ->leftJoin('nso007_project_stock as project', 'project.id', '=', 'item.item_projectid')
                                ->leftJoin('nso007_category_stock as category', 'category.id', '=', 'item.item_categoryid')
                                ->select(
                                    'nso007_client.id',
                                    'nso007_client.client_fullname',
                                    'nso007_client.client_gender',
                                    'nso007_client.client_phone1',
                                    'nso007_client.client_address',
                                    'project.project_name',
                                    'category.category_name',
                                    'item.item_name',
                                    'sale.sale_date'
                                )
                                ->where('sale.agency_id', $log_id)
                                ->get();
		return Datatables::of($client)
        ->addColumn('client_gender', function($client) {
            $gender = '';
            switch ($client->client_gender) {
                case '1':
                    $gender = "Male";
                    break;
                
                default:
                    $gender = "Female";
                    break;
            }
			return $gender;    
        })
        ->addColumn('sale_date',function($client){
            return date('d-m-Y',strtotime($client->sale_date));
        })
		->addIndexColumn()
        ->rawColumns(['client_gender' => 'client_gender'])
        ->make(true);
    }
}
