<?php
namespace app\controller;

use think\facade\Request;
use app\model\Videos;

class Video extends CommonController
{

    // 视频列表
    public function index()
    {
        // 分页参数
        $pn       = Request::param('pn') ?? 1;
        $pagesize = 10;
        $limit    = ($pn - 1) * $pagesize;
        $where    = ['status' => 1];
        $search   = Request::param('search') ?? '';
        $field    = ['id' , 'title' , 'video_url' , 'page_view'];
        if($search) {
            // 获取视频数据
            $list     = Videos::field($field)->where($where)->where('title' , 'like' , "%$search%")->limit($limit , $pagesize)->select();
            $count    = Videos::where($where)->where("title" , "like" , "%$search%")->count();
        } else {
            // 获取视频数据
            $list     = Videos::field($field)->where($where)->limit($limit , $pagesize)->select();
            $count    = Videos::where($where)->count();
        }

        foreach ($list as $key => $value) {
            $is_http = $_SERVER['SERVER_PORT'] == 443 ? 'https://jm.admin.jiankangche.cn' : 'http://localhost:8124';
            $list[$key]['video_url'] = $is_http . '/uploads/' . $value['video_url'];
        }
        return $this->_json_succ(
            [
                'count' => $count , 'list' => $list
            ]
        );
    }


    // 更新浏览次数
    public function recordPv()
    {
        $id = Request::param('v_id') ?? '';
        if(!$id) return $this->_json_error('请求有误！');
        $result = Videos::where('id' , $id)->inc('page_view' , 1)->update();
        if($result) {
            return $this->_json_succ();
        }
        return $this->_json_error();
    }
}
