<?php

include_once "authentication.php";


//echo "<pre>"; print_r($_POST); echo "</pre>";

/*
$_POST['tw_delivery_id'] = 2;
$_POST['tw_sub_id'] = 3169;
*/

$q_delivery = "SELECT * FROM `delivery` WHERE `id` = {$_POST['tw_delivery_id']}";
$res_delivery = $sql->query($q_delivery);
$row_delivery = $res_delivery->fetch_assoc();

$q_account = "SELECT * FROM `account` WHERE `id` = {$row_delivery['account_id']}";
$res_account = $sql->query($q_account);
$row_account = $res_account->fetch_assoc();


if(isset($_POST['date_due'])){
	
	
	
	$time = time();
	$date_due = explode('-',$_POST['date_due']);
	$date_due = mktime(0,0,0,$date_due[1],$date_due[0],$date_due[2]);
	$aid = $_POST['tw_account_id'];
	$did = $_POST['tw_delivery_id'];
	$nb = $_POST['nb_stb'];
	$credit = (isset($_POST['chk_credit'])) ? 1 : 0;
	$fbase = $_POST['forfbase'];
	$fthem = (isset($_POST['theme_forfait'])) ? json_encode($_POST['theme_forfait']) : '';
	
	
	//die("<pre>$fthem -- {$row_account['customer_id']}</pre>");
	
	$subject = change_quote("{$row_delivery['city']} | [TELE $nb STB] {$row_delivery['name']}");
	$msg = '';
	
	
	$q_ticket = "INSERT INTO `ticket` (`account_id`, `subject`, `dept_id`, `open_by`, `assign_to`, `due_date`, `date_create`, `last_update`) VALUES ($aid, '$subject', 41, $userid, 3301, '$date_due','$time','$time')";
	if($sql->query($q_ticket)){
		
		$ticket_id = $sql->insert_id;
		
		$q_wiz = "INSERT INTO `tele_wiz` (`account_id`, `delivery_id`, `ticket_id`, `nb_stb`, `credit`, `fbase`, `fthem`) VALUES ($aid,$did,$ticket_id,$nb,$credit,$fbase,'$fthem')";
		$sql->query($q_wiz);
		$wiz_id = $sql->insert_id;
		
		$msg = $_POST['memo'] . "<br><br> Activer le(s) STB en cliquant <a href='https://store.targo.ca/targo/rep_ticket/connect_stb.php?wid=$wiz_id' target='_blank'>ici</a>";	
		$msg = $sql->real_escape_string($msg);
		
		$q_ticket_msg = "INSERT INTO `ticket_msg` (`ticket_id`, `staff_id`, `msg`, `date_orig`) VALUES ($ticket_id,$userid,'$msg','$time')";
		$sql->query($q_ticket_msg);
		
		
		if($_POST['opt_carte'] == 1){
			//next step chaine a la carte
			die("<form id='f_tele_carte_wiz' method='POST' action='accueil.php?menu=tele_carte_add'>
					<input type='hidden' name='fromaccount_accid' value='$aid'>
					<input type='hidden' name='fromaccount_cusid' value='{$row_account['customer_id']}'>
					<input type='hidden' id='from_account_delid' name='from_account_delid' value='$did'>
				</form>
				<script>$('#f_tele_carte_wiz').submit();</script>");
		}
		else{
			die("<script>location.replace('accueil.php?menu=ticket_view&id=$ticket_id');</script>");
		}
		

		
		##echo "<pre>$q_ticket<br>$q_ticket_msg<br>$q_wiz</pre>";
		
	}

}




$option_base = "<option value='634'>Skinny</option><option value='635'>Standard</option><option value='636'>Evolue</option>";

$q_forfait = "SELECT `product`.`id`,`product`.`sku`, `product_translate`.`name` FROM `product` LEFT JOIN `product_translate` ON `product_translate`.`product_id` = `product`.`id` WHERE `product`.`type` = '4' AND `product_translate`.`language_id` = 'francais' AND `product`.`active` = 1";
$res_forfait = $sql->query($q_forfait);
$option_forfait = "";
$black_list = array(629,634,635,636);
while($row_forfait = $res_forfait->fetch_assoc()){
	
	if(in_array($row_forfait['id'],$black_list)) continue;
	
	$option_forfait .= "<option value='{$row_forfait['id']}_{$row_forfait['name']}'>{$row_forfait['name']}</option>";
	
}

$memo = "# Client:{$row_account['customer_id'] }\nNom: {$row_delivery['name']}\nAdresse: {$row_delivery['address1']}\nVille: {$row_delivery['city']}\nCode Postal:{$row_delivery['zip']}\nTéléphone: {$row_delivery['tel_home']}\nCell: {$row_delivery['cell']}\nEmail: {$row_delivery['email']}\n\n";


?>


<h3>Tele Wiz - Package</h3><br>

<form class="form-horizontal" id='f_tele_wiz' method='POST' data-toggle="validator" role="form">

	<input type='hidden' name='tw_account_id' value='<?php echo $row_account['id'];?>'>	
	<input type='hidden' name='tw_delivery_id' value='<?php echo $_POST['tw_delivery_id'];?>'>
	<input type='hidden' name='tw_sub_id' value='<?php echo $_POST['tw_sub_id'];?>'>	

	<div class="form-group">
		<label for="invoice_date_due" class="col-sm-2 control-label">Date installation</label>
		<div class='col-sm-3'>
			<div class='input-group date' id='datetimepicker_date_due'>		
				<input type='text' class="form-control" id='date_due' name='date_due' value="" required>
				<span class="input-group-addon">
					<span class="glyphicon glyphicon-calendar"></span>
				</span>
			</div>
		</div>
		<span class='help-block with-errors'></span>
	</div>
	<div class="form-group">
		<label class="col-sm-2 control-label">Nombre de STB</label>
		<div class="col-sm-1">
			<input type="number" class="form-control" id="nb_stb" name="nb_stb" value='1' min=1 max=6>
		</div>
		<div class="col-sm-4">
			<div class="checkbox">
				<label>
					<input type="checkbox" id='chk_credit' name='chk_credit' checked> Appliquer un credit pour le premier STB
				</label>
			</div>
		</div>
		<span class="help-block with-errors"></span>
	</div>
	<div class="form-group">
		<label for="title" class="col-sm-2 control-label">Ajouter a la carte</label>
		<div class="col-sm-6">
			<label class="radio-inline">
				<input type="radio" name="opt_carte" value="1"> Oui
			</label>
			<label class="radio-inline">
				<input type="radio" name="opt_carte" value="0" checked> Non
			</label>
		</div>
	</div>
	<br>
	<div class="form-group">
		<label for="title" class="col-sm-2 control-label">Forfait de Base</label>
		<div class="col-sm-3">
			<select class="form-control" id="forfbase" name="forfbase">
				<?php echo $option_base; ?>
			</select>
		</div>
	</div>
	<div class="form-group">
		<label for="title" class="col-sm-2 control-label">Forfait Thematique</label>
		<div class="col-sm-3">
			<select class="form-control" id="sel_fthem" name="sel_fthem">
				<?php echo $option_forfait; ?>
			</select>
		</div>
		<div class="col-sm-2">
			<button class="btn btn-default" type="button" onclick='add_forfait($("#sel_fthem").val(),"ul_forfait","theme")'>Ajouter</button>
		</div>
	</div>
	<br>
	<div class="form-group">
		<div class="col-sm-offset-2 col-sm-5">
			<ul id='ul_forfait'></ul>
		</div>	
	</div>
	<br>
	<div class="form-group">
		<label class="col-sm-2 control-label">Msg Ticket</label>
		<div class="col-sm-4">
			<textarea class='form-control' id='memo' name='memo' rows=15><?php echo $memo;?></textarea>
		</div>
	</div>
	<br>
	<button id='btn_submit' type='submit' class='btn btn-success'>Enregistrer</button>

</form>

<script>


$('#f_save').validator().on('submit', function (e) {
  if (!e.isDefaultPrevented()) {
    
		var validated = 1;
		$("#btn_submit").prop('disabled',true);	
		
		if(validated == 0){
			e.preventDefault();
			$("#btn_submit").prop('disabled',false);			
		}
  } 
});


$(function () {
	$('#datetimepicker_date_due').datetimepicker({
		showTodayButton: true,
		showClear: true,
		showClose: true,
		useCurrent: false,
		format: "DD-MM-YYYY",
		locale: 'fr'
	});
});

function add_forfait(value, parent, type){
		
	tmp = value.split('_');
	id = tmp[0];
	//name = revert_quote(tmp[1]);
	name = tmp[1].replace("&#039;","'");
		
	if(!document.getElementById(parent+"_li_forfait_"+id)){
	
		var node = document.createElement("LI");
		var textnode = document.createTextNode(name);
		var inputnode = document.createElement("INPUT");
		inputnode.setAttribute("type","hidden");
		inputnode.setAttribute("id",parent+"_input_forfait_"+id);
		inputnode.setAttribute("name",type+"_forfait[]");
		inputnode.setAttribute("value",id);
		
		node.setAttribute("id",parent+"_li_forfait_"+id);
		node.setAttribute("title","Cliquez pour effacer");
		node.setAttribute("style","cursor:pointer;");
		node.setAttribute("onclick","remove_forfait('"+parent+"_li_forfait_"+id+"')");
		node.appendChild(textnode);
		node.appendChild(inputnode);
		document.getElementById(parent).appendChild(node);
	}
	
}

function remove_forfait(id){

	item = document.getElementById(id);
	item.parentNode.removeChild(item);

}


</script>