<?php
namespace app\appapi\controller;

use app\appapi\BaseController;
use support\Request;
use think\facade\Db;

class ProductController extends BaseController
{
    public function getCategory(Request $request)
    {
        $param = $request->get();

        if(empty($param['cat_id'])){
            return $this->Success([]);
        }

        $where = [];

        $category = Db::name('category')
            ->where('id',$param['cat_id'])
            ->find();

        $category['parent_name'] = Db::name('category')
            ->where('id',$category['parent_id'])
            ->value('name');

        $result = $category;

        return $this->Success($result);
    }

    public function getProductList(Request $request)
    {
        $param = $request->get();

        if(empty($param['cat_id'])){
            return $this->Success([]);
        }

        $list = Db::name('product')
            ->where('lang',$param['lang'])
            ->where('cat_id',$param['cat_id'])
            ->order('sort_order', 'asc')
            ->order('id', 'asc')
            ->select()
            ->toArray();


        foreach($list as $key => $val){
            // $list[$key]['imageurl1'] = getUrl().$val['imageurl1'];
            // $list[$key]['imageurl2'] = getUrl().$val['imageurl2'];
        }

        $result = $list;

        return $this->Success($result);
    }

    public function getProduct(Request $request)
    {
        $param = $request->get();

        $where = [];

        $product = Db::name('product')
            ->where('id',$param['id'])
            ->find();


        $result = $product;

        return $this->Success($result);
    }

    public function searchProduct(Request $request)
    {
        $param = $request->get();

        $list = Db::name('product')
            ->where('lang',$param['lang'])
            ->where('name|description|content','like','%'.$param['keyword'].'%')
            ->order('sort_order', 'asc')
            ->order('id', 'asc')
            ->select()
            ->toArray();


        // foreach($list as $key => $val){
            // $list[$key]['imageurl1'] = getUrl().$val['imageurl1'];
            // $list[$key]['imageurl2'] = getUrl().$val['imageurl2'];
        // }

        $result = $list;

        return $this->Success($result);
    }
}
