<?php


namespace App\Admin\Actions\Users;

use App\Models\UserInfoModel;
use Dcat\Admin\Admin;
use Dcat\Admin\Grid\RowAction;
use Dcat\Admin\Widgets\Modal;
use Dcat\Admin\Widgets\Table;

class UserAction extends RowAction
{
    protected $title = '更多资料';

    protected $model;

    public function __construct(string $model = null)
    {
        $this->model = $model;
    }

    /**
     * 设置确认弹窗信息，如果返回空值，则不会弹出弹窗
     *
     * 允许返回字符串或数组类型
     *
     * @return array|string|void
     */
    public function confirm()
    {

    }

    /**
     * 处理请求
     *
     * @param Request $request
     *
     * @return \Dcat\Admin\Actions\Response
     */
    public function handle(Request $request)
    {
        return $this->response()
            ->success('Processed successfully: '.$this->getKey())
            ->redirect('/');
    }

    /**
     * 设置要POST到接口的数据
     *
     * @return array
     */
    public function parameters()
    {
        return [];
    }

    public function render()
    {
        return Modal::make()
            ->lg()
            ->title($this->title)
            ->body($this->table($this->getKey()))
            ->button('<i class="feather icon-grid"></i> 更多资料');
    }

    protected function table($user_id)
    {
        $userInfo = UserInfoModel::query()->find($user_id);
        if(!$userInfo){
            return ;
        }
        $photoDiv = "";
        if(isset($userInfo->photo)&&!empty($userInfo->photo)){
            $photo = json_decode($userInfo->photo,true);
            $photoDiv = "<div class='show_img_div'>";
            if(is_array($photo)&&count($photo)>0){
                foreach ($photo as $v){
                    $photoDiv.= "<img  src='".$v['url']."' style='width:80px;height:80px;margin-right:10px'/>";
                }
            }
            $photoDiv .= "</div>";
        }
        $videoDiv = "";
        if(isset($userInfo->video)&&!empty($userInfo->video)){
            $video = json_decode($userInfo->video,true);
            $videoDiv = "<div>";
            if(is_array($video)&&count($video)>0){
                foreach ($video as $v){
                    if(isset($v['url'])){
                        $videoDiv .= "<video src='".$v['url']."' style='width:200px;height:200px;margin-right:10px' controls='controls'></video>";
                    }

                }
            }
            $videoDiv .= "</div>";
        }
        Admin::style('.table td{padding: .85rem .55rem}');
        Admin::script(<<<JS
                $('.show_img_div>img').click(function(){
                    Dcat.swal.fire(
                         {imageUrl:this.src,
                         imageMaxWidth:1200,
                         imageMaxHeight:900,
                         imageAlt:'图片打开失败'}
                    );
                });
            JS);
        $data = [
            ['name' => '相册',    'value' => $photoDiv],
            ['name' => '视频',    'value' => $videoDiv],
            ['name' => '生日',    'value' => $userInfo->birthday],
            ['name' => '身高',    'value' => $userInfo->height.'cm'],
            ['name' => '体重',    'value' => $userInfo->weight.'kg'],
            ['name' => '职业',    'value' => $userInfo->work],
            ['name' => '个人简介', 'value' =>$userInfo->info],
            ['name' => '地区',    'value' => $userInfo->area],
            ['name' => '身材',    'value' => $userInfo->figure],
            ['name' => '感情状态', 'value' => $userInfo->feeling],
            ['name' => '学历',    'value' => $userInfo->education],
            ['name' => '年收入',   'value' => $userInfo->income],
            ['name' => '兴趣爱好', 'value' => $userInfo->hobby],
            ['name' => '抽烟喝酒',  'value' => $userInfo->drink],
        ];
        return Table::make(['名称', '值'], $data);
    }
}
