<?php
namespace app\admin\controller;

use app\admin\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()
        {


            if($this->request->isAjax())
            {

                $limit = input('limit/d',20);
                $page = input('page/d',1);
                $keywords = trim(input('keywords/s'));
                $status = trim(input('status/d'));
                $workerid = trim(input('workerid/d'));
                $map = array();
                if(!empty($keywords))
                {
                    $map[] =['a.title', 'like', '%'.$keywords.'%'];
                }

                if (!empty($workerid)){
                    $map[] = ['a.workerid', '=', $workerid];
                }

                if (!empty($status)){
                    $map[] = ['a.status', '=', $status];
                }

                $data = Db::name('video')
                        ->alias('a')
                        ->field('a.id,a.workerid,a.title video_title,a.imageurl,a.videourl,from_unixtime(a.createtime) createtime,a.status,b.title worker_title')
                        ->join('worker b','b.id = a.workerid','LEFT')
                        ->where($map)
                        ->page($page)
                        ->limit($limit)
                        ->order('a.id desc')
                        ->select()
                        ->toArray();

                $count = Db::name('video')
                        ->alias('a')
                        ->join('worker b','b.id = a.workerid','LEFT')
                        ->where($map)
                        ->count();

                foreach($data as $k=>$v)
                {
                    if($v['status'] == 1)
                    {
                        $data[$k]['status_text'] = '待审核';
                    }elseif($v['status'] == 2){
                        $data[$k]['status_text'] = '未通过';
                    }elseif($v['status'] == 3){
                        $data[$k]['status_text'] = '通过';
                    }
                }

                //dd($data,$count);
                return array(
                            'code' 	=> 0,
                            'msg'	=> "",
                            'count' => $count,
                            'data'  => $data
                         );
            }else{

                $worker_data = Db::name('worker')->field('id,title')->select()->toArray();
                return view('video/videoList',['worker_data'=>$worker_data]);

            }

        }


        public function videoform()
        {
            $id = input('id/d');
            if($this->request->isAjax())
            {
                // $old_imageurl = trim(input('old_imageurl'));
                // if(!$old_imageurl)
                // {
                    // $data['imageurl'] = ;
                // }
				
				$imgstr = trim(input('imageurl'));
				
				$imgdata = substr($imgstr,strpos($imgstr,",") + 1);
				$decodedData = base64_decode($imgdata);
				$data['imageurl'] = "attachment/images/".date("Ymd")."/".md5(time()).".jpg";
				file_put_contents($data['imageurl'], $decodedData );
				
                $data['status'] = trim(input('status/d'));
                if(!empty($id))
                {
					$data['imageurl'] = request()->domain()."/".$data['imageurl']; 
                    $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 = Db::name('video')
                                ->alias('a')
                                ->field('a.id,a.workerid,a.title video_title,a.imageurl,a.videourl,from_unixtime(a.createtime) createtime,a.status,b.title worker_title')
                                ->join('worker b','a.workerid = b.id','LEFT')
                                ->where('a.id',$id)
                                ->find();

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

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


}