<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use App\Models\AlternatifModel;
use App\Models\KriteriaModel;

class AlternatifController extends Controller
{
    public function index()
    {
        $id_user_level = session('log.id_user_level');
        
        if ($id_user_level != 1) {
            ?>
            <script>
                window.location='<?php echo url("Dashboard"); ?>'
                alert('Anda tidak berhak mengakses halaman ini!');
            </script>
            <?php
        }

        $data['page'] = "Alternatif";
        $data['list'] = AlternatifModel::all();
        return view('alternatif.index', $data);
    }

    public function tambah()
    {
        $id_user_level = session('log.id_user_level');
        
        if ($id_user_level != 1) {
            ?>
            <script>
                window.location='<?php echo url("Dashboard"); ?>'
                alert('Anda tidak berhak mengakses halaman ini!');
            </script>
            <?php
        }

        $data['page'] = "Alternatif";
        return view('alternatif.tambah', $data);
    }

    public function simpan(Request $request)
    {
        $id_user_level = session('log.id_user_level');
        
        if ($id_user_level != 1) {
            ?>
            <script>
                window.location='<?php echo url("Dashboard"); ?>'
                alert('Anda tidak berhak mengakses halaman ini!');
            </script>
            <?php
        }

        $this->validate($request, [
            'nama' => 'required'
        ]);

        $data = [
            'nama' => $request->nama
        ];

        $result = AlternatifModel::create($data);

        if ($result) {
            $request->session()->flash('message', '<div class="alert alert-success" role="alert">Data berhasil disimpan!</div>');
            return redirect('Alternatif');
        } else {
            $request->session()->flash('message', '<div class="alert alert-danger" role="alert">Data gagal disimpan!</div>');
            return redirect('Alternatif/tambah');
        }
    }

    public function perbandingan()
    {
        $id_user_level = session('log.id_user_level');
        
        if ($id_user_level != 1) {
            ?>
            <script>
                window.location='<?php echo url("Dashboard"); ?>'
                alert('Anda tidak berhak mengakses halaman ini!');
            </script>
            <?php
        }
        
        $data['page'] = "Alternatif";
        $data['list'] = KriteriaModel::all();
        return view('alternatif.perbandingan', $data);
    }

    public function edit($id_alternatif)
    {
        $id_user_level = session('log.id_user_level');
        
        if ($id_user_level != 1) {
            ?>
            <script>
                window.location='<?php echo url("Dashboard"); ?>'
                alert('Anda tidak berhak mengakses halaman ini!');
            </script>
            <?php
        }

        $data['page'] = "Alternatif";
        $data['alternatif'] = AlternatifModel::findOrFail($id_alternatif);
        return view('alternatif.edit', $data);
    }

    public function update(Request $request, $id_alternatif)
    {
        $id_user_level = session('log.id_user_level');
        
        if ($id_user_level != 1) {
            ?>
            <script>
                window.location='<?php echo url("Dashboard"); ?>'
                alert('Anda tidak berhak mengakses halaman ini!');
            </script>
            <?php
        }

        $this->validate($request, [
            'nama' => 'required'
        ]);

        $data = [
            'nama' => $request->nama
        ];

        $alternatif = AlternatifModel::findOrFail($id_alternatif);
        $alternatif->update($data);

        $request->session()->flash('message', '<div class="alert alert-success" role="alert">Data berhasil diupdate!</div>');
        return redirect('Alternatif');
    }

    public function destroy(Request $request, $id_alternatif)
    {
        $id_user_level = session('log.id_user_level');
        
        if ($id_user_level != 1) {
            ?>
            <script>
                window.location='<?php echo url("Dashboard"); ?>'
                alert('Anda tidak berhak mengakses halaman ini!');
            </script>
            <?php
        }
        
        AlternatifModel::findOrFail($id_alternatif)->delete();
        $request->session()->flash('message', '<div class="alert alert-success" role="alert">Data berhasil dihapus!</div>');
        return redirect('Alternatif');
    }

    public function ahp_alternatif(Request $request, $id_kriteria)
    {
        $data['page'] = "Alternatif";
        $data['kriteria'] = KriteriaModel::findOrFail($id_kriteria);
        $data['alternatif'] = AlternatifModel::all();

        if ($request->has('save')) {
            AlternatifModel::delete_alternatif_ahp($id_kriteria);
            $i = 0;
            foreach ($data['alternatif'] as $row1) {
                $ii = 0;
                foreach ($data['alternatif'] as $row2) {
                    if ($i < $ii) {
                        $nilai_input = request()->input('nilai_' . $row1->id_alternatif . '_' . $row2->id_alternatif);
                        $nilai_1 = 0;
                        $nilai_2 = 0;
                        if ($nilai_input < 1) {
                            $nilai_1 = abs($nilai_input);
                            $nilai_2 = 1 / abs($nilai_input);
                        } elseif ($nilai_input > 1) {
                            $nilai_1 = 1 / abs($nilai_input);
                            $nilai_2 = abs($nilai_input);
                        } elseif ($nilai_input == 1) {
                            $nilai_1 = 1;
                            $nilai_2 = 1;
                        }
                        $params = array(
                            'id_kriteria' => $id_kriteria,
                            'id_alternatif_1' => $row1->id_alternatif,
                            'id_alternatif_2' => $row2->id_alternatif,
                            'nilai_1' => $nilai_1,
                            'nilai_2' => $nilai_2,
                        );
                        AlternatifModel::add_alternatif_ahp($params);
                    }
                    $ii++;
                }
                $i++;
            }
            $request->session()->flash('message', '<div class="alert alert-success" role="alert">Nilai perbandingan alternatif berhasil disimpan!</div>');
            return redirect()->route('ahp_alternatif', ['id_kriteria' => $id_kriteria]);
        }

        if ($request->has('check')) {
            if (count($data['alternatif']) < 3) {					
                $request->session()->flash('pesan_error', '<div class="alert alert-success" role="alert">Jumlah alternatif kurang, minimal 3!</div>');
            } else {
                $id_alternatif = array();
                foreach ($data['alternatif'] as $row)
                    $id_alternatif[] = $row->id_alternatif;
            }

            $matrik_alternatif = $this->ahp_get_matrik_alternatif($id_kriteria, $id_alternatif);
            $jumlah_kolom = $this->ahp_get_jumlah_kolom($matrik_alternatif);
            $matrik_normalisasi = $this->ahp_get_normalisasi($matrik_alternatif, $jumlah_kolom);
            $prioritas = $this->ahp_get_prioritas($matrik_normalisasi);
            $matrik_baris = $this->ahp_get_matrik_baris($prioritas, $matrik_alternatif);
            $jumlah_matrik_baris = $this->ahp_get_jumlah_matrik_baris($matrik_baris);
            $hasil_tabel_konsistensi = $this->ahp_get_tabel_konsistensi($jumlah_matrik_baris, $prioritas);
            if ($this->ahp_uji_konsistensi($hasil_tabel_konsistensi)) {
                AlternatifModel::delete_penilaian($id_kriteria);
                $request->session()->flash('message', '<div class="alert alert-success" role="alert">Nilai perbandingan : KONSISTEN!</div>');
                $i = 0;
                foreach ($data['alternatif'] as $row) {
                    $params = array(
                        'id_kriteria' => $id_kriteria,
                        'id_alternatif' => $row->id_alternatif,
                        'nilai' => $prioritas[$i],
                    );
                    AlternatifModel::add_penilaian($params);
                    $i++;
                }

                $data['list_data'] = $this->tampil_data_1($id_kriteria, $matrik_alternatif, $jumlah_kolom);
                $data['list_data2'] = $this->tampil_data_2($id_kriteria, $matrik_normalisasi, $prioritas);
                $data['list_data3'] = $this->tampil_data_3($id_kriteria, $matrik_baris, $jumlah_matrik_baris);
                $list_data = $this->tampil_data_4($id_kriteria, $jumlah_matrik_baris, $prioritas, $hasil_tabel_konsistensi);
                $data['list_data4'] = $list_data[0];
                $data['list_data5'] = $list_data[1];
            } else {
                $request->session()->flash('message', '<div class="alert alert-success" role="alert">Nilai perbandingan : TIDAK KONSISTEN</div>');
            }
        }

        $result = array();
        $i = 0;
        foreach ($data['alternatif'] as $row1) {
            $ii = 0;
            foreach ($data['alternatif'] as $row2) {
                if ($i < $ii) {
                    $kriteria_ahp = AlternatifModel::get_alternatif_ahp($id_kriteria, $row1->id_alternatif, $row2->id_alternatif);
                    if (empty($kriteria_ahp)) {
                        $params = array(
                            'id_kriteria' => $id_kriteria,
                            'id_alternatif_1' => $row1->id_alternatif,
                            'id_alternatif_2' => $row2->id_alternatif,
                            'nilai_1' => 1,
                            'nilai_2' => 1,
                        );
                        AlternatifModel::add_alternatif_ahp($params);
                        $nilai_1 = 1;
                        $nilai_2 = 1;
                    } else {
                        $nilai_1 = $kriteria_ahp->nilai_1;
                        $nilai_2 = $kriteria_ahp->nilai_2;
                    }
                    $nilai = 0;
                    if ($nilai_1 < 1) {
                        $nilai = $nilai_2;
                    } elseif ($nilai_1 > 1) {
                        $nilai = -$nilai_1;
                    } elseif ($nilai_1 == 1) {
                        $nilai = 1;
                    }
                    $result[$row1->id_alternatif][$row2->id_alternatif] = $nilai;
                }
                $ii++;
            }
            $i++;
        }

        $data['alternatif_ahp'] = $result;
        $data['id_kriteria'] = $id_kriteria;
        return view('alternatif.ahp_alternatif', $data);
    }

    public function reset(Request $request, $id_kriteria)
    {
        AlternatifModel::delete_alternatif_ahp($id_kriteria);
        $params = array(
            'nilai' => null,
        );
        AlternatifModel::update_prioritas($id_kriteria, $params);
        $request->session()->flash('message', '<div class="alert alert-success" role="alert">Data berhasil direset!</div>');
        return redirect()->route('ahp_alternatif', ['id_kriteria' => $id_kriteria]);

    }
		
    
    public function ahp_get_matrik_alternatif($id_kriteria, $alternatif)
    {
        $matrik = array();
        $i = 0;
        foreach ($alternatif as $row1) {
            $ii = 0;
            foreach ($alternatif as $row2) {
                if ($i == $ii) {
                    $matrik[$i][$ii] = 1;
                } else {
                    if ($i < $ii) {
                        $alternatif_ahp = AlternatifModel::get_alternatif_ahp($id_kriteria, $row1, $row2);
                        if (empty($alternatif_ahp)) {
                            $matrik[$i][$ii] = 1;
                            $matrik[$ii][$i] = 1;
                        } else {
                            $matrik[$i][$ii] = $alternatif_ahp->nilai_1;
                            $matrik[$ii][$i] = $alternatif_ahp->nilai_2;
                        }
                    }
                }
                $ii++;
            }
            $i++;
        }
        return $matrik;
    }

    public function ahp_get_jumlah_kolom($matrik)
    {
        $jumlah_kolom = array();
        for ($i = 0; $i < count($matrik); $i++) {
            $jumlah_kolom[$i] = 0;
            for ($ii = 0; $ii < count($matrik); $ii++) {
                $jumlah_kolom[$i] = $jumlah_kolom[$i] + $matrik[$ii][$i];
            }
        }
        return $jumlah_kolom;
    }

    public function ahp_get_normalisasi($matrik, $jumlah_kolom)
    {
        $matrik_normalisasi = array();
        for ($i = 0; $i < count($matrik); $i++) {
            for ($ii = 0; $ii < count($matrik); $ii++) {
                $matrik_normalisasi[$i][$ii] = $matrik[$i][$ii] / $jumlah_kolom[$ii];
            }
        }
        return $matrik_normalisasi;
    }

    public function ahp_get_prioritas($matrik_normalisasi)
    {
        $prioritas = array();
        for ($i = 0; $i < count($matrik_normalisasi); $i++) {
            $prioritas[$i] = 0;
            for ($ii = 0; $ii < count($matrik_normalisasi); $ii++) {
                $prioritas[$i] = $prioritas[$i] + $matrik_normalisasi[$i][$ii];
            }
            $prioritas[$i] = $prioritas[$i] / count($matrik_normalisasi);
        }
        return $prioritas;
    }

    public function ahp_get_matrik_baris($prioritas, $matrik_alternatif)
    {
        $matrik_baris = array();
        for ($i = 0; $i < count($matrik_alternatif); $i++) {
            for ($ii = 0; $ii < count($matrik_alternatif); $ii++) {
                $matrik_baris[$i][$ii] = $prioritas[$ii] * $matrik_alternatif[$i][$ii];
            }
        }
        return $matrik_baris;
    }

    public function ahp_get_jumlah_matrik_baris($matrik_baris)
    {
        $jumlah_baris = array();
        for ($i = 0; $i < count($matrik_baris); $i++) {
            $jumlah_baris[$i] = 0;
            for ($ii = 0; $ii < count($matrik_baris); $ii++) {
                $jumlah_baris[$i] = $jumlah_baris[$i] + $matrik_baris[$i][$ii];
            }
        }
        return $jumlah_baris;
    }

    public function ahp_get_tabel_konsistensi($jumlah_matrik_baris, $prioritas)
    {
        $jumlah = array();
        for ($i = 0; $i < count($jumlah_matrik_baris); $i++) {
            $jumlah[$i] = $jumlah_matrik_baris[$i] + $prioritas[$i];
        }
        return $jumlah;
    }

    public function ahp_uji_konsistensi($tabel_konsistensi)
    {
        $jumlah = array_sum($tabel_konsistensi);
        $n = count($tabel_konsistensi);
        $lambda_maks = $jumlah / $n;
        $ci = ($lambda_maks - $n) / ($n - 1);
        $ir = array(0, 0, 0.58, 0.9, 1.12, 1.24, 1.32, 1.41, 1.45, 1.49, 1.51, 1.48, 1.56, 1.57, 1.59);
        if ($n <= 15) {
            $ir = $ir[$n - 1];
        } else {
            $ir = $ir[14];
        }
        $cr = $ci / $ir;

        if ($cr <= 0.1) {
            return true;
        } else {
            return false;
        }
    }
    
    public function tampil_data_1($id_kriteria, $matrik_alternatif, $jumlah_kolom)
    {
        $alternatif = AlternatifModel::all();
        $list_data = '';
        $list_data .= '<trclass="text-center"><td></td>';
        foreach ($alternatif as $row) {
            $list_data .= '<td class="text-center">' . $row->nama . '</td>';
        }
        $list_data .= '</tr>';
        $i = 0;
        foreach ($alternatif as $row) {
            $list_data .= '<tr class="text-center">';
            $list_data .= '<td>' . $row->nama . '</td>';
            $ii = 0;
            foreach ($alternatif as $row2) {
                $list_data .= '<td class="text-center">' . $matrik_alternatif[$i][$ii] . '</td>';
                $ii++;
            }
            $list_data .= '</tr>';
            $i++;
        }
        $list_data .= '<tr class="text-center"><td class="font-weight-bold">Jumlah</td>';
        for ($i = 0; $i < count($jumlah_kolom); $i++) {
            $list_data .= '<td class="text-center font-weight-bold">' . $jumlah_kolom[$i] . '</td>';
        }
        $list_data .= '</tr>';
        return $list_data;
    }

    public function tampil_data_2($id_kriteria, $matrik_normalisasi, $prioritas)
    {
        $alternatif = AlternatifModel::all();
        $list_data2 = '';
        $list_data2 .= '<tr class="text-center"><td></td>';
        foreach ($alternatif as $row) {
            $list_data2 .= '<td class="text-center">' . $row->nama . '</td>';
        }
        $list_data2 .= '<td class="text-center font-weight-bold">Jumlah</td>';
        $list_data2 .= '<td class="text-center font-weight-bold">Prioritas</td>';
        $list_data2 .= '</tr>';
        $i = 0;
        foreach ($alternatif as $row) {
            $list_data2 .= '<tr class="text-center">';
            $list_data2 .= '<td>' . $row->nama . '</td>';
            $jumlah = 0;
            $ii = 0;
            foreach ($alternatif as $row2) {
                $list_data2 .= '<td class="text-center">' . $matrik_normalisasi[$i][$ii] . '</td>';
                $jumlah += $matrik_normalisasi[$i][$ii];
                $ii++;
            }
            $list_data2 .= '<td class="text-center font-weight-bold">' . $jumlah . '</td>';
            $list_data2 .= '<td class="text-center font-weight-bold">' . $prioritas[$i] . '</td>';
            $list_data2 .= '</tr>';
            $i++;
        }
        return $list_data2;
    }

    public function tampil_data_3($id_kriteria, $matrik_baris, $jumlah_matrik_baris)
    {
        $alternatif = AlternatifModel::all();
        $list_data3 = '';
        $list_data3 .= '<tr class="text-center"><td></td>';
        foreach ($alternatif as $row) {
            $list_data3 .= '<td class="text-center">' . $row->nama . '</td>';
        }
        $list_data3 .= '<td class="text-center font-weight-bold">CM</td>';
        $list_data3 .= '</tr>';
        $i = 0;
        foreach ($alternatif as $row) {
            $list_data3 .= '<tr class="text-center">';
            $list_data3 .= '<td>' . $row->nama . '</td>';
            $ii = 0;
            foreach ($alternatif as $row2) {
                $list_data3 .= '<td class="text-center">' . $matrik_baris[$i][$ii] . '</td>';
                $ii++;
            }
            $list_data3 .= '<td class="text-center font-weight-bold">' . $jumlah_matrik_baris[$i] . '</td>';
            $list_data3 .= '</tr>';
            $i++;
        }
        return $list_data3;
    }

    public function tampil_data_4($id_kriteria, $jumlah_matrik_baris, $prioritas, $hasil_tabel_konsistensi)
    {
        $alternatif = AlternatifModel::all();
        $list_data4 = '';
        $list_data4 .= '<tr class="text-center"><td></td>';
        $list_data4 .= '<td class="text-center">CM</td>';
        $list_data4 .= '<td class="text-center">Prioritas</td>';
        $list_data4 .= '</tr>';
        $i = 0;
        foreach ($alternatif as $row) {
            $list_data4 .= '<tr class="text-center">';
            $list_data4 .= '<td>' . $row->nama . '</td>';
            $list_data4 .= '<td class="text-center">' . $jumlah_matrik_baris[$i] . '</td>';
            $list_data4 .= '<td class="text-center">' . $prioritas[$i] . '</td>';
            $list_data4 .= '</tr>';
            $i++;
        }
        
        $n = count($jumlah_matrik_baris);
        $lambda_maks = array_sum($jumlah_matrik_baris);
        $ci = ($lambda_maks - $n) / ($n - 1);
        $ir = array(0, 0, 0.58, 0.9, 1.12, 1.24, 1.32, 1.41, 1.45, 1.49, 1.51, 1.48, 1.56, 1.57, 1.59);
        if ($n <= 15) {
            $ir = $ir[$n - 1];
        } else {
            $ir = $ir[14];
        }
        $cr = $ci / $ir;

        $list_data5 = '';
        $list_data5 .= '<table class="table">
        <tr>
            <td width="20%">n </td>
            <td>= ' . $n . '</td>
        </tr>
        <tr>
            <td>λ maks</td>
            <td>= ' . $lambda_maks . '</td>
        </tr>
        <tr>
            <td>CI</td>
            <td>= ' . $ci . '</td>
        </tr>
        <tr>
            <td>CR</td>
            <td>= ' . $cr . '</td>
        </tr>
        <tr>
            <td>CR <= 0.1</td>';
                if ($cr <= 0.1) {
                    $list_data5 .= '
            <td>Konsisten</td>';
                } else {
                    $list_data5 .= '
            <td>Tidak Konsisten</td>';
                }
                $list_data5 .= '
        </tr>
        </table>';
        return array($list_data4, $list_data5);
    }

}
