<?php

declare(strict_types=1);

/**
 * Contract create form — Karvi Partner API fields + full partner form (single submit).
 *
 * @var array $layoutGroups
 * @var bool $usingPartnerApi
 * @var bool $usingLocalStore
 * @var string $action
 */

require_once dirname(__DIR__, 2) . '/includes/layout.php';
require_once dirname(__DIR__, 2) . '/includes/contract_form.php';

render_header('title_new_contract', ['layout' => 'admin', 'page' => 'create']);
render_flash();
?>

<?php if ($usingPartnerApi): ?>
<div class="mb-4 flex items-start gap-3 p-3 rounded-xl border border-karvi-200 bg-karvi-50 text-karvi-700">
    <span class="material-symbols-rounded shrink-0 text-base">cloud_upload</span>
    <p class="text-xs leading-relaxed"><?= e(__('partner_api_create_mode')) ?></p>
</div>
<?php elseif ($usingLocalStore): ?>
<div class="mb-4 flex items-start gap-3 p-3 rounded-xl border border-karvi-200 bg-karvi-50 text-karvi-700">
    <span class="material-symbols-rounded shrink-0 text-base">info</span>
    <p class="text-xs leading-relaxed"><?= e(__('local_store_mode')) ?></p>
</div>
<?php endif; ?>

<div class="card !p-5 sm:!p-6">
    <div class="flex flex-col sm:flex-row sm:items-center sm:justify-between gap-2 mb-5 pb-4 border-b border-karvi-200">
        <div>
            <h2 class="font-display font-bold text-lg text-karvi-950"><?= e(__('partner_info')) ?></h2>
            <p class="text-xs text-karvi-500 mt-0.5">Create contract — POST /karvipartner/contracts</p>
        </div>
    </div>

    <form method="POST" action="<?= e($action) ?>" class="compact-form space-y-4" id="contractCreateForm">
        <div class="grid grid-cols-1 sm:grid-cols-2 gap-4 p-4 rounded-xl bg-karvi-50 border border-karvi-200">
            <div class="form-group sm:col-span-2">
                <label class="form-label" for="type">type</label>
                <select id="type" name="type" class="form-input" required>
                    <?php foreach (['partner' => 'partner (default)', 'website' => 'website', 'pos' => 'pos'] as $value => $label): ?>
                    <option value="<?= e($value) ?>" <?= (($_POST['type'] ?? 'partner') === $value) ? 'selected' : '' ?>><?= e($label) ?></option>
                    <?php endforeach; ?>
                </select>
            </div>
            <div class="form-group sm:col-span-2">
                <label class="form-label" for="restaurant_name">restaurant_name</label>
                <input type="text" id="restaurant_name" name="restaurant_name" class="form-input" required value="<?= e((string)($_POST['restaurant_name'] ?? '')) ?>">
            </div>
            <div class="form-group">
                <label class="form-label" for="brand">brand</label>
                <input type="text" id="brand" name="brand" class="form-input" value="<?= e((string)($_POST['brand'] ?? contract_field_default('brand'))) ?>">
            </div>
            <div class="form-group">
                <label class="form-label" for="owner_name">owner_name</label>
                <input type="text" id="owner_name" name="owner_name" class="form-input" value="<?= e((string)($_POST['owner_name'] ?? '')) ?>" placeholder="<?= e(__('field_owner_name')) ?>">
            </div>
            <div class="form-group">
                <label class="form-label" for="company_phone">company_phone</label>
                <input type="tel" id="company_phone" name="company_phone" class="form-input" value="<?= e((string)($_POST['company_phone'] ?? '')) ?>">
            </div>
            <div class="form-group">
                <label class="form-label" for="email">email</label>
                <input type="email" id="email" name="email" class="form-input" value="<?= e((string)($_POST['email'] ?? '')) ?>">
            </div>
            <div class="form-group">
                <label class="form-label" for="street_address">street_address</label>
                <input type="text" id="street_address" name="street_address" class="form-input" value="<?= e((string)($_POST['street_address'] ?? $_POST['street'] ?? '')) ?>">
            </div>
            <div class="form-group">
                <label class="form-label" for="zip_city">zip_city</label>
                <input type="text" id="zip_city" name="zip_city" class="form-input" value="<?= e((string)($_POST['zip_city'] ?? $_POST['postal_city'] ?? '')) ?>">
            </div>
            <div class="form-group sm:col-span-2">
                <label class="form-label" for="contract_url">contract_url</label>
                <input type="url" id="contract_url" name="contract_url" class="form-input" value="<?= e((string)($_POST['contract_url'] ?? '')) ?>" placeholder="<?= e(function_exists('api_partner_create_contract_url') ? api_partner_create_contract_url() : '') ?>">
                <p class="cf-hint text-xs text-karvi-500 mt-1">Leave empty to use the default contract terms URL.</p>
            </div>
            <div class="form-group sm:col-span-2">
                <label class="form-label" for="raw_data_json">raw_data (optional JSON)</label>
                <textarea id="raw_data_json" name="raw_data_json" class="form-input font-mono text-xs min-h-[80px]" placeholder='{"anything":"saved as-is","nested":{"k":1}}'><?= e((string)($_POST['raw_data_json'] ?? '')) ?></textarea>
            </div>
        </div>

        <div class="cf-row cf-row-2">
            <?php foreach ($layoutGroups['top'] as $section): ?>
                <?php render_form_section_card($section); ?>
            <?php endforeach; ?>
        </div>

        <div class="cf-row cf-row-1">
            <?php foreach ($layoutGroups['company'] as $section): ?>
                <?php render_form_section_card($section); ?>
            <?php endforeach; ?>
        </div>

        <div class="cf-row cf-row-3">
            <?php foreach ($layoutGroups['commissions'] as $section): ?>
                <?php render_form_section_card($section); ?>
            <?php endforeach; ?>
        </div>

        <div class="pt-3 border-t border-karvi-200">
            <button type="submit" class="btn-primary w-full !py-2.5">
                <span class="material-symbols-rounded">bolt</span>
                <?= e(__('generate_contract')) ?>
            </button>
        </div>
    </form>
</div>

<?php render_footer(['layout' => 'admin', 'scripts' => ['/assets/js/contract-form.js']]); ?>
