<?php

namespace app\controller;

use app\model\dynasty\DynastyAuthor;
use app\model\dynasty\DynastyCategory;
use QL\QueryList;
use support\Request;

class IndexController
{
    public function index(Request $request)
    {
        $ql = QueryList::getInstance()->get("https://www.gushiji.cc/dianji/")->rules([
//            "name"      => ['.gushi-info>h3','texts'],
//            "link"      => ['.gushi-info>h3>a','attrs(href)'],
            "name"     => ['.gushi-info>.cipai>ul>li','texts'],
            "link"      => ['.gushi-info>.cipai>ul>li>a','attrs(href)'],
        ])->query()->getData(function ($item,$key){
            foreach ($item['link'] as $k=>$v){
                $link = explode("/",$v);
                $item['link'][$k] = $link[2]??'';
            }
            return $item;
        });
        $resp = $ql->all();
        foreach ($resp['name'] as $k=>$v){
            $cate = (new DynastyAuthor)->where("pinyin",$resp['link'][$k])->findOrEmpty();
            if ($cate->isEmpty()){
                $cate->insertGetId([
                    "pinyin"    => $resp['link'][$k],
                    "name"      => $v,
                    "type"      => 2,
                    "initial"       => strtoupper(substr($resp['link'][$k], 0, 1)),
                ]);
            } else {
                $cate->insertGetId([
                    "pinyin"    => $resp['link'][$k],
                    "name"      => $v,
                    "type"      => 2,
                    "initial"       => strtoupper(substr($resp['link'][$k], 0, 1)),
                ]);
            }
        }
        return json($resp);
    }

    protected function getAuthor()
    {
        $ql = QueryList::getInstance()->get("https://www.gushiji.cc/shiren/")->rules([
            "name"      => ['.simple-people>li','texts'],
            "link"      => ['.simple-people>li>a','attrs(href)'],
            "avatar"    => ['.simple-people>li>a>img','attrs(src)'],
            "page"      => ['#pagenum>li.info','text'],
        ])->query()->getData(function ($item,$key){
            foreach ($item['avatar'] as $k=>$v){
                if (empty($v)){
                    $v = "/skin/images/nopicture.jpg";
                }
                $item['avatar'][$k] = "https://www.gushiji.cc".$v;
            }
            foreach ($item['link'] as $k=>$v){
                $link = explode("/",$v);
                $item['link'][$k] = $link[2]??'';
            }
            $poem = [];
            foreach ($item['name'] as $k=>$v){
                preg_match('/([^()（）]+)[(（](\d+)[)）]/u', $v, $match);
                $item['name'][$k] = $match[1]??'';
                $poem[$k] = $match[2]??0;
            }
            preg_match_all('/\d+/', $item['page'], $matches);
            $item['page'] = $matches[0][0] ?? 0;
            $item['total'] = $matches[0][1] ?? 0;
            $item['poem'] = $poem;
            return $item;
        });
        return $ql->all();
    }

    public function view(Request $request)
    {
        return view('index/view', ['name' => 'webman']);
    }

    public function json(Request $request)
    {
        return json(['code' => 0, 'msg' => 'ok']);
    }

}
