<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use App\Models\JaheshOperation;
use Illuminate\Support\Facades\Auth;
use Carbon\Carbon;

class JaheshOperationController extends Controller
{
    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function index()
    {
        $projects = JaheshOperation::where('province_id', '=', Auth::user()->province_id)->get();

        return response()->json([
            'status' => 'succeed',
            'message' => '',
            'data' => $projects
        ], 200);
    }

    public function publicIndex()
    {
        $projects = JaheshOperation::where('province_id', '=', 9)->get();

        return response()->json([
            'status' => 'succeed',
            'message' => '',
            'data' => $projects
        ], 200);
    }

    /**
     * Display the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function show($id)
    {
        $project = JaheshOperation::find($id);

        return response()->json([
            'status' => 'succeed',
            'message' => '',
            'data' => $project
        ], 200);
    }

    /**
     * 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)
    {
        $request->validate([
            'operation_1' => 'string',
            'operation_2' => 'string',
            'operation_3' => 'string',
            'operation_4' => 'string',
            'operation_5' => 'string',
            'operation_6' => 'string',
            'operation_7' => 'string',
            'operation_8' => 'string',
            'operation_9' => 'string',
            'operation_10' => 'string',
            'operation_11' => 'string',
            'operation_12' => 'string'
        ]);

        $project = JaheshOperation::find($id);

        /*$project->operationHistories()->create([
            'user_id' => $request->user()->id,
            'previous_operation_1' => $project->operation_1,
            'previous_operation_2' => $project->operation_2,
            'previous_operation_3' => $project->operation_3,
            'previous_operation_4' => $project->operation_4,
            'previous_operation_5' => $project->operation_5,
            'previous_operation_6' => $project->operation_6,
            'previous_operation_7' => $project->operation_7,
            'previous_operation_8' => $project->operation_8,
            'previous_operation_9' => $project->operation_9,
            'previous_operation_10' => $project->operation_10,
            'previous_operation_11' => $project->operation_11,
            'previous_operation_12' => $project->operation_12
        ]);*/

        $operations[0] = ($request->input('operation_1')) ? explode(',', $request->input('operation_1')) : $project->operation_1;
        $operations[1] = ($request->input('operation_2')) ? explode(',', $request->input('operation_2')) : $project->operation_2;
        $operations[2] = ($request->input('operation_3')) ? explode(',', $request->input('operation_3')) : $project->operation_3;
        $operations[3] = ($request->input('operation_4')) ? explode(',', $request->input('operation_4')) : $project->operation_4;
        $operations[4] = ($request->input('operation_5')) ? explode(',', $request->input('operation_5')) : $project->operation_5;
        $operations[5] = ($request->input('operation_6')) ? explode(',', $request->input('operation_6')) : $project->operation_6;
        $operations[6] = ($request->input('operation_7')) ? explode(',', $request->input('operation_7')) : $project->operation_7;
        $operations[7] = ($request->input('operation_8')) ? explode(',', $request->input('operation_8')) : $project->operation_8;
        $operations[8] = ($request->input('operation_9')) ? explode(',', $request->input('operation_9')) : $project->operation_9;
        $operations[9] = ($request->input('operation_10')) ? explode(',', $request->input('operation_10')) : $project->operation_10;
        $operations[10] = ($request->input('operation_11')) ? explode(',', $request->input('operation_11')) : $project->operation_11;
        $operations[11] = ($request->input('operation_12')) ? explode(',', $request->input('operation_12')) : $project->operation_12;

        $newOperations = [];

        foreach ($operations As $key => $operation) {
            $newOperations[$key] = $project['operation_' . ($key + 1)];
            $newOperations[$key][2] = $operation[2];

            if ($request->input('operation_' . ($key + 1)) != null) {
                $newOperations[$key][3] = verta()->formatJalaliDatetime();
            }
        }

        $project->operation_1 = $newOperations[0];
        $project->operation_2 = $newOperations[1]; 
        $project->operation_3 = $newOperations[2];
        $project->operation_4 = $newOperations[3];
        $project->operation_5 = $newOperations[4];
        $project->operation_6 = $newOperations[5];
        $project->operation_7 = $newOperations[6];
        $project->operation_8 = $newOperations[7];
        $project->operation_9 = $newOperations[8];
        $project->operation_10 = $newOperations[9];
        $project->operation_11 = $newOperations[10];
        $project->operation_12 = $newOperations[11];

        //$project->save();

        return response()->json([
            'status' => 'succeed',
            'message' => '',
            'data' => $project
        ], 200);
    }

    public function showJaheshEditForm() {
        return view('rmto-preview.pages.jahesh-operations');
    }
}
