<?php
error_reporting(0);
define("_VALID_PHP", true);
require_once("../../init.php");
$page = basename($_SERVER["PHP_SELF"]);
$heder = "Prijava kvara";
$mali = " > forma za prijavu";
$korisnik = $user->username;
$lokal = $user->fname;
$level = $user->userlevel;
$prezime = $user->lname;
$koje = $user->name;
//echo $koje;
$ip = $_SERVER['REMOTE_ADDR']; 
$kome = $user->sendto;
$id_user = $user->uid;
$dept = $user->todept;
//echo $id_user;
$gdeje = "(".$dept.")";
//echo $gdeje;
$firma = $user->country;
$upit = 'manager REGEXP "[[:<:]]'.$id_user.'[[:>:]]"';
$upits = 'serviser REGEXP "[[:<:]]'.$id_user.'[[:>:]]"';
//echo $upits;
	
include('../../xcrud/xcrud.php');
Xcrud_config::$jquery_no_conflict = true; // jquery conflicts with SyntaxHighlighter
//odavde idu komnade ka xcrud-u
$xcrud = Xcrud::get_instance();
$xcrud->set_var('korisnik',$korisnik);
$xcrud->set_var('level',$level);
$xcrud->set_var('kome',$gdeje);
$xcrud->set_var('userid',$id_user);
$xcrud->table('tickets');
$xcrud->table_name('Prijava kvara');
	if ($user->levelCheck("1")) //objekat
	{
	$xcrud->pass_var('vreme_open', 'NOW()');
	$xcrud->no_quotes('vreme_open');
	$xcrud->pass_var('user_name', $korisnik);
	$xcrud->pass_var('manager', $kome);
	$xcrud->pass_var('lokal', $lokal);
	$xcrud->pass_var('ip', $ip);
	$xcrud->where('status',0);
	$xcrud->where('user_name',$korisnik);
	$xcrud->columns('department,aparat,broj,category,opis,vreme_open,status');
	$xcrud->relation('department','department','department_id','name','department_id IN ('.$dept.')');
	$xcrud->relation('aparat','slot','slot_id','name','','','','','','department_id','department');
	$xcrud->relation('category','slot_cat','category_id','name_cat','','','','','','slot_id','aparat');
	$xcrud->fields('department,aparat,broj,category,opis');
	$xcrud->unset_edit();
	$xcrud->unset_remove();
	$xcrud->unset_print();
	$xcrud->unset_csv();
	$xcrud->hide_button('save_new');
	$xcrud->hide_button('save_edit');
	$xcrud->change_type('status','select','',array(1=>'Zatvoren',0=>'Otvoren'));
	$xcrud->after_insert('saljem_kvar');//radnik salje
	$xcrud->before_list('statpanel');//update stat_panel
	}
	if ($user->levelCheck("2"))//serviser
	{	
	$xcrud->where($upits);
	$xcrud->where('status',0);
	$xcrud->where('ser_resio',0);
	$xcrud->columns('lokal,department,aparat,broj,category,opis,vreme_open,status,ser_resio,ser_been');
	$xcrud->disabled('lokal,department,aparat,broj,category,opis,vreme_open,vreme_close,man_note');
	$xcrud->fields('lokal,department,aparat,broj,category,opis,vreme_open,vreme_close,ser_note,man_note,ser_resio,ser_been');
	$xcrud->change_type('ser_note','textarea');
	$xcrud->condition('ser_resio','=','1','disabled','man_note,ser_note,ser_resio');
	$xcrud->label(array('department' => 'Odeljenju','ser_note' => 'Napomena','aparat' => 'Aparat/Igra','broj' => 'Broj u lokalu','opis' => 'Opis kvara','ser_resio' => 'Popravljen','vreme_open' => 'Prijavljen','vreme_close' => 'Zatvoren(Poseta)','category' => 'Deo/Opis','ser_been' => 'Bio serviser'));
	$xcrud->relation('category','category','category_id','name');
	$xcrud->relation('department','department','department_id','name');
	$xcrud->relation('aparat','slot','slot_id','name','','','','','','department_id','department');
	$xcrud->unset_add();	
	$xcrud->unset_remove();	
	$xcrud->unset_print();
	$xcrud->unset_csv();
	$xcrud->hide_button('save_new');
	$xcrud->hide_button('save_edit');
	$xcrud->column_pattern('lokal', '<a href="#" class="xcrud-action" data-task="edit" data-primary="{id}">{value}</a>');
	$xcrud->pass_var('ser_punoime',$koje);
	$xcrud->after_update('serviser');
	$xcrud->before_list('statpanel');
	/*$zamenjeno = $xcrud->nested_table('zdeo','id','rpart','ticket_id');
	$zamenjeno->relation('category_id','category','category_id','name');
	$zamenjeno->relation('part_id','parts','part_id','name','','','','','','category_id','category_id');
	$zamenjeno->table_name('Zamenjeni delovi');
	$zamenjeno->unset_remove();	
	$zamenjeno->unset_print();
	$zamenjeno->unset_csv();
	$zamenjeno->unset_search();
	$zamenjeno->unset_limitlist();
	$zamenjeno->hide_button('save_new');
	$zamenjeno->hide_button('save_edit');
	$zamenjeno->change_type('note','textarea');
	$zamenjeno->columns('part_id,new,qty,note');
	$zamenjeno->fields('category_id,part_id,new,qty,note');
	$zamenjeno->label(array('category_id' => 'Deo/Opis','part_id' => 'Deo','new' => 'Novo','qty' => 'Kom.','note' => 'Napomena'));*/
	}
	if ($user->levelCheck("3"))//manager
	{	
	$xcrud->where('status',0);
	//$xcrud->where($upit);
	if($korisnik == 'milan'){
	}
	else
	{
	$xcrud->where('FIND_IN_SET('.$id_user.',sent_mail)');
	//$xcrud->where('department IN '.$gdeje);
	}
	$xcrud->columns('lokal,department,aparat,broj,category,opis,vreme_open,status,ser_resio');
	$xcrud->disabled('department,aparat,broj,category,opis,vreme_open,vreme_close,ser_note');
	$xcrud->fields('department,aparat,broj,category,opis,vreme_open,vreme_close,man_note,ser_note,status');
	$xcrud->change_type('man_note','textarea');
	$xcrud->condition('ser_resio','=','1','disabled','ser_resio');
	$xcrud->relation('category','category','category_id','name');
	//$xcrud->change_type('ser_resio','bool');
	$xcrud->label(array('department' => 'Odeljenju','aparat' => 'Aparat/Igra','broj' => 'Broj u lokalu','opis' => 'Opis kvara','ser_resio' => 'Popravljen',',man_note' => 'Napomena','ser_note' => 'Objašnjenje','vreme_open' => 'Prijavljen','category' => 'Deo/Opis','status' => 'Zatvori'));
	$xcrud->column_pattern('lokal', '<a href="#" class="xcrud-action" data-task="edit" data-primary="{id}">{value}</a>');
	$xcrud->relation('department','department','department_id','name');
	$xcrud->relation('aparat','slot','slot_id','name','','','','','','department_id','department');
	$xcrud->change_type('status','bool','',array(1=>'Zatvoren',0=>'Otvoren'));
	$xcrud->unset_add();	
	$xcrud->unset_remove();	
	$xcrud->unset_print();
	//$xcrud->unset_csv();
	$xcrud->hide_button('save_new');
	$xcrud->hide_button('save_edit');
	$xcrud->before_list('statpanel');//update stat_panel
	}
	if ($user->levelCheck("6"))//manager
	{	
	$xcrud->where('status',0);
	//$xcrud->where($upit);
	if($korisnik == 'borko'){
	$xcrud->where('FIND_IN_SET('.$id_user.',sent_mail)');
	}
	else
	{
	//$xcrud->where('FIND_IN_SET('.$id_user.',sent_mail)');
	//$xcrud->where('department IN '.$gdeje);
	}
	$xcrud->columns('lokal,department,aparat,broj,category,opis,vreme_open,status,ser_resio');
	$xcrud->disabled('department,aparat,broj,category,opis,vreme_open,vreme_close,ser_note,status');
	$xcrud->fields('department,aparat,broj,category,opis,vreme_open,vreme_close,man_note,ser_note,status');
	$xcrud->change_type('man_note','textarea');
	$xcrud->condition('ser_resio','=','1','disabled','ser_resio');
	$xcrud->relation('category','category','category_id','name');
	//$xcrud->change_type('ser_resio','bool');
	$xcrud->label(array('department' => 'Odeljenju','aparat' => 'Aparat/Igra','broj' => 'Broj u lokalu','opis' => 'Opis kvara','ser_resio' => 'Popravljen',',man_note' => 'Napomena','ser_note' => 'Objašnjenje','vreme_open' => 'Prijavljen','category' => 'Deo/Opis','status' => 'Zatvori'));
	$xcrud->column_pattern('lokal', '<a href="#" class="xcrud-action" data-task="edit" data-primary="{id}">{value}</a>');
	$xcrud->relation('department','department','department_id','name');
	$xcrud->relation('aparat','slot','slot_id','name','','','','','','department_id','department');
	$xcrud->change_type('status','bool','',array(1=>'Zatvoren',0=>'Otvoren'));
	$xcrud->unset_add();	
	$xcrud->unset_remove();	
	$xcrud->unset_print();
	//$xcrud->unset_csv();
	$xcrud->hide_button('save_new');
	$xcrud->hide_button('save_edit');
	$xcrud->before_list('statpanel');//update stat_panel
	}
	
	if ($user->levelCheck("9"))//admin
	{	
	$xcrud->where('status',0);
	//$xcrud->where($upit);
	$xcrud->columns('lokal,aparat,broj,category,opis,vreme_open,status,ser_resio,ser_punoime');
	$xcrud->disabled('department,aparat,broj,category,opis,vreme_open,vreme_close,ser_note');
	$xcrud->fields('department,aparat,broj,category,opis,vreme_open,vreme_close,man_note,ser_note,status');
	$xcrud->change_type('man_note','textarea');
	$xcrud->condition('ser_resio','=','1','disabled','ser_resio');
	//$xcrud->change_type('ser_resio','bool');
	$xcrud->label(array('department' => 'Odeljenju','aparat' => 'Aparat/Igra ','broj' => 'Broj u lokalu','opis' => 'Opis kvara','ser_resio' => 'Popravljen',',man_note' => 'Napomena','ser_note' => 'Objašnjenje','vreme_open' => 'Prijavljen','category' => 'Deo/Opis','ser_punoime' => 'Serviser','status' => 'Zatvori'));
	$xcrud->relation('department','department','department_id','name','department_id IN ('.$dept.')');
	$xcrud->relation('category','category','category_id','name');
	$xcrud->relation('aparat','slot','slot_id','name','','','','','','department_id','department');
	$xcrud->change_type('status','bool','',array(1=>'Zatvoren',0=>'Otvoren'));
	$xcrud->column_pattern('lokal', '<a href="#" class="xcrud-action" data-task="edit" data-primary="{id}">{value}</a>');
	$xcrud->unset_add();	
	$xcrud->unset_remove();	
	$xcrud->unset_print();
	//$xcrud->unset_csv();
	$xcrud->hide_button('save_new');
	$xcrud->hide_button('save_edit');
	$xcrud->before_list('statpanel');//update stat_panel
	}
//$xcrud->where('country=',$firma);
$xcrud->label(array('department' => 'Odeljenju','aparat' => 'Aparat/Igra','broj' => 'Broj','opis' => 'Opis kvara','vreme_open' => 'Prijavljen','category' => 'Deo/Opis'));
//$xcrud->relation('category','category','category_id','name');
//$xcrud->relation('aparat','slot','slot_id','name');
//$xcrud->relation('department','department','department_id','name');
$xcrud->validation_pattern('broj','integer');
$xcrud->validation_required('department')->validation_required('aparat')->validation_required('category')->validation_required('opis')->validation_required('broj');
$xcrud->change_type('opis','textarea');
$xcrud->column_width('opis','40%');	
$xcrud->order_by('vreme_open','DESC');
$xcrud->highlight('status', '=', 1, '#E56B6A');
$xcrud->highlight('status', '=', 0, '#dff0d8');
$xcrud->highlight('ser_resio', '=', 1, '#45B6AF');
$xcrud->highlight('ser_been', '=', 1, '#FFB848');



?>
	<?php //citam za stats panele podatke

	?>
<!-- BEGIN PAGE HEADER-->
<h3 class="page-title">
<?php echo $heder;?><small><?php echo $mali;?></small>
</h3>
<div class="page-bar">
	<ul class="page-breadcrumb">
		<li>
			<i class="fa fa-home"></i>
			<a href="index.php">Home</a>
			<i class="fa fa-angle-right"></i>
		</li>
		<li>
			<a href="#"><?php echo $heder;?></a>
		</li>
	</ul>
	<?php
	if ($user->levelCheck("9"))//admin
	{
	echo'	
	<div class="page-toolbar">
    <select id="reload_interval" class="pull-right tooltips btn btn-fit-height green thin uppercase">
	<option value="">No refresh</option>
	<option value="10">Na 10 sekundi</option>
	<option value="20">Na 20 sekundi</option>
	<option value="30">Na 30 sekundi</option>
	<option value="60">Na 60 sekundi</option>
	</select>
	</div>';
	}
	?>
	</div>
	<div id="alert_green">
	</div>
	<!-- END PAGE HEADER-->
	<?php include '../inc/stat_panel.php'; ?>
	<!-- BEGIN PAGE CONTENT-->
	<div class="row">
	<!-- AUTO RELOAD-->
	<script>
	var interval = 0;
	var auto_refresh = 0;
	$(document).ready(function() {
	$('#reload_interval').change(function() {
    var reload = $(this).val();
    interval = reload * 1000;
    if (auto_refresh) {
      clearInterval(auto_refresh); //clear the previous interval before setting a new one
    }
    if (reload) {
      auto_refresh = setInterval(function() {
        //alert(interval);
		Xcrud.reload();
      }, interval);
    }

		});
	});
	</script>
	<!-- END AUTORELOAD-->
	<div class="col-md-12">
	<?php echo $xcrud->render(); ?>
	</div>
</div>
<!-- END PAGE CONTENT-->
<script type="text/javascript">
jQuery(document).on("xcrudafterrequest",function(event,container){
    if(Xcrud.current_task == 'save')
    {
     Metronic.alert({
     place: 'append', // append or prepent in container 
     type: 'success',  // bootstrap state classes
     <?php 
	 if ($user->levelCheck("1"))
	 {
	 echo "message: 'Uspešna prijava kvara!!!',  // alert's message";
	 }
	 if ($user->levelCheck("2"))
	 {
	 echo "message: 'Uspešna popravak/izmena podataka kvara!!!',  // alert's message";
	 }
	 if ($user->levelCheck("3,9"))
	 {
	 echo "message: 'Uspešno zatvoren kvar / izmena podataka kvara!!!',  // alert's message";
	 }
	 
	 ?>
     close: true, // make alert closable
     reset: true, // close all previouse alerts first
     focus: true, // auto scroll to the alert after shown
     closeInSeconds: 7, // auto close after defined seconds
     icon: 'check' // put font awesome icon before the message
	});
	}
	});
	

</script>
