<?php

namespace App\Http\Controllers\Admin;

use App\Models\BaseAttachmentModel;
use App\Models\Content;
use Illuminate\Http\Request;

class ContentController extends Controller
{
    protected $redirect_index = '/admin/Content/index';

    protected $view_path = 'admin.contents.';

    protected $pre_uri = '/admin/Content/';

    protected $model_name = '内容';

    protected $model;

    public function __construct()
    {
        parent::__construct();
        $this->model = new Content();
    }

    public function index(Request $request)
    {
        $type = $this->model->getModelType($request->input('type', 1));
        $list = $this->model->where('type', $type)->orderBy('sort');

        if(!empty($request->input('keyword')) && !empty(trim($request->input('keyword')))) {
            $keyword = '%' . trim($request->input('keyword')) . '%';
            $list = $list->where('title', 'like', $keyword);
        }

        $list = $list->paginate()->withPath($this->getPaginateUrl());

        list($pre_uri, $model_name) = array($this->pre_uri, $this->model->getModelName($type));
        return view($this->view_path . 'index', compact('list', 'pre_uri', 'model_name', 'type'));
    }

    public function create(Request $request)
    {
        $type = $this->model->getModelType($request->input('type', 1));
        list($pre_uri, $model_name) = array($this->pre_uri, $this->model->getModelName($type));
        return view($this->view_path . 'create', compact('pre_uri', 'model_name', 'type', 'model'));
    }

    public function store(Request $request)
    {
        if(!$request->isMethod('POST')) {
            return $this->showWarning('访问错误');
        }

        if(empty($request->input('data')) || !is_array($request->input('data'))) {
            return $this->showWarning('数据错误');
        }

        $data = $request->input('data');
        if($request->hasFile('video')) {
            $data['content'] = (new BaseAttachmentModel())->upload($request->file('video'), '视频');
        }
        $res = $this->model->create($data);

        if(!$res) {
            return $this->showWarning('数据库保存失败！');
        }
        return $this->showMessage('操作成功', $this->redirect_index . '?type=' . $res->type);
    }

    public function edit(Request $request)
    {
        if(empty($request->input('id')) || empty($item = $this->model->find($request->input('id')))) {
            return $this->showWarning('数据错误！');
        }
        $type = $this->model->getModelType($item->type);
        list($pre_uri, $model_name, $model) = array($this->pre_uri, $this->model->getModelName($type), $this->model);

        return view($this->view_path . 'edit', compact('item','pre_uri', 'model_name', 'model', 'type'));
    }

    public function update(Request $request)
    {
        if(!$request->isMethod('POST')) {
            return $this->showWarning('访问错误');
        }

        if(empty($request->input('id')) || empty($request->input('data')) || !is_array($request->input('data'))) {
            return $this->showWarning('数据错误');
        }

        $item = $this->model->where('id', $request->input('id'))->first();
        if(empty($item)) {
            return $this->showWarning('找不到数据');
        }

        $data = $request->input('data');
        if($request->hasFile('video')) {
            $data['content'] = (new BaseAttachmentModel())->upload($request->file('video'), '视频');
        }
        $res = $this->model->where('id', $request->input('id'))->update($data);

        if(!$res) {
            return $this->showWarning('数据库保存失败！');
        }

        return $this->showMessage('操作成功', $this->redirect_index . '?type=' . $item->type);
    }


    public function delete(Request $request)
    {
        if(!$request->isMethod('POST')) {
            return $this->showWarning('访问错误');
        }
        if(empty($request->input('id')) || empty($item = $this->model->find($request->input('id')))) {
            return $this->showWarning('访问错误');
        }
        $res = $item->delete();
        if(!$res) {
            return $this->showWarning('数据库删除失败');
        }
        return $this->showMessage('操作成功');
    }
}
