<?php
/** PSI 物料表单（面辅料） */
$m = $material;
$isEdit = !empty($m);
$catOpts = ['面料' => '面料', '辅料' => '辅料', '纱线' => '纱线', '包装' => '包装', '其他' => '其他'];
$curCat = $m['category'] ?? '面料';
if ($curCat && !isset($catOpts[$curCat])) { $catOpts[$curCat] = $curCat; }
?>
<div class="page-head"><h1><?php echo $isEdit ? '编辑物料' : '新增物料'; ?></h1></div>
<div class="panel">
    <form method="post" action="<?php echo site_url($isEdit ? 'PSI/materials/update/' . $m['id'] : 'PSI/materials/store'); ?>">
        <?php echo csrf_field(); ?>
        <div class="form-grid">
            <div class="field"><label>编码</label><input name="code" value="<?php echo e($m['code'] ?? ''); ?>"></div>
            <div class="field"><label>名称 *</label><input name="name" value="<?php echo e($m['name'] ?? ''); ?>" required></div>
            <div class="field"><label>分类</label>
                <select name="category">
                    <?php foreach ($catOpts as $k => $v): ?>
                        <option value="<?php echo $k; ?>" <?php echo $curCat === $k ? 'selected' : ''; ?>><?php echo $v; ?></option>
                    <?php endforeach; ?>
                </select>
            </div>
            <div class="field"><label>规格</label><input name="spec" value="<?php echo e($m['spec'] ?? ''); ?>"></div>
            <div class="field"><label>单位</label><input name="unit" value="<?php echo e($m['unit'] ?? '个'); ?>"></div>
            <div class="field"><label>颜色</label><input name="color" value="<?php echo e($m['color'] ?? ''); ?>"></div>
            <div class="field"><label>成分</label><input name="composition" value="<?php echo e($m['composition'] ?? ''); ?>" placeholder="如：100%棉 / 65%涤35%棉"></div>
            <div class="field"><label>克重(g/m²)</label><input name="weight_gsm" type="number" step="0.01" value="<?php echo e($m['weight_gsm'] ?? '0'); ?>"></div>
            <div class="field"><label>幅宽(cm)</label><input name="width_cm" type="number" step="0.01" value="<?php echo e($m['width_cm'] ?? '0'); ?>"></div>
            <div class="field"><label>缸号/批号</label><input name="batch_no" value="<?php echo e($m['batch_no'] ?? ''); ?>"></div>
            <div class="field"><label>初始库存</label><input name="stock" type="number" step="0.01" value="<?php echo e($m['stock'] ?? '0'); ?>"></div>
            <div class="field"><label>单价</label><input name="price" type="number" step="0.01" value="<?php echo e($m['price'] ?? '0'); ?>"></div>
            <div class="field"><label>默认供应商</label>
                <select name="supplier_id">
                    <option value="0">无</option>
                    <?php foreach ($suppliers as $s): ?>
                        <option value="<?php echo e($s['id']); ?>" <?php echo ($m['supplier_id'] ?? 0) == $s['id'] ? 'selected' : ''; ?>><?php echo e($s['name']); ?></option>
                    <?php endforeach; ?>
                </select>
            </div>
        </div>
        <div class="field"><label>备注</label><textarea name="remark" rows="2"><?php echo e($m['remark'] ?? ''); ?></textarea></div>
        <button class="btn-primary" type="submit">保存</button>
        <a class="btn-soft" href="<?php echo site_url('PSI/materials'); ?>">取消</a>
    </form>
</div>
