// src/stores/OrderProductionItemStore.js import { defineStore } from 'pinia' import api from 'src/services/api' export const useOrderProductionItemStore = defineStore('orderproductionitems', { state: () => ({ items: [], header: null, products: [], colorOptionsByCode: {}, secondColorOptionsByKey: {}, loading: false, saving: false, error: null }), actions: { async fetchHeader (orderHeaderID) { if (!orderHeaderID) { this.header = null return } this.loading = true this.error = null try { const res = await api.get(`/order/get/${encodeURIComponent(orderHeaderID)}`) this.header = res?.data?.header || null } catch (err) { this.header = null this.error = err?.response?.data || err?.message || 'Siparis bilgisi alinamadi' } finally { this.loading = false } }, async fetchItems (orderHeaderID) { if (!orderHeaderID) { this.items = [] return } this.loading = true this.error = null try { const res = await api.get(`/orders/production-items/${encodeURIComponent(orderHeaderID)}`) const data = res?.data this.items = Array.isArray(data) ? data : [] } catch (err) { this.items = [] this.error = err?.response?.data || err?.message || 'Liste alinamadi' } finally { this.loading = false } }, async fetchProducts () { this.error = null try { const res = await api.get('/products') const data = res?.data this.products = Array.isArray(data) ? data : [] } catch (err) { this.products = [] this.error = err?.response?.data || err?.message || 'Urun listesi alinamadi' } }, async fetchColors (productCode) { const code = String(productCode || '').trim() if (!code) return [] if (this.colorOptionsByCode[code]) { return this.colorOptionsByCode[code] } try { const res = await api.get('/product-colors', { params: { code } }) const data = res?.data const list = Array.isArray(data) ? data : [] this.colorOptionsByCode[code] = list return list } catch (err) { this.error = err?.response?.data || err?.message || 'Renk listesi alinamadi' return [] } }, async fetchSecondColors (productCode, colorCode) { const code = String(productCode || '').trim() const color = String(colorCode || '').trim() if (!code || !color) return [] const key = `${code}::${color}` if (this.secondColorOptionsByKey[key]) { return this.secondColorOptionsByKey[key] } try { const res = await api.get('/product-secondcolor', { params: { code, color } }) const data = res?.data const list = Array.isArray(data) ? data : [] this.secondColorOptionsByKey[key] = list return list } catch (err) { this.error = err?.response?.data || err?.message || '2. renk listesi alinamadi' return [] } }, async validateUpdates (orderHeaderID, lines) { if (!orderHeaderID) return { missingCount: 0, missing: [] } this.saving = true this.error = null try { const res = await api.post( `/orders/production-items/${encodeURIComponent(orderHeaderID)}/validate`, { lines } ) return res?.data || { missingCount: 0, missing: [] } } catch (err) { this.error = err?.response?.data || err?.message || 'Kontrol basarisiz' throw err } finally { this.saving = false } }, async applyUpdates (orderHeaderID, lines, insertMissing) { if (!orderHeaderID) return { updated: 0, inserted: 0 } this.saving = true this.error = null try { const res = await api.post( `/orders/production-items/${encodeURIComponent(orderHeaderID)}/apply`, { lines, insertMissing } ) return res?.data || { updated: 0, inserted: 0 } } catch (err) { this.error = err?.response?.data || err?.message || 'Guncelleme basarisiz' throw err } finally { this.saving = false } } } })