<?php

if (!$db) {
    include "$DOCUMENT_ROOT/s/config.php";
}

echo "<style>
.box-container {
display: grid;
/* width: 466px;
height: 100%; */
/*grid-template-areas: \"a-Ogl2E a-XQx07-0 a-mqRn2-0\"
\"right a-XQx07-1 a-mqRn2-1\"
\"a-pZwqR-0 a-pZwqR-1 a-pZwqR-2\";*/
/* grid-template-columns: 1fr 1fr 1fr;
grid-template-rows: 1fr 1fr 1fr 1fr; */
grid-template-columns: repeat(auto-fit, minmax(150px, 1fr));
}
.box-container > div {
/*border: 1px dashed #888;*/
/*height: 80px;*/
padding: 4px;
margin: 4px;
}

.box-jo {
    -webkit-box-shadow: 4px 4px 11px -6px rgba(0,0,0,0.5);
    box-shadow: 4px 4px 11px -6px rgba(0,0,0,0.5);
    background: #EAEAEA;
    height: 61px;
    padding: 4px;
    position: relative;
    border-radius: 6px;
}
.box-jo:hover {
    -webkit-box-shadow: 5px 5px 14px -2px rgba(0,0,0,0.9);
    box-shadow: 5px 5px 14px -2px rgba(0,0,0,0.9);
    background: #F4F4F4;
}
body#tinymce {
    background: #FFFFFF
}

.box-jml {
    position: absolute;
    text-align: right;
    bottom: 0;
    right: 0;
    padding: 4px;
}
</style>";

$mpp_id = $f->getLastMppUsed();
$kd_satker = $db->getOne("select b.kode_satker_organisasi from spg_data_current a inner join spg_08_unit_organisasi b on a.kd_unit_org=b.kd_unit_org where a.nip='$login_nip'");
$sql_check_kirim = "select sts_kirim from REC_SATKER_AKTIFITAS where mpp_id='$mpp_id' and kd_satker = '$kd_satker' order by tgl_kirim desc";
$sts_kirim_satker = 0;
$sts_kirim_satker = $db->getOne($sql_check_kirim);

$sql_check = "select c.TANGGAL_PUBLISH,c.TANGGAL_SELESAI, c.JAM_SELESAI, c.TGL_BATAS_WAKTU_REKOMENDASI, c.JAM_BATAS_WAKTU_REKOMENDASI, c.STS_LOCK
from SPG_PEGAWAI_ELIGIBLE a
left join REC_RENCANA_PEMENUHAN_PEG b on a.PERIODE_PEMENUHAN=b.MPP_ID
inner join REC_PERMINTAAN_ADVERTISING c on b.ID_FORMASI=c.ID_FORMASI
where a.LEVEL_JABATAN_PEMENUHAN=b.NM_GOL and  a.nip='$login_nip' and a.eligible_combine = 'ELIGIBLE'
order by c.TANGGAL_SELESAI DESC, c.JAM_SELESAI desc";
$res = $f->get_last_record($sql_check);
// $f->pre($res);
$tgl_publish_posting_jo = substr($res['tanggal_publish'], 0, 10);
$tgl_selesai_posting_jo = $f->convert_date($res['tanggal_selesai']);
$jam_selesai_posting_jo = $res['jam_selesai'];
$sts_lock_posting_jo = $res['sts_lock'];
$time_publish_job_opening = strtotime($tgl_publish_posting_jo." 00:01");
$time_now = strtotime("now");
$lowongan_internal = "";

if ($sts_lock_posting_jo && !empty($tgl_selesai_posting_jo) && !$sts_kirim_satker && $time_now > $time_publish_job_opening) {
	$sql_cek_fto = "SELECT * FROM dbo.REC_RENCANA_PEMENUHAN_PEG WHERE MPP_ID = '$mpp_id' AND LEFT(TRIM(dampak), 5) = '[FTO]'";
	$result_cek_fto = $db->Execute($sql_cek_fto);
	if (!$result_cek_fto) {
        print $db->ErrorMsg();
    }
	$num_cek_fto = $result_cek_fto->RecordCount();
	//print_r($num_cek_fto);
	/*if ($num_cek_fto>0) {
		$sql = "select
				b.kd_bidang,
				b.bidang, COUNT(*) AS jumlah
				from spg_pegawai_eligible a
				left join (
					select b.req_id,a.ptk_id,c.satker,e.NM_UNIT_ORG as depkom,c.nm_kantor as kantor, d.KD_UNIT_ES1 as kd_bidang,
					m.masa_dinas_minimal,m.sisa_dinas,m.tanggal_dinas, c.bidang, n.NM_PEND_UMUM,
					isnull(n.KD_PEND_UMUM,0) as kd_pend_umum, c.FORMASI as posisi_jabatan,a.kd_jabatan,b.tanggal_publish,
					b.tanggal_selesai,b.jam_selesai,m.kd_gol,a.mpp_id,m.tanggal_buat,a.NM_GOLONGAN,
					DATEADD(day, 0, DATEDIFF(day, 0, b.tanggal_selesai)) + DATEADD(day, 0 - DATEDIFF(day, 0, b.jam_selesai), b.jam_selesai) as closing_date,
					b.KETERANGAN
					from rec_ptk a
					left join rec_permintaan_advertising b ON a.ptk_id=b.req_id
					left join REC_RENCANA_PEMENUHAN_PEG c on a.ID_FORMASI=c.ID_FORMASI
					left join SPG_08_UNIT_ORGANISASI d on a.KD_UNIT_ORG=d.KD_UNIT_ORG
					left join SPG_08_UNIT_ORGANISASI e on d.KD_UNIT_ES2=e.KD_UNIT_ORG
					left join REC_PERSYARATAN m on (m.KD_GOL=a.KD_GOL and m.periode=a.mpp_id)
					left join SPG_JNS_PEND_UMUM n on m.PENDIDIKAN = n.KD_PEND_UMUM
					where b.ram_id='RAM-000020' and b.rac_id='RAC-000001'
				) b ON a.periode_pemenuhan = b.mpp_id and a.level_jabatan_pemenuhan = b.nm_golongan
				where 
				a.nip='$login_nip' 
				and b.closing_date >= getdate() 
				and a.eligible_combine = 'ELIGIBLE' 
				and b.mpp_id = '$mpp_id'
				and LEFT(TRIM(b.KETERANGAN), 5) = '[FTO]'
				GROUP BY b.kd_bidang, b.bidang
				ORDER BY b.kd_bidang";
	} else {*/
		/*$sql = "select
				b.kd_bidang,
				b.bidang, COUNT(*) AS jumlah,
				sum(b.fto) AS fto
				from spg_pegawai_eligible a
				left join (
					select b.req_id,a.ptk_id,c.satker,e.NM_UNIT_ORG as depkom,c.nm_kantor as kantor, d.KD_UNIT_ES1 as kd_bidang,
					m.masa_dinas_minimal,m.sisa_dinas,m.tanggal_dinas, c.bidang, n.NM_PEND_UMUM,
					isnull(n.KD_PEND_UMUM,0) as kd_pend_umum, c.FORMASI as posisi_jabatan,a.kd_jabatan,b.tanggal_publish,
					b.tanggal_selesai,b.jam_selesai,m.kd_gol,a.mpp_id,m.tanggal_buat,a.NM_GOLONGAN,
					DATEADD(day, 0, DATEDIFF(day, 0, b.tanggal_selesai)) + DATEADD(day, 0 - DATEDIFF(day, 0, b.jam_selesai), b.jam_selesai) as closing_date,
					CASE WHEN LEFT(LTRIM(t.alasan), 5) = '[FTO]' THEN 1 ELSE 0 END AS fto
					from rec_ptk a
					left join rec_permintaan_advertising b ON a.ptk_id=b.req_id
					left join REC_RENCANA_PEMENUHAN_PEG c on a.ID_FORMASI=c.ID_FORMASI
					left join SPG_08_UNIT_ORGANISASI d on a.KD_UNIT_ORG=d.KD_UNIT_ORG
					left join SPG_08_UNIT_ORGANISASI e on d.KD_UNIT_ES2=e.KD_UNIT_ORG
					left join REC_PERSYARATAN m on (m.KD_GOL=a.KD_GOL and m.periode=a.mpp_id)
					left join SPG_JNS_PEND_UMUM n on m.PENDIDIKAN = n.KD_PEND_UMUM
					left join REC_MPP_TARGET t on a.KD_JABATAN = t.KD_JABATAN and a.KD_UNIT_ORG = t.KD_UNIT_ORG and t.mpp_id = a.mpp_id
					where b.ram_id='RAM-000020' and b.rac_id='RAC-000001'
				) b ON a.periode_pemenuhan = b.mpp_id and a.level_jabatan_pemenuhan = b.nm_golongan
				where 
				a.nip='$login_nip' 
				and b.closing_date >= getdate() 
				and a.eligible_combine = 'ELIGIBLE' 
				and b.mpp_id = '$mpp_id'
				GROUP BY b.kd_bidang, b.bidang
				ORDER BY b.kd_bidang";*/
		$sql = "SELECT * FROM [dbo].[func_lowongan_summary] ('$mpp_id','$login_nip') ORDER BY kd_bidang";
	//}
	//echo $sql;
    $result = $db->Execute($sql);
    if (!$result) {
        print $db->ErrorMsg();
    }

    $jmllowongan = $result->RecordCount();

    if ($jmllowongan > 0) {
        echo "
		<script>
		$( document ).ready(function() {
			$('#runningtext').on('click', function(e) {
				//e.preventDefault();
				$('html, body').animate({
					scrollTop: ($('#section-jo').offset().top)
				}, 2000);
			});
		});
		</script>
		<div class=\"row col-md-12\">
            <!--a href=\"#section-jo\" style=\"color: #000 !important;\" -->
                <marquee id=\"runningtext\" onmouseover=\"this.stop();\" onmouseout=\"this.start();\" style='cursor: pointer;font-size: 14px;margin-bottom: 5px;'>
                    Yuk.. Segera daftarkan diri Anda.. Batas Waktu Pendaftaran Lowongan Jabatan $tgl_selesai_posting_jo Pukul $jam_selesai_posting_jo. Video cara melamar bisa dilihat <a href=\"http://onedrive.ojk.go.id/sites/DOSM/Shared%20Documents/SIMFOSIA/Job%20Opening/JO%202022.mp4\" target=\"_blank\"><span style=\"color: blue;font-weight: bold;\">disini</span></a>
                </marquee>
            <!-- /a -->
        </div>";
    }

	// link lama: http://onedrive.ojk.go.id/sites/DOSM/Shared%20Documents/Video%20JO/JO%202021%20watermark.mp4?csf=1&e=GZSZ0Z
	// link baru: http://onedrive.ojk.go.id/sites/DOSM/Shared%20Documents/SIMFOSIA/Job%20Opening/JO%202022.mp4
	$lowongan_internal = "<div style=\"text-align: center; padding-bottom: 10px;\">Video petunjuk cara melamar bisa dilihat <a href=\"http://onedrive.ojk.go.id/sites/DOSM/Shared%20Documents/SIMFOSIA/Job%20Opening/JO%202022.mp4\" target=\"_blank\"><span style=\"color: blue;font-weight: bold;\">disini</span></a></div>";
    $lowongan_internal .= "<div class=\"box-container\">";
    while ($row = $result->FetchRow()) {
		// jika ada fto
		if ($num_cek_fto>0) {
			$lowongan_internal .= "<div><div class=\"box-jo\"><a href=\"#\" onclick=\"javascrpt:parent.addTab('Lowongan Internal','/lowongan_internal_v3_detail.php?es1=" . $row['kd_bidang'] . "')\" title=\"" . $row['bidang'] . "\">" . $row['bidang'] . "</a><div class=\"box-jml\" title=\"Total " . $row['jumlah'] . " lowongan di " . $row['bidang'] . "\" style=\"width:100%; font-size:10px; cursor: default;\">
		
			<div style=\"width: 50%; float:left; text-align: left; font-size:10px; cursor: default;\">Reguler: " . $row['reguler'] . "</div>
			<div style=\"width: 50%; float:right; text-align: right; font-size:10px; cursor: default;\">FTO: " . $row['fto'] . "</div>
			
			</div></div></div>";
		} else {
			// jika tidak ada fto
			$lowongan_internal .= "<div><div class=\"box-jo\"><a href=\"#\" onclick=\"javascrpt:parent.addTab('Lowongan Internal','/lowongan_internal_v3_detail.php?es1=" . $row['kd_bidang'] . "')\" title=\"" . $row['bidang'] . "\">" . $row['bidang'] . "</a><div class=\"box-jml\" title=\"" . $row['jumlah'] . " lowongan di " . $row['bidang'] . "\">[" . $row['jumlah'] . "]</div></div></div>";
		}
    }
    $lowongan_internal .= "</div>";
}

//----------home
$sql1 = "SELECT DISTINCT
    a.tanggal_apply,
    a.status,
    b.req_id,
    b.ptk_id,
    b.satker,
    b.depkom,
    b.kantor,
    b.masa_dinas_minimal,
    b.sisa_dinas,
    b.tanggal_dinas,
    b.bidang,
    b.NM_PEND_UMUM,
    b.kd_pend_umum,
    b.posisi_jabatan,
    b.kd_jabatan,
    b.tanggal_publish,
    b.tanggal_selesai,
    b.jam_selesai,
    b.mpp_id,
    b.tanggal_buat,
    b.nilai1,
    b.nilai2,
    b.nilai3,
    b.closing_date,
    s.STATUS_PELAMAR,
    a.ID_ROOM
FROM
    rec_pelamar_internal a
    INNER JOIN
        (
            SELECT
                b.req_id,
                a.ptk_id,
                e.nm_unit_org AS satker,
                f.nm_unit_org AS depkom,
                c.nm_kantor AS kantor,
                m.masa_dinas_minimal,
                m.sisa_dinas,
                m.tanggal_dinas,
                g.nm_unit_org AS bidang,
                n.NM_PEND_UMUM,
                ISNULL(n.KD_PEND_UMUM, 0) AS kd_pend_umum,
                j.posisi AS posisi_jabatan,
                a.kd_jabatan,
                b.tanggal_publish,
                b.tanggal_selesai,
                b.jam_selesai,
                m.kd_gol,
                a.mpp_id,
                m.tanggal_buat,
                h.NM_GOLONGAN,
                m.nilai_kinerja AS nilai1,
                m.nilai_kinerja_1 AS nilai2,
                m.nilai_kinerja_2 AS nilai3,
                DATEADD(DAY, 0, DATEDIFF(DAY, 0, b.tanggal_selesai))
                + DATEADD(DAY, 0 - DATEDIFF(DAY, 0, b.jam_selesai), b.jam_selesai) AS closing_date,
                d.KD_UNIT_ES1
            FROM
                rec_ptk a
                LEFT JOIN
                rec_permintaan_advertising b ON
                    a.ptk_id = b.req_id
                LEFT JOIN
                SPG_08_KANTOR c ON
                    a.kd_kantor = c.kd_kantor
                LEFT JOIN
                spg_08_unit_organisasi d ON
                    a.kd_unit_org = d.kd_unit_org
                LEFT JOIN
                SPG_08_UNIT_ORGANISASI e ON
                    d.KD_UNIT_ES3 = e.KD_UNIT_ORG
                LEFT JOIN
                SPG_08_UNIT_ORGANISASI f ON
                    d.KD_UNIT_ES2 = f.KD_UNIT_ORG
                LEFT JOIN
                SPG_08_UNIT_ORGANISASI g ON
                    d.KD_UNIT_ES1 = g.KD_UNIT_ORG
                LEFT JOIN
                SPG_08_JABATAN_UNIT j ON
                    (
                        a.KD_JABATAN = j.KD_JABATAN
                        AND a.KD_UNIT_ORG = j.KD_UNIT_ORG
                    )
                LEFT JOIN
                SPG_08_JABATAN_STR k ON
                    a.kd_jabatan = k.KD_JABATAN_STR
                LEFT JOIN
                SPG_JABATAN_PANGKAT l ON
                    a.kd_jabatan = l.KD_JABATAN_STR
                LEFT JOIN
                REC_PERSYARATAN m ON
                    (
                        m.KD_GOL = l.KD_GOL
                        AND m.periode = a.mpp_id
                    )
                LEFT JOIN
                spg_golongan h ON
                    m.kd_gol = h.kd_gol
                LEFT JOIN
                spg_eselon i ON
                    h.KD_ESELON = i.KD_ESELON
                LEFT JOIN
                SPG_JNS_PEND_UMUM n ON
                    m.PENDIDIKAN = n.KD_PEND_UMUM
            WHERE
                b.ram_id = 'RAM-000020'
                AND b.rac_id = 'RAC-000001'
        ) b ON
        a.req_id = b.req_id
    INNER JOIN REC_STATUS_PELAMAR as s ON a.status = s.KD_STATUS_PELAMAR
WHERE
    a.nip = '$login_nip'
    AND b.mpp_id = '$mpp_id'
UNION ALL
SELECT DISTINCT
    a.tanggal_apply,
    a.status,
    b.req_id,
    b.ptk_id,
    b.satker,
    b.depkom,
    b.kantor,
    b.masa_dinas_minimal,
    b.sisa_dinas,
    b.tanggal_dinas,
    b.bidang,
    b.NM_PEND_UMUM,
    b.kd_pend_umum,
    b.posisi_jabatan,
    b.kd_jabatan,
    b.tanggal_publish,
    b.tanggal_selesai,
    b.jam_selesai,
    b.mpp_id,
    b.tanggal_buat,
    b.nilai1,
    b.nilai2,
    b.nilai3,
    b.closing_date,
    s.STATUS_PELAMAR,
    a.ID_ROOM
FROM
    REC_PELAMAR_INTERNAL_HISTORY a
    INNER JOIN
        (
            SELECT
                b.req_id,
                a.ptk_id,
                e.nm_unit_org AS satker,
                f.nm_unit_org AS depkom,
                c.nm_kantor AS kantor,
                m.masa_dinas_minimal,
                m.sisa_dinas,
                m.tanggal_dinas,
                g.nm_unit_org AS bidang,
                n.NM_PEND_UMUM,
                ISNULL(n.KD_PEND_UMUM, 0) AS kd_pend_umum,
                j.posisi AS posisi_jabatan,
                a.kd_jabatan,
                b.tanggal_publish,
                b.tanggal_selesai,
                b.jam_selesai,
                m.kd_gol,
                a.mpp_id,
                m.tanggal_buat,
                h.NM_GOLONGAN,
                m.nilai_kinerja AS nilai1,
                m.nilai_kinerja_1 AS nilai2,
                m.nilai_kinerja_2 AS nilai3,
                DATEADD(DAY, 0, DATEDIFF(DAY, 0, b.tanggal_selesai))
                + DATEADD(DAY, 0 - DATEDIFF(DAY, 0, b.jam_selesai), b.jam_selesai) AS closing_date,
                d.KD_UNIT_ES1
            FROM
                rec_ptk a
                LEFT JOIN
                rec_permintaan_advertising b ON
                    a.ptk_id = b.req_id
                LEFT JOIN
                SPG_08_KANTOR c ON
                    a.kd_kantor = c.kd_kantor
                LEFT JOIN
                spg_08_unit_organisasi d ON
                    a.kd_unit_org = d.kd_unit_org
                LEFT JOIN
                SPG_08_UNIT_ORGANISASI e ON
                    d.KD_UNIT_ES3 = e.KD_UNIT_ORG
                LEFT JOIN
                SPG_08_UNIT_ORGANISASI f ON
                    d.KD_UNIT_ES2 = f.KD_UNIT_ORG
                LEFT JOIN
                SPG_08_UNIT_ORGANISASI g ON
                    d.KD_UNIT_ES1 = g.KD_UNIT_ORG
                LEFT JOIN
                SPG_08_JABATAN_UNIT j ON
                    (
                        a.KD_JABATAN = j.KD_JABATAN
                        AND a.KD_UNIT_ORG = j.KD_UNIT_ORG
                    )
                LEFT JOIN
                SPG_08_JABATAN_STR k ON
                    a.kd_jabatan = k.KD_JABATAN_STR
                LEFT JOIN
                SPG_JABATAN_PANGKAT l ON
                    a.kd_jabatan = l.KD_JABATAN_STR
                LEFT JOIN
                REC_PERSYARATAN m ON
                    (
                        m.KD_GOL = l.KD_GOL
                        AND m.periode = a.mpp_id
                    )
                LEFT JOIN
                spg_golongan h ON
                    m.kd_gol = h.kd_gol
                LEFT JOIN
                spg_eselon i ON
                    h.KD_ESELON = i.KD_ESELON
                LEFT JOIN
                SPG_JNS_PEND_UMUM n ON
                    m.PENDIDIKAN = n.KD_PEND_UMUM
            WHERE
                b.ram_id = 'RAM-000020'
                AND b.rac_id = 'RAC-000001'
        ) b ON
        a.req_id = b.req_id
    INNER JOIN REC_STATUS_PELAMAR as s ON a.status = s.KD_STATUS_PELAMAR
WHERE
    a.nip = '$login_nip'
    AND a.status = '11'
    AND b.mpp_id = '$mpp_id'";
$jml_lamaran = $db->Execute($sql1);
$jmllamaran = $jml_lamaran->RecordCount();
//$home_lowongan = "<a href=\"#\" class=\"list-group-item\" ";
if ($jmllamaran > 0) {
    //$home_lowongan .= "onClick=\"parent.addTab('Lowongan Yang Dilamar', '/kepegawaian/recruitment/list_lamaran.php')";
    $home_lowongan = "<table style=\"width:100%\" cellspacing=\"0\" cellpadding=\"1\" class=\"list\"><tr><td>Lowongan Yang Dilamar</td><td align=\"right\"><a href=\"#\" onClick=\"parent.addTab('Lowongan Yang Dilamar', '/kepegawaian/recruitment/list_lamaran.php')\"> $jmllamaran </a></td></tr></table>";
}
//$home_lowongan .= "\"><i class=\"fa fa-plane fa-fw\"></i> Lowongan yang dilamar<span class=\"pull-right text-muted small\"><em>$jmllamaran</em></span></a>";