<? global $vis_language;?>
<section id="breadcrumb" class="row m-b m-l-none m-r-none">
    <div class="col-sm-6">
        <h3 class="m-b-xs text-black"><? echo $vis_language['TXTCONTRACT']; ?></h3>
        <ul class="breadcrumb bg-empty no-border m-b-xxs no-padder">
            <li><a class="link" href="#/home/quick_view/<? echo $vis_language['TXTHOME']; ?>"><? echo $vis_language['TXTHOME']; ?></a></li>
            <li class="active"><a class="link" href="javascript:void(0);" onclick="$('#vis_workpanel_div').visExecution('load_module','<? echo $vis_language['TXTCONTRACT'];?>',{'vis_module':'<?=$operation['vis_module'];?>','vis_operation':'contract'});"><? echo $vis_language['TXTCONTRACT'];?></a></li>
            <li class="active"><? echo $vis_language['TXTUPDATECONTRACT'];?></li>
        </ul>
    </div>
</section>
<section class="padder">
    <section class="panel panel-default">
        <header class="panel-heading sub_header"><? echo $vis_language['TXTUPDATECONTRACT']; ?></header>
        <div class="panel-body">
            <form class="form-horizontal" role="form" id="vis_update_contract_form" name="vis_update_contract_form" method="post">
				<input type="hidden" name="vis_contract_id" id="vis_contract_id" value="<?=$contract_details['contract_id']?>">
				<div class="form-group">
					<label class="col-sm-2 control-label txt_nowrap"><? echo $vis_language['TXTDEPARTMENT']; ?></label>
					<div class="col-sm-10">
						<select name="vis_department" id="vis_department" class="form-control input-sm selectpicker"<? if(count($departments)>10) echo ' data-live-search="true"';?> onchange="domain_onchange();">
							<option value=""><? echo $vis_language['TXTSELECT']; ?></option>
							<? if($departments!=-1) for($i=0;$i<count($departments); $i++){
								$selected = ($contract_details['department_id']==$departments[$i]['department_id']) ? 'selected="selected"': '';
								echo '<option value="'.$departments[$i]['department_id'].'" '.$selected.'>'.$departments[$i]['domain_name'].' :: '.$departments[$i]['department_name'].'</option>';
								} ?>
						</select>
					</div>
				</div>
				<div class="form-group">
					<label for="vis_firstname" class="col-sm-2 control-label"><? echo $vis_language['TXTNAME']; ?></label>
					<div class="col-sm-10">
						<input type="text" class="form-control input-sm" data-type="text" data-required="true" name="vis_name" id="vis_name" value="<?=$contract_details['name']?>">
					</div>
				</div>
				<div class="form-group">
					<label for="vis_firstname" class="col-sm-2 control-label"><? echo $vis_language['TXTCONTRACTTYPE']; ?></label>
					<div class="col-sm-10" id="vis_contract_typesel">
						<select class="form-control selectpicker" name="vis_contract_type" id="vis_contract_type" <? if(count($contract_type_details)>10) echo ' data-live-search="true"';?>>
							<option value=""><? echo $vis_language['TXTSELECT']; ?></option>
						<? 	for($j=0;$j<count($contract_type_details);$j++){
								$selected = ($contract_details['type']==$contract_type_details[$j]['contract_type_id']) ? 'selected="selected"': '';
								echo '<option value="'.$contract_type_details[$j]['contract_type_id'].'" '.$selected.'>'.$contract_type_details[$j]['name'].'</option>';
							}
						?>
						</select>
					</div>
				</div>
				<div class="form-group">
					<label for="vis_firstname" class="col-sm-2 control-label"><? echo $vis_language['TXTPRODUCT']; ?></label>
					<div class="col-sm-10" id="vis_productsel">
						<select class="form-control selectpicker" name="vis_product" id="vis_product" <? if(count($ci_product_details)>10) echo ' data-live-search="true"';?> onchange="onchangevis_product(this.value);">
							<option value=""><? echo $vis_language['TXTSELECT']; ?></option>
							<?
							for($j=0;$j<count($ci_product_details);$j++){
								$selected = ($contract_details['product']==$ci_product_details[$j]['product_id'])?'selected="selected"': '';
								echo '<option value="'.$ci_product_details[$j]['product_id'].'" '.$selected.'>'.$ci_product_details[$j]['name'].'</option>';
							}
							?>
						</select>
					</div>
				</div>

				<div class="form-group">
					<label for="vis_firstname" class="col-sm-2 control-label"><? echo $vis_language['TXTVENDOR']; ?></label>
					<div class="col-sm-10" id="vendor_list">
						<select class="form-control selectpicker" name="vis_vendor" id="vis_vendor" >
							<option value=""><? echo $vis_language['TXTSELECT']; ?></option>
						</select>
					</div>
				</div>

				<div class="form-group">
					<label for="vis_firstname" class="col-sm-2 control-label"><? echo $vis_language['TXTLICENSECOUNT']; ?></label>
					<div class="col-sm-10">
						<input type="text" class="form-control input-sm" data-type="text" data-required="true" name="vis_license_count" id="vis_license_count"  value="<?=$contract_details['license_count']?>">
					</div>
				</div>
				<div class="form-group">
					<label for="vis_firstname" class="col-sm-2 control-label"><? echo $vis_language['TXTLICENSETYPE']; ?></label>
					<div class="col-sm-10" id="vis_license_typesel">
						<select class="form-control selectpicker" name="vis_license_type" id="vis_license_type" <? if(count($license_type_details)>10) echo ' data-live-search="true"';?>>
							<option value=""><? echo $vis_language['TXTSELECT']; ?></option>
						<?
							for($j=0;$j<count($license_type_details);$j++){
								$selected = ($contract_details['license_type']==$license_type_details[$j]['license_type_id']) ? 'selected="selected"': '';
								echo '<option value="'.$license_type_details[$j]['license_type_id'].'" '.$selected.'>'.$license_type_details[$j]['name'].'</option>';
							}
						?>
						</select>
					</div>
				</div>
				<div class="form-group">
					<label for="vis_firstname" class="col-sm-2 control-label"><? echo $vis_language['TXTAPROVER']; ?></label>
					<div class="col-sm-10" id="aprover_list">
						<select class="form-control selectpicker" name="vis_aprover" id="vis_aprover">
							<option value=""><? echo $vis_language['TXTSELECT']; ?></option>
							<? foreach ($staff_list as $staff) {
							$selected = ($contract_details['approver']==$staff['staff_id']) ? 'selected="selected"': '';
							?>
								<option value="<?=$staff['staff_id'];?>" <?=$selected?>><?=$staff['first_name'].' '.$staff['last_name'];?></option>
							<? } ?>
						</select>
					</div>
				</div>
				<div class="form-group">
					<label for="vis_firstname" class="col-sm-2 control-label" id="cost_currency"><? echo $vis_language['TXTCOST']; ?></label>
					<div class="col-sm-10">
						<input type="text" class="form-control input-sm" data-type="text" data-required="true" name="vis_cost" id="vis_cost"  value="<?=$contract_details['cost']?>">
					</div>
				</div>
				<div class="form-group">
					<label for="vis_firstname" class="col-sm-2 control-label"><? echo $vis_language['TXTDESCRIPTION']; ?></label>
					<div class="col-sm-10">
						<textarea name="vis_description" id="vis_description" class="form-control" rows="10"><? echo stripslashes($contract_details['description'])?></textarea>
					</div>
				</div>
				<div class="form-group">
					<label for="vis_firstname" class="col-sm-2 control-label"><? echo $vis_language['TXTATTACHCI']; ?></label>
					<div class="col-sm-10">
						<span id="vis_select_cit_btn"><a href="javascript:void(0);" data-toggle="ajaxModal" data-modal-arguments="vis_module=<?=$operation['vis_module']?>&vis_operation=attach_ci&vis_department=<?=$contract_details['department_id']?>&vis_static_area=vis_add_ciproduct_dialog&attached_ci_ids=<?=$contract_details['attach_ci']?>" class="btn btn-rounded btn-default btn-sm dropdown-toggle"><i class="vh vh-plus" style="font-size:10px;"></i> <? echo $vis_language['TXTSELECTCI']; ?></a></span>
						<div id="vis_attach_ci">
							<input type="hidden" name="vis_ci_attach" id="vis_ci_attach" value="<?=$contract_details['attach_ci']?>" />
							<?php
							if($attached_ci!=-1){
								for($j=0;$j<count($attached_ci);$j++){ ?>
							<div class="btn-group wrapper-xxs">
								<span class="btn btn-xs alert-success"><?php echo $attached_ci[$j]['name'];?></span>
								<button onclick="$(this).parent().remove(); $(this).visExecution('load_operations', {'vis_module':'ticket', 'vis_operation':'remove_attachment', 'vis_attachment_id':<?php echo $attached_ci[$j]['assets_id'];?>});vis_remove_ci_attachment(<?php echo $attached_ci[$j]['assets_id'];?>);" data-dismiss="alert" type="button" class="btn btn-xs alert-success"><i class="vh vh-cross"></i></button>
							</div>
						<?php }}?>
						</div>
					</div>
				</div>
				<div class="form-group">
					<label for="vis_firstname" class="col-sm-2 control-label"><? echo $vis_language['TXTATTACHFILE']; ?></label>
					<div class="col-sm-10">
						<input type="file" name="files[]" multiple="multiple" onchange="$(this).visExecution('upload_file', {'vis_static_area':'#vis_csv_attachments', 'vis_form':'vis_update_contract_form', 'vis_form_attachment_area':'#vis_attachments'})" class="filestyle" data-input="false" data-icon="true" data-classButton="btn btn-link link" data-iconName="vh-chat" data-buttonText="<?=$vis_language['TXTCHOOSEFILE']?>" data-badge="true"/><input type="hidden" name="vis_location" value="<? echo VIS_DOMAIN; ?>" />
						<input type="hidden" name="vis_attachments" id="vis_attachments" value="<?=$contract_details['attach_file']?>" />
						<div id="vis_csv_attachments">
						<?php
							if($contract_details['attach_file']!=''){
								for($j=0;$j<count($contract_details['files']);$j++){
						?>
						<a href="../attachment/<?php echo $contract_details['files'][$j]['content']; ?>" download>
							<div class="btn-group wrapper-xxs">
								<span class="btn btn-xs alert-success"><?php echo $contract_details['files'][$j]['file_name'];?></span>
								<button onclick="$(this).parent().remove(); $(this).visExecution('load_operations', {'vis_module':'ticket', 'vis_operation':'remove_attachment', 'vis_attachment_id':<?php echo $contract_details['files'][$j]['attachment_id'];?>});vis_remove_attachment(<?php echo $contract_details['files'][$j]['attachment_id'];?>,'vis_attachments');" data-dismiss="alert" type="button" class="btn btn-xs alert-success"><i class="vh vh-cross"></i></button>
							</div>
						<?php }}?>
						</div>
					</div>
				</div>
				<div class="form-group">
					<label for="vis_firstname" class="col-sm-2 control-label"><? echo $vis_language['TXTSTARTDATE']; ?></label>
					<div class="col-sm-10">
						<input type="text" class="form-control input-sm from_date" data-type="text" data-required="true" name="vis_start_date" id="vis_start_date"  value="<?=date("M-d, Y",$contract_details['start_date'])?>">
					</div>
				</div>
				<div class="form-group">
					<label for="vis_firstname" class="col-sm-2 control-label"><? echo $vis_language['TXTENDDATE']; ?></label>
					<div class="col-sm-10">
						<input type="text" class="form-control input-sm from_date" data-type="text" data-required="true" name="vis_end_date" id="vis_end_date"   value="<?=date("M-d, Y",$contract_details['end_date'])?>">
					</div>
				</div>
				<div class="form-group">
					<label for="vis_firstname" class="col-sm-2 control-label"><? echo $vis_language['TXTNOTIFYEXPIRY']; ?></label>
					<div class="col-sm-10">
						<label class="checkbox-inline i-checks">
							<input type="checkbox" name="vis_notify_expiry" value="1" id="vis_notify_expiry" onchange="check_expiry();" <?php echo ($contract_details['if_notify_expiry']==1)? 'checked="checked"':'';?>><i></i>
     					</label>
					</div>
				</div>
				<div id="notify_expiry">
					<div class="form-group">
						<label for="vis_notify_before" class="col-sm-2 control-label"><? echo $vis_language['TXTNOTIFYBEFORE']; ?></label>
						<div class="col-sm-10">
							<select class="form-control selectpicker" name="vis_notify_before" id="vis_notify_before">
								<?php $notify_before = $contract_details['notify_before'];?>
								<option value="8" <? echo ($notify_before==8)?'selected="selected"':'';?>>8 <? echo $vis_language['TXTDAYS']; ?></option>
								<option value="30" <? echo ($notify_before==30)?'selected="selected"':'';?>>30 <? echo $vis_language['TXTDAYS']; ?></option>
								<option value="45" <? echo ($notify_before==45)?'selected="selected"':'';?>>45 <? echo $vis_language['TXTDAYS']; ?></option>
							</select>
						</div>
					</div>
					<div class="form-group">
						<label for="vis_firstname" class="col-sm-2 control-label"><? echo $vis_language['TXTNOTIFYTO']; ?></label>
						<div class="col-sm-10">
							<input type="text" class="form-control input-sm" data-type="text" data-required="true" name="vis_notify_to" id="vis_notify_to"  value="<?=$contract_details['notify_to']?>">
						</div>
					</div>
				</div>

            </form>

            <div class="b-2x b-t m-b-md"></div>
				<div class="col-sm col-sm-offset-2">
					<?=$btn_class?>
				</div> <br/>
			</div>
    </section>
</section>
<? if (!defined('VIS_HTML_EDITOR') || VIS_HTML_EDITOR == 1) { ?>	<script src="<?= VIS_PATH_PLUGINS; ?>redactor/source.js"></script> <?}?>
<script src="<?= VIS_PATH_SCRIPT; ?>file-input/bootstrap-filestyle.min.js"></script>
<script>
	change_product();
	check_expiry();//domain_onchange();
	<? if (!defined('VIS_HTML_EDITOR') || VIS_HTML_EDITOR == 1) { ?>
	$(function() {
            $('#vis_description').redactor({
				buttonSource: true,
				minHeight: 180, // pixels
                maxHeight: 180,
                imageUpload: false,
                imageUploadParam: 'files',
                deniedTags: ['html', 'head', 'link', 'body', 'meta', 'script','applet'],
                plugins: ['source']			
            });
			$(".redactor-toolbar").attr('z-index', '3 !important');
        });
    <?php }?>
	$(".selectpicker").selectpicker();
	$('.from_date').datetimepicker({format: "MMM-D, YYYY"});

	function change_product(){
		var data = {'vis_product_id':$("#vis_product").val(), 'vis_vendor_id': '<?=$contract_details['vendor']?>'};
		$.ajax({
			url: 'index.php?vis_module=<?=$operation['vis_module'];?>&vis_operation=change_vendor',
			data: data,
			type: "POST",
			dataType: "json",
			success: function(data) {
				$("#vendor_list").html(data.list);
			},
			error: function(xhr, ajaxOptions, thrownError) {$("#vis_global_msg_div").visInterface("load_global_message", "Error: "+xhr.status+" - "+thrownError);}
		});
	}
	function check_expiry(){
		if ($('#vis_notify_expiry').is(":checked"))$('#notify_expiry').show();
		else $('#notify_expiry').hide();
	}
	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 + ',', '') );
	}
	function vis_remove_ci_attachment(ticket_id){
		var department_id = $("#vis_department").val();
		var result = department_id;
		var attach_modi_val =$("#vis_ci_attach").val().replace(ticket_id + ',', '');
		$("#vis_ci_attach").val( attach_modi_val);
		var attach_ci_btn = '<a href="javascript:void(0);" data-toggle="ajaxModal" data-modal-arguments="vis_module=cmdb&vis_operation=attach_ci&vis_department='+result+'&vis_static_area=vis_add_ciproduct_dialog&attached_ci_ids='+attach_modi_val+'" class="btn btn-rounded btn-default btn-sm dropdown-toggle"><i class="vh vh-plus" style="font-size:10px;"></i><?=$vis_language['TXTSELECTCI']?></a>';
		$('#vis_select_cit_btn').html(attach_ci_btn);
	}
	$(document).ready(function () {
		$(":file").filestyle({input: false});
	});
	function onchangevis_product(val) {
		var data = {'vis_product_id':val};
		$.ajax({
			url: 'index.php?vis_module=<?=$operation['vis_module'];?>&vis_operation=change_vendor',
			data: data,
			type: "POST",
			dataType: "json",
			success: function(data) {
				$("#vendor_list").html(data.list);
				$(".selectpicker").selectpicker();
			},
			error: function(xhr, ajaxOptions, thrownError) {$("#vis_global_msg_div").visInterface("load_global_message", "Error: "+xhr.status+" - "+thrownError);}
		});
	}
	function domain_onchange(){
		var department_id = $("#vis_department").val();
		var result = department_id;
		var approver_id = (result == <?=$contract_details['department_id']?>) ? <?=$contract_details['approver']?> : 0;
		var data = {'department_id':department_id, 'approver_id': approver_id};

		var attachedci=$("#vis_ci_attach").val();
		var attach_ci_btn = '<a href="javascript:void(0);" data-toggle="ajaxModal" data-modal-arguments="vis_module=cmdb&vis_operation=attach_ci&vis_department='+result+'&vis_static_area=vis_add_ciproduct_dialog&attached_ci_ids='+attachedci+'" class="btn btn-rounded btn-default btn-sm dropdown-toggle"><i class="vh vh-plus" style="font-size:10px;"></i><?=$vis_language['TXTSELECTCI']?></a>';
		$('#vis_select_cit_btn').html(attach_ci_btn);


		$.ajax({
			url: 'index.php?vis_module=<?=$operation['vis_module'];?>&vis_operation=change_aprover',
			data: data,
			type: "POST",
			dataType: "json",
			success: function(data) {
				$("#aprover_list").html(data.list);
				$("#cost_currency").html(data.cost);
				$(".selectpicker").selectpicker();
			},
			error: function(xhr, ajaxOptions, thrownError) {$("#vis_global_msg_div").visInterface("load_global_message", "Error: "+xhr.status+" - "+thrownError);}
		});
		$.ajax({
			url: 'index.php?vis_module=cmdb&vis_operation=get_contract_type_list',
			data: data,
			type: "POST",
			success: function(data) {
			parseSelectXML(data, '#vis_contract_typesel','vis_contract_type','row','contract_type_id','description');
			$(".selectpicker").selectpicker();
			},
			error: function(xhr, ajaxOptions, thrownError) {$("#vis_global_msg_div").visInterface("load_global_message", "Error: "+xhr.status+" - "+thrownError);}
		});
		$.ajax({
			url: 'index.php?vis_module=cmdb&vis_operation=get_product_list',
			data: data,
			type: "POST",
			success: function(data) {
			parseSelectXML(data, '#vis_productsel','vis_product','row','product_id','name','onchangevis_product(this.value);');
			$(".selectpicker").selectpicker();
			},
			error: function(xhr, ajaxOptions, thrownError) {$("#vis_global_msg_div").visInterface("load_global_message", "Error: "+xhr.status+" - "+thrownError);}
		});
		
		$.ajax({
			url: 'index.php?vis_module=cmdb&vis_operation=get_license_type_list',
			data: data,
			type: "POST",
			success: function(data) {
			parseSelectXML(data, '#vis_license_typesel','vis_license_type','row','license_type_id','name');
			$(".selectpicker").selectpicker();
			},
			error: function(xhr, ajaxOptions, thrownError) {$("#vis_global_msg_div").visInterface("load_global_message", "Error: "+xhr.status+" - "+thrownError);}
		});
		
	}
	function parseSelectXML(xml,divid, selectid, xmlnode,val,text,onchange='') {
    var selecthtml = '';
	var count=0;
    $(xml).find(xmlnode).each(function() {
        var selectvalue = $(this).find(''+val+'').html();
        var selecttext = $(this).find(''+text+'').html();
        selecthtml += '<option value="'+selectvalue +'">'+selecttext+'</option>';
		count++;
    });
	$(divid).html('');
	var data='<select class="form-control  input-sm  v-middle selectpicker" name="'+selectid+'" id="'+selectid+'"';
	if(count>10) data+=' data-live-search="true" ';
	if(onchange!='')
		data+=' onchange="'+onchange+'" ';
	data+='><option value=""><? echo $vis_language['TXTSELECT']; ?></option>'+selecthtml+'</select>';
	$(divid).html(data);
}
</script>
</section>
