<?php
include_once "../global.php";

$pageTitle = 'Add New Rule - ';
include_once '../header.php';
?>

<!--breadcrumb-->
<div class="page-breadcrumb d-none d-sm-flex align-items-center mb-3">
    <div class="breadcrumb-title pe-3">WhatsApp Management</div>
    <div class="ps-3">
        <nav aria-label="breadcrumb">
            <ol class="breadcrumb mb-0 p-0">
                <li class="breadcrumb-item"><a href="javascript:;"></a>
                </li>
                <li class="breadcrumb-item active" aria-current="page">Add New Rule</li>
            </ol>
        </nav>
    </div>
</div>
<!--end breadcrumb-->
<div class="card">
    <div class="card-body p-4">
        <h5 class="mb-4">Add New Rule</h5>
        <form id="ruleForm" class="row g-3" method="post">
            <div class="col-md-3">
                <label for="r_name" class="form-label">Name <span>*</span></label>
                <input type="text" name="name" id="r_name" class="form-control" required>
            </div>
            <div class="col-md-3">
                <label for="r_priority" class="form-label">Priority</label>
                <input type="number" name="priority" id="r_priority" class="form-control" value="100">
            </div>
            <div class="col-md-3">
                <label class="form-label d-block">Active</label>
                <div class="form-check form-check-inline">
                    <input class="form-check-input" type="radio" name="active" id="r_active_yes" value="1" checked>
                    <label class="form-check-label" for="r_active_yes">Yes</label>
                </div>
                <div class="form-check form-check-inline">
                    <input class="form-check-input" type="radio" name="active" id="r_active_no" value="0" required>
                    <label class="form-check-label" for="r_active_no">No</label>
                </div>
            </div>
            <div class="col-md-6">
                <label for="r_match_type" class="form-label">Match Type</label>
                <select id="r_match_type" name="match_type" class="form-select">
                    <option value="keyword">keyword</option>
                    <option value="first_message">first_message</option>
                    <option value="always">always</option>
                </select>
            </div>
            <div class="col-md-6">
                <label for="r_match_value" class="form-label">Match Value (CSV for keywords)</label>
                <input type="text" name="match_value" id="r_match_value" class="form-control"
                    placeholder="hi, hello, hey">
            </div>
            <div class="col-md-3">
                <label for="r_template_id" class="form-label">Template <span>*</span></label>
                <select name="template_id" id="r_template_id" class="form-select" required>
                    <?php
                    $tpls = $dbF->getRows("SELECT id,name FROM wa_templates ORDER BY name ASC");
                    foreach ($tpls as $t) {
                        echo '<option value="'.$t['id'].'">'.htmlspecialchars($t['name']).'</option>';
                    }
                    ?>
                </select>
            </div>
            <div class="col-md-3">
                <label for="r_delay" class="form-label">Delay (minutes)</label>
                <input type="number" name="schedule_delay_minutes" id="r_delay" class="form-control" value="0">
            </div>
            <div class="col-md-3">
                <label class="form-label d-block">Stop After Match</label>
                <div class="form-check form-check-inline">
                    <input class="form-check-input" type="radio" name="stop_after_match" id="r_stop_yes" value="1"
                        checked>
                    <label class="form-check-label" for="r_stop_yes">Yes</label>
                </div>
                <div class="form-check form-check-inline">
                    <input class="form-check-input" type="radio" name="stop_after_match" id="r_stop_no" value="0"
                        required>
                    <label class="form-check-label" for="r_stop_no">No</label>
                </div>
            </div>
            <div class="col-md-12">
                <button type="submit" class="btn btn-grd-primary px-4">Submit</button>
            </div>
        </form>
    </div>
</div>

<script>
    $("#ruleForm").submit(function(e) {
        e.preventDefault();
        var formData = new FormData(this);

        $.ajax({
            url: '<?php echo WEB_URL; ?>/ajax_call?page=saveWaRule',
            type: 'POST',
            data: formData,
            processData: false,
            contentType: false,
            dataType: 'json',
            beforeSend: function() {
                $("#spinner-overlay").fadeIn('slow');
            },
            success: function(response) {
                showAlert(response.message);
                if (response.status === "success") {
                    setTimeout(() => {
                        window.location.href = '<?php echo WEB_URL; ?>/whatsapp/whatsapp_automation';
                    }, 1000);
                }
            },
            error: function(xhr, status, errorThrown) {
                console.log(xhr.responseText);
            },
            complete: function() {
                $("#spinner-overlay").fadeOut('slow');
            }
        });
    });
</script>

<?php
include_once '../footer.php';
?>