<?php
namespace App\Controllers\PSI;

use App\Controllers\Controller;
use App\Models\PSI\Material;
use App\Models\PSI\Supplier;

/** 物料管理（面料/辅料等） */
class MaterialsController extends Controller
{
    use StockHelper;



    public function index()
    {
        $materials = (new Material())->all();
        $suppliers = (new Supplier())->all();
        $smap = [];
        foreach ($suppliers as $s) { $smap[$s['id']] = $s['name']; }
        return $this->renderSubsys('psi', 'psi/materials', ['materials' => $materials, 'smap' => $smap], \psi_nav(), 'materials');
    }

    public function create()
    {
        subsys_admin('psi') or \Core\App::forbidden('需要 PSI 管理员权限');
        $suppliers = (new Supplier())->all();
        return $this->renderSubsys('psi', 'psi/material_form', ['material' => null, 'suppliers' => $suppliers], \psi_nav(), 'materials');
    }

    public function store()
    {
        subsys_admin('psi') or \Core\App::forbidden('需要 PSI 管理员权限');
        if (!csrf_check()) return $this->redirect('PSI/materials');
        (new Material())->insert($this->collect());
        return $this->redirect('PSI/materials');
    }

    public function edit($id)
    {
        subsys_admin('psi') or \Core\App::forbidden('需要 PSI 管理员权限');
        $material = (new Material())->find($id);
        if (!$material) return $this->redirect('PSI/materials');
        $suppliers = (new Supplier())->all();
        return $this->renderSubsys('psi', 'psi/material_form', ['material' => $material, 'suppliers' => $suppliers], \psi_nav(), 'materials');
    }

    public function update($id)
    {
        subsys_admin('psi') or \Core\App::forbidden('需要 PSI 管理员权限');
        if (!csrf_check()) return $this->redirect('PSI/materials');
        $material = (new Material())->find($id);
        if (!$material) return $this->redirect('PSI/materials');
        (new Material())->update($id, $this->collect());
        return $this->redirect('PSI/materials');
    }

    public function destroy($id)
    {
        subsys_admin('psi') or \Core\App::forbidden('需要 PSI 管理员权限');
        (new Material())->delete($id);
        return $this->redirect('PSI/materials');
    }

    /** 手动调整库存（盘盈/盘亏/报损） */
    public function adjust($id)
    {
        subsys_admin('psi') or \Core\App::forbidden('需要 PSI 管理员权限');
        if ($_SERVER['REQUEST_METHOD'] === 'POST') {
            if (!csrf_check()) return $this->redirect('PSI/materials');
            $qty = (float)$this->post('qty');
            $direction = $this->post('direction') === 'out' ? 'out' : 'in';
            $this->adjustStock('material', (int)$id, abs($qty), $direction, 'ADJ-' . date('Ymd'));
            return $this->redirect('PSI/materials');
        }
        $material = (new Material())->find($id);
        if (!$material) return $this->redirect('PSI/materials');
        return $this->renderSubsys('psi', 'psi/adjust_form', ['item' => $material, 'type' => 'material'], \psi_nav(), 'materials');
    }

    private function collect(): array
    {
        return [
            'code'        => trim($this->post('code')),
            'name'        => trim($this->post('name')),
            'spec'        => trim($this->post('spec')),
            'unit'        => trim($this->post('unit')) ?: '个',
            'category'    => trim($this->post('category')),
            'composition' => trim($this->post('composition')),
            'weight_gsm'  => (float)$this->post('weight_gsm'),
            'width_cm'    => (float)$this->post('width_cm'),
            'color'       => trim($this->post('color')),
            'batch_no'    => trim($this->post('batch_no')),
            'stock'       => (float)$this->post('stock'),
            'price'       => (float)$this->post('price'),
            'supplier_id' => (int)$this->post('supplier_id'),
            'remark'      => trim($this->post('remark')),
            'created_at'  => date('Y-m-d'),
        ];
    }
}
