<?php

namespace App\Distributor\Controllers;

use App\Distributor\Repositories\DistProduct;
use App\Distributor\Repositories\DistProductCategory;
use App\Distributor\Repositories\DistVideo;
use App\Distributor\Repositories\DistVideoCategory;
use App\Distributor\Repositories\SitePages;
use App\Distributor\Repositories\SitePagesTag;
use Illuminate\Http\Request;
use Illuminate\Routing\Controller;

class ApiController extends Controller
{
    /**
     * 产品下接API，默认返回50个最新的产品
     */
    public function products(Request $request)
    {
        $q = $request->get('q');
        if ($q != null) {
            // 模糊搜索
            $obj = new DistProduct();
            return $obj->model()->where('title', 'like', "%$q%")->where('dist_id', getDistributorId())->where('status', 2)->paginate(null, ['id', 'title as text']);
        } else {
            // 获取最新的N个
            $selectOptionsNew =  DistProduct::selectOptionsNew();
            return $this->changeOptions($selectOptionsNew);
        }
    }

    public function pages(Request $request)
    {
        $q = $request->get('q');
        if ($q != null) {
            // 模糊搜索
            $obj = new SitePages();
            return $obj->model()->where('title', 'like', "%$q%")->where('dist_id', getDistributorId())->where('page_type', 0)->paginate(null, ['id', 'title as text']);
        } else {
            // 获取最新的N个
            $selectOptionsNew =  SitePages::selectOptionsNew(30,0);
            return $this->changeOptions($selectOptionsNew);
        }
    }


    public function landingPages(Request $request)
    {
        $q = $request->get('q');
        if ($q != null) {
            // 模糊搜索
            $obj = new SitePages();
            return $obj->model()->where('title', 'like', "%$q%")->where('dist_id', getDistributorId())->where('page_type', 1)->paginate(null, ['id', 'title as text']);
        } else {
            // 获取最新的N个
            $selectOptionsNew =  SitePages::selectOptionsNew(30,1);
            return $this->changeOptions($selectOptionsNew);
        }
    }

    public function videos(Request $request)
    {
        $q = $request->get('q');
        if ($q != null) {
            // 模糊搜索
            $obj = new DistVideo();
            return $obj->model()->where('title', 'like', "%$q%")->where('dist_id', getDistributorId())->paginate(null, ['id', 'title as text']);
        } else {
            // 获取最新的N个
            $selectOptionsNew =  DistVideo::selectOptionsNew(30);
            return $this->changeOptions($selectOptionsNew);
        }
    }




    public function tag(Request $request)
    {
        $q = $request->get('q');
        if ($q != null) {
            // 模糊搜索
            $obj = new SitePagesTag();
            return $obj->model()->where('name', 'like', "%$q%")->where('dist_id', getDistributorId())->paginate(null, ['id', 'name as text']);
        } else {
            // 获取最新的N个
            $selectOptionsNew =  SitePagesTag::selectOptionsNew();
            return $this->changeOptions($selectOptionsNew);
        }
    }

    public function generateSlug(Request $request)
    {
        $model = $request->get('model');
        $title = $request->get('title');
        $result = null;
        switch ($model) {
            case 'pages':
                $obj = new SitePages();
                $result = $obj->generateSlug($title);
                break;
            case 'productCategory':
                $obj = new DistProductCategory();
                $result = $obj->generateSlug($title);
                break;
            case 'videoCategory':
                $obj = new DistVideoCategory();
                $result = $obj->generateSlug($title);
                break;
            case 'pagesTag':
                $obj = new SitePagesTag();
                $result = $obj->generateSlug($title);
                break;
        }
        return ['slug' => $result];
    }


    /*
     * 把数据转换成select需要的格式
     */
    private function changeOptions($data) {
        // 初始化结果数组
        $result = [];
        // 遍历原始数据并转换格式
        foreach ($data as $id => $text) {
            $result[] = [
                'id' => (int)$id,  // 将字符串转换为整数
                'text' => $text
            ];
        }
        return $result;
    }

}
