<?php
/** PSI 销售出库表单 */
?>
<div class="page-head"><h1>新增销售出库</h1></div>
<div class="panel">
    <form method="post" action="<?php echo site_url('PSI/sales/store'); ?>">
        <?php echo csrf_field(); ?>
        <div class="form-grid">
            <div class="field"><label>客户/买方</label><input name="customer" value="" placeholder="出口填海外客户名"></div>
            <div class="field"><label>渠道</label>
                <select name="channel">
                    <option value="domestic">内销（终端/批发）</option>
                    <option value="export">外贸出口</option>
                </select>
            </div>
            <div class="field"><label>销售区域</label><input name="region" value="" placeholder="如：华东/海外"></div>
            <div class="field"><label>出库类型</label>
                <select name="item_type" id="item_type2">
                    <option value="product">成品</option>
                    <option value="material">物料</option>
                </select>
            </div>
            <div class="field"><label>成品/物料</label>
                <select name="item_id" id="item_id2">
                    <optgroup label="成品">
                        <?php foreach ($products as $p): ?><option value="<?php echo e($p['id']); ?>"><?php echo e($p['name']); ?>(库存<?php echo e((float)($p['stock'] ?? 0)); ?>)</option><?php endforeach; ?>
                    </optgroup>
                    <optgroup label="物料">
                        <?php foreach ($materials as $m): ?><option value="<?php echo e($m['id']); ?>"><?php echo e($m['name']); ?>(库存<?php echo e((float)($m['stock'] ?? 0)); ?>)</option><?php endforeach; ?>
                    </optgroup>
                </select>
            </div>
            <div class="field"><label>数量 *</label><input name="qty" type="number" step="0.01" required></div>
            <div class="field"><label>单价</label><input name="price" type="number" step="0.01" value="0"></div>
            <div class="field"><label>批次/缸号</label><input name="batch_no" placeholder="出库批次/缸号"></div>
        </div>
        <div class="field"><label>备注</label><textarea name="remark" rows="2"></textarea></div>
        <div class="field" style="margin-bottom:16px">
            <label style="display:flex;align-items:center;gap:8px;cursor:pointer;font-weight:normal">
                <input type="checkbox" name="auto_print" value="1" checked style="width:auto;margin:0"> 保存后打印预览
            </label>
        </div>
        <button class="btn-primary" type="submit">出库并提交</button>
        <a class="btn-soft" href="<?php echo site_url('PSI/sales'); ?>">取消</a>
    </form>
</div>
<script>
document.getElementById('item_type2').addEventListener('change', function(){
    var type = this.value;
    var sel = document.getElementById('item_id2');
    Array.prototype.forEach.call(sel.querySelectorAll('optgroup'), function(og){
        og.style.display = (og.label === (type==='material'?'物料':'成品')) ? '' : 'none';
    });
    sel.selectedIndex = 0;
});
</script>
