<?php

namespace BYDAuto\Commands;

use BYDAuto\Models\Contact;
use BYDAuto\Models\Testdrive;
use Illuminate\Console\Command;

class BYDAutoCommand extends Command
{
    protected $signature = 'bydauto {action?}';

    protected $description = 'BYDAuto Command';

    public function handle()
    {
        $action = $this->argument('action');
        $method = "action_{$action}";
        if (method_exists($this, $method)) {
            $this->{$method}();
        } else {
            $this->error("不支持的操作 {$action}");
        }
    }

    public function action_init()
    {
        $this->call('db:wipe');
        $this->call('migrate');
    }

    public function action_testdrive()
    {
        $items = Testdrive::query()->orderBy('id', 'desc')->get();
        $this->table(['id', 'date_created', 'status', 'name', 'mobile'], $items->map(function (Testdrive $v) {
            return [$v->id, $v->date_created, $v->status, $v->name, $v->mobile];
        }));
    }

    public function action_contact()
    {
        $items = Contact::all();
        $this->table(['id', 'email', 'remark', 'status'], $items->map(function (Contact $v) {
            return [$v->id, $v->email, $v->remark, $v->status];
        }));
    }

    public function action_contact_add()
    {
        $data['email'] = $this->ask('Email');
        $data['remark'] = $this->ask('Remark');
        $data['status'] = Contact::STATUS_PUBLISHED;

        $contact = new Contact($data);
        $contact->save();

        $this->info('Contact added');
    }
}
