<?php
use WeboneSMS\WeboneSMS\Enums\WoocommerceOrderVariables;
use WeboneSMS\WeboneSMS\Enums\WoocommerceProductStatuses;
use WeboneSMS\WeboneSMS\Enums\WoocommerceProductVariables;
?>

<h2 style="padding: 10px; background-color: whitesmoke; text-align: center;">ایجاد پیامک جدید</h2>

<form method="post" id="form-woocommerce-template" action="<?php echo add_query_arg(['action' => $_GET['action'] ?? 'create']); ?>">

    <?php if (!isset($_GET['tab']) || 'customer' === $_GET['tab'] || 'admin' === $_GET['tab']): ?>
    <input type="hidden" name="meta[subject]" value="order" />
    <input type="hidden" name="meta[owner]" value="<?php echo $_GET['tab'] ?? 'customer'; ?>" />
    <?php endif; ?>

    <?php if (isset($_GET['tab']) && 'product' === $_GET['tab']): ?>
    <input type="hidden" name="meta[subject]" value="product" />
    <input type="hidden" name="meta[owner]" value="admin" />
    <?php endif; ?>

    <table class="form-table">
        <tbody>
            <tr>
                <th scope="row">
                    <label for="input_name">عنوان</label>
                </th>
                <td>
                    <input type="text" name="title" id="input_name" value="<?php echo $data['template']['title'] ?? ''; ?>" required style="min-width: 400px;"/>
                </td>
            </tr>
            <tr>
                <th scope="row">وضعیت</th>
                <td>
                    <input type="radio" name="is_active" value="1" <?php echo isset($data['template']['is_active']) && $data['template']['is_active'] ? 'checked' : ''; ?> checked />
                    <label>فعال</label>
                    <br/>
                    <input type="radio" name="is_active" value="0" <?php echo isset($data['template']['is_active']) && !$data['template']['is_active'] ? 'checked' : ''; ?> />
                    <label>غیرفعال</label>
                </td>
            </tr>
            <?php if (!isset($_GET['tab']) || 'customer' === $_GET['tab'] || 'admin' === $_GET['tab']): ?>
                <tr>
                    <th scope="row">
                        <label for="select_event">وضعیت سفارش</label>
                    </th>
                    <td>
                        <select name="meta[order_status]" id="select_event" required style="min-width: 400px;">
                            <option value="">انتخاب</option>
                            <?php foreach ($data['order_statuses'] as $key => $status): ?>
                                <option value="<?= $key ?>"
                                        <?php echo ($data['template']['meta']['order_status'] ?? null) === $key ? 'selected' : ''; ?>
                                >
                                    <?php echo $status . " ($key)"; ?>
                                </option>
                            <?php endforeach; ?>
                        </select>
                    </td>
                </tr>
            <?php endif; ?>
            <?php if (isset($_GET['tab']) && 'product' === $_GET['tab']): ?>
                <tr>
                    <th scope="row">
                        <label for="select_product_status">وضعیت محصول</label>
                    </th>
                    <td>
                        <select name="meta[product_status]" id="select_product_status" required style="min-width: 400px;">
                            <option value="">انتخاب</option>
                            <?php foreach (WoocommerceProductStatuses::$items as $key => $status): ?>
                                <option value="<?= $key ?>"
                                        <?php echo ($data['template']['meta']['product_status'] ?? null) === $key ? 'selected' : '' ?>
                                >
                                    <?php echo $status . " ($key)"; ?>
                                </option>
                            <?php endforeach; ?>
                        </select>
                    </td>
                </tr>
            <?php endif; ?>
            <?php if(isset($_GET['tab']) && ('admin' === $_GET['tab'] || 'product' === $_GET['tab'])): ?>
                <tr>
                    <th scope="row">
                        <label for="select_contacts">دریافت کنندگان</label>
                    </th>
                    <td>
                        <select name="meta[contacts_ids][]" id="select_contacts" multiple required style="min-width: 400px;">
                            <option value="">انتخاب</option>
                            <?php
                            $contacts_ids = $data['template']['meta']['contacts_ids'] ?? '';
                            $contacts_ids = explode(',', $contacts_ids);
                            foreach($data['contacts'] as $contact):
                            ?>
                                <option value="<?php echo $contact['id']; ?>"
                                        <?php echo in_array($contact['id'], $contacts_ids) ? 'selected' : ''; ?>
                                >
                                    <?php echo $contact['name']; ?>
                                </option>
                            <?php endforeach; ?>
                        </select>
                    </td>
                </tr>
            <?php endif; ?>
            <tr>
                <th scope="row">
                    <label for="select-pattern">الگو</label>
                </th>
                <td>
                    <select name="pattern_id" id="select-pattern" required style="min-width: 400px;">
                        <option value="">انتخاب</option>
                        <?php foreach($data['patterns'] as $pattern): ?>
                            <option value="<?= $pattern['id'] ?>"
                                    data-pattern_text="<?php echo $pattern['pattern']; ?>"
                                    data-pattern_variables='<?php echo $pattern['variables_json']; ?>'
                                    <?php echo isset($data['template']['pattern_id']) && $data['template']['pattern_id'] == $pattern['id'] ? 'selected' : ''; ?>
                            >
                                <?= $pattern['title'] ?>
                            </option>
                        <?php endforeach; ?>
                    </select>
                    <p id="p-show-pattern"></p>
                </td>
            </tr>
            <tr>
                <th scope="row">
                    <label for="">جایگذاری</label>
                </th>
                <td style="padding-top: 0;">
                    <table>
                        <thead>
                            <tr>
                                <th>متغیر الگو</th>
                                <th>متغیر فرم</th>
                                <th>مقدار پیش فرض</th>
                            </tr>
                        </thead>
                        <tbody id="tbody-variables-and-values">
                            <!-- show inputs by jquery -->
                            <?php if (isset( $data['template']['variables_and_values'] )): ?>
                                <?php
                                     if (!isset($_GET['tab']) || 'customer' === $_GET['tab'] || 'admin' === $_GET['tab']) {
                                        $select_variables = WoocommerceOrderVariables::$items;
                                     } elseif ('product' === $_GET['tab']) {
                                        $select_variables = WoocommerceProductVariables::$items;
                                     }
                                     foreach($data['template']['variables_and_values'] as $item):
                                ?>
                                    <tr>
                                        <td>
                                            <?php echo $item['index'] . ' - ' . $item['variable']; ?>
                                            <input type="hidden" name="variables_and_values[<?php echo $item['index']; ?>][index]" value="<?php echo $item['index']; ?>" />
                                            <input type="hidden" name="variables_and_values[<?php echo $item['index']; ?>][variable]" value="<?php echo $item['variable']; ?>" />
                                        </td>
                                        <td>
                                            <select name="variables_and_values[<?php echo $item['index']; ?>][value]" class="select-variable-value" data-index="<?php echo $item['index']; ?>">
                                                <option value="">انتخاب</option>
                                                <?php /*
                                                <option value="{NAME}" <?php echo '{NAME}' === $item['value'] ? 'selected' : ''; ?>>NAME</option>
                                                <option value="{DOMAIN}" <?php echo '{DOMAIN}' === $item['value'] ? 'selected' : ''; ?>>DOMAIN</option>
                                                <option value="{OTP}" <?php echo '{OTP}' === $item['value'] ? 'selected' : ''; ?>>OTP</option>
                                                */?>

                                                <?php foreach ($select_variables as $key => $value) : ?>
                                                    <option value="<?php echo $key; ?>"
                                                            <?php echo $item['value'] === $key ? 'selected' : ''; ?>
                                                    >
                                                        <?php echo $value; ?>
                                                    </option>
                                                <?php endforeach; ?>
                                            </select>
                                        </td>
                                        <td>
                                            <input type="text" name="variables_and_values[<?php echo $item['index']; ?>][default]" value="<?php echo $item['default']; ?>" class="input-variable-default" data-index="<?php echo $item['index']; ?>" <?php echo empty($item['value']) ? 'required' : ''; ?> />
                                        </td>
                                    </tr>
                                <?php endforeach; ?>
                            <?php endif; ?>
                        </tbody>
                    </table>
                </td>
            </tr>
        </tbody>
    </table>

    <?php if (!isset($_GET['tab']) || 'customer' === $_GET['tab'] || 'admin' === $_GET['tab']): ?>
        <input type="hidden" name="" id="input-order-variables" value='<?php echo json_encode(WoocommerceOrderVariables::$items); ?>' />
    <?php elseif ('product' === $_GET['tab']): ?>
        <input type="hidden" name="" id="input-order-variables" value='<?php echo json_encode(WoocommerceProductVariables::$items); ?>' />
    <?php endif; ?>

    <button type="submit" class="button button-primary">
        &nbsp;
        ایجاد
        &nbsp;
    </button>
</form>

<h2 style="padding: 10px; background-color: whitesmoke; text-align: center;">پیامک‌های ایجاد شده</h2>

<?php $data['table']->display(); ?>