<?php

namespace App\Http\Controllers\Admin;

use App\Models\FormData;
use App\Models\Order;
use Carbon\Carbon;
use Illuminate\Http\Request;

class OrderController extends Controller
{
    protected $redirect_index = '/admin/Order/index';

    protected $view_path = 'admin.orders.';

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

    protected $model_name = '订单';

    protected $model;

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

    public function index(Request $request)
    {
        $list = $this->model->where('id', '>', 0);

        if(!empty($request->input('pay_status')) && in_array($request->input('pay_status'), [1, 2, 3, 4, 5])) {
            $list = $list->where('pay_status', $request->input('pay_status'));
        }
        if(!empty($request->input('keyword')) && !empty(trim($request->input('keyword')))) {
            $keyword = '%' . trim($request->input('keyword')) . '%';
            $list = $list->whereHas('student', function ($query) use($keyword) {
                $query->where('name', 'like', $keyword);
            });
        }

        if(!empty($request->input('start_time'))) {
            $start_time = Carbon::createFromTimestamp(strtotime($request->input('start_time')))->toDateTimeString();
        } else {
            $start_time = Carbon::now()->subYears(10)->toDateTimeString();
        }

        if(!empty($request->input('end_time'))) {
            $end_time = Carbon::createFromTimestamp(strtotime($request->input('end_time')))->addDay()->toDateTimeString();
        } else {
            $end_time = Carbon::now()->addYears(10)->toDateTimeString();
        }

        $list = $list->where([
            ['created_at', '>=', $start_time],
            ['created_at', '<', $end_time],
        ]);

        $list = $list->orderBy('created_at', 'desc')->paginate()->withPath($this->getPaginateUrl());

        list($pre_uri, $model, $model_name) = array($this->pre_uri, $this->model, $this->model_name);

        $seven_days_ago = Carbon::now()->subDays(7)->toDateTimeString();
        $one_month_ago = Carbon::now()->subMonth()->toDateTimeString();
        return view($this->view_path . 'index', compact('list', 'pre_uri', 'model', 'model_name', 'seven_days_ago', 'one_month_ago'));
    }

    public function show(Request $request)
    {
        if(empty($request->input('id')) || empty($item = $this->model->find($request->input('id')))) {
            return $this->showWarning('找不到订单');
        }
        list($pre_uri, $model, $model_name) = array($this->pre_uri, $this->model, $this->model_name);
        $form_data = FormData::find($item->form_data_id);
        $form_data->text_1_arr = explode(':', $form_data->text_1, 2);
        $form_data->text_2_arr = explode(':', $form_data->text_2, 2);
        $form_data->text_3_arr = explode(':', $form_data->text_3, 2);
        $form_data->text_4_arr = explode(':', $form_data->text_4, 2);
        $form_data->multi_text_arr = explode(':', $form_data->multi_text, 2);
        $form_data->radio_arr = explode(':', $form_data->radio, 2);
        $form_data->checkbox_arr = explode(':', $form_data->checkbox, 2);
        return view($this->view_path . 'show', compact('item','pre_uri', 'model', 'model_name', 'form_data'));
    }

    public function showRemarkForm(Request $request)
    {
        if(empty($request->input('id')) || empty($item = $this->model->find($request->input('id')))) {
            return $this->showWarning('找不到订单');
        }
        list($pre_uri, $model, $model_name) = array($this->pre_uri, $this->model, $this->model_name);
        return view($this->view_path . 'show-remark-form', compact('item','pre_uri', 'model', 'model_name'));
    }

    public function updateRemark(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('找不到订单');
        }
        $item->remark = $request->input('remark', '');
        if(!$item->save()) {
            return $this->showWarning('保存失败');
        }
        return $this->showMessage('操作成功');
    }

    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('操作成功');
    }
}
