USE regonline;

CREATE DEFINER=`root`@`localhost` TRIGGER `reservasi_before_insert` BEFORE INSERT ON `reservasi` FOR EACH ROW BEGIN
	IF NEW.JENIS_APLIKASI = 0 OR NEW.JENIS_APLIKASI = 1 OR NEW.JENIS_APLIKASI = 3 OR NEW.JENIS_APLIKASI = 22 OR NEW.JENIS_APLIKASI = 33 OR NEW.JENIS_APLIKASI = 2 THEN
		SET NEW.JAM = (SELECT `getJamAntrian`(NEW.NO, NEW.POS_ANTRIAN, NEW.POLI, NEW.TANGGALKUNJUNGAN));
		SET NEW.JAM_PELAYANAN = (SELECT `getJamPelayanan`(NEW.ANTRIAN_POLI, NEW.POS_ANTRIAN, NEW.POLI, NEW.TANGGALKUNJUNGAN));
		SET NEW.ESTIMASI_PELAYANAN = (SELECT `getEstimasiJamAntrianBpjs`(NEW.DOKTER, NEW.POLI_BPJS, NEW.TANGGALKUNJUNGAN, NEW.JAM_PRAKTEK));
	END IF;
END