<?php

namespace App\Http\Controllers\Admin;

use App\Http\Resources\Admin\DailyCollection;
use App\Models\Daily\DailyPushModel;
use App\Models\User\Openid;
use Illuminate\Http\Request;
use App\Http\Controllers\Controller;

class DailyController extends Controller
{


    /**
     * 列表
     * @param  Request  $request
     * @return \Illuminate\Contracts\Routing\ResponseFactory|\Symfony\Component\HttpFoundation\Response
     */
    public function index(Request $request)
    {
        $articles = DailyPushModel::when($request->filled('date'), function ($query) use ($request) {
            return $query->where('date', $request->date);
        })->orderBy('date', 'desc')->paginate(10);
        return new DailyCollection($articles);
    }


    /**
     * 创建文章
     * @param  Request  $request
     * @return \Illuminate\Contracts\Routing\ResponseFactory|\Symfony\Component\HttpFoundation\Response
     */
    public function store(Request $request)
    {
        $this->validate($request, [
            'push_content' => 'required',
            'article_url' => 'required',
            'reply_content' => 'required',
            'date' => 'required|date',
        ]);
        if (DailyPushModel::where('date', $request->date)->first()) {
            return response([
                'code' => 409,
                'message' => '日期已存在'
            ]);
        }
        $article = DailyPushModel::create($request->all());
        return response([
            'code' => 200,
            'data' => $article
        ]);
    }


    /**
     * 修改文章
     * @param  Request  $request
     * @param $id
     * @return \Illuminate\Contracts\Routing\ResponseFactory|\Symfony\Component\HttpFoundation\Response
     */
    public function update(Request $request, $id)
    {
        $this->validate($request, [
            'push_content' => '',
            'article_url' => '',
            'reply_content' => '',
            'date' => 'date',
        ]);
        $article = DailyPushModel::findOrFail($id);
        if (DailyPushModel::where('date', $request->date)->where('id', '<>', $id)->first()) {
            return response([
                'code' => 409,
                'message' => '日期已存在'
            ]);
        }
        $article->update($request->all());
        return response([
            'code' => 200,
            'data' => $article
        ]);
    }


    /**
     * 删除
     * @param  Request  $request
     * @param $id
     * @return \Illuminate\Contracts\Routing\ResponseFactory|\Symfony\Component\HttpFoundation\Response
     */
    public function destroy(Request $request, $id)
    {
        $article = DailyPushModel::findOrFail($id);
        $article->delete();
        return response([
            'code' => 200,
            'message' => 'OK'
        ]);
    }


    /**
     * 发送预览
     * @param  Request  $request
     * @param $id
     * @return \Illuminate\Contracts\Routing\ResponseFactory|\Symfony\Component\HttpFoundation\Response
     */
    public function preview(Request $request, $id)
    {
        $this->validate($request, [
            'uid' => 'required|integer',
        ]);
        $uid = $request->uid;
        $publicId = 'gh_b598cb7474d8';
        $openid = Openid::where('public_id', $publicId)->where('uid', $uid)->value('openid');
        $article = DailyPushModel::findOrFail($id);

        $content = sprintf(
            $article->content,
            "<a href=\"weixin://bizmsgmenu?msgmenucontent=每日&msgmenuid=234234\" >这里</a>"
        );
        $content .= "\n若不想收到每日精选，回复【JXTD】可进行退订~";

        $result = \WeChat::KfAccount($openid, 'text', [
            'content' => $content
        ]);

        if ($result['code'] == 0) {
            return response([
                'code' => 200,
                'message' => $result['msg']
            ]);
        } else {
            return response([
                'code' => 500,
                'message' => $result['msg']
            ]);
        }
    }
}
