<?php 
include '../../connection.php';

if (isset($_POST['removing_schedule'])) {
	$id = $_POST['id'];
	$query = "DELETE FROM tbl_transaction_schedule WHERE schedule_id = '$id'";
	$query_result  = $conn->query($query);
}

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

    echo '
    <table id="mytable" class="table table-striped table-sm">
        <thead>
            <tr class="text-center">
                <th>#</th>
                <th>Complainant</th>
                <th>Respondent</th>
                <th>Complaint</th>
                <th>Incident Address</th>
                <th>Incident Date</th>
                <th>Hearing Date</th>
                <th>Action</th>
            </tr>
        </thead>
        <tbody>
    ';

    $query = "
       SELECT 
            ts.schedule_id,
            ts.UniqueID,
            ts.settle_date,
            ts.status AS schedule_status,
            ts.date_created AS schedule_created_date,
            t.resident_id,
            t.respondent,
            t.incident_address,
            t.complaint_type_id,
            t.description,
            t.incident_date,
            r.first_name,
            r.middle_name,
            r.last_name,
            ty.complain_name
        FROM tbl_transaction_schedule AS ts
        LEFT JOIN tbl_transaction AS t ON ts.UniqueID = t.UniqueID
        LEFT JOIN tbl_resident AS r ON t.resident_id = r.resident_id
        LEFT JOIN tbl_complaint_types ty ON t.complaint_type_id = ty.complaint_type_id
        ORDER BY ts.date_created DESC
    ";

    $query_result = $conn->query($query);

    if ($query_result->num_rows > 0) {
        $count = 1;
        foreach ($query_result as $row) {
            $fullname = $row['last_name'] . ', ' . $row['first_name'] . ' ' . $row['middle_name'];
            echo '
            <tr>
                <td class="text-center">'.$count++.'</td>
                <td>'.$fullname.'</td>
                <td>'.$row['respondent'].'</td>
                 <td>'.$row['complain_name'].'</td>
                <td>'.$row['incident_address'].'</td>
                <td>'.date("F d, Y", strtotime($row['incident_date'])).'</td>
                <td>'. date("F d, Y", strtotime($row['settle_date'])).'</td>
                <td class="text-center">
                <div class="dropdown">
				  <a class="btn btn-secondary dropdown-toggle" href="#" role="button" data-toggle="dropdown" aria-expanded="false">
				    Action
				  </a>

				  <div class="dropdown-menu">
				    <a class="dropdown-item" href="pages/form_three.php?page='.$row['UniqueID'].'" target="__blank"><i class="fa fa-eye"></i> View</a>
				    <a class="dropdown-item remove-schedule" data-id="'.$row['schedule_id'].'"><i class="fa fa-trash text-danger"></i> Remove</a>
				  </div>
				</div>
                </td>
            </tr>
            ';
        }
    }

    echo '
        </tbody>
    </table>
    ';
}
?>
