<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use App\TaskPipeline;
use App\Pipeline;
use App\Status;
use App\Task;
use App\Unit;
use DB;

class FilterTaskController extends Controller
{




    public function __construct()
    {

        parent::__construct();

        $this->middleware('auth');


    }



    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function index(Request $request, $unit = null, $status = null)
    {

        $pipelines  = Pipeline::all();

        $data = array(
            'pipelines' => $pipelines,
            'status'   => Status::where('status_type','task')->get(),
            'units'    => Unit::all(),
            'currentUnit' => $unit,
            'currentStatus' => $status
        );

        // dd(Status::where('status_type','task')->get()->toArray());

        return view('task.client.filterTask', $data);
    }




    public function getData(Request $request){


            // dd($request->all());



            $pipelines  = Pipeline::all();    
            $unit    = Unit::all();  
            $status  = Status::all();  

            $limited = !empty($request->limit_page)?$request->limit_page:10;



            $tasks = DB::table('task_pipeline')
                ->select('tasks.*','task_pipeline.pipeline_id as pipeline_id_data')
                    ->join('tasks', 'task_pipeline.task_id', '=', 'tasks.id')        
                   // ->join('taskdata', 'tasks.id', '=', 'taskdata.id')
                  //   ->where('task_pipeline.pipeline_id',$request->line_prod)


                    ->when(!empty($request->line_prod), function ($q) use ($request) {

                        $q->where('task_pipeline.pipeline_id', $request->line_prod);

                    })


                    ->when(($request->not_subtask=="true"), function ($q) use ($request) {

                         $q->leftJoin('subtasks', 'tasks.id', '=', 'subtasks.task_id');
                          $q->where('subtasks.task_id','=',null);

                    })



                    ->when(!empty($request->name_title), function ($q) use ($request) {

                        $q->where('tasks.title','like', '%'.$request->name_title.'%');

                    })

                    ->when(!empty($request->fecha_cre), function ($q) use ($request) {

                        $q->whereDate('tasks.created_at', '>=', $request->fecha_cre);

                    })
            
                    ->when(!empty($request->fecha_end), function ($q) use ($request) {

                        $q->whereDate('tasks.created_at', '<=', $request->fecha_end);

                    })

                    ->when(!empty($request->select_status), function ($q) use ($request) {

                         $q->whereIn('tasks.status_id', $request->select_status);

                    })

                    ->when(!empty($request->select_unit), function ($q) use ($request) {


                         $q->whereIn('tasks.unit_id', $request->select_unit);

                    })

                   ->paginate($limited);


 // dd($tasks);

/*
                    $name_line = "";

                    foreach ($pipelines as $key2 => $value2) {
                    
                        if($value2->id==$request->line_prod)
                          $name_line = $value2->name;
                    
                    }

*/
        if (!$request->line_prod){
            $request->line_prod = 0;
        }
        if (!$request->name_title){
            $request->name_title = 0;
        }
        if (!$request->fecha_cre){
            $request->fecha_cre = 0;
        }
        if (!$request->fecha_end){
            $request->fecha_end = 0;
        }
        if (!$request->select_status){
            $request->select_status = [];
        }
        if (!$request->select_unit){
            $request->select_unit = [];
        }

        $data = array(
                    'tasks'     => !empty($tasks)?$tasks:null,
                    'filters'=> !empty($filters)?$filters:null,
                    'unit'      => !empty($unit)?$unit:null,
                    'status'    => !empty($status)?$status:null,
                    'pipelines' => !empty($pipelines)?$pipelines:null,
                    'line_prod' => $request->line_prod,
                    'name_title' => $request->name_title,
                    'fecha_cre' => $request->fecha_cre,
                    'fecha_end' => $request->fecha_end,
                    'select_status' => $request->select_status,
                    'select_unit' => $request->select_unit,
                    'limit_page' => $limited
                );


            $returnHTML = view('task.client.getajax')->with($data)->render();
            return response()->json(array('success' => true, 'html'=>$returnHTML));




    }

    function printResult(Request $request){
        $pipelines  = Pipeline::all();
        $unit    = Unit::all();
        $status  = Status::all();


        $tasks = DB::table('task_pipeline')
            ->select('tasks.*','task_pipeline.pipeline_id as pipeline_id_data')
            ->join('tasks', 'task_pipeline.task_id', '=', 'tasks.id')

            ->when(!empty($request->line_prod), function ($q) use ($request) {

                $q->where('task_pipeline.pipeline_id', $request->line_prod);

            })

            ->when(!empty($request->name_title), function ($q) use ($request) {

                $q->where('tasks.title','like', '%'.$request->name_title.'%');

            })

            ->when(!empty($request->fecha_cre), function ($q) use ($request) {

                $q->whereDate('tasks.created_at', '>=', $request->fecha_cre);

            })

            ->when(!empty($request->fecha_end), function ($q) use ($request) {

                $q->whereDate('tasks.created_at', '<=', $request->fecha_end);

            })

            ->when(!empty($request->select_status), function ($q) use ($request) {

                $q->whereIn('tasks.status_id', $request->select_status);

            })

            ->when(!empty($request->select_unit), function ($q) use ($request) {


                $q->whereIn('tasks.unit_id', $request->select_unit);

            })
            ->get();

        $data = array(
            'tasks'     => !empty($tasks)?$tasks:null,
            'unit'      => !empty($unit)?$unit:null,
            'status'    => !empty($status)?$status:null,
            'pipelines' => !empty($pipelines)?$pipelines:null,
        );
        return view('task.client.printResult', $data);

    }



    /**
     * Show the form for creating a new resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function create()
    {
        //
    }

    /**
     * Store a newly created resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @return \Illuminate\Http\Response
     */
    public function store(Request $request)
    {
        //
    }

    /**
     * Display the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function show($id)
    {
        //
    }

    /**
     * Show the form for editing the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function edit($id)
    {
        //
    }

    /**
     * Update the specified resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function update(Request $request, $id)
    {
        //
    }

    /**
     * Remove the specified resource from storage.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function destroy($id)
    {
        //
    }
}
