<?php
if (!defined('ABSPATH')) exit;

$option_key = "simpinby_mail_settings";

$cur = simpinby_get_currency();
$CURRENCY = $cur["symbol"];


$default = [
  "subject" => "Your Invoice {invoice_number}",
  "message" => "Hello {customer_name},\n\nPlease find your invoice {invoice_number} attached.\nTotal: ".esc_html($CURRENCY)."{invoice_total}\n\nThank you."
];

$data = get_option($option_key, $default);
$data = wp_parse_args($data, $default);

if (isset($_POST["simpinby_save_mail_settings"])) {

  if (!current_user_can('manage_options')) {
      wp_die('Permission denied.');
  }

  check_admin_referer("simpinby_mail_settings_save");

  $data["subject"] = isset($_POST["subject"])
      ? sanitize_text_field( wp_unslash($_POST["subject"]) )
      : "";

  $data["message"] = isset($_POST["message"])
      ? sanitize_textarea_field( wp_unslash($_POST["message"]) )
      : "";

  update_option($option_key, $data);

  echo '<div class="updated notice"><p><strong>' .
      esc_html__('Saved!', 'simpinby-invoice-manager') .
      '</strong> ' .
      esc_html__('Mail settings updated.', 'simpinby-invoice-manager') .
      '</p></div>';
}

?>

<h2>Mail Settings</h2>

<form method="post">
  <?php wp_nonce_field("simpinby_mail_settings_save"); ?>

  <table class="form-table" style="max-width:800px;">
    <tr>
      <th><label>Email Subject</label></th>
      <td>
        <input type="text" name="subject" class="large-text" value="<?php echo esc_attr($data["subject"]); ?>">
      </td>
    </tr>

    <tr>
      <th><label>Email Message</label></th>
      <td>
        <textarea name="message" class="large-text" rows="8"><?php echo esc_textarea($data["message"]); ?></textarea>
        <p class="description">
          Variables: <code>{customer_name}</code>, <code>{invoice_number}</code>, <code>{invoice_total}</code>, <code>{invoice_date}</code>
        </p>
      </td>
    </tr>
  </table>

  <p>
    <button type="submit" name="simpinby_save_mail_settings" class="button button-primary">
      Save Mail Settings
    </button>
  </p>
</form>
