<?php
namespace app\worker\controller;

use app\worker\BaseController;
//use app\common\model\Param as ParamModel;

use app\common\model\Video as VideoModel;

use think\facade\Session;
use think\facade\Db;
use think\facade\Request;

class Video extends BaseController
{
        public function videoList()
        {
            $keywords = trim(input('keywords/s'));
            if($this->request->isAjax())
            {
                $workerid = $this->access_worker['id'];
                $map = array();
                $map[] = ['workerid', '=', $workerid];
                if(!empty($keywords))
                {
                    $map[] =['title', 'like', '%'.$keywords.'%'];
                }

                $data = VideoModel::where($map)->select()->toArray();
                $count = count($data);
                return array(
                            'code' 	=> 0,
                            'msg'	=> "",
                            'count' => $count,
                            'data'  => $data
                         );
            }else{

                return view('video/videoList');

            }

        }


        public function videoform()
        {
            $id = input('id/d');
            if($this->request->isAjax())
            {
                $data['workerid'] = $this->access_worker['id'];
                $data['title'] = trim(input('title/s'));
                $data['imageurl'] = trim(input('imageurl'));
//                $old_videourl = trim(input('old_videourl/s'));
                $new_videourl = trim(input('new_videourl/s'));

                if(!$id)
                {
                    $data['createtime'] = time();
                    $data['videourl'] = $new_videourl;
                    $res = Db::name('video')->insert($data);
                }else{
                    if(!empty($new_videourl))
                    {
                        $data['videourl'] = $new_videourl;
                    }
                    $data['id'] = $id;
                    $res = Db::name('video')->update($data);
                }

                if($res)
                {
                    $rtn['code'] = 0;
                    $rtn['message'] = '上传成功';
                }else{
                    $rtn['code'] = 1;
                    $rtn['message'] = '上传失败';
                }

                return $rtn;

            }else{

                 $video_data = VideoModel::findOrEmpty($id);

                return view('video/videoform',[
                    'video_data'=>$video_data
                ]);
            }
        }

    public function delVideo()
    {
        $idarr = input('idarr/a');
        $result = VideoModel::whereIn('id',$idarr)->where('workerid',$this->access_worker['id'])->delete();
        if ($result){
            exit(json_encode(array(
                'code' 	=> 0,
                'msg'	=> ""
            )));
        }
        exit(json_encode(array(
            'code' 	=> 1,
            'msg'	=> "删除失败，请稍后重试"
        )));
    }


}