<?php

namespace App\Http\Controllers;

use Auth;
use Illuminate\Http\Request;
use \App\Models\{Oti, OtiPlan, OP, Pieza, Variable, OtiMotivo, OtiLog};

class OrdenController extends Controller
{
    //
    public function orden(Request $request){

        $data = Oti::with('sector')->with('plans')->with('incidencias')->with('incidencia_pendiente')->orderBy('priority')->get();

        return $data;
    }

    public function store(Request $request)
    {
        $op = (new OP)->find($request->production_id);
        
        $item = new Oti();
 
        $item->material = $request->material;
        $item->number = $request->number;
        $item->piece = $request->piece;
        $item->production_id = $request->production_id;
        if ($op) {
            $item->client_id = $op->client;
        }
        $item->quantity = $request->quantity;
        $item->status = $request->status;
        $item->time = $request->time;
        $item->tratamiento = isset($request->tratamiento_id) && $request->tratamiento_id != null ? $request->tratamiento_id : $request->tratamiento;
        if ($request->machine_id) {
            $item->machine_id = $request->machine_id;            
        }
        if ($request->sector_id) {
            $item->sector_id = $request->sector_id;            
        }
        if ($request->product_id) {
            $item->product_id = $request->product_id;
        }
        $item->priority = $request->priority;
        $item->save();
        
        if ($request->planos) {
            $this->updatePlanos($item, $request->planos);
        }

        return ['status' => 'success', 'data' => $item];
    }

    private function updatePlanos($oti, $planos)
    {
        (new OtiPlan)->where('oti_id', $oti->id)->delete();
        $model = new OtiPlan();
        foreach ($planos as $plano) {
            $model->create([
                    'oti_id' => $oti->id,
                    'plano_id' => $plano['id']
                ]);   
        }
    }

    public function show($id)
    {
        $data = Oti::with('plans')->find($id);
        return $data;
    }

    public function updatePriority(Request $request, $id)
    {
        $data = Oti::find($id);
        $data->priority = $request->priority;
        $data->save();

        return $data;
    }

    private function getMotive($motive)
    {
        $motives = ['', 'Falta material', 'Mantenimiento', 'Corte de luz', 'Almuerzo', 'Fin de turno', 'Otro'];
        return $motives[$motive];
    }

    private function updateMotives($oti, $motive = null)
    {
        $motive_id = null;
        if ($motive) {
            $motives = new OtiMotivo();
            $motives->oti_id = $oti->id;
            $motives->motive_id = $motive;
            $motives->descripcion = $this->getMotive($motive);

            $motives->save();
            $motive_id = $motives->id;
        }

        $log = new OtiLog();
        $log->oti_id = $oti->id;
        $log->time = $oti->time;
        $log->oti_motivo_id = $motive_id;
        $log->status = $oti->status;
        $log->user_id = Auth::user()->id;
        $log->save();
    }

    public function update(Request $request, $id)
    {
        $item = Oti::find($id);
        $item->status = $request->status;
        if ($request->current_operator) {            
            $item->current_operator = $request->current_operator;
        }
        if ($request->time) {            
            $item->time = $request->time;
        }
        $item->save();

        if ($request->motive || $item->status == 2) {
            $this->updateMotives($item, $request->motive);
        }

        return ['status' => 'success', 'data' => $item];
    }

    public function otibyuser($id){
        $data = Oti::where('current_operator', $id)->with('plans')->with('logs')->orderBy('priority')->get();
        return $data;
    }

    public function pieza_controls($id){

        $pieza = Pieza::find($id);
        $pieza->variables = Variable::with('instrument')->where('pieza_id', $id)->get();

        foreach($pieza->variables as $key => $variable){
            // for from 0 to $pieza->variable->controls
            for($i = 0; $i < $variable->controls; $i++){
                $controls[$key][$i] = ['status' => true, 'operator' => 'TEST'];
            }
            
        }

        return ['variables' => $pieza->variables, 'controls' => $controls];
    }


}
