<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use Illuminate\Support\Collection;
use Illuminate\Support\Facades\Validator;
use Illuminate\Support\Facades\Storage;
use Illuminate\Support\Str;

use DataTables;
use GuzzleHttp\Client;
use Carbon\Carbon;

use App\Models\Kriteria;
use App\Models\Alternatif;
use App\Models\AnalisaAlternatif;
use App\Models\AnalisaKriteria;
use App\Models\Nilai;

class AnalisaController extends Controller
{
    public function alternatif()
    {
        $data = [
            'subtitle' => 'Analisa Alternatif',
        ];

        return view('admin.app.content.analisa.alternatif', compact('data'));
    }

    public function kriteria()
    {
        $data = [
            'subtitle' => 'Analisa Kriteria',
        ];

        return view('admin.app.content.analisa.kriteria', compact('data'));
    }

    public function kriteriaTabel(Request $request)
    {
        if($request->isMethod('post')) {
            insertBobot($_POST['C11'],$_POST['nl1'],$_POST['C21'])?'':updateBobot($_POST['C11'],$_POST['nl1'],$_POST['C21']);
            insertBobot($_POST['C12'],$_POST['nl2'],$_POST['C32'])?'':updateBobot($_POST['C12'],$_POST['nl2'],$_POST['C32']);
            insertBobot($_POST['C13'],$_POST['nl3'],$_POST['C43'])?'':updateBobot($_POST['C13'],$_POST['nl3'],$_POST['C43']);
            insertBobot($_POST['C14'],$_POST['nl4'],$_POST['C54'])?'':updateBobot($_POST['C14'],$_POST['nl4'],$_POST['C54']);
            insertBobot($_POST['C25'],$_POST['nl5'],$_POST['C35'])?'':updateBobot($_POST['C25'],$_POST['nl5'],$_POST['C35']);
            insertBobot($_POST['C26'],$_POST['nl6'],$_POST['C46'])?'':updateBobot($_POST['C26'],$_POST['nl6'],$_POST['C46']);
            insertBobot($_POST['C27'],$_POST['nl7'],$_POST['C57'])?'':updateBobot($_POST['C27'],$_POST['nl7'],$_POST['C57']);
            insertBobot($_POST['C38'],$_POST['nl8'],$_POST['C48'])?'':updateBobot($_POST['C38'],$_POST['nl8'],$_POST['C48']);
            insertBobot($_POST['C39'],$_POST['nl9'],$_POST['C59'])?'':updateBobot($_POST['C39'],$_POST['nl9'],$_POST['C59']);
            insertBobot($_POST['C410'],$_POST['nl10'],$_POST['C510'])?'':updateBobot($_POST['C410'],$_POST['nl10'],$_POST['C510']);

            insertBobot($_POST['C21'],1/$_POST['nl1'],$_POST['C11'])?'':updateBobot($_POST['C21'],1/$_POST['nl1'],$_POST['C11']);
            insertBobot($_POST['C32'],1/$_POST['nl2'],$_POST['C12'])?'':updateBobot($_POST['C32'],1/$_POST['nl2'],$_POST['C12']);
            insertBobot($_POST['C43'],1/$_POST['nl3'],$_POST['C13'])?'':updateBobot($_POST['C43'],1/$_POST['nl3'],$_POST['C13']);
            insertBobot($_POST['C54'],1/$_POST['nl4'],$_POST['C14'])?'':updateBobot($_POST['C54'],1/$_POST['nl4'],$_POST['C14']);
            insertBobot($_POST['C35'],1/$_POST['nl5'],$_POST['C25'])?'':updateBobot($_POST['C35'],1/$_POST['nl5'],$_POST['C25']);
            insertBobot($_POST['C46'],1/$_POST['nl6'],$_POST['C26'])?'':updateBobot($_POST['C46'],1/$_POST['nl6'],$_POST['C26']);
            insertBobot($_POST['C57'],1/$_POST['nl7'],$_POST['C27'])?'':updateBobot($_POST['C57'],1/$_POST['nl7'],$_POST['C27']);
            insertBobot($_POST['C48'],1/$_POST['nl8'],$_POST['C38'])?'':updateBobot($_POST['C48'],1/$_POST['nl8'],$_POST['C38']);
            insertBobot($_POST['C59'],1/$_POST['nl9'],$_POST['C39'])?'':updateBobot($_POST['C59'],1/$_POST['nl9'],$_POST['C39']);
            insertBobot($_POST['C510'],1/$_POST['nl10'],$_POST['C410'])?'':updateBobot($_POST['C510'],1/$_POST['nl10'],$_POST['C410']);
        }

        $data = [
            'subtitle' => 'Analisa Kriteria Tabel',
        ];

        return view('admin.app.content.analisa.kriteria-tabel', compact('data'));
    }

    public function deleteKriteria()
    {
        $exec = AnalisaKriteria::delete();
        if($exec) {
            return redirect()->route('analisa.kriteria')->with('success', 'Data kriteria berhasil di hapus');
        } else {
            return redirect()->back()->with('error', 'Data kriteria gagal di hapus');
        }
    }

    public function alternatifTabel(Request $request)
    {
        $altKriteria = !empty($request->kriteria) ? $request->kriteria : $request->query('kriteria');
        if(!empty($altKriteria)) {
            $namaKriteria = Kriteria::where('id_kriteria', $altKriteria)->first()->nama_kriteria;
        }

        if($request->isMethod('post')) {
            insert($_POST['A11'],$_POST['nl1'],$_POST['A21'],$altKriteria)?'':update($_POST['A11'],$_POST['nl1'],$_POST['A21'],$altKriteria);
            insert($_POST['A12'],$_POST['nl2'],$_POST['A32'],$altKriteria)?'':update($_POST['A12'],$_POST['nl2'],$_POST['A32'],$altKriteria);
            insert($_POST['A13'],$_POST['nl3'],$_POST['A43'],$altKriteria)?'':update($_POST['A13'],$_POST['nl3'],$_POST['A43'],$altKriteria);
            insert($_POST['A14'],$_POST['nl4'],$_POST['A54'],$altKriteria)?'':update($_POST['A14'],$_POST['nl4'],$_POST['A54'],$altKriteria);
            insert($_POST['A25'],$_POST['nl5'],$_POST['A35'],$altKriteria)?'':update($_POST['A25'],$_POST['nl5'],$_POST['A35'],$altKriteria);
            insert($_POST['A26'],$_POST['nl6'],$_POST['A46'],$altKriteria)?'':update($_POST['A26'],$_POST['nl6'],$_POST['A46'],$altKriteria);
            insert($_POST['A27'],$_POST['nl7'],$_POST['A57'],$altKriteria)?'':update($_POST['A27'],$_POST['nl7'],$_POST['A57'],$altKriteria);
            insert($_POST['A38'],$_POST['nl8'],$_POST['A48'],$altKriteria)?'':update($_POST['A38'],$_POST['nl8'],$_POST['A48'],$altKriteria);
            insert($_POST['A39'],$_POST['nl9'],$_POST['A59'],$altKriteria)?'':update($_POST['A39'],$_POST['nl9'],$_POST['A59'],$altKriteria);
            insert($_POST['A410'],$_POST['nl10'],$_POST['A510'],$altKriteria)?'':update($_POST['A410'],$_POST['nl10'],$_POST['A510'],$altKriteria);

            insert($_POST['A21'],1/$_POST['nl1'],$_POST['A11'],$altKriteria)?'':update($_POST['A21'],1/$_POST['nl1'],$_POST['A11'],$altKriteria);
            insert($_POST['A32'],1/$_POST['nl2'],$_POST['A12'],$altKriteria)?'':update($_POST['A32'],1/$_POST['nl2'],$_POST['A12'],$altKriteria);
            insert($_POST['A43'],1/$_POST['nl3'],$_POST['A13'],$altKriteria)?'':update($_POST['A43'],1/$_POST['nl3'],$_POST['A13'],$altKriteria);
            insert($_POST['A54'],1/$_POST['nl4'],$_POST['A14'],$altKriteria)?'':update($_POST['A54'],1/$_POST['nl4'],$_POST['A14'],$altKriteria);
            insert($_POST['A35'],1/$_POST['nl5'],$_POST['A25'],$altKriteria)?'':update($_POST['A35'],1/$_POST['nl5'],$_POST['A25'],$altKriteria);
            insert($_POST['A46'],1/$_POST['nl6'],$_POST['A26'],$altKriteria)?'':update($_POST['A46'],1/$_POST['nl6'],$_POST['A26'],$altKriteria);
            insert($_POST['A57'],1/$_POST['nl7'],$_POST['A27'],$altKriteria)?'':update($_POST['A57'],1/$_POST['nl7'],$_POST['A27'],$altKriteria);
            insert($_POST['A48'],1/$_POST['nl8'],$_POST['A38'],$altKriteria)?'':update($_POST['A48'],1/$_POST['nl8'],$_POST['A38'],$altKriteria);
            insert($_POST['A59'],1/$_POST['nl9'],$_POST['A39'],$altKriteria)?'':update($_POST['A59'],1/$_POST['nl9'],$_POST['A39'],$altKriteria);
            insert($_POST['A510'],1/$_POST['nl10'],$_POST['A410'],$altKriteria)?'':update($_POST['A510'],1/$_POST['nl10'],$_POST['A410'],$altKriteria);
        }

        $data = [
            'subtitle' => 'Analisa Alternatif Tabel',
        ];

        return view('admin.app.content.analisa.alternatif-tabel', compact('data', 'altKriteria', 'namaKriteria'));
    }

    public function deleteAlternatif()
    {
        $exec = AnalisaAlternatif::delete();
        if($exec) {
            return redirect()->route('analisa.alternatif')->with('success', 'Data alternatif berhasil di hapus');
        } else {
            return redirect()->back()->with('error', 'Data alternatif gagal di hapus');
        }
    }
}
