<?php

namespace App\Http\Controllers;


use App\Models\Clause;
use App\Models\StatTreeLineClause;
use App\Models\StatTreeLineReportColumn;
use Illuminate\Http\Request;
use App\Models\StatTree;
use App\Models\StatTreeLine;
use App\Models\Client;
use App\Models\Pro;
use Illuminate\Support\Facades\DB;
use Ramsey\Uuid\Uuid;

class StatTreeLineReportColumnController extends Controller
{

    // eps
    public function create(Request $request) {
        $column = new StatTreeLineReportColumn();
        $nextId = DB::select("select nextval('stat_tree_line_report_column_id_seq')");
        $column->id = $nextId[0]->nextval;
        $column->uid = Uuid::uuid4();
        $column->stat_tree_line_id = $request->input('statTreeLineId');
        $column->label = $request->input('label');
        $column->display_key = $request->input('displayKey');
        $positionIndex = DB::select("select max(position_index) from stat_tree_line_report_column where stat_tree_line_id = {$column->stat_tree_line_id}");
        $column->position_index = is_numeric($positionIndex[0]->max) ? $positionIndex[0]->max + 1 : 1;
        $column->save();
        return $this->pass();
    }
    public function update(Request $request) {
        $column = StatTreeLineReportColumn::where('uid', $request->input('uid'))->first();
        if(!$column) return $this->fail('Column not found!');
        $column->label = $request->input('label');
        $column->display_key = $request->input('displayKey');
        $column->save();
        return $this->pass();
    }
    public function remove(Request $request) {
        $column = StatTreeLineReportColumn::where('uid', $request->input('uid'))->first();
        if(!$column) return $this->fail('Column not found!');
        DB::select("delete from stat_tree_line_report_column where id = {$column->id}");
        return $this->pass();
    }
}
