<? 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 $title;?></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_merge_form" id="vis_ticket_merge_form">
				<input type="hidden" name="vis_filter_id" id="vis_filter_id" value="<?=$operation['vis_filter_id']?>"/>
                    <div class="form-group">
                        <label class="col-sm-3 control-label txt_nowrap"><? echo $vis_language['TXTTICKETHASH']; ?></label>
                        <div class="col-sm-9">
                            <select name="vis_parent_ticket_id" id="vis_parent_ticket_id" class="form-control input-sm selectpicker" <? if(count($ticket_list)>10) echo 'data-live-search="true"';?> onChange="javascript:update_remaining_tickets(this.value,'<?=$operation['vis_ticket_id']?>');">
                         <? for($i=(count($ticket_list)-1);$i>=0;$i--) {
                                echo '<option value="'.$ticket_list[$i]['ticket_id'].'">'.$ticket_list[$i]['ticket_hash'].' - '.$ticket_list[$i]['subject'].'</option>';
                            } ?></select>
                        </div>
                    </div>
                    <? for($i=(count($ticket_list)-1);$i>=0;$i--) { ?>
                    <input type="hidden" name="vis_ticket_mass_chk_<?= $i; ?>" id="vis_ticket_mass_chk_<?= $i; ?>" value="<?= $ticket_list[$i]['ticket_id']; ?>"/>
                   <? } ?>
				   <div class="form-group">
					   <label class="col-sm-3 control-label"><? echo $vis_language['TXTLINK']; ?></label>
					   <div class="col-sm-9">
							<select id="vis_link_type" name="vis_link_type" class="form-control input-sm selectpicker">
								<option value="1" selected="selected"><?= $vis_language['TXTRELATESTO'];?></option>
								<option value="2"><?= $vis_language['TXTISRELATEDTO'];?></option>
								<option value="3"><?= $vis_language['TXTISAPARENTOF'];?></option>
								<option value="4"><?= $vis_language['TXTISACHILDOF'];?></option>
								<option value="5"><?= $vis_language['TXTDEPENDSON'];?></option>
								<option value="6"><?= $vis_language['TXTISDEPENDEDON'];?></option>
								<option value="7"><?= $vis_language['TXTDUPLICATES'];?></option>
								<option value="8"><?= $vis_language['TXTISADUPLICATEDBY'];?></option>
							</select>
					   </div>
					</div>
					<div class="form-group">
                        <label class="col-sm-3 control-label txt_nowrap"><? echo $vis_language['TXTTICKETS']; ?></label>
                        <div class="col-sm-9 scrollable" style="max-height: 350px" id="remaining_tickets">
                         <?
						$html='<table class="table table-hover b-t b-light m-b-none"><thead class="table_head"><tr><th>'.$vis_language['TXTTICKETHASH'].'</th><th>'.$vis_language['TXTSUBJECT'].'</th></tr></thead><tbody>';
		
							for($i=(count($ticket_list)-1);$i>=0;$i--) {
							 if($i!=(count($ticket_list)-1))
								$html.='<tr><td><a href="#/ticket/ticket_details/'.$ticket_list[$i]['ticket_hash'].'/'.$ticket_list[$i]['ticket_id'].'" class="link">'.$ticket_list[$i]['ticket_hash'].'</a></td><td>'.$ticket_list[$i]['subject'].'</td></tr>';
							}
							$html.='</tbody></table>';
							echo $html;
						?>
                        </div>
                    </div>
                </form>
            </div>
        </div>
     </div>
    <div class="modal-footer">
 	  <a data-dismiss="modal" class="btn btn-default">Cancel</a>
	  <a class="btn btn-primary" onclick="$(this).visExecution('submit_form',{'vis_module':'ticket', 'vis_operation':'link_tickets', 'vis_form':'vis_ticket_merge_form'});"><? echo $vis_language['TXTUPDATE']; ?></a>
    </div>
  </div>
</div>
<script type="text/javascript">
   $(".selectpicker").selectpicker();
   function update_remaining_tickets(selectedid,ticket_list){
	   $.ajax({
    			url: 'index.php?vis_tmpst=new Date().getTime()',
    			data: {'vis_module':'ticket', 'vis_operation':'get_remaining_tickets_link', 'vis_ticket_id':selectedid, 'vis_ticket_list':ticket_list},
    			type: "POST",
    			dataType: "html",
    			success: function(data) {
					$("#remaining_tickets").html(data);
    			},
    			error: function(xhr, ajaxOptions, thrownError) {$("#vis_global_msg_div").visInterface("load_global_message", "Error: "+xhr.status+" - "+thrownError);}
    		});
   }
</script>