<?php

namespace App\Http\Controllers\Admin;

use App\Pipeline;
use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use App\Unit;
use Illuminate\Support\Facades\DB;
use App\Http\Requests\StorePipeline;
use App\Http\Requests\UpdatePipeline;

class PipelineController extends Controller
{
    public function index()
    {

        $pipelines = Pipeline::all();
        $units = Unit::all();

        $data = array(
            'pipelines' => $pipelines,
            'units' => $units
        );
        return view('pipeline.collection', $data);
    }


    public function show()
    {

    }

    public function create()
    {
        return view('pipeline.create');
    }

    public function edit($id)
    {

        $pipelines = Pipeline::find($id);
        $data = array(
            'pipeline' => $pipelines
        );
        return view('pipeline.edit', $data);

    }

    public function update(UpdatePipeline $request)
    {

        $pipeline = Pipeline::find($request->id);
        $pipeline->description = $request->description;
        $pipeline->name = $request->name;
        $pipeline->slug = $request->slug;
        $pipeline->public =$request->public;
        $pipeline->hidden =$request->hidden;
        $pipeline->deprecated =$request->deprecated;
        $pipeline->public_name =$request->public_name;
        $pipeline->save();
        return redirect()->back()->with('message', 'Categoria Actualizada');

    }

    public function store(StorePipeline $request)
    {

        Pipeline::create(array(
            'name' => $request->name,
            'slug' => $request->slug,
            'description' => $request->description,
            'public'=> $request->public,
            'hidden'=> $request->hidden,
            'deprecated'=> $request->deprecated,
            'public_name'=> $request->public_name,
        ));

        return redirect('/admin/pipeline');


    }

    public function delete($id)
    {

        $pipeline = Pipeline::find($id);


        $pipeline->delete();

        return redirect('/admin/pipeline');
    }

    public function deleteUnitToPipeline()
    {
        $unit_id = $_POST['unit_id'];
        $pipeline_id = $_POST['pipeline_id'];

        DB::table('unit_pipeline')->where('unit_id', '=', $unit_id)->where('pipeline_id', '=', $pipeline_id)->delete();


        $pipeline = Pipeline::find($pipeline_id);

        $units = $pipeline->unitsByRevision($pipeline->latestRevision);



        $mt = md5(microtime().rand());

        $pipeline->latestRevision=$mt;
        $pipeline->save();

        $temp =array();


        foreach ($units as $index=>$unit){



            array_push( $temp , array(
                'unit_id'=>$unit->id,
                'pipeline_id'=>$pipeline_id,
                'order'=>$index,
                'revision'=>$mt,
            ));



        }
        DB::table('unit_pipeline')->insert($temp);


        return response(json_encode('true'), 200)->header('Content-Type', 'application/json');
    }


    public function addUnitToPipeline()
    {
        $unit_id = $_POST['unit_id'];
        $pipeline_id = $_POST['pipeline_id'];
        $pipeline = Pipeline::find($pipeline_id);


        $unit_pipeline = DB::table('unit_pipeline')->insert(array(

            array(

                'unit_id' => (int)$unit_id,
                'pipeline_id' => (int)$pipeline_id,
                'order' => 99,
                'revision'=>$pipeline->latestRevision,

                )


        ));



        $units = $pipeline->unitsByRevision($pipeline->latestRevision);

        $mt = md5(microtime().rand());
        $pipeline->latestRevision=$mt;
        $pipeline->save();

        $temp =array();

        foreach ($units as $index=>$unit){
            array_push( $temp , array(
                'unit_id'=>$unit->id,
                'pipeline_id'=>$pipeline_id,
                'order'=>$index,
                'revision'=>$mt,
            ));


        }
        DB::table('unit_pipeline')->insert($temp);


        if ($unit_pipeline) {

            $unit = Unit::find($unit_id);

            return response(json_encode($unit), 200)->header('Content-Type', 'application/json');

        } else {

            $error = array(
                'error' => 'ha ocurrido un error intentando grabar pivot unit_pipeline'
            );

            return response(json_encode($error), 409)->header('Content-Type', 'application/json');
        }


    }

    public function orderPipelineUnits(Request $request)
    {

        $pipeline_id =request('id');
        var_dump($pipeline_id);
        $units = json_decode(request('units'));
        var_dump($units);

       // $query = DB::table('unit_pipeline')->where('pipeline_id','=',$pipeline_id )->delete();

        //Store pipeline revision


        $pipeline = Pipeline::find($pipeline_id);

        $temp =array();
        $mt = md5(microtime().rand());
        $pipeline->latestRevision=$mt;
        $pipeline->save();

        foreach ($units as $index=>$unit_id){
            array_push( $temp , array(
                'unit_id'=>(int)$unit_id,
                'pipeline_id'=>(int)$pipeline_id,
                'order'=>$index,
                'revision'=>$mt,
            ));


        }

        DB::table('unit_pipeline')->insert($temp);

    }

}
