<?php

namespace app\api\controller;

use app\common\controller\Api;
use think\Db;
/**
 * 家庭乐园
 */
class Family extends Api
{
    protected $noNeedLogin = [];
    protected $noNeedRight = ['*'];

    //图片，视频的列表
    public function lists(){
        $type = input('type',1);

        $list = Db::name('family')->where('user_id',$this->auth->id)->where('type',$type)->order('id desc')->autopage()->select();
        if(!empty($list)){
            foreach($list as $key => &$val){
                $val['media_file'] = localpath_to_netpath($val['media_file']);

                if($val['type'] == 2){
                    $images_url = explode('.', $val['media_file']);
                    unset($images_url[count($images_url) - 1]);
                    $val['thumb_image'] = join('.', $images_url) . '_0.jpg';
                }else{
                    $val['thumb_image'] = $val['media_file'] . '?imageView2/1/w/100/h/100/q/85';
                }

            }
        }

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

    //上传
    public function addone(){
        $media_data = input('media_data','','htmlspecialchars_decode');
        $type       = input('type','');

        if(!$media_data || !$type){
            $this->error();
        }

        $media_data = json_decode($media_data,true);
        if(!is_array($media_data)){
            $this->error();
        }

        $all_data = [];
        foreach($media_data as $key => $val){

            if(
                !isset($val['media_file']) ||
                !isset($val['media_name']) ||
                !isset($val['media_size']) ||
                 empty($val['media_file']) ||
                 empty($val['media_name']) ||
                 empty($val['media_size'])
            ){continue;}

            $data = [
                'user_id'    => $this->auth->id,
                'media_file' => $val['media_file'],
                'media_name' => $val['media_name'],
                'media_size' => format_bytes($val['media_size']),
                'createtime' => time(),
                'type'       => $type,
            ];

            $all_data[] = $data;
        }

        Db::name('family')->insertAll($all_data);
        $this->success();
    }

    //删除
    public function delete(){
        $ids = input('ids','','trim');
        if(empty($ids)){
            $this->error();
        }

        $ids = explode(',',$ids);
        if(empty($ids)){
            $this->error();
        }

        Db::name('family')->where('id','IN',$ids)->delete();
        $this->success();
    }



}
