<?php

declare(strict_types=1);

namespace app\admin\controller\goods;

use app\common\controller\Backend;
use think\annotation\route\Group;
use think\annotation\route\Route;
use app\admin\traits\Actions;
use app\common\model\StockConfig;
use app\common\model\ProductConfig as ProductConfigModel;
use app\common\model\PackSpecs as PackSpecsModel;

#[Group("goods/product_config")]
class ProductConfig extends Backend
{

    use Actions;

    protected function _initialize()
    {
        parent::_initialize();
        $this->model = new ProductConfigModel();

        $this->assign('varietyList', StockConfig::where('type_id', 'variety_name')->column('title', 'id'));
        $this->assign('packSpecsList', PackSpecsModel::column('title', 'id'));
    }

    //删除
    #[Route('GET,JSON', 'index')]
    public function index()
    {
        if (false === $this->request->isAjax()) {
            return $this->fetch();
        }
        if ($this->request->post('selectpage')) {
            return $this->selectpage();
        }
        [$where, $order, $limit, $with] = $this->buildparams();
        
        $list = $this->model
            ->alias('p')
            ->leftJoin('pack_specs s',"p.pack_specs_id = s.id")
            ->field('p.*,s.title as pack_specs_title,s.labor_cost_money,s.one_surcharge_money,s.two_surcharge_money')
            ->where($where)
            ->order($order)
            ->paginate($limit)
            ->each(function ($item, $key) {
                return $item;
            });
        $result = ['total' => $list->total(), 'rows' => $list->items()];
        return json($result);
    }


    //导入
    #[Route("GET", "import")]
    public function import()
    {
        return "导入";
    }

    //下载
    #[Route("GET", "download")]
    public function download()
    {
        return "下载";
    }
}
