<?php
    include_once('../header.php');
    if(!check_login()){
        header("Location: " . WEB_URL . "logout.php");
        die();
    }
    if(isset($_SESSION['login_type']) && $_SESSION['login_type'] !== "super"){
      header("Location: ".WEB_URL."logout.php");
      die();
    }
    if($_SERVER["REQUEST_METHOD"] === "POST"){
      $post = $_POST;
      
      $template_names = array("end_of_rent", "eviction_notice", "welcome", "maintenance_notification", "payment_reminder");
      //End of Rent Rimder
      foreach($template_names as $name){
        $subject_index = $name . "_subject";
        $message_index = $name . "_message";
        if(isset($post[$subject_index]) && isset($post[$message_index]) && $post[$subject_index] !== "" && $post[$message_index] !== ""){
          $link->begin_transaction(MYSQLI_TRANS_START_READ_WRITE);
          $link->query("DELETE FROM message_templates WHERE name = '$name'");
          $stmt = $link->prepare("INSERT INTO message_templates(`name`, `subject`, `message`, `last_edited_by`) VALUES(?, ?, ?, ?)");
          $stmt->bind_param("sssi", $name, $post[$subject_index], $post[$message_index], $_SESSION["user_id"]);
          $stmt->execute();
          $link->commit();
        }
      }
      $url = WEB_URL . 'mailsms/templates.php';
      header("Location: $url");
      die();
    }else{
      $messages = array();
      $result = $link->query("SELECT * FROM message_templates");

      while($message = mysqli_fetch_assoc($result)){
        $data1["name"] = $message["name"]; 
        $data1["subject"] = $message["subject"]; 
        $data1["message"] = $message["message"]; 
        array_push($messages, $data1);
        if($message["name"] === "end_of_rent"){
          $subject = $message["subject"];
          $body = $message["message"];
          }
      }
    }


?>
<section class="content-header">
  <h1> Templates </h1>
  <ol class="breadcrumb">
    <li><a href="<?= WEB_URL ?> dashboard.php"><i class="fa fa-dashboard"></i><?= $_data['home_breadcam']; ?></a></li>
    <li class="active">Templates</li>
  </ol>
</section>
<!-- Main content -->
<section class="content template_content">
    <form method="POST" enctype="multipart/form-data" action="">
        <div class="box-body">
          <ul class="nav nav-tabs">
            <li class="active">
              <a data-toggle="tab" href="#1"><h5>End of Rent Reminder</h5></a>
            </li>
            <li>
              <a data-toggle="tab" href="#2"><h5>Eviction Notice</h5></a>
            </li>
            <li>
              <a data-toggle="tab" href="#3"><h5>Welcome Message</h5></a>
            </li>
            <li>
              <a data-toggle="tab" href="#4"><h5>Maintenance Notice</h5></a>
            </li>
            <li>
              <a data-toggle="tab" href="#5"><h5>Rent Payment Notification</h5></a>
            </li>
          </ul>
          <div class="tab-content">
            <div id="1" class="tab-pane fade in active">
              <?php 
                unset($subject, $body);
                foreach($messages as $message){ 
                  if($message["name"] === "end_of_rent"){
                    $subject = $message["subject"];
                    $body = $message["message"];
                  }
                }
                ?>
                <div class="form-group">
                  <label for="end_of_rent_subject" class="label">Subject <span class="validate-field">*</span> </label>
                  <input class="form-control" id="end_of_rent_subject" name="end_of_rent_subject" value="<?= $subject ?? '' ?>"/>
                </div>
                <div class="form-group">
                  <label for="end_of_rent_message" class="label">Message <span class="validate-field">*</span> </label>
                  <textarea class="ckeditor" id="end_of_rent_message" name="end_of_rent_message" class="form-control"><?= $body ?? '' ?></textarea>
                </div>
            </div>
            <div id="2" class="tab-pane fade">
              <?php 
                unset($subject, $body);
                foreach($messages as $message){ 
                  if($message["name"] === "eviction_notice"){
                    $subject = $message["subject"];
                    $body = $message["message"];
                  }
                }
                ?>
                <div class="form-group">
                  <label for="eviction_notice_subject" class="label">Subject <span class="validate-field">*</span> </label>
                  <input class="form-control" id="eviction_notice_subject" name="eviction_notice_subject" value="<?= $subject ?? '' ?>"/>
                </div>
                <div class="form-group">
                  <label for="eviction_notice_message" class="label">Message <span class="validate-field">*</span> </label>
                  <textarea class="ckeditor" id="eviction_notice_message" name="eviction_notice_message" class="form-control"><?= $body ?? '' ?></textarea>
                </div>
            </div>
            <div id="3" class="tab-pane fade">
              <?php 
                unset($subject, $body);
                foreach($messages as $message){ 
                  if($message["name"] === "welcome"){
                  $subject = $message["subject"];
                    $body = $message["message"];
                  }
                }
                ?>
              <div class="form-group">
                <label for="welcome_subject" class="label">Subject <span class="validate-field">*</span> </label>
                <input class="form-control" id="welcome_subject" name="welcome_subject" value="<?= $subject?? '' ?>"/>
              </div>
              <div class="form-group">
                <label for="welcome_message" class="label">Message <span class="validate-field">*</span> </label>
                <textarea class="ckeditor" id="welcome_message" name="welcome_message" class="form-control"><?= $body ?? '' ?></textarea>
              </div>
            </div>
            <div id="4" class="tab-pane fade">
              <?php 
                unset($subject, $body);
                foreach($messages as $message){ 
                  if ($message["name"] === "maintenance_notification") {
                    $subject = $message["subject"];
                    $body = $message["message"];
                  }
                }
                        ?>
                  <div class="form-group">
                    <label for="maintenance_notification_subject" class="label">Subject <span class="validate-field">*</span> </label>
                    <input class="form-control" id="maintenance_notification_subject" name="maintenance_notification_subject" value="<?= $subject ?? '' ?>"/>
                  </div>
                  <div class="form-group">
                    <label for="maintenance_notification_message" class="label">Message <span class="validate-field">*</span> </label>
                    <textarea class="ckeditor" id="maintenance_notification_message" name="maintenance_notification_message" class="form-control"><?= $body ?? '' ?></textarea>
                  </div>
            </div>
            <div id="5" class="tab-pane fade">
              <?php 
                unset($subject, $body);
                foreach($messages as $message){ 
                  if ($message["name"] === "payment_reminder") {
                      $subject = $message["subject"];
                      $body = $message["message"];
                  }
                }
                        ?>
                <div class="form-group">
                  <label for="payment_reminder_subject" class="label">Subject <span class="validate-field">*</span> </label>
                  <input class="form-control" id="payment_reminder_subject" name="payment_reminder_subject" value="<?= $subject ?? '' ?>"/>
                </div>
                <div class="form-group">
                  <label for="payment_reminder_message" class="label">Message <span class="validate-field">*</span> </label>
                  <textarea class="ckeditor" id="payment_reminder_message" name="payment_reminder_message" class="form-control"><?= $body ?? '' ?></textarea>
                </div>
            </div>
          </div>
          <div class="form-group pull-left pl-4 mt-3">
            <button type="submit" class="btn btn-success"><i class="fa fa-floppy-o"></i> save</button>
          </div>
        </div>
      </form>

      <script src="<?= WEB_URL; ?>assets/js/jquery-ui.js"></script>
<link href="//cdnjs.cloudflare.com/ajax/libs/jqueryui/1.12.1/jquery-ui.css" rel="stylesheet">
<script type="text/javascript">
 
</script>
<style type="text/css">
</style>
<?php include_once('../footer.php'); ?>