<?php

namespace App\Http\Controllers;

use App\OrtuUsers;
use Illuminate\Support\Facades\Validator;
use Illuminate\Http\Request;
use Carbon\Carbon;
use App\DeviceUser;
use DB;

class OrtuUsersController extends Controller
{
    /**
     * Create a new controller instance.
     *
     * @return void
     */
    public function __construct()
    {
        //
    }

    //
    public function userLogin(Request $request){
        $validator = Validator::make($request->all(), [
            'username' => 'required',
            'password' => 'required'
        ]);

        if($validator->fails()){
            return response()->json([
                'error' => true,
                'message' => $validator->errors()->all(),
                'ortu' => null
            ]);
        }

        $ortu = OrtuUsers::where('username', $request->input('username'))->first();

        if(count(array($ortu))){
            if(password_verify($request->input('password'), $ortu->password)){
                unset($siswa->password);

                $device = DeviceUser::create([
                    'user_device_id' => $request->input('token_user'),
                    'ortu_id' => $ortu->pk_ortu,
                    'siswa_id' => $ortu->fk_siswa,
                    'date_created' => Carbon::now()
                ]);

                return response()->json(['error' => false, 
                'message' => 'Login Sukses',
                'ortu' => $ortu]);
            } else {
                return response()->json(['error' => true, 
                'message' => 'Password Salah',
                'ortu' => null]);
            }
        } else {
            return response()->json(['error' => true, 
            'message' => 'Anda belum terdaftar',
            'ortu' => null]);
        }
    }

    public function getLaporanSiswa($pkOrtu, $bulan, $tahun){
        $laporanSiswa = DB::table('siswa_presensi_utama as a')
            ->join('ortu_users as b', 'a.fk_siswa', '=', 'b.fk_siswa')
            ->select(DB::raw('a.pk_presensi, b.pk_ortu, a.kd_sekolah, a.tanggal_absen, MONTH(a.tanggal_absen) as bulan, YEAR(a.tanggal_absen) as tahun, a.jam_datang, a.jam_pulang, a.lat, a.long'))
            ->where('b.pk_ortu', $pkOrtu)
            ->whereRaw('MONTH(a.tanggal_absen) = ?', [$bulan])
            ->whereRaw('YEAR(a.tanggal_absen) = ?', [$tahun])
            ->orderBy('tanggal_absen', 'desc')
            ->get();
        
        if(!$laporanSiswa->isEmpty()){
            return response()->json([
                'error' => false,
                'message' => 'Daftar filter laporan ditemukan',
                'laporan' => $laporanSiswa
            ]);
        } else {
            return response()->json([
                'error' => true,
                'message' => 'Daftar filter laporan tidak ditemukan',
                'laporan' => null
            ]);
        }
    }

    public function getSanksiSiswa($idOrtu, $tahunAjar)
    {
        $listSanksi = DB::table('akd_sanksi_siswa as a')
            ->select('a.tanggal_sanksi', 'c.sanksi', 'c.poin')
            ->join('akd_akademisi as b', 'a.fk_akademisi', '=', 'b.pk_akademisi')
            ->join('akd_sanksi as c', 'c.pk_sanksi', '=', 'a.fk_sanksi')
            ->join('ortu_users as d', 'b.fk_siswa', '=', 'd.fk_siswa')
            ->where('d.pk_ortu', $idOrtu)
            ->whereRaw('YEAR(a.tanggal_sanksi) = ?', [$tahunAjar])
            ->orderBy('a.tanggal_sanksi', 'desc')
            ->get();
        
        if(!$listSanksi->isEmpty()){
            return response()->json([
                'error' => false,
                'sanksi' =>  $listSanksi
            ]);
        } else {
            return response()->json([
                'error' => true,
                'sanksi' => null
            ]);
        }
    }

    public function getPembayaranSiswa($fkSiswa, $tahunTagihan){
        $pembayaranSiswa = DB::table('keu_penerimaan as a')
            ->join('keu_penerimaan_detail as b', 'a.pk_penerimaan', '=', 'b.fk_penerimaan')
            ->join('keu_tarif as c', 'c.pk_tarif', '=', 'a.fk_tarif')
            ->join('keu_jenis_tarif as d', 'd.pk_jenis_tarif', '=', 'c.fk_jenis_tarif')
            ->join('akd_akademisi as e', 'e.pk_akademisi', '=', 'a.fk_akademisi')
            ->join('akd_siswa as f', 'f.pk_siswa', '=', 'e.fk_siswa')
            ->join('akd_tahun_ajar as g', 'g.pk_tahun_ajar', '=', 'e.fk_tahun_ajar')
            ->where('e.fk_siswa', $fkSiswa)
            ->whereRaw('g.kode_tahun = ?', [$tahunTagihan])
            ->whereBetween('a.tanggal_tagihan', [DB::raw('g.periode_awal'), DB::raw('g.periode_akhir')])
            ->orderBy('a.tanggal_tagihan', 'desc')
            ->select(DB::raw('b.tgl_bayar_detil, d.nama_tarif, MONTH(a.tanggal_tagihan) as bulan_tagihan, a.biaya, b.subtotal_bayar, a.keringanan, g.kode_tahun, g.tahun_ajar, g.periode_awal, g.periode_akhir'))
            ->get();

        if(!$pembayaranSiswa->isEmpty()){
            return response()->json([
                'error' => false,
                'pembayaran_siswa' => $pembayaranSiswa
            ]);
        } else {
            return response()->json([
                'error' => true,
                'pembayaran_siswa' => null
            ]);
        }
    }

    public function getTahunAjaran()
    {
        $tahunAjaran = DB::table('akd_tahun_ajar')
                        ->select('kode_tahun', 'tahun_ajar', 'periode_awal', 'periode_akhir')
                        ->orderBy('kode_tahun', 'asc')
                        ->get();

        if($tahunAjaran){
            return response()->json([
                'error' => false,
                'data_tahun_ajar' => $tahunAjaran
            ]);
        } else {
            return response()->json([
                'error' => true,
                'data_tahun_ajar' => null
            ]);
        }
    }

    public function getFilterPembayaran($fkSiswa, $kodeTahun, $tglAwal, $tglAkhir)
    {
        $pembayaranSiswa = DB::table('keu_penerimaan as a')
            ->join('keu_penerimaan_detail as b', 'a.pk_penerimaan', '=', 'b.fk_penerimaan')
            ->join('keu_tarif as c', 'c.pk_tarif', '=', 'a.fk_tarif')
            ->join('keu_jenis_tarif as d', 'd.pk_jenis_tarif', '=', 'c.fk_jenis_tarif')
            ->join('akd_akademisi as e', 'e.pk_akademisi', '=', 'a.fk_akademisi')
            ->join('akd_siswa as f', 'f.pk_siswa', '=', 'e.fk_siswa')
            ->join('akd_tahun_ajar as g', 'g.pk_tahun_ajar', '=', 'e.fk_tahun_ajar')
            ->where('e.fk_siswa', $fkSiswa)
            ->whereRaw('g.kode_tahun = ?', [$kodeTahun])
            ->whereBetween('a.tanggal_tagihan', [$tglAwal, $tglAkhir])
            ->orderBy('a.tanggal_tagihan', 'desc')
            ->select(DB::raw('b.tgl_bayar_detil, d.nama_tarif, MONTH(a.tanggal_tagihan) as bulan_tagihan, a.biaya, b.subtotal_bayar, a.keringanan, g.kode_tahun, g.tahun_ajar, g.periode_awal, g.periode_akhir'))
            ->get();

        if(!$pembayaranSiswa->isEmpty()){
            return response()->json([
                'error' => false,
                'pembayaran_siswa' => $pembayaranSiswa
            ]);
        } else {
            return response()->json([
                'error' => true,
                'pembayaran_siswa' => null
            ]);
        }
    }

    public function logoutOrtu(Request $request)
    {
        # code...
        $ortuId = $request->input('id_ortu');
        $device = $request->input('device_id');

        $ortuDevice = DB::table('device_users')
            ->where('ortu_id', $ortuId)
            ->where('user_device_id', $device)
            ->delete();
        
        if($ortuDevice){
            return response()->json([
                'error' => false,
                'message' => 'Logout Sukses'
            ]);
        } else {
            return response()->json([
                'error' => true,
                'message' => 'Upps terjadi kesalahan'
            ]);
        }
    }
}
