<?php
$getStaffId = $_SESSION['staff'] ? $_SESSION['staff']['s_id'] : header("Location: ?pagecontent=login_staff");
;
$guestDetail = getDetailByTable('staffs', $getStaffId);
?>

<style>
  .title_left {
    width: 90% !important;
    font-size: 14px;
  }
</style>

<!-- page content -->
<div class="right_col" role="main">
  <div class="">
    <div class="page-title">
      <div class="title_left">
        <H3>UPDATE EMERGENCY CONTACT</H3>
      </div>

    </div>

    <div class="clearfix"></div>

    <div class="row">

      <div class="col-md-6 col-sm-6 col-xs-12">
        <div class="x_panel">
          <div class="x_title">
            <h2>EMERGENCY CONTACT</h2>
            <div class="clearfix"></div>
          </div>
          <div class="x_content">
            <form method="post" id="demo-form2" data-parsley-validate class="form-horizontal form-label-left">

              <div class="form-group">
                <label class="control-label col-md-3 col-sm-3 col-xs-12">Full Name</label>
                <div class="col-md-9 col-sm-3 col-xs-12">
                  <input id="name" class="form-control col-md-12 col-xs-12" name="fullName" type="text"
                    value="<?php echo $guestDetail['s_fullname']; ?>" disabled>
                </div>
              </div>

              <div class="form-group">
                <label class="control-label col-md-3 col-sm-3 col-xs-12">Contact</label>
                <div class="col-md-9 col-sm-3 col-xs-12">
                  <input id="name" class="form-control col-md-12 col-xs-12" name="contact" type="text"
                    value="<?php echo $guestDetail['s_contact']; ?>" disabled>
                </div>
              </div>

              <div class="form-group">
                <label class="control-label col-md-3 col-sm-3 col-xs-12">Emergency Contact</label>
                <div class="col-md-9 col-sm-3 col-xs-12">
                  <input id="" class="form-control col-md-12 col-xs-12" name="emergencyContact" type="text"
                    value="<?php echo $guestDetail['s_e_contact']; ?>" required>
                </div>
              </div>

              <div class="form-group">
                <label class="control-label col-md-3 col-sm-3 col-xs-12">Emergency Name</label>
                <div class="col-md-9 col-sm-3 col-xs-12">
                  <input id="" class="form-control col-md-12 col-xs-12" name="emergencyName" type="text"
                    value="<?php echo $guestDetail['s_e_name']; ?>" required>
                </div>
              </div>

              <div class="form-group">
                <label class="control-label col-md-3 col-sm-3 col-xs-12">Relationship</label>
                <div class="col-md-9 col-sm-3 col-xs-12">
                  <select class="form-control" name="emergencyRelationship" required>
                    <!-- <option value="0">NONE</option> -->
                    <?php foreach (getRelationshipById("ALL") as $item) { ?>
                      <option value="<?php echo $item['id']; ?>" <?php echo select($guestDetail['s_e_relationship'], $item['id']); ?>>
                        <?php echo $item['title']; ?>
                      </option>
                    <?php } ?>
                  </select>
                </div>
              </div>

              <div class="ln_solid"></div>

              <div class="form-group">
                <div class="col-md-6 col-md-offset-3">
                  <button id="send" type="submit" class="btn btn-success" name="submit">Update</button>
                </div>
              </div>

            </form>
            <?php
            if (isset($_POST['btnPayment'])) {
              $payment = $_POST['payment'];
              mysqli_query($conn, "UPDATE housekeep SET hk_payment=$payment WHERE hk_id=$housekeepId");

              header("Location: ?pagecontent=housekeep_cleaning_update&hk=$getHousekeepId&room=$getRoomId&date=$getDate");
            }
            ?>
          </div>
        </div>
      </div>

    </div>
  </div>
</div>
<!-- /page content -->
<?php
if (isset($_POST['submit'])) {

  $postEmergencyContact = $_POST['emergencyContact'];
  $postEmergencyName = $_POST['emergencyName'];
  $postEmergencyRelationship = $_POST['emergencyRelationship'];

  $sql = "UPDATE staffs SET 
  s_e_contact='$postEmergencyContact', 
  s_e_name='$postEmergencyName', 
  s_e_relationship=$postEmergencyRelationship
  WHERE s_id=$getStaffId";

  //TELEGRAM NOTIFICATION
  $messaggio = "STAFF {$_SESSION['staff']['s_nickname']} UPDATES EMERGENCY INFO\n\nEmergency Contact : {$postEmergencyContact}\nEmergency Name : {$postEmergencyName}\nEmergency Relationship : {$postEmergencyRelationship}\n\n" . $ipAddress;

  if (mysqli_query($conn, $sql)) {
    $_SESSION['staff']['s_e_contact'] = $postEmergencyContact;
    $_SESSION['staff']['s_e_name'] = $postEmergencyName;
    $_SESSION['staff']['s_e_relationship'] = $postEmergencyRelationship;

    // sendMessage2(26236173, $messaggio);
    sendMessage2(-1001643278548, $messaggio);

    header("Location: ?pagecontent=housekeep_cleaning_list");
  } else {
    echo '<script>alert("Error updating emergency info!")</script>';
  }
}
?>