<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use App\Category;
use App\Skill;
use App\JobPost;
use DB;
use Illuminate\Support\Facades\Auth;
use Illuminate\Support\Facades\Session;

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

    /**
     * Show the application dashboard.
     *
     * @return \Illuminate\Contracts\Support\Renderable
     */
    public function index()
    {
        return view('JobPost', ['categories' => Category::all(), 'skills' => Skill::all()]);
    }

    public function saveJob(Request $request)
    {
        /* $this->validate($request, [
            'job-title' => 'required',
            'category'=>'required',
            'budget'=>'required|Numeric',
            'skill'=>'required',
            'job-desc'=>'required',
            ]); */
        //dd($request->all());
        $data = [];
        if ($request->hasFile('file')) {
            foreach ($request->file as $file) {
                $name = $file->getClientOriginalName();
                $file->move(public_path() . '/files/', $name);
                $data[] = $name;
            }
        }
        $p = new JobPost();
        $p->client_id = Auth::id();
        $p->title = request('job-title');
        $p->category_id = $request->category;
        $p->budget = $request->budget;
        $p->skills = $request->skill;
        $p->job_desc = request('job-desc');
        $p->files = join(',', $data);
        $p->save();
        Session::flash('alert', "Successfully Posted");
        return redirect('/job-posted');
    }

    public function all_posted_jobs()
    {
        $client_id = Auth::id();
        $posted_jobs =  DB::select("SELECT a.id,a.client_id,a.title,a.category_id,a.budget,a.skills,a.job_desc,a.files,a.created_at,b.count FROM `job_posts` a  LEFT JOIN (SELECT job_id,count(*) as count  from bids GROUP by job_id) b on a.id=b.job_id WHERE a.client_id = '$client_id'");

        //$posted_jobs = JobPost::where('client_id', 1)->get();
        return view('AllPostedJob', ['jobs' => $posted_jobs]);
    }

    public function job_view($id)
    {
        $id = base64_decode($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 = '$id'");
        $bids =  DB::select("SELECT a.id,a.job_id,a.worker_id,a.status,b.name,b.picture,a.amount,a.cv,a.created_at FROM bizba_worker.bids a ,bizba_affiliate.users_detail b WHERE a.worker_id=b.id and a.job_id='$id'");
        return view('JobView', ['job' => $job, 'bids' => $bids]);
    }

    public function job_edit($id)
    {
        $id = base64_decode($id);
        $job = JobPost::where('id', $id)->get();
        return view('JobEdit', ['job' => $job, 'categories' => Category::all(), 'skills' => Skill::all()]);
    }

    public function job_update(Request $request, $id)
    {
        $data = [];
        if ($request->hasFile('file')) {
            foreach ($request->file as $file) {
                $name = $file->getClientOriginalName();
                $file->move(public_path() . '/files/', $name);
                $data[] = $name;
            }
        }
        $file = JobPost::select('files')->where('id', $id)->get();
        $file_arr = explode(",", $file[0]->files);
        $new_file_arr = array_merge($file_arr, $data);
        $file_str = join(",", $new_file_arr);

        JobPost::where('id', $id)->update([
            'title' => request('job-title'),
            'category_id' => $request->category,
            'budget' => $request->budget,
            'skills' => $request->skill,
            'job_desc' => request('job-desc'),
            'files' => $file_str
        ]);
        Session::flash('alert', "Successfully Edited");

        return redirect("/job-posted");
    }

    public function job_del($id)
    {
        $id = base64_decode($id);
        $file = JobPost::select('files')->where('id', $id)->get();
        $file_arr = explode(",", $file[0]->files);
        foreach($file_arr as $sngl_file){
            @unlink(public_path() . '/files/' . $sngl_file);
        }
        JobPost::where('id', $id)->delete();
        Session::flash('alert', "Successfully Deleted");
        return redirect()->back();
    }

    public function file_del($filename, $id)
    {
        $id = base64_decode($id);
        @unlink(public_path() . '/files/' . $filename);
        $file = JobPost::select('files')->where('id', $id)->get();
        $file_arr = explode(",", $file[0]->files);
        $new_file_arr = array_diff($file_arr, [$filename]);
        $file_str = join(",", $new_file_arr);
        JobPost::where('id', $id)->update(['files' => $file_str]);
        Session::flash('alert', "Successfully Deleted");
        return redirect()->back();
    }

}
