<?php

class TransformativeAction extends Eloquent
{
    protected $table = 'transformative_actions';
    protected $appends = ['status'];

    public function getStatusAttribute()
    {
        if (isset($this->semester_id)) {
            Log::info("llegaremos?");
            return DB::table('transformative_action_status')
                ->where('trans_id', $this->id)
                ->where('semester_id', $this->semester_id)
                ->first();
        }
        return null;
    }


    public static function getTypCoursesWithSemesters($trans_id)
    {
        return DB::table('transformative_actions')
            ->join('annual_plan_transformative', 'annual_plan_transformative.trans_id', '=', 'transformative_actions.id')
            ->join('typ_semester_courses', 'typ_semester_courses.id', '=', 'annual_plan_transformative.typ_semester_course_id')
            ->join('typ_semester_objectives', 'typ_semester_objectives.id', '=', 'typ_semester_courses.typ_semester_objective_id')
            ->join('typ_semester_outcome', 'typ_semester_outcome.id', '=', 'typ_semester_objectives.typ_semester_outcome_id')
            ->join('semesters', 'semesters.id', '=', 'typ_semester_outcome.semester_id')
            ->where('transformative_actions.id', $trans_id)
            ->select(
                'transformative_actions.*',
                'transformative_actions.id as trans_id',
                'semesters.id',
                'semesters.code',
                'typ_semester_course_id as typ_future_course_id'
            )
            ->get();
    }


    public static function getCategoriesHtml($program_id)
    {
        $categories = "<option value='0'>Nothing Selected</option>";

        $types = DB::table('transformative_actions')
            ->select('type_of_TA', 'is_custom')
            ->where('type_of_TA', '<>', '')
            ->where(function ($query) use (&$program_id) {
                $query->whereNull('program_id')
                    ->orWhere('program_id', $program_id);
            })
            ->where('by_professor', 0)
            ->groupBy('type_of_TA')
            ->get();
        $optGroupGeneral = "<optgroup label='General Transformative Actions'>";
        $optGroupCustom = "<optgroup label ='Program Custom Actions' id = 'program_custom_action'>";
        foreach ($types as $type) {

            if ($type->is_custom) {
                $optGroupCustom .= "<option value = '" . $type->type_of_TA . "' data-is-custom = '1'>" . $type->type_of_TA . "</option>";
            } else {
                $optGroupGeneral .= "<option value = '" . $type->type_of_TA . "' data-is-custom = '0'>" . $type->type_of_TA . "</option>";
            }
        }
        $categories .= $optGroupGeneral . '</optgroup>';
        $categories .= $optGroupCustom . '</optgroup>';


        $categories .= '<option value ="new"> New Type of Transformative Action</option>';
        return $categories;
    }
}