<?php  include VFORM_PLUGIN_PATH."inc/admin/notification-bar.php"; ?>

<?php
global $wpdb;
$table_automations = $wpdb->prefix . 'vform_automations';
$table_forms = $wpdb->prefix . 'vform';
$forms = $wpdb->get_results("SELECT id, formname FROM $table_forms ORDER BY id DESC");
$automations = $wpdb->get_results("SELECT * FROM $table_automations ORDER BY id DESC");

$license_key = get_option('vpsuform_secret_key'); 
$automation_count = count($automations);
// delete_option('vpsuform_secret_key');

?>
<div class="wrap vform-automation-wrap">
  <div class="vform-automation-header">
    <h2>Automated Campaign</h2>
    <p>Automated Campaign helps you send a sequence of emails automatically at scheduled intervals.<br>
    You set multiple emails, choose delays between each email (like 1 day, 3 days, 5 days), and the system will send them one-by-one to your contacts without you needing to do anything manually.
    </p>
    <div>

     <?php if(!$license_key && $automation_count >= 1){}else{ ?>
      <button id="vf-new-workflow" class="vf-btn vf-btn-primary"><i class="dashicons dashicons-plus"></i> New
        Workflow</button>
      <?php } ?>
    </div>
  </div>

  <div class="vf-panel" id="vf-dashboard-panel" style="margin-top:14px;">
    <h3>Existing Workflows</h3>

    <?php if(!$license_key && $automation_count >= 1): ?>
        <div style="background:#fff9d7;border-left:4px solid #ff9800;padding:15px;margin-bottom:15px;">
          <strong>Upgrade Required</strong><br>
          You can only create <b>1</b> automation in Free plan.<br>
          Enter your Secret License Key to unlock unlimited automations.

          <div style="margin-top:10px;">
            <input type="text" id="vf-secret-key" style="width:300px;" placeholder="Enter Secret Key">
            <button class="button button-primary" id="vf-save-key">Activate Key</button>
          </div>

          <br>
          <small>After purchase you will receive this key in your email.</small>
          <br>
          <a href="https://vpsuform.info/product/vpsuform-pro-automation/" class="btn button-secondary" target="_blank">Purchase License Key Here</a>
        </div>
        <script>
          jQuery("#vf-save-key").on("click", function() {
            jQuery(this).text('Checking...');
              let key = jQuery("#vf-secret-key").val();
              
              jQuery.post(ajaxurl, {
                  action: "vpsuform_save_key",
                  key: key
              }, function(r){
                  jQuery("#vf-save-key").text('Activate Key');
                  alert(r.msg);
                  if(r.success){
                      location.reload();
                  }
              }, "json");
          });

        </script>
    <?php endif; ?>
    
    <table class="widefat striped">
      <thead>
        <tr>
          <th>ID</th>
          <th>Form</th>
          <th>Name</th>
          <th>Actions</th>
        </tr>
      </thead>
      <tbody>
        <?php foreach ($automations as $a): ?>
        <?php
                 // find form name
                 $fn = $wpdb->get_var($wpdb->prepare("SELECT formname FROM {$table_forms} WHERE id=%d", $a->form_id));
              ?>
        <tr>
          <td><?php echo $a->id; ?></td>
          <td><?php echo esc_html($fn ?: $a->form_id); ?></td>
          <td><?php echo esc_html($a->sequence_name); ?></td>
          <td>
            <button class="vf-btn vf-btn-edit vf-btn-sm edit-automation" data-id="<?php echo $a->id; ?>">Edit</button>
            <button class="vf-btn vf-btn-danger vf-btn-sm delete-automation"
              data-id="<?php echo $a->id; ?>">Delete</button>
          </td>
        </tr>
        <?php endforeach; ?>
      </tbody>
    </table>
    
  </div>

  <div class="vform-automation-main">
    <div class="vform-left">
      <div class="vf-panel">
        <h3>Setup</h3>

        <label>Choose Form</label>
        <select id="vf-form-id">
          <option value="">— select form —</option>
          <?php foreach ($forms as $f): ?>
          <option value="<?php echo esc_attr($f->id); ?>"><?php echo esc_html($f->formname); ?></option>
          <?php endforeach; ?>
        </select>

        <label>Choose Email Field</label>
        <select id="vf-email-field-select">
          <option value="">— select email field —</option>
        </select>


        <label>Workflow Name</label>
        <input id="vf-workflow-name" type="text" placeholder="e.g. Welcome series">
        <input type="hidden" id="automation_id" value="">

        <div style="margin-top:12px;" class="vf-automation-actions">
          <button id="vf-add-delay" class="vf-btn vf-btn-primary">
            <i class="dashicons dashicons-clock"></i> Add Delay
          </button>
          <button id="vf-add-email" class="vf-btn vf-btn-primary">
            <i class="dashicons dashicons-plus-alt"></i> Add Email
          </button>
        </div>

        <p style="margin-top:14px; color:#666; font-size:13px;">
          Drag cards to reorder. Click an Email card to open the editor. Delay supports minutes/hours/days.
        </p>

        <div style="margin-top:12px;" class="vf-automation-actions">
          <button id="vf-save-workflow" class="vf-btn vf-btn-primary">Save Workflow</button>
          <button id="vf-clear-workflow" class="vf-btn vf-btn-secondary" style="margin-left:8px">Cancel</button>
        </div>
      </div>



    </div>

    <div class="vform-canvas-wrap">
      <div class="vform-canvas" id="vf-canvas">
        <!-- Cards will be appended here -->
        <div class="vf-canvas-start">Start</div>
      </div>
    </div>
  </div>
</div>



<!-- Email editor modal (hidden) -->
<div id="vf-email-modal" class="vf-modal" aria-hidden="true">
  <div class="vf-modal-inner">
    <h3>Edit Email</h3>

    <label>Subject</label>
    <input id="vf-modal-subject" type="text">

    <label style="margin-top:10px">Editor</label>
    <!-- <div class="vf-editor-toolbar">
      <button type="button" data-cmd="bold"><strong>B</strong></button>
      <button type="button" data-cmd="italic"><em>I</em></button>
      <button type="button" data-cmd="createLink">Link</button>
      <button type="button" data-cmd="insertImage">Image</button>
      <button type="button" data-cmd="viewHtml">HTML</button>
    </div> -->

    <div id="vf-editor">
       <?php 
        wp_enqueue_editor();
          wp_editor( '', 'vf_email_editor', [
            'textarea_name' => 'vf_email_editor',
            'media_buttons' => true,
            'teeny' => false,
            'tinymce' => true
          ]);
        ?>
    </div>

    <div style="text-align:right; margin-top:12px;">
      <button id="vf-modal-cancel" class="button">Cancel</button>
      <button id="vf-modal-save" class="button button-primary">Save</button>
    </div>
  </div>
</div>



<script src="<?php echo VFORM_PLUGIN_URL."assets/js/jquery-ui.js"; ?>"></script>
