<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use App\HiredWorker;
use Illuminate\Support\Facades\Auth;
use App\Bids;
use Illuminate\Support\Facades\DB;
use Hamcrest\Type\IsArray;
use App\Chats;

class HiredWorkerController extends Controller
{
    /**
     * Create a new controller instance.
     *
     * @return void
     */
    public function __construct()
    {
        $this->middleware('auth');
    }

    public function index()
    {
        $client_id = Auth::id();
        $hired = DB::select("SELECT a.id,a.job_id,c.title,c.budget,b.amount,a.created_at,a.status FROM bizba_worker.hired_workers a,bizba_worker.bids b ,bizba_worker.job_posts c WHERE a.job_id = c.id AND a.bid_id = b.id AND a.client_id='$client_id'");
        return view('HiredWorkers', ['hired' => $hired]);
    }

    public function hire_worker(Request $request)
    {
        Bids::where('id', $request->bid_id)->update(['status' => 1]);

        $h = new HiredWorker();
        $h->job_id = $request->job_id;
        $h->client_id = Auth::id();
        $h->worker_id = $request->worker_id;
        $h->bid_id = $request->bid_id;
        $h->save();

        return redirect('/hired-workers');
    }

    public function contract_view($id)
    {
        $id = base64_decode($id);
        $h = DB::select("SELECT a.id,a.job_id,a.client_id,b.name as c_name,a.worker_id,c.name as w_name,a.bid_id,d.amount,a.paid,a.status,a.created_at FROM bizba_worker.hired_workers  a,bizba_worker.users b,bizba_affiliate.users_detail c,bizba_worker.bids d WHERE a.client_id = b.id and a.worker_id = c.id and a.bid_id = d.id AND a.id ='$id'");
        if (isset($h[0])) {
            $job_id = $h[0]->job_id;
            $job =  DB::select("SELECT a.id,a.client_id,a.title,b.name as cat_name,a.budget,a.skills,a.job_desc,a.files,a.created_at FROM `job_posts` a , categories b WHERE a.category_id=b.id AND a.id = '$job_id'");
            $chats = Chats::where('hired_workers_id', $h[0]->id)->orderBy('created_at', 'desc')->get();
            return view('ContractView', ['job' => $job, 'hired' => $h[0], 'chats' => $chats]);
        } else {
            return redirect('/hired-workers');
        }
    }

    public function give_paid($hired_id)
    {
        $hired = DB::select("SELECT a.id,a.job_id,a.client_id,b.name as c_name,a.worker_id,c.name as w_name,a.bid_id,d.amount,a.paid,a.status,a.created_at FROM bizba_worker.hired_workers  a,bizba_worker.users b,bizba_affiliate.users_detail c,bizba_worker.bids d WHERE a.client_id = b.id and a.worker_id = c.id and a.bid_id = d.id AND a.id ='$hired_id'");
        $user_id = $hired[0]->worker_id;
        $amount = $hired[0]->amount;
        HiredWorker::where('id', $hired_id)->update(['paid' => $amount, 'status' => 1]);
        DB::select("INSERT INTO bizba_affiliate.work_earned(`user_id`, `amount`) VALUES ('$user_id','$amount')");
        DB::select("UPDATE bizba_affiliate.total_balance SET `work_earn_amount`= '$amount' + work_earn_amount WHERE user_id = '$user_id'");

        return redirect()->back();
    }
}
