<? global $vis_language; ?>
<div class="modal-dialog">
  <div class="modal-content">
    <div class="modal-header">
      <button type="button" class="close" data-dismiss="modal">&times;</button>
      <h4 class="modal-title"><? echo $vis_language['TXTMASSREPLY'];?></h4>
    </div>
    <div class="modal-body">
        <div class="row">
            <div class="col-sm-12">
                <form role="form"  target="vis_hidden_frame" class="form-horizontal" name="vis_ticket_reply_form" id="vis_ticket_reply_form">
					<div class="form-group">
                        <label class="col-sm-2 control-label txt_nowrap"><? echo $vis_language['TXTTICKETHASH']; ?></label>
                        <div class="col-sm-10">
							<?php
							if(count($ticket_list) > 0){
								for($j=(count($ticket_list)-1);$j>=0;$j--){ ?>
							<div class="btn-group wrapper-xxs">
								<span class="btn btn-xs alert-success"><?php echo $ticket_list[$j]['ticket_hash'];?></span>
								<button onclick="vis_remove_mass_ticket(<?php echo $ticket_list[$j]['ticket_id'];?>);" data-dismiss="alert" type="button" class="btn btn-xs alert-success"><i class="vh vh-cross"></i></button>
							</div>
						<?php }}?>
						
                        </div>
                    </div>
					<?php if($reply_module=='problem'){?>
					<div class="form-group">
                        <label class="col-sm-2 control-label txt_nowrap"><? echo $vis_language['TXTSELFHELP']; ?></label>
                        <div class="col-sm-5">
							<select id="vis_reply_solution" name="vis_reply_solution" class="form-control input-sm selectpicker">
								<option value=""><?= $vis_language['TXTSELECT']; ?></option>
								<option value="0"><?= $vis_language['TXTPERMANANTSOLUTION']; ?></option>
								<option value="1"><?= $vis_language['TXTWORKAROUNDSOLUTION']; ?></option>
							</select>
                        </div>
					</div>
					<?php }?>
					
					<?php if($reply_module=='changes'){?>
					<div class="form-group">
                        <label class="col-sm-2 control-label txt_nowrap"><? echo $vis_language['TXTSELFHELP']; ?></label>
                        <div class="col-sm-5">
							<select id="vis_reply_solution1" name="vis_reply_solution1" class="form-control input-sm selectpicker">
								<option value=""><?= $vis_language['TXTSELECT']; ?></option>
								<option value="0"><?= $vis_language['TXTROLLOUTPLAN']; ?></option>
								<option value="1"><?= $vis_language['TXTBACKOUTPLAN']; ?></option>
							</select>
                        </div>
					</div>
					<?php }?>
                    <div class="form-group">
                        <div class="col-sm-12">
							<textarea id="vis_ticket_post" name="vis_ticket_post" type="text" class="form-control input-sm"></textarea>
                        </div>
                    </div>
					<input id="vis_attachments" type="hidden"  name="vis_attachments" value="" >
					
					<div id="vis_changes_attachments"> 	</div>
					
					<div class="form-group">
                        <label class="col-sm-1 control-label txt_nowrap"><? echo $vis_language['TXTSTATUS']; ?></label>
                        <div class="col-sm-4">
							<select id="vis_status" name="vis_status" <? if(count($list)>10) echo 'data-live-search="true"';?> class="form-control input-sm selectpicker">
								<? for($i=0; $i<count($list); $i++) {	?>
								<option value="<?= $list[$i]['status_id']; ?>"><?= $list[$i]['status_name']; ?></option>
								<?  } ?>
							</select>
                        </div>
						<div class="col-sm-1 p-l-none"></div>
						<div class="col-sm-3 p-l-none">
							<label class="checkbox-inline i-checks">
							<input name="vis_sendmail" id="vis_sendmail" checked="checked" value="1" type="checkbox"><i></i> <? echo $vis_language['TXTSENDEMAIL']; ?></label>
						</div>
						<div class="col-sm-3">
							<label class="checkbox-inline i-checks">
							<input name="vis_private" id="vis_private" checked="checked" value="1" type="checkbox"><i></i> <? echo $vis_language['TXTVISIBLETOCLIENT']; ?>
							</label>
						</div>
                    </div>
                    <input type="hidden" name="vis_ticket_mass_id" id="vis_ticket_mass_id" value="<?= $operation['vis_ticket_id']; ?>"/>					
					<input type="hidden" name="reply_module" id="reply_module" value="<?=$reply_module;?>">
                </form>
            </div>
        </div>
     </div>
    <div class="modal-footer">
	  <a class="btn btn-primary" onclick="$(this).visExecution('submit_form',{'vis_module':'ticket', 'vis_operation':'mass_reply_tickets', 'vis_form':'vis_ticket_reply_form'});"><? echo $vis_language['TXTUPDATE']; ?></a>
    </div>
  </div>
</div>
<script type="text/javascript">
    $(".selectpicker").selectpicker();
   	<? if (!defined('VIS_HTML_EDITOR') || VIS_HTML_EDITOR == 1) { ?>
		$(function() {
            $('#vis_ticket_post').redactor({
				buttonSource: true,
				minHeight: 180, // pixels
                maxHeight: 180,
                imageUpload: false,
                imageUploadParam: 'files',
				plugins: ['source'],
                deniedTags: ['html', 'head', 'link', 'body', 'meta', 'script','applet']			
            });
			$(".redactor-toolbar").attr('z-index', '3 !important');
        });
    <?php }?>
	function vis_remove_mass_ticket(ticket_id){
		var ticket_lenght = ticket_id.toString().length;
		var mass_id = $("#vis_ticket_mass_id").val();
		var laststring = mass_id.substr(mass_id.length - ticket_lenght);
		if(laststring == ticket_id)var attach_modi_val =mass_id.replace( ',' +ticket_id, '');
		else var attach_modi_val =mass_id.replace(ticket_id + ',', '');
		var allMassTicket = attach_modi_val.split(',');
		if(mass_id.toString().length==ticket_lenght){$(".modal").modal("toggle");}
		$("#vis_ticket_mass_id").val(attach_modi_val);
	}
	$("#vis_reply_solution").change(function(){
		var vis_solution_type = $("#vis_reply_solution").val();
		if(vis_solution_type=='')return false;
		var data = {'vis_problem_id':'<?=$operation['vis_problem_id']?>', 'vis_solution_type':vis_solution_type };
		$.ajax({
			url: 'index.php?vis_module=ticket&vis_operation=get_problem_solution',
			data: data,
			type: "POST",
			dataType: "json",
			success: function(data) {
				if(data.list!=''){
					var ticket_post = $("#vis_ticket_post").val();
					$("#vis_ticket_post").val(data.list+'  '+ticket_post);
					$('#vis_ticket_post').redactor('code.set', data.list+' <br> '+ticket_post);
				}
			},
			error: function(xhr, ajaxOptions, thrownError) {$("#vis_global_msg_div").visInterface("load_global_message", "Error: "+xhr.status+" - "+thrownError);}
		});
	});
	
	
	
	$("#vis_reply_solution1").change(function(){
		var vis_solution_type = $("#vis_reply_solution1").val();
		if(vis_solution_type=='')return false;
		var data = {'vis_changes_id':'<?=$operation['vis_changes_id']?>', 'vis_solution_type':vis_solution_type };
		$.ajax({
			url: 'index.php?vis_module=ticket&vis_operation=get_changes_solution',
			data: data,
			type: "POST",
			dataType: "json",
			success: function(data) { 
				if(data.list!=''){ //alert(data.list);
					var list_d  = data.list;
					var res = list_d.split("#*****#"); 
					var ticket_post = $("#vis_ticket_post").val();
					$("#vis_ticket_post").val(res[0]+'  '+ticket_post);
					$('#vis_ticket_post').redactor('code.set', res[0]+' <br> '+ticket_post);
					
					var attachments = $("#vis_attachments").val();
					attachments = attachments+res[1];
					attachments = attachments.replace(",<br>", ","); 
					$("#vis_attachments").val(attachments);
					//alert(res[2]);
					var vis_changes_attachments = $("#vis_changes_attachments").html();
					
					vis_changes_attachments = vis_changes_attachments.replace("<br>", " "); 
					
					$("#vis_changes_attachments").html(vis_changes_attachments+res[2]);
					//alert(vis_changes_attachments);
					
				}
			},
			error: function(xhr, ajaxOptions, thrownError) {$("#vis_global_msg_div").visInterface("load_global_message", "Error: "+xhr.status+" - "+thrownError);}
		});
	});
	function vis_remove_attachment(ticket_id, attach_location){
		var loc = '';
		loc = (attach_location != "" && typeof(attach_location) != "undefined") ? attach_location : "vis_image_attachments";
		$("#" + loc).val( $("#" + loc).val().replace(ticket_id + ',', '') );
	}
</script>
