<?php
namespace App\Controllers\PSI;

use App\Controllers\Controller;
use App\Models\PSI\Product;

/** 成品管理（降温服/成衣） */
class ProductsController extends Controller
{
    use StockHelper;



    public function index()
    {
        $products = (new Product())->all();
        return $this->renderSubsys('psi', 'psi/products', ['products' => $products], \psi_nav(), 'products');
    }

    public function create()
    {
        subsys_admin('psi') or \Core\App::forbidden('需要 PSI 管理员权限');
        return $this->renderSubsys('psi', 'psi/product_form', ['product' => null], \psi_nav(), 'products');
    }

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

    public function edit($id)
    {
        subsys_admin('psi') or \Core\App::forbidden('需要 PSI 管理员权限');
        $product = (new Product())->find($id);
        if (!$product) return $this->redirect('PSI/products');
        return $this->renderSubsys('psi', 'psi/product_form', ['product' => $product], \psi_nav(), 'products');
    }

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

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

    public function adjust($id)
    {
        subsys_admin('psi') or \Core\App::forbidden('需要 PSI 管理员权限');
        if ($_SERVER['REQUEST_METHOD'] === 'POST') {
            if (!csrf_check()) return $this->redirect('PSI/products');
            $qty = (float)$this->post('qty');
            $direction = $this->post('direction') === 'out' ? 'out' : 'in';
            $this->adjustStock('product', (int)$id, abs($qty), $direction, 'ADJ-' . date('Ymd'));
            return $this->redirect('PSI/products');
        }
        $product = (new Product())->find($id);
        if (!$product) return $this->redirect('PSI/products');
        return $this->renderSubsys('psi', 'psi/adjust_form', ['item' => $product, 'type' => 'product'], \psi_nav(), 'products');
    }

    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')),
            'style_no'   => trim($this->post('style_no')),
            'color'      => trim($this->post('color')),
            'size_run'   => trim($this->post('size_run')),
            'season'     => trim($this->post('season')),
            'year'       => trim($this->post('year')),
            'stock'      => (float)$this->post('stock'),
            'cost'       => (float)$this->post('cost'),
            'price'      => (float)$this->post('price'),
            'remark'     => trim($this->post('remark')),
            'created_at' => date('Y-m-d'),
        ];
    }
}
