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

$option_key = "simpinby_pdf_settings";

$default = [
  "company_name" => "My Company",
  "footer_text" => "Thank you for your business!",
  "logo_url" => ""
];

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

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

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

    check_admin_referer("simpinby_pdf_settings_save");

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

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

    $data["company_email"] = isset($_POST["company_email"])
        ? sanitize_email( wp_unslash($_POST["company_email"]) )
        : "";

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

    $data["company_website"] = isset($_POST["company_website"])
        ? esc_url_raw( wp_unslash($_POST["company_website"]) )
        : "";

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

    $data["logo_url"] = isset($_POST["logo_url"])
        ? esc_url_raw( wp_unslash($_POST["logo_url"]) )
        : "";

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

    update_option($option_key, $data);

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

?>

<h2>Display & PDF Settings</h2>

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

  <table class="form-table" style="max-width:700px;">

    <tr>
        <th><label>Currency</label></th>
        <td>
            <?php
            $currencies = [
                "INR" => ["₹", "Indian Rupee (INR)"],
                "USD" => ["$", "US Dollar (USD)"],
                "EUR" => ["€", "Euro (EUR)"],
                "GBP" => ["£", "British Pound (GBP)"],
                "AED" => ["د.إ", "UAE Dirham (AED)"],
                "SAR" => ["﷼", "Saudi Riyal (SAR)"],
                "CAD" => ["$", "Canadian Dollar (CAD)"],
                "AUD" => ["$", "Australian Dollar (AUD)"],
            ];

            $selected_currency = $data["currency_code"] ?? "INR";
            ?>

            <select name="currency_code" id="simpinby_currency_code">
            <?php foreach ($currencies as $code => $info): ?>
                <option value="<?php echo esc_attr($code); ?>" <?php selected($selected_currency, $code); ?>>
                <?php echo esc_html($info[1]); ?>
                </option>
            <?php endforeach; ?>
            </select>

            <p class="description">Currency will be used in Create Invoice, View Invoice and PDF.</p>
        </td>
    </tr>

    <tr>
      <th><label>Company Name</label></th>
      <td>
        <input type="text" name="company_name" class="regular-text" value="<?php echo esc_attr($data["company_name"]); ?>">
      </td>
    </tr>

    <tr>
        <th><label>Company Phone</label></th>
        <td><input type="text" name="company_phone" class="regular-text" value="<?php echo esc_attr($data["company_phone"] ?? ""); ?>"></td>
    </tr>

    <tr>
        <th><label>Company Email</label></th>
        <td><input type="email" name="company_email" class="regular-text" value="<?php echo esc_attr($data["company_email"] ?? ""); ?>"></td>
    </tr>

    <tr>
        <th><label>Company Address</label></th>
        <td><textarea name="company_address" class="large-text" rows="3"><?php echo esc_textarea($data["company_address"] ?? ""); ?></textarea></td>
    </tr>

    <tr>
        <th><label>Company Website</label></th>
        <td><input type="text" name="company_website" class="regular-text" value="<?php echo esc_attr($data["company_website"] ?? ""); ?>"></td>
    </tr>


    <tr>
        <th><label>Company Logo</label></th>
        <td>
            <input type="text" id="simpinby_logo_url" name="logo_url" class="regular-text"
            value="<?php echo esc_attr($data["logo_url"]); ?>" />

            <button type="button" class="button" id="simpinby_upload_logo_btn">
            Upload / Select Logo
            </button>

            <button type="button" class="button" id="simpinby_remove_logo_btn">
            Remove
            </button>

            <p class="description">Select logo from WordPress Media Library.</p>

            <div style="margin-top:10px;">
            <?php if (!empty($data["logo_url"])): ?>
                <img id="simpinby_logo_preview" src="<?php echo esc_url($data["logo_url"]); ?>"
                style="max-height:70px; border:1px solid #ddd; padding:6px; border-radius:8px; background:#fff;" />
            <?php else: ?>
                <img id="simpinby_logo_preview" src=""
                style="display:none; max-height:70px; border:1px solid #ddd; padding:6px; border-radius:8px; background:#fff;" />
            <?php endif; ?>
            </div>
        </td>
    </tr>


    <tr>
      <th><label>Footer Text</label></th>
      <td>
        <input type="text" name="footer_text" class="regular-text" value="<?php echo esc_attr($data["footer_text"]); ?>">
      </td>
    </tr>
  </table>

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