
<?php 
session_start();
include 'koneksi.php';

require_once 'config_001.php';

if (isset($config_001[$kode_aplikasi])) {
    $host       = $config[$kode_aplikasi]['host'];
    $username   = $config[$kode_aplikasi]['username'];
    $password   = $config[$kode_aplikasi]['password'];
    $database   = $config[$kode_aplikasi]['database'];

    // koneksi ke database
    $koneksi    = mysqli_connect($host, $username, $password, $database);
} else {
    // kode aplikasi tidak ditemukan
    die('Koneksi Database Gagal ke SERVER database - Kode Koneksi '.$kode_aplikasi.' tidak dikenal');
}
///////////////////////////////////////////////

$usere = $_POST['usere'];
$passw = $_POST['passw'];

$query = mysqli_query($koneksi, "SELECT * FROM user WHERE user_username = '$usere'");
$data = mysqli_fetch_assoc($query);

	$_SESSION['id']         = $data['user_id'];
	$_SESSION['nama_user']  = $data['user_nama'];
	$_SESSION['level']      = $data['user_level'];
	$_SESSION['username']   = $data['user_username'];
	$_SESSION['id_kar']     = $data['id_kar'];
	$_SESSION['kdc']        = '001';
	$_SESSION['status']     = "administrator_logedin";
	$_SESSION['hosting']    = $host ;
	$_SESSION['lokasi_gambar']='https://koperasidesamerahputih.xyz/masjid_001/gambar';
	
    $karyawan = mysqli_query($koneksi, "SELECT nama AS tnama FROM karyawan WHERE id='$data[id_kar]'");
    list($tnama) = mysqli_fetch_array($karyawan);
    $_SESSION['nama_karyawan'] = $tnama;
    
    $kdkanca = mysqli_query($koneksi, "SELECT kanca,alamat,foto_bag,kelurahan,id_warna,nama_singkat,gambar FROM kdkanca WHERE kd='$_SESSION[kdc]'");
    list($kanca,$alamat,$foto_bag,$kelurahan,$id_warna,$nama_singkat,$gambar) = mysqli_fetch_array($kdkanca);
    $_SESSION['kanca']      = $kanca;
    $_SESSION['alamat']     = $alamat;
    $_SESSION['kelurahan']  = $kelurahan;
    $_SESSION['foto_bag']   = $foto_bag;
    $_SESSION['nama_singkat']  = $nama_singkat;
    $_SESSION['logo_kop']   = $gambar;

    $kdwarna = mysqli_query($koneksi,"select * from kdwarna where id='$id_warna'");
    $w = mysqli_fetch_assoc($kdwarna);            
    $_SESSION['warna_bag']= $w['warna'] ; 

    /////////////////////// tgl_dr dan tgl_sd
            $date_awal_bulan = date('Y-m-01'); // Tanggal awal bulan ini
            $date_hari_ini = date('Y-m-d'); // Tanggal hari ini
            
            $_SESSION['tgl_dr'] = $date_awal_bulan;
            $_SESSION['tgl_sd'] = $date_hari_ini;
            $tgl_awal= '2025-01-01' ;
            $_SESSION['tgl_awal'] = $tgl_awal;
    ////////////////////////////////////////////////////////////////////
$hash = $data['user_password_hash'];    
if (password_verify($passw, $hash)) {
    // Password valid, maka pengguna dapat masuk    
	header("location:admin/");
}else{
	header("location:index.php?alert=gagal");
}