<?php

namespace app\api\controller;

use app\common\controller\Api;
use think\Db;
use app\common\library\Keyworld;
/**
 * 视频
 */
class Video extends Api
{
    protected $noNeedLogin = ['typelist','lists','info','good'];
    protected $noNeedRight = ['*'];

    public function typelist(){
        $list = Db::name('video_type')->where('is_show',1)->order('weigh desc')->select();

        $this->success('success',$list);
    }


    public function lists(){
        $type_id = input('type_id',0);

        $where = [
            'is_show' => 1
        ];
        if($type_id){
            $where['type_id'] = $type_id;
        }

        $list = Db::name('video')->where($where)->order('weigh desc')->autopage()->select();
        $list = list_domain_image($list,['video_file','video_image']);

        $this->success('success',$list);
    }

    //获取详情
    public function info(){

        $id = input('id',0);

        $info = Db::name('video')->where('id',$id)->find();
        $info = info_domain_image($info,['video_file','video_image']);

        $this->success('success',$info);
    }

    //评论列表
    public function answer_list(){
        $id = input('id',0);

        $list = Db::name('video_answer')->alias('va')
            ->field('va.info,va.createtime,user.nickname,user.avatar')
            ->join('user','va.user_id = user.id','LEFT')
            ->where('va.video_id',$id)
            ->order('va.id desc')->autopage()->select();
        if(!empty($list)){
            foreach($list as $key => &$val){
                $val['createtime'] = get_last_time($val['createtime']);
                $val['avatar'] = localpath_to_netpath($val['avatar']);
            }
        }

        $this->success(1,$list);
    }

    //点赞
    public function good(){
        $id = input('id');

        Db::name('video')->where('id',$id)->setInc('goodnum');

        $this->success('点赞成功');
    }

    //评论
    public function answer(){
        $id = input('id',0);
        $info = input('info','');

        if(empty($info) || empty($id)){
            $this->error();
        }

        //关键字替换
        $info = Keyworld::sensitive($info);

        //data
        $data = [
            'video_id' => $id,
            'user_id' => $this->auth->id,
            'info' => $info,
            'createtime' => time(),
            'updatetime' => time(),
        ];

        Db::startTrans();

        $rs = Db::name('video_answer')->insertGetId($data);
        $answernum = Db::name('video')->where('id',$id)->setInc('answernum');

        Db::commit();

        $this->success('评价成功');
    }


}
