create.blade.php 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475
  1. @extends('layouts.main')
  2. @section('content')
  3. <!-- CONTENT HEADER -->
  4. <section class="content-header">
  5. <div class="container-fluid">
  6. <div class="row mb-2">
  7. <div class="col-sm-6">
  8. <h1>Servers</h1>
  9. </div>
  10. <div class="col-sm-6">
  11. <ol class="breadcrumb float-sm-right">
  12. <li class="breadcrumb-item"><a href="{{ route('home') }}">Dashboard</a></li>
  13. <li class="breadcrumb-item"><a href="{{ route('servers.index') }}">Servers</a>
  14. <li class="breadcrumb-item"><a class="text-muted"
  15. href="{{ route('servers.create') }}">Create</a>
  16. </li>
  17. </ol>
  18. </div>
  19. </div>
  20. </div>
  21. </section>
  22. <!-- END CONTENT HEADER -->
  23. <!-- MAIN CONTENT -->
  24. <section x-data="serverApp()" class="content">
  25. <div class="container">
  26. <!-- FORM -->
  27. <form action="{{route('servers.store')}}" method="post" class="row">
  28. @csrf
  29. <div class="col-md-8">
  30. <div class="card">
  31. <div class="card-header">
  32. <div class="card-title"><i class="fas fa-cogs mr-2"></i>{{__('Server configuration')}}</div>
  33. </div>
  34. @if($productCount === 0 || $nodeCount === 0 || count($nests) === 0 || count($eggs) === 0 )
  35. <div class="alert alert-danger p-2 m-2">
  36. <h5><i class="icon fas fa-exclamation-circle"></i>{{__('Error!')}}</h5>
  37. <p class="pl-4">
  38. @if(Auth::user()->role == 'admin')
  39. {{__('Make sure to link your products to nodes and eggs.')}} <br>
  40. {{__('There has to be at least 1 valid product for server creation')}}
  41. @endif
  42. </p>
  43. <ul>
  44. @if($productCount === 0 )
  45. <li> {{__('No products available!')}}</li>
  46. @endif
  47. @if($nodeCount === 0 )
  48. <li>{{__('No nodes have been linked!')}}</li>
  49. @endif
  50. @if(count($nests) === 0 )
  51. <li>{{__('No nests available!')}}</li>
  52. @endif
  53. @if(count($eggs) === 0 )
  54. <li>{{__('No eggs have been linked!')}}</li>
  55. @endif
  56. </ul>
  57. </div>
  58. @endif
  59. <div x-show="loading" class="overlay dark">
  60. <i class="fas fa-2x fa-sync-alt"></i>
  61. </div>
  62. <div class="card-body">
  63. @if ($errors->any())
  64. <div class="alert alert-danger">
  65. <ul class="list-group pl-3">
  66. @foreach ($errors->all() as $error)
  67. <li>{{ $error }}</li>
  68. @endforeach
  69. </ul>
  70. </div>
  71. @endif
  72. <div class="form-group">
  73. <label for="name">{{__('Name')}}</label>
  74. <input x-model="name" id="name" name="name" type="text" required="required"
  75. class="form-control @error('name') is-invalid @enderror">
  76. @error('name')
  77. <div class="invalid-feedback">
  78. {{ $message }}
  79. </div>
  80. @enderror
  81. </div>
  82. <div class="row">
  83. <div class="col-md-6">
  84. <div class="form-group">
  85. <label for="nest">{{__('Software / Games')}}</label>
  86. <select class="custom-select"
  87. required
  88. name="nest"
  89. id="nest"
  90. x-model="selectedNest"
  91. @change="setEggs();">
  92. <option selected disabled hidden
  93. value="null">{{count($nests) > 0 ? __('Please select software ...') : __('---')}}</option>
  94. @foreach ($nests as $nest)
  95. <option value="{{ $nest->id }}">{{ $nest->name }}</option>
  96. @endforeach
  97. </select>
  98. </div>
  99. </div>
  100. <div class="col-md-6">
  101. <div class="form-group">
  102. <label for="egg">{{__('Specification ')}}</label>
  103. <div>
  104. <select id="egg"
  105. required
  106. name="egg"
  107. :disabled="eggs.length == 0"
  108. x-model="selectedEgg"
  109. @change="fetchLocations();"
  110. required="required"
  111. class="custom-select">
  112. <option x-text="getEggInputText()"
  113. selected disabled hidden value="null"></option>
  114. <template x-for="egg in eggs" :key="egg.id">
  115. <option x-text="egg.name" :value="egg.id"></option>
  116. </template>
  117. </select>
  118. </div>
  119. </div>
  120. </div>
  121. </div>
  122. <div class="form-group">
  123. <label for="node">{{__('Node')}}</label>
  124. <select name="node"
  125. required
  126. id="node"
  127. x-model="selectedNode"
  128. :disabled="!fetchedLocations"
  129. @change="fetchProducts();"
  130. class="custom-select">
  131. <option
  132. x-text="getNodeInputText()"
  133. disabled selected hidden value="null">
  134. </option>
  135. <template x-for="location in locations" :key="location.id">
  136. <optgroup :label="location.name">
  137. <template x-for="node in location.nodes" :key="node.id">
  138. <option x-text="node.name"
  139. :value="node.id">
  140. </option>
  141. </template>
  142. </optgroup>
  143. </template>
  144. </select>
  145. </div>
  146. <div class="form-group">
  147. <label for="product">{{__('Resources')}}</label>
  148. <select name="product"
  149. required
  150. id="product"
  151. :disabled="!fetchedProducts"
  152. x-model="selectedProduct"
  153. @change="updateSelectedObjects()"
  154. class="custom-select">
  155. <option
  156. x-text="getProductInputText()"
  157. disabled selected hidden value="null"></option>
  158. <template x-for="product in products" :key="product.id">
  159. <option :disabled="product.minimum_credits > user.credits"
  160. x-text="getProductOptionText(product)"
  161. :value="product.id">
  162. </option>
  163. </template>
  164. </select>
  165. </div>
  166. </div>
  167. </div>
  168. </div>
  169. <div class="col-md-4 mb-4">
  170. <div class="card">
  171. <div class="card-header">
  172. <div class="card-title">
  173. <i class="fas fa-list mr-2"></i>{{__('Server details')}}
  174. </div>
  175. </div>
  176. <div class="card-body">
  177. <ul class="list-group mb-3">
  178. <li x-show="selectedNestObject.name"
  179. class="list-group-item d-flex justify-content-between lh-condensed">
  180. <div>
  181. <h6 class="my-0">{{__('Software / Games')}}</h6>
  182. <small x-text="selectedNestObject?.name ?? '{{__('No selection')}}'"
  183. class="text-muted"></small>
  184. </div>
  185. </li>
  186. <li class="list-group-item d-flex justify-content-between lh-condensed">
  187. <div>
  188. <h6 class="my-0">{{__('Specification')}}</h6>
  189. <small x-text="selectedEggObject?.name ?? '{{__('No selection')}}'"
  190. class="text-muted"></small>
  191. </div>
  192. </li>
  193. <li
  194. class="list-group-item d-flex justify-content-between lh-condensed">
  195. <div>
  196. <h6 class="my-0">{{__('Node')}}</h6>
  197. <small x-text="selectedNodeObject?.name ?? '{{__('No selection')}}'"
  198. class="text-muted"></small>
  199. </div>
  200. </li>
  201. <li class="list-group-item">
  202. <div>
  203. <h6 class="my-0">{{__('Resources')}}</h6>
  204. <small x-text="selectedProductObject?.name ?? '{{__('No selection')}}'"
  205. class="text-muted"></small>
  206. </div>
  207. <template x-if="selectedProductObject?.name">
  208. <ul class="pl-0">
  209. <li class="d-flex justify-content-between">
  210. <small class="text-muted d-inline-block">{{__('CPU')}}</small>
  211. <small class="text-muted d-inline-block"
  212. x-text="selectedProductObject.cpu + ' %'"></small>
  213. </li>
  214. <div class="d-flex justify-content-between">
  215. <small class="text-muted d-inline-block">{{__('Memory')}}</small>
  216. <small class="text-muted d-inline-block"
  217. x-text="selectedProductObject.memory + ' {{__('MB')}}'"></small>
  218. </div>
  219. <div class="d-flex justify-content-between">
  220. <small class="text-muted d-inline-block">{{__('Disk')}}</small>
  221. <small class="text-muted d-inline-block"
  222. x-text="selectedProductObject.disk + ' {{__('MB')}}'"></small>
  223. </div>
  224. <div class="d-flex justify-content-between">
  225. <small class="text-muted d-inline-block">{{__('Databases')}}</small>
  226. <small class="text-muted d-inline-block"
  227. x-text="selectedProductObject.databases + ' {{__('MySQL')}}'"></small>
  228. </div>
  229. <div class="d-flex justify-content-between">
  230. <small class="text-muted d-inline-block">{{__('Backups')}}</small>
  231. <small class="text-muted d-inline-block"
  232. x-text="selectedProductObject.backups"></small>
  233. </div>
  234. <div class="d-flex justify-content-between">
  235. <small class="text-muted d-inline-block">{{__('Allocations')}} ({{__('ports')}})</small>
  236. <small class="text-muted d-inline-block" x-text="selectedProductObject.allocations"></small>
  237. </div>
  238. </ul>
  239. </template>
  240. </li>
  241. </ul>
  242. <ul class="list-group mb-3">
  243. <li class="list-group-item d-flex justify-content-between">
  244. <span>{{CREDITS_DISPLAY_NAME}} {{__('per month')}}</span>
  245. <strong>
  246. <i x-show="selectedProductObject?.price" class="fas fa-coins"></i>
  247. <span x-text="selectedProductObject?.price ?? ''"></span>
  248. </strong>
  249. </li>
  250. </ul>
  251. <button :disabled="!isFormValid()" :class="isFormValid() ? '' : 'disabled'"
  252. class="btn btn-primary btn-block">
  253. {{__('Create server')}}
  254. </button>
  255. </div>
  256. </div>
  257. </div>
  258. </form>
  259. <!-- END FORM -->
  260. </div>
  261. </section>
  262. <!-- END CONTENT -->
  263. <script>
  264. function serverApp() {
  265. return {
  266. //loading
  267. loading: false,
  268. fetchedLocations: false,
  269. fetchedProducts: false,
  270. //input fields
  271. name: null,
  272. selectedNest: null,
  273. selectedEgg: null,
  274. selectedNode: null,
  275. selectedProduct: null,
  276. //selected objects based on input
  277. selectedNestObject: {},
  278. selectedEggObject: {},
  279. selectedNodeObject: {},
  280. selectedProductObject: {},
  281. //values
  282. user: {!! $user !!},
  283. nests: {!! $nests !!},
  284. eggsSave:{!! $eggs !!}, //store back-end eggs
  285. eggs: [],
  286. locations: [],
  287. products: [],
  288. /**
  289. * @description set available eggs based on the selected nest
  290. * @note called whenever a nest is selected
  291. * @see selectedNest
  292. */
  293. async setEggs() {
  294. this.fetchedLocations = false;
  295. this.fetchedProducts = false;
  296. this.locations = [];
  297. this.products = [];
  298. this.selectedEgg = 'null';
  299. this.selectedNode = 'null';
  300. this.selectedProduct = 'null';
  301. this.eggs = this.eggsSave.filter(egg => egg.nest_id == this.selectedNest)
  302. //automatically select the first entry if there is only 1
  303. if (this.eggs.length === 1) {
  304. this.selectedEgg = this.eggs[0].id;
  305. await this.fetchLocations();
  306. return;
  307. }
  308. this.updateSelectedObjects()
  309. },
  310. /**
  311. * @description fetch all available locations based on the selected egg
  312. * @note called whenever a server configuration is selected
  313. * @see selectedEg
  314. */
  315. async fetchLocations() {
  316. this.loading = true;
  317. this.fetchedLocations = false;
  318. this.fetchedProducts = false;
  319. this.locations = [];
  320. this.products = [];
  321. this.selectedNode = 'null';
  322. this.selectedProduct = 'null';
  323. let response = await axios.get(`{{route('products.locations.egg')}}/${this.selectedEgg}`)
  324. .catch(console.error)
  325. this.fetchedLocations = true;
  326. this.locations = response.data
  327. //automatically select the first entry if there is only 1
  328. if (this.locations.length === 1 && this.locations[0]?.nodes?.length === 1) {
  329. this.selectedNode = this.locations[0]?.nodes[0]?.id;
  330. await this.fetchProducts();
  331. return;
  332. }
  333. this.loading = false;
  334. this.updateSelectedObjects()
  335. },
  336. /**
  337. * @description fetch all available products based on the selected node
  338. * @note called whenever a node is selected
  339. * @see selectedNode
  340. */
  341. async fetchProducts() {
  342. this.loading = true;
  343. this.fetchedProducts = false;
  344. this.products = [];
  345. this.selectedProduct = 'null';
  346. let response = await axios.get(`{{route('products.products.node')}}/${this.selectedEgg}/${this.selectedNode}`)
  347. .catch(console.error)
  348. this.fetchedProducts = true;
  349. this.products = response.data
  350. //automatically select the first entry if there is only 1
  351. if (this.products.length === 1) {
  352. this.selectedProduct = this.products[0].id;
  353. }
  354. this.loading = false;
  355. this.updateSelectedObjects()
  356. },
  357. /**
  358. * @description map selected id's to selected objects
  359. * @note being used in the server info box
  360. */
  361. updateSelectedObjects() {
  362. this.selectedNestObject = this.nests.find(nest => nest.id == this.selectedNest) ?? {}
  363. this.selectedEggObject = this.eggs.find(egg => egg.id == this.selectedEgg) ?? {}
  364. this.selectedNodeObject = {};
  365. this.locations.forEach(location => {
  366. if (!this.selectedNodeObject?.id) {
  367. this.selectedNodeObject = location.nodes.find(node => node.id == this.selectedNode) ?? {};
  368. }
  369. })
  370. this.selectedProductObject = this.products.find(product => product.id == this.selectedProduct) ?? {}
  371. },
  372. /**
  373. * @description check if all options are selected
  374. * @return {boolean}
  375. */
  376. isFormValid() {
  377. if (Object.keys(this.selectedNestObject).length === 0) return false;
  378. if (Object.keys(this.selectedEggObject).length === 0) return false;
  379. if (Object.keys(this.selectedNodeObject).length === 0) return false;
  380. if (Object.keys(this.selectedProductObject).length === 0) return false;
  381. return !!this.name;
  382. },
  383. getNodeInputText() {
  384. if (this.fetchedLocations) {
  385. if (this.locations.length > 0) {
  386. return '{{__('Please select a node ...')}}';
  387. }
  388. return '{{__('No nodes found matching current configuration')}}'
  389. }
  390. return '{{__('---')}}';
  391. },
  392. getProductInputText() {
  393. if (this.fetchedProducts) {
  394. if (this.products.length > 0) {
  395. return '{{__('Please select a resource ...')}}';
  396. }
  397. return '{{__('No resources found matching current configuration')}}'
  398. }
  399. return '{{__('---')}}';
  400. },
  401. getEggInputText() {
  402. if (this.selectedNest) {
  403. return '{{__('Please select a configuration ...')}}';
  404. }
  405. return '{{__('---')}}';
  406. },
  407. getProductOptionText(product) {
  408. let text = product.name + ' (' + product.description + ')';
  409. if (product.minimum_credits > this.user.credits) {
  410. return '{{__('Not enough credits!')}} | ' + text;
  411. }
  412. return text;
  413. }
  414. }
  415. }
  416. </script>
  417. @endsection