Du kan inte välja fler än 25 ämnen Ämnen måste starta med en bokstav eller siffra, kan innehålla bindestreck ('-') och vara max 35 tecken långa.

138 lines
8.3KB

  1. <script>
  2. window.__customerTypes = <?= json_encode($model->customerTypes, JSON_HEX_TAG | JSON_THROW_ON_ERROR) ?>;
  3. window.__initialCtId = <?= json_encode($model->form['customer_type_id'], JSON_HEX_TAG | JSON_THROW_ON_ERROR) ?>;
  4. window.__initialCtVals = <?= json_encode($model->form['attribute_values'], JSON_HEX_TAG | JSON_THROW_ON_ERROR) ?>;
  5. </script>
  6. <section class="content-stack">
  7. <div class="page-toolbar">
  8. <div class="section-heading">
  9. <h1><?= e($model->title) ?></h1>
  10. <p>Select a customer type, then fill in the attribute values.</p>
  11. </div>
  12. <a class="button button-secondary" href="/customers">&larr; Back to list</a>
  13. </div>
  14. <?php if (!$model->customerTypes): ?>
  15. <div class="alert alert-error">
  16. No customer types exist yet. <a href="/customer-types/create">Create a customer type</a> before adding customers.
  17. </div>
  18. <?php else: ?>
  19. <section class="section-panel" x-data="customerForm(window.__customerTypes, window.__initialCtId, window.__initialCtVals)">
  20. <?php if (isset($model->errors['_token'])): ?>
  21. <div class="alert alert-error"><?= e($model->errors['_token'][0]) ?></div>
  22. <?php endif; ?>
  23. <?php if (isset($model->errors['_duplicate'])): ?>
  24. <div class="alert alert-error"><?= $model->errors['_duplicate'][0] ?></div>
  25. <?php endif; ?>
  26. <form method="post" action="/customers" class="ct-form" novalidate>
  27. <?= csrf_field() ?>
  28. <div class="form-section">
  29. <label class="field field-full">
  30. <span>Customer type <span class="required-mark">*</span></span>
  31. <select class="input<?= isset($model->errors['customer_type_id']) ? ' input-error' : '' ?>"
  32. name="customer_type_id" x-model="selectedTypeId" x-on:change="onTypeChange()" required>
  33. <option value="0">— Select a customer type —</option>
  34. <?php foreach ($model->customerTypes as $ct): ?>
  35. <option value="<?= e((string) $ct['id']) ?>"
  36. <?= (int) $model->form['customer_type_id'] === $ct['id'] ? 'selected' : '' ?>>
  37. <?= e($ct['name']) ?>
  38. </option>
  39. <?php endforeach; ?>
  40. </select>
  41. <?php if (isset($model->errors['customer_type_id'])): ?>
  42. <small class="field-error"><?= e($model->errors['customer_type_id'][0]) ?></small>
  43. <?php endif; ?>
  44. </label>
  45. </div>
  46. <div class="form-section" x-show="currentAttributes.length > 0">
  47. <div class="attributes-header">
  48. <h3>Attribute values</h3>
  49. <p class="attributes-hint">Fields defined by the selected customer type.</p>
  50. </div>
  51. <div class="form-grid">
  52. <template x-for="attr in currentAttributes" :key="attr.name">
  53. <label class="field" :class="{ 'api-lookup-label': attr.type === 'api_lookup' }">
  54. <span x-text="attr.name"></span>
  55. <template x-if="attr.type === 'boolean'">
  56. <select class="input"
  57. :name="`attribute_values[${attr.name}]`"
  58. x-on:change="attributeValues[attr.name] = $event.target.value">
  59. <option value="" :selected="!attributeValues[attr.name]">— Select —</option>
  60. <option value="true" :selected="attributeValues[attr.name] === 'true'">True</option>
  61. <option value="false" :selected="attributeValues[attr.name] === 'false'">False</option>
  62. </select>
  63. </template>
  64. <template x-if="attr.type === 'api_lookup'">
  65. <div class="api-lookup-field">
  66. <input class="input" type="text"
  67. :name="`attribute_values[${attr.name}]`"
  68. x-model="attributeValues[attr.name]"
  69. placeholder="Type to search…"
  70. autocomplete="off"
  71. x-on:focus="openApiLookup(attr.name)"
  72. x-on:blur="closeApiLookup(attr.name)"
  73. x-on:keydown.escape.prevent="closeApiLookup(attr.name)">
  74. <div class="api-lookup-dropdown"
  75. x-show="apiLookupOpen[attr.name]"
  76. x-on:mousedown.prevent>
  77. <p class="api-lookup-loading" x-show="apiLookupState[attr.name] === 'loading'">Loading…</p>
  78. <div class="api-lookup-table" x-show="apiLookupState[attr.name] !== 'loading' && apiLookupState[attr.name] !== 'error'">
  79. <div class="api-lookup-thead"
  80. :style="`grid-template-columns: repeat(${getApiOptions(attr.name).fields.length || 1}, 1fr)`">
  81. <template x-for="f in getApiOptions(attr.name).fields" :key="f">
  82. <span x-text="f"></span>
  83. </template>
  84. </div>
  85. <div class="api-lookup-tbody">
  86. <template x-for="rec in getFilteredRecords(attr.name, attributeValues[attr.name])" :key="rec._primary">
  87. <div class="api-lookup-tr"
  88. :class="{ 'is-selected': attributeValues[attr.name] === rec._primary }"
  89. :style="`grid-template-columns: repeat(${getApiOptions(attr.name).fields.length || 1}, 1fr)`"
  90. x-on:click="selectApiOption(attr, rec)">
  91. <template x-for="(v, i) in rec._display" :key="i">
  92. <span x-text="v"></span>
  93. </template>
  94. </div>
  95. </template>
  96. <div class="api-lookup-empty" x-show="!getFilteredRecords(attr.name, attributeValues[attr.name]).length">No results.</div>
  97. </div>
  98. </div>
  99. <small class="field-error" x-show="apiLookupState[attr.name] === 'error'" x-text="apiLookupError[attr.name] || 'Fetch failed.'"></small>
  100. </div>
  101. </div>
  102. </template>
  103. <template x-if="attr.type !== 'boolean' && attr.type !== 'api_lookup'">
  104. <input class="input" :type="inputType(attr.type)"
  105. :name="`attribute_values[${attr.name}]`"
  106. :value="attributeValues[attr.name] ?? ''"
  107. x-on:input="attributeValues[attr.name] = $event.target.value">
  108. </template>
  109. </label>
  110. </template>
  111. </div>
  112. </div>
  113. <p class="attributes-hint" x-show="selectedTypeId && currentAttributes.length === 0">
  114. This customer type has no attributes defined.
  115. </p>
  116. <div class="form-actions">
  117. <button class="button button-primary" type="submit">Save Customer</button>
  118. <a class="button button-secondary" href="/customers">Cancel</a>
  119. </div>
  120. </form>
  121. </section>
  122. <?php endif; ?>
  123. </section>

Powered by TurnKey Linux.