<?php
	include(''. $config['LocalPath'] . '/includes/.sql_con.php');


if(isset($_POST['asset_id'])){

	$asset_id=$_POST['asset_id'];
	$job_id=$_POST['job_id'];
	$task_schedule_id=$_POST['schedule_id'];

if($asset_id==0){
echo "You must select a Asset Type";
}
else{
		$get_asset_info=mysql_query("SELECT SERVICE_ID, CUSTOMER_ID, SERVICE_NAME FROM ASSET WHERE ID=$asset_id");
			while ($assign_asset_row = mysql_fetch_array($get_asset_info)){
					$asset_service_id=$assign_asset_row['SERVICE_ID'];
					$asset_customer_id=$assign_asset_row['CUSTOMER_ID'];
					$asset_service_name=$assign_asset_row['SERVICE_NAME'];
	
					$assign_job_to_asset ="INSERT INTO `TASK` (SERVICE_ID, CUSTOMER_ID, SERVICE_NAME, JOB_ID, JOB_STATUS_ID, SCHEDULE_ID, TASK_STATUS_ID, LAST_COMPLETED) VALUES ('$asset_service_id' , '$asset_customer_id', '$asset_service_name', '$job_id', '5', '$task_schedule_id', '1', NOW())";
					mysql_query($assign_job_to_asset)or die ('Add Failed</br>'.mysql_error());
echo "Job assignment Successful!";
}
}
}
?>

<div class="row">
	<div class="col-lg-5 col-lg-offset-3 col-md-5 col-md-offset-3 col-sm-6 col-sm-offset-3 col-xs-5 col-xs-offset-3">
		<div class="card card-mini">
			<div class="card-header">
				<div class="card-title"><b><center>Add Job to Asset</center></b></div>
			</div>
		<div class="card-body">

			<form class="form form-horizontal" method="post" id="form">
				<div class="form-group">
					<div class="col-md-3">
						<label class="control-label"><b>Select Asset</b></label>
					</div>
					<div class="col-md-5">
                                        <select class="select2" name="asset_id">
                                        <option value="0">Please Selet Asset</option>
                                                        <?php
                                                $asset_fetch=mysql_query("SELECT ID, SERVICE_NAME FROM ASSET");

                                                while ($assetrow = mysql_fetch_array($asset_fetch)){
                                                        $asset_name= $assetrow['SERVICE_NAME'];
                                                        $asset_id = $assetrow['ID'];
                                                        ?>
                                                        <option value="<?php echo $asset_id;?>"><?php echo $asset_name;?></option>
                                                <?php
                                                }
                                                ?>


                                        </select>
					</div>
				</div>
		<div class="form-group">
			<div class="col-md-3">
				<label class="control-label"><b>Select Job</b></label>
			</div>
			<div class="col-md-5">				
                                        <select class="select2" name="job_id">
                                        <option value="0">Please Selet Job</option>
                                                        <?php
                                                $job_fetch=mysql_query("SELECT JOB_ID, JOB_NAME FROM JOB");

                                                while ($jobrow = mysql_fetch_array($job_fetch)){
                                                        $job_name= $jobrow['JOB_NAME'];
                                                        $job_ID = $jobrow['JOB_ID'];
                                                        ?>
                                                        <option value="<?php echo $job_ID;?>"><?php echo $job_name;?></option>
                                                <?php
                                                }
                                                ?>


                                        </select>

			</div>
		</div>
		<!--<div class="form-group">
			<label class="col-md-3 control-label"><b>Asset Name</b></label>
			<div class="col-md-5">
					<input type="text" name="servname" class="form-control" placeholder="Device.hostirian.com">
			</div>
		</div>-->
		<!--<div class="form-group">
			<label class="col-md-3 control-label">Type</label>
			<div class="col-md-9">
				<div class="radio radio-inline">
					<input type="radio" name="radio4" id="radio10" value="option10">
					<label for="radio10">
					Global
					</label>
				</div>
				<div class="radio radio-inline">
					<input type="radio" name="radio4" id="radio11" value="option11" checked>
					<label for="radio11">
					Local
					</label>
				</div>
			</div>
		</div>-->
		<div class="form-group">
			<label class="col-md-3 control-label"><b>Task Schedule</b></label>
			<div class="col-md-5">
				<div class="input-group">
					<select class="select2" name="schedule_id">
					<option value="0">Please Selet The Schedule for this Task</option>
							<?php 
						$schedule_id_fetch=mysql_query("SELECT ID, DESCRIPTION FROM SCHEDULE_ID");

						while ($schedulerow = mysql_fetch_array($schedule_id_fetch)){
							$schedule_id= $schedulerow['ID'];
						        $schedule_description = $schedulerow['DESCRIPTION'];
        						?>
							<option value="<?php echo $schedule_id;?>"><?php echo $schedule_description;?></option>
						<?php
						}
						?>


					</select>
				</div>
			</div>
		</div>
<div class="form-footer">
	<div class="form-group">
		<div class="col-md-5 col-md-offset-3">
			<center>	<button type="submit" class="btn btn-primary">Assign Job to Asset</button>
			<button type="button" class="btn btn-default" onclick="resetForm()">Clear</button> </center>
				<script>
					function resetForm() {
						document.getElementById("form").reset();
					}
				</script>
		</div>
	</div>
</div>
</form>
</div>
</div>
</div>
</div>
<!--<div class="row">
	<div class="col-lg-12 col-md-12 col-sm-12 col-xs-12">
		<div class="card card-mini">
      <div class="card-header">
        <div class="card-title"><b><center>Active Assets</center></b></div>
        <ul class="card-action">
          <li>
            <a href="#" onClick="MyFunction();">
              <i class="fa fa-refresh"></i>
            </a>
		<script>
			function MyFunction() {
				location.reload();
			}
		</script>
          </li>
        </ul>
      </div>
      <div class="card-body no-padding table-responsive">
        <table class="table card-table">
          <thead>
            <tr>
              <th><b>Service ID</b></th>
              <th><b>Customer ID</b></th>
	      <th><b>Device Name</b></th>
	      <th><b>Asset Type</b></th>
            </tr>
          </thead>
          <tbody>
<?php


$tasks_result = mysql_query('SELECT a.SERVICE_ID as asset_service_id, a.CUSTOMER_ID as asset_customer_id, a.SERVICE_NAME as asset_service_name, t.TYPE as asset_type  FROM ASSET AS a INNER JOIN ASSET_TYPE as t on a.ASSET_TYPE_ID=t.ID');

while ($row=mysql_fetch_array($tasks_result)){
        $service_id=$row['asset_service_id'];
        $customer_id=$row['asset_customer_id'];
	$service_name=$row['asset_service_name'];
	$asset_type_id=$row['asset_type'];
        


?>
            <tr>
              <td><?php echo $service_id;?></td>
              <td><?php echo $customer_id;?></td>
              <td><?php echo $service_name;?></td>
	      <td><?php echo $asset_type_id;?></td>
            </tr>
<?php
}
?>
          </tbody>
        </table>
      </div>-->
    </div>

	</div>
</div>	
