<?php $__env->startSection('title', 'Edit Purchase'); ?>

<?php $__env->startSection('content'); ?>
<div class="container-fluid" id="purchaseFormApp">

    <div class="page-title-head d-flex align-items-center">
        <div class="flex-grow-1">
            <h4 class="page-main-title m-0">
                Edit Purchase
                <span class="badge bg-soft-secondary text-secondary ms-2"><?php echo e($purchase->invoice_number); ?></span>
            </h4>
        </div>
        <div class="text-end">
            <ol class="breadcrumb m-0 py-0">
                <li class="breadcrumb-item"><a href="<?php echo e(url('/')); ?>">Dashboard</a></li>
                <li class="breadcrumb-item"><a href="<?php echo e(route('purchases.index')); ?>">Purchases</a></li>
                <li class="breadcrumb-item active">Edit</li>
            </ol>
        </div>
    </div>

    <?php if($purchase->isPosted()): ?>
        <div class="alert alert-info d-flex align-items-start gap-2">
            <i class="ti ti-info-circle fs-lg mt-1"></i>
            <div>
                <strong>This purchase is already posted.</strong> Saving will adjust the stock ledger
                accordingly &mdash; the current items are reversed and the new items are posted in their place.
                This is only possible because no stock from this purchase has been sold yet.
            </div>
        </div>
    <?php endif; ?>

    <form id="purchaseForm" novalidate @submit.prevent="submit(false)" :class="{ 'was-validated': wasValidated }">
        <div class="row g-3">
            <div class="col-xl-10">

                <div class="card">
                    <div class="card-body">
                        <div class="row g-3">
                            <div class="col-md-3">
                                <label class="form-label">Supplier</label>
                                <input type="text" class="form-control bg-light" readonly
                                       value="<?php echo e($purchase->supplier ? ($purchase->supplier->company_name ?: $purchase->supplier->name) : ''); ?>">
                                <small class="text-muted">Cannot be changed.</small>
                            </div>
                            <div class="col-md-2">
                                <label class="form-label">Purchase Date</label>
                                <input type="date" class="form-control" v-model="form.purchase_date">
                            </div>
                            <div class="col-md-2">
                                <label class="form-label">Invoice #</label>
                                <input type="text" class="form-control bg-light" :value="form.invoice_number_preview" readonly>
                            </div>
                            <div class="col-md-2">
                                <label class="form-label">Tax Type</label>
                                <select class="form-select" v-model="form.tax_type">
                                    <option value="none">No Tax</option>
                                    <option value="cgst_sgst">CGST + SGST</option>
                                    <option value="igst">IGST</option>
                                </select>
                            </div>

                            <div class="col-md-3">
                                <label class="form-label">Location <span class="text-danger">*</span></label>
                                <select class="form-select" v-model.number="form.location_id"
                                        :class="{ 'is-invalid': errors.location_id }"
                                        required>
                                    <option :value="null">&mdash; Select location &mdash;</option>
                                    <option v-for="loc in locations" :key="loc.id" :value="loc.id">
                                        {{ loc.name }} &middot; {{ loc.location_code }}
                                    </option>
                                </select>
                                <div class="invalid-feedback">{{ errors.location_id }}</div>
                            </div>
                        </div>
                    </div>
                </div>

                <div class="card">
                    <div class="card-header border-light d-flex align-items-center gap-2">
                        <i class="ti ti-plus fs-18 text-primary"></i>
                        <h5 class="card-title mb-0">Add Item</h5>
                    </div>
                    <div class="card-body">
                        <div class="row g-2">

                            <div class="col-md-2">
                                <label class="form-label">Stone <span class="text-danger">*</span></label>
                                <select class="form-select" v-model.number="addForm.category_id"
                                        :class="{ 'is-invalid': addErrors.category_id }"
                                        :disabled="!form.supplier_id">
                                    <option :value="null">— Select Stone —</option>
                                    <option v-for="c in categoryOptions" :key="c.id" :value="c.id">{{ c.name }}</option>
                                </select>
                                <div class="invalid-feedback">{{ addErrors.category_id }}</div>
                                <small class="text-muted" v-if="supplierCategories.length">Filtered to this supplier.</small>
                            </div>

                            <div class="col-md-2" hidden>
                                <label class="form-label">Title</label>
                                <input type="text" class="form-control" v-model="addForm.title" maxlength="200"
                                       placeholder="e.g. Paraiba Tourmaline, loose (optional)">
                            </div>

                            <div class="col-md-1">
                                <label class="form-label">Type <span class="text-danger">*</span></label>
                                <select class="form-select" v-model="addForm.type">
                                    <option value="box">Box</option>
                                    <option value="piece">Piece</option>
                                </select>
                            </div>

                            <div class="col-md-1">
                                <label class="form-label">{{ addForm.type === 'piece' ? 'Qty' : 'Pcs' }} <span class="text-danger">*</span></label>
                                <input type="number" min="1" class="form-control" v-model.number="addForm.package_qty"
                                       :class="{ 'is-invalid': addErrors.package_qty }">
                                <div class="invalid-feedback">{{ addErrors.package_qty }}</div>
                            </div>

                            <div class="col-md-1">
                                <label class="form-label">Carat Weight <span class="text-danger" v-if="addFormIsGemstone">*</span></label>
                                <div class="input-group">
                                    <input type="number" step="0.001" min="0.001" class="form-control"
                                           v-model.number="addForm.carat_weight"
                                           :class="{ 'is-invalid': addErrors.carat_weight }">
                                    <span class="input-group-text">ct</span>
                                    <div class="invalid-feedback">{{ addErrors.carat_weight }}</div>
                                </div>
                            </div>

                            <div class="col-md-2">
                                <label class="form-label">Barcode</label>
                                <input type="text" class="form-control" v-model="addForm.barcode"
                                       placeholder="scan/type, optional">
                            </div>

                            <div class="col-md-1">
                                <label class="form-label">Price</label>
                                <input type="number" step="0.01" min="0" class="form-control"
                                       v-model.number="addForm.price" placeholder="optional">
                            </div>

                            <div class="col-md-2">
                                <label class="form-label">Country of Origin</label>
                                <select class="form-select" v-model.number="addForm.country_of_origin_id">
                                    <option :value="null">— Select —</option>
                                    <option v-for="o in countriesOfOrigin" :key="o.id" :value="o.id">{{ o.name }}</option>
                                </select>
                            </div>

                            <div class="col-md-2">
                                <label class="form-label">Selling Price</label>
                                <input type="number" step="0.01" min="0" class="form-control"
                                       v-model.number="addForm.website_price" placeholder="optional">
                                <small class="text-muted">Set later if unsure.</small>
                            </div>

                            <div class="col-md-2">
                                <label class="form-label d-block">Website</label>
                                <div class="form-check form-switch mt-1">
                                    <input class="form-check-input" type="checkbox" role="switch"
                                           id="addFormWebsiteEnabled" v-model="addForm.website_enabled">
                                    <label class="form-check-label" for="addFormWebsiteEnabled">
                                        {{ addForm.website_enabled ? 'Enabled' : 'Disabled' }}
                                    </label>
                                </div>
                                <small class="text-muted">Lists this item for sale online.</small>
                            </div>

                            <template v-if="addFormIsGemstone">
                                <div class="col-12"><hr class="my-1"></div>
                                <div class="col-md-2">
                                    <label class="form-label">Stone Type <span class="text-danger">*</span></label>
                                    <select class="form-select" v-model="addForm.stone_type"
                                            :class="{ 'is-invalid': addErrors.stone_type }">
                                        <option :value="null">— Select —</option>
                                        <?php $__currentLoopData = \App\Models\Product::STONE_TYPES; $__env->addLoop($__currentLoopData); foreach($__currentLoopData as $type): $__env->incrementLoopIndices(); $loop = $__env->getLastLoop(); ?>
                                            <option value="<?php echo e($type); ?>"><?php echo e($type); ?></option>
                                        <?php endforeach; $__env->popLoop(); $loop = $__env->getLastLoop(); ?>
                                    </select>
                                    <div class="invalid-feedback">{{ addErrors.stone_type }}</div>
                                </div>
                                <div class="col-md-2">
                                    <label class="form-label">Treatment <span class="text-danger">*</span></label>
                                    <select class="form-select" v-model="addForm.treatment"
                                            :class="{ 'is-invalid': addErrors.treatment }">
                                        <option :value="null">— Select —</option>
                                        <?php $__currentLoopData = \App\Models\Product::TREATMENTS; $__env->addLoop($__currentLoopData); foreach($__currentLoopData as $treatment): $__env->incrementLoopIndices(); $loop = $__env->getLastLoop(); ?>
                                            <option value="<?php echo e($treatment); ?>"><?php echo e($treatment); ?></option>
                                        <?php endforeach; $__env->popLoop(); $loop = $__env->getLastLoop(); ?>
                                    </select>
                                    <div class="invalid-feedback">{{ addErrors.treatment }}</div>
                                </div>
                                <div class="col-md-2">
                                    <label class="form-label">Cut / Shape</label>
                                    <select class="form-select" v-model="addForm.cut_shape">
                                        <option :value="null">— Select —</option>
                                        <?php $__currentLoopData = \App\Models\Product::CUT_SHAPES; $__env->addLoop($__currentLoopData); foreach($__currentLoopData as $shape): $__env->incrementLoopIndices(); $loop = $__env->getLastLoop(); ?>
                                            <option value="<?php echo e($shape); ?>"><?php echo e($shape); ?></option>
                                        <?php endforeach; $__env->popLoop(); $loop = $__env->getLastLoop(); ?>
                                    </select>
                                </div>
                                <div class="col-md-2">
                                    <label class="form-label">Clarity Grade</label>
                                    <select class="form-select" v-model="addForm.clarity_grade">
                                        <option :value="null">— Select —</option>
                                        <?php $__currentLoopData = \App\Models\Product::CLARITY_GRADES; $__env->addLoop($__currentLoopData); foreach($__currentLoopData as $clarity): $__env->incrementLoopIndices(); $loop = $__env->getLastLoop(); ?>
                                            <option value="<?php echo e($clarity); ?>"><?php echo e($clarity); ?></option>
                                        <?php endforeach; $__env->popLoop(); $loop = $__env->getLastLoop(); ?>
                                    </select>
                                </div>
                                <div class="col-md-2">
                                    <label class="form-label">Colour Grade</label>
                                    <input type="text" class="form-control" v-model="addForm.colour_grade"
                                           maxlength="100" placeholder="e.g. Vivid Blue">
                                </div>
                                <div class="col-md-12">
                                    <label class="form-label">Stone Description</label>
                                    <textarea class="form-control" v-model="addForm.stone_description" rows="2"
                                              placeholder="Additional grading notes, inclusions, brilliance, etc. (optional)"></textarea>
                                </div>
                            </template>

                            <div class="col-12 d-flex align-items-center gap-2 mt-2">
                                <button type="button" class="btn btn-primary" @click="addLine">
                                    <i class="ti ti-plus me-1"></i> Add to Purchase
                                </button>
                                <small class="text-muted" v-if="addForm.type === 'box' && addForm.package_qty > 1">
                                    Creates {{ addForm.package_qty }} separate products — each gets its own photos and listing afterward.
                                </small>
                                <small class="text-muted" v-else-if="addForm.type === 'piece' && addForm.package_qty > 1">
                                    {{ addForm.package_qty }} identical pieces sharing one row/product.
                                </small>
                            </div>

                            <div class="col-12" v-if="formMessage">
                                <div class="alert mb-0 py-2" :class="formAlertClass">
                                    <i :class="formIconClass"></i>
                                    {{ formMessage }}
                                </div>
                            </div>
                        </div>
                    </div>
                </div>

                <?php echo $__env->make('purchases._partials._line_table', array_diff_key(get_defined_vars(), ['__data' => 1, '__path' => 1]))->render(); ?>

            </div>

            <div class="col-xl-2">
                <?php echo $__env->make('purchases._partials._summary_card', array_diff_key(get_defined_vars(), ['__data' => 1, '__path' => 1]))->render(); ?>
            </div>
        </div>
    </form>
</div>
<?php $__env->stopSection(); ?>

<?php $__env->startPush('scripts'); ?>
<?php echo $__env->make('purchases._partials._purchase_app_script', [
    'mode'             => 'edit',
    'suppliersJson'    => $suppliers->toJson(),
    'locationsJson'    => $locations->toJson(),
    'racksJson'        => $racks->toJson(),
    'categoriesJson'   => $categories->toJson(),
    'countriesOfOriginJson' => $countriesOfOrigin->toJson(),
    'previewUrl'       => route('purchases.preview-invoice-number'),
    'lotCodePreviewUrl'=> route('purchases.preview-lot-code'),
    'submitUrl'        => route('purchases.update', $purchase),
    'submitMethod'     => 'PUT',
    'existingPurchase' => $purchase,
    'currencySymbol'   => $currencySymbol,
], array_diff_key(get_defined_vars(), ['__data' => 1, '__path' => 1]))->render(); ?>
<?php $__env->stopPush(); ?>

<?php echo $__env->make('layout.app', array_diff_key(get_defined_vars(), ['__data' => 1, '__path' => 1]))->render(); ?><?php /**PATH F:\VScode LIve ftp\paces\resources\views/purchases/edit.blade.php ENDPATH**/ ?>