product-attributes.html 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488
  1. {% load static %}
  2. <!DOCTYPE html>
  3. <html lang="en"> <!--begin::Head-->
  4. <head>
  5. <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
  6. <title>Upload</title><!--begin::Primary Meta Tags-->
  7. <meta name="viewport" content="width=device-width, initial-scale=1.0">
  8. <meta name="title" content="CQT | Upload">
  9. <link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/@fontsource/source-sans-3@5.0.12/index.css"
  10. integrity="sha256-tXJfXfp6Ewt1ilPzLDtQnJV4hclT9XuaZUKyUvmyr+Q=" crossorigin="anonymous">
  11. <!--end::Fonts--><!--begin::Third Party Plugin(OverlayScrollbars)-->
  12. <link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/overlayscrollbars@2.3.0/styles/overlayscrollbars.min.css"
  13. integrity="sha256-dSokZseQNT08wYEWiz5iLI8QPlKxG+TswNRD8k35cpg=" crossorigin="anonymous">
  14. <!--end::Third Party Plugin(OverlayScrollbars)--><!--begin::Third Party Plugin(Bootstrap Icons)-->
  15. <link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/bootstrap-icons@1.11.0/font/bootstrap-icons.min.css"
  16. integrity="sha256-Qsx5lrStHZyR9REqhUF8iQt73X06c8LGIUPzpOhwRrI=" crossorigin="anonymous">
  17. <!--end::Third Party Plugin(Bootstrap Icons)--><!--begin::Required Plugin(AdminLTE)-->
  18. <link rel="stylesheet" href="{% static './css/adminlte.css' %}"><!--end::Required Plugin(AdminLTE)--><!-- apexcharts -->
  19. <link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/apexcharts@3.37.1/dist/apexcharts.css"
  20. integrity="sha256-4MX+61mt9NVvvuPjUWdUdyfZfxSB1/Rf9WtqRHgG5S0=" crossorigin="anonymous">
  21. <link href="https://cdn.jsdelivr.net/npm/select2@4.1.0-rc.0/dist/css/select2.min.css" rel="stylesheet" />
  22. <link rel="stylesheet" href="{% static './css/select2-bootstrap4.min.css' %}">
  23. <link rel="stylesheet" href="{% static './css/custom.css' %}">
  24. <link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/datatables@1.10.18/media/css/jquery.dataTables.min.css">
  25. <style>
  26. .select2-container .select2-search--inline .select2-search__field {
  27. position: absolute;
  28. top: 3px;
  29. font-size: 14px;
  30. }
  31. #full-page-loader {
  32. position: fixed;
  33. top: 0;
  34. left: 0;
  35. width: 100%;
  36. height: 100%;
  37. background-color: rgba(0, 0, 0, 0.6); /* semi-transparent black */
  38. z-index: 9999;
  39. display: flex;
  40. justify-content: center;
  41. align-items: center;
  42. }
  43. .loader-overlay .spinner-border {
  44. width: 3rem;
  45. height: 3rem;
  46. }
  47. </style>
  48. </head>
  49. <body class="layout-fixed sidebar-expand-lg sidebar-mini app-loaded sidebar-collapse">
  50. <!--begin::App Wrapper-->
  51. <div class="app-wrapper"> <!--begin::Header-->
  52. {% include 'header.html' %}
  53. {% include 'sidebar.html' %}
  54. <main class="app-main"> <!--begin::App Content Header-->
  55. <div class="app-content-header"> <!--begin::Container-->
  56. <div class="container-fluid"> <!--begin::Row-->
  57. <div class="row">
  58. <div class="col-sm-6">
  59. <h3 class="mb-0">🏷️ Product Attributes
  60. &nbsp;<span><a href="./../media/examples/product_attribute_template.xlsx" title="Example file" download><i class="bi bi-download"></i></a></span>
  61. </h3>
  62. </div>
  63. <div class="col-sm-6">
  64. <ol class="breadcrumb float-sm-end">
  65. <li class="breadcrumb-item"><a href="{% url 'file-upload' %}">Home</a></li>
  66. <li class="breadcrumb-item active" aria-current="page"><a href="{% url 'product-attributes' %}"></a>
  67. 🏷️ Product Attributes</a>
  68. </li>
  69. </ol>
  70. </div>
  71. </div> <!--end::Row-->
  72. </div> <!--end::Container-->
  73. </div>
  74. <div class="app-content"> <!--begin::Container-->
  75. <div class="container-fluid"> <!-- Info boxes -->
  76. <div id="full-page-loader" style="display: none;">
  77. <div class="loader-overlay">
  78. <div class="spinner-border text-light" role="status">
  79. <!-- <span class="sr-only">Loading...</span> -->
  80. </div>
  81. </div>
  82. </div>
  83. <div class="row">
  84. <!-- upload attributes -->
  85. <div class="col-md-12">
  86. <div class="card mb-4">
  87. <div class="card-header">
  88. <h5 class="card-title">Upload Attributes</h5>
  89. </div>
  90. <div class="card-body p-0">
  91. <form id="uploadForm" method="POST" enctype="multipart/form-data"> <!--begin::Body-->
  92. {% csrf_token %}
  93. <div class="card-body">
  94. <div class="form-group row mb-3">
  95. <label for="file" class="col-sm-2 col-form-label">Select Input
  96. File <span class="text-danger">*</span></label>
  97. <div class="col-sm-6">
  98. <div class="input-group mb-3">
  99. <input type="file" class="form-control"
  100. id="fileInput" name="file" required
  101. >
  102. </div>
  103. </div>
  104. <div class="form-group row mb-3 ">
  105. <label for="file" class="col-sm-2 col-form-label"></label>
  106. <div class="col-sm-6">
  107. <button type="submit" class="btn btn-primary" id="submitBtn" disabled>Upload</button>
  108. </div>
  109. </div>
  110. </div>
  111. </form>
  112. <div id="responseMessage" class="mt-3"></div>
  113. </div>
  114. </div> <!-- /.card -->
  115. </div>
  116. <!-- upload attributes end -->
  117. <div class="col-md-12">
  118. <div class="card mb-4">
  119. <div class="card-header">
  120. <div class="d-flex justify-content-between align-items-center mb-3">
  121. <h3>Manage Attributes</h3>
  122. <!-- <button class="btn btn-primary" data-bs-toggle="modal" data-bs-target="#addProductTypeModal">Add New Product Type</button>
  123. <button class="btn btn-info" data-bs-toggle="modal" data-bs-target="#addProductAttributeModal">Add New Attribute</button> -->
  124. </div>
  125. </div>
  126. <div class="card-body">
  127. <table class="table table-bordered table-striped" cellspacing="0" width="100%" id="product-attribute-list">
  128. <thead class="table-dark" >
  129. <tr>
  130. <th>ID</th>
  131. <th>Product Type</th>
  132. <th>Product Attribute List</th>
  133. <!-- <th>Actions</th> -->
  134. </tr>
  135. </thead>
  136. <tbody id="dataBody">
  137. <!-- <tr>
  138. <td>1</td>
  139. <td>Canvas Art Prints</td>
  140. <td><ul>
  141. <li>color</li>
  142. <li>Shade</li>
  143. </ul>
  144. </td>
  145. <td>
  146. <button class="btn btn-sm btn-warning" data-bs-toggle="modal" data-bs-target="#editModal">Edit</button>
  147. </td>
  148. </tr> -->
  149. </tbody>
  150. </table>
  151. </div>
  152. </div>
  153. </div>
  154. </div>
  155. <div class="modal fade" id="addProductTypeModal" tabindex="-1" aria-labelledby="addProductTypeModal" aria-hidden="true">
  156. <div class="modal-dialog">
  157. <form class="modal-content">
  158. <div class="modal-header">
  159. <h5 class="modal-title" id="addTypeModalLabel">Add Product Type</h5>
  160. <button type="button" class="btn-close" data-bs-dismiss="modal" aria-label="Close"></button>
  161. </div>
  162. <div class="modal-body">
  163. <div class="mb-3">
  164. <label for="addProductTypeName" class="form-label">Name</label>
  165. <input type="text" class="form-control" id="addName" required>
  166. </div>
  167. </div>
  168. <div class="modal-footer">
  169. <button type="submit" class="btn btn-success">Save</button>
  170. </div>
  171. </form>
  172. </div>
  173. </div>
  174. <div class="modal fade" id="addProductAttributeModal" tabindex="-1" aria-labelledby="addProductAttributeModal" aria-hidden="true">
  175. <div class="modal-dialog">
  176. <form class="modal-content">
  177. <div class="modal-header">
  178. <h5 class="modal-title" id="addAttributeModalLabel">Add Product Attribute</h5>
  179. <button type="button" class="btn-close" data-bs-dismiss="modal" aria-label="Close"></button>
  180. </div>
  181. <div class="modal-body">
  182. <div class="mb-3">
  183. <label for="addProductType" class="form-label">Product Type</label>
  184. <select name="productType" class="form-control" id="productType" aria-describedby="Select Product Type">
  185. <option value="">Select Product Type</option>
  186. <option value="color">Color</option>
  187. <option value="shade">Shade</option>
  188. </select>
  189. </div>
  190. <div class="mb-3">
  191. <label for="addAttributeName" class="form-label">Name</label>
  192. <input type="text" class="form-control" id="addAttributeName" required>
  193. </div>
  194. </div>
  195. <div class="modal-footer">
  196. <button type="submit" class="btn btn-success">Save</button>
  197. </div>
  198. </form>
  199. </div>
  200. </div>
  201. <div class="modal fade" id="editModal" tabindex="-1" aria-labelledby="editModalLabel" aria-hidden="true">
  202. <div class="modal-dialog">
  203. <form class="modal-content">
  204. <div class="modal-header">
  205. <h5 class="modal-title" id="editModalLabel">Edit Product Attribute</h5>
  206. <button type="button" class="btn-close" data-bs-dismiss="modal" aria-label="Close"></button>
  207. </div>
  208. <div class="modal-body">
  209. <input type="hidden" id="editId">
  210. <div class="mb-3">
  211. <label for="editProductType" class="form-label">Product Type</label>
  212. <select name="editProductType[]" class="form-control" id="editProductType" aria-describedby="Select Product Type" multiple>
  213. <option value="">Select Product Type</option>
  214. <option value="color">Color</option>
  215. <option value="shade">Shade</option>
  216. </select>
  217. </div>
  218. <div class="mb-3">
  219. <label for="editAttributeName" class="form-label">Name</label>
  220. <input type="text" class="form-control" id="editAttributeName" required>
  221. </div>
  222. </div>
  223. <div class="modal-footer">
  224. <button type="submit" class="btn btn-primary">Update</button>
  225. </div>
  226. </form>
  227. </div>
  228. </div>
  229. </div> <!--end::Container-->
  230. </div> <!--end::App Content-->
  231. </main> <!--end::App Main--> <!--begin::Footer-->
  232. {% include 'footer.html' %}
  233. </div>
  234. <script src="https://code.jquery.com/jquery-3.7.1.min.js"
  235. integrity="sha256-/JqT3SQfawRcv/BIHPThkBvs0OEvtFFmqPF/lYI/Cxo=" crossorigin="anonymous"></script>
  236. <script src="https://cdn.jsdelivr.net/npm/overlayscrollbars@2.3.0/browser/overlayscrollbars.browser.es6.min.js"
  237. integrity="sha256-H2VM7BKda+v2Z4+DRy69uknwxjyDRhszjXFhsL4gD3w=" crossorigin="anonymous"></script>
  238. <!--end::Third Party Plugin(OverlayScrollbars)--><!--begin::Required Plugin(popperjs for Bootstrap 5)-->
  239. <script src="https://cdn.jsdelivr.net/npm/@popperjs/core@2.11.8/dist/umd/popper.min.js"
  240. integrity="sha256-whL0tQWoY1Ku1iskqPFvmZ+CHsvmRWx/PIoEvIeWh4I=" crossorigin="anonymous"></script>
  241. <!--end::Required Plugin(popperjs for Bootstrap 5)--><!--begin::Required Plugin(Bootstrap 5)-->
  242. <script src="https://cdn.jsdelivr.net/npm/bootstrap@5.3.2/dist/js/bootstrap.min.js"
  243. integrity="sha256-YMa+wAM6QkVyz999odX7lPRxkoYAan8suedu4k2Zur8=" crossorigin="anonymous"></script>
  244. <script src="https://cdn.jsdelivr.net/npm/select2@4.1.0-rc.0/dist/js/select2.min.js"></script>
  245. <!--end::Required Plugin(Bootstrap 5)--><!--begin::Required Plugin(AdminLTE)-->
  246. <script src="{% static './js/adminlte.js' %}"></script>
  247. <script src="https://cdn.jsdelivr.net/npm/datatables@1.10.18/media/js/jquery.dataTables.min.js"></script>
  248. <!--end::Required Plugin(AdminLTE)--><!--begin::OverlayScrollbars Configure-->
  249. <script>
  250. const SELECTOR_SIDEBAR_WRAPPER = ".sidebar-wrapper";
  251. const Default = {
  252. scrollbarTheme: "os-theme-light",
  253. scrollbarAutoHide: "leave",
  254. scrollbarClickScroll: true,
  255. };
  256. document.addEventListener("DOMContentLoaded", function () {
  257. const sidebarWrapper = document.querySelector(SELECTOR_SIDEBAR_WRAPPER);
  258. if (
  259. sidebarWrapper &&
  260. typeof OverlayScrollbarsGlobal?.OverlayScrollbars !== "undefined"
  261. ) {
  262. OverlayScrollbarsGlobal.OverlayScrollbars(sidebarWrapper, {
  263. scrollbars: {
  264. theme: Default.scrollbarTheme,
  265. autoHide: Default.scrollbarAutoHide,
  266. clickScroll: Default.scrollbarClickScroll,
  267. },
  268. });
  269. }
  270. });
  271. $(document).ready(function () {
  272. // $('#product-attribute-list').DataTable();
  273. });
  274. </script> <!--end::OverlayScrollbars Configure--> <!-- OPTIONAL SCRIPTS --> <!-- apexcharts -->
  275. </script>
  276. </body><!--end::Body-->
  277. <script>
  278. $(document).ready(function () {
  279. $('#product-attribute-list').on('click', '.btn-warning', function () {
  280. var $row = $(this).closest('tr');
  281. // Extract the Product Attribute List (list items inside <ul>)
  282. var selectedAttributes = [];
  283. $row.find('td:nth-child(3) ul li').each(function () {
  284. selectedAttributes.push($(this).text().trim().toLowerCase());
  285. });
  286. // Clear previous selection
  287. $('#editProductType option').prop('selected', false);
  288. // Select the ones that match
  289. $('#editProductType option').each(function () {
  290. if (selectedAttributes.includes($(this).val().toLowerCase())) {
  291. $(this).prop('selected', true);
  292. }
  293. });
  294. // Also update attribute name if needed
  295. var attributeName = selectedAttributes.length ? selectedAttributes[0] : '';
  296. $('#editAttributeName').val(attributeName);
  297. });
  298. });
  299. </script>
  300. <script>
  301. $(document).ready(function () {
  302. $('#editProductType').select2({
  303. dropdownParent: $('#editModal'), // important for modal
  304. placeholder: 'Select product attributes'
  305. });
  306. });
  307. var API_DATA = []
  308. let product_types = [];
  309. document.addEventListener('DOMContentLoaded', function () {
  310. const form = document.getElementById('uploadForm');
  311. const fileInput = document.getElementById('fileInput');
  312. const submitBtn = document.getElementById('submitBtn');
  313. const responseDiv = document.getElementById('responseMessage');
  314. // Enable submit button when file is selected
  315. fileInput.addEventListener('change', function () {
  316. submitBtn.disabled = fileInput.files.length === 0;
  317. });
  318. // Handle form submission
  319. form.addEventListener('submit', function (e) {
  320. e.preventDefault();
  321. $('#full-page-loader').show();
  322. // Disable button during upload
  323. submitBtn.disabled = true;
  324. submitBtn.textContent = 'Uploading...';
  325. const formData = new FormData(form);
  326. fetch('/attr/products/upload-attributes/', {
  327. method: 'POST',
  328. body: formData,
  329. headers: {
  330. 'X-CSRFToken': document.querySelector('[name=csrfmiddlewaretoken]').value
  331. }
  332. })
  333. .then(response => response.json())
  334. .then(data => {
  335. // if (data.success) {
  336. responseDiv.innerHTML = `<div class="alert alert-success">✅ ${data.message}</div>`;
  337. fileInput.value = ''; // Clear file input
  338. submitBtn.disabled = true; // Keep disabled until new file selected
  339. $('#full-page-loader').hide();
  340. setTimeout(()=>{
  341. window.location.reload();
  342. },3000)
  343. // } else {
  344. // responseDiv.innerHTML = `<div class="alert alert-danger">❌ ${data.error}</div>`;
  345. // submitBtn.disabled = false;
  346. // $('#full-page-loader').hide();
  347. // }
  348. })
  349. .catch(error => {
  350. responseDiv.innerHTML = `<div class="alert alert-danger">❌ Upload failed: ${error}</div>`;
  351. submitBtn.disabled = false;
  352. $('#full-page-loader').hide();
  353. })
  354. .finally(() => {
  355. submitBtn.textContent = 'Upload';
  356. $('#full-page-loader').hide();
  357. // Remove message after 5 seconds
  358. setTimeout(() => {
  359. responseDiv.innerHTML = '';
  360. }, 3000);
  361. });
  362. });
  363. });
  364. document.addEventListener('DOMContentLoaded', () => {
  365. jQuery('#full-page-loader').show();
  366. try{
  367. fetch('/attr/products/attributes', {
  368. method: 'GET', // or 'POST' if your API expects POST
  369. headers: {
  370. 'X-CSRFToken': document.querySelector('[name=csrfmiddlewaretoken]')?.value || ''
  371. }
  372. })
  373. .then(response => response.json())
  374. .then(data => {
  375. console.log("data",data);
  376. API_DATA = data;
  377. renderAttributeTable(API_DATA);
  378. // 2. ONLY THEN, initialize DataTables
  379. if (jQuery.fn.DataTable) { // Optional safety check
  380. jQuery('#product-attribute-list').DataTable({
  381. // Ensure the table structure is maintained if you re-render
  382. destroy: true
  383. });
  384. } else {
  385. console.error("DataTables plugin not loaded.");
  386. }
  387. jQuery('#full-page-loader').hide();
  388. });
  389. }catch(err){
  390. console.log("err",err);
  391. jQuery('#full-page-loader').hide();
  392. }
  393. });
  394. // Function to generate the data and populate the table
  395. function renderAttributeTable(apiData) {
  396. const $dataBody = jQuery('#dataBody');
  397. $dataBody.empty(); // Clear existing rows
  398. if (!apiData || apiData.length === 0) {
  399. $dataBody.append('<tr><td colspan="3">No product attributes found.</td></tr>');
  400. jQuery('#full-page-loader').hide();
  401. return;
  402. }
  403. // 1. Iterate directly over the flat API data
  404. apiData.forEach((item, index) => {
  405. // Prepare the content for the third column (Attribute List)
  406. const attributeName = item.attribute_name;
  407. const isMandatory = item.is_mandatory;
  408. const possibleValues = item.possible_values;
  409. const productType = item.product_type;
  410. // --- Column 3: Product Attribute List (UL/LI) ---
  411. const $attributeList = jQuery('<ul></ul>');
  412. let className = isMandatory === 'Yes' ? 'mandatory-attr' : 'optional-attr';
  413. let status = isMandatory === 'Yes' ? ' (Mandatory)' : ' (Optional)';
  414. // Create a single <li> for this attribute
  415. const liContent = `<strong>${attributeName}</strong>${status}: ${possibleValues}`;
  416. const $listItem = jQuery(`<li class="${className}"></li>`).html(liContent);
  417. $attributeList.append($listItem);
  418. // --- Create the Table Row ---
  419. const $newRow = jQuery('<tr></tr>');
  420. // Column 1: ID (Using index + 1 for simple row numbering)
  421. $newRow.append(`<td>${index + 1}</td>`);
  422. // Column 2: Product Type
  423. $newRow.append(`<td>${productType}</td>`);
  424. // Column 3: Product Attribute List
  425. // Note: Each row now contains only one attribute name in the list
  426. const $listCell = jQuery('<td></td>').append($attributeList);
  427. $newRow.append($listCell);
  428. // Column 4: Actions (Edit Button)
  429. const $actionsCell = jQuery('<td></td>');
  430. // const $editButton = jQuery('<button class="btn btn-sm btn-warning" data-bs-toggle="modal" data-bs-target="#editModal">Edit</button>');
  431. // Attach data specific to this attribute for modal editing
  432. // $editButton.data('attributeData', item);
  433. // $actionsCell.append($editButton);
  434. // $newRow.append($actionsCell);
  435. // Append the row to the table body
  436. $dataBody.append($newRow);
  437. });
  438. }
  439. </script>
  440. </html>