<?php

namespace App\Admin\Actions;

use App\Models\Vote;
use Encore\Admin\Actions\Action;
use Illuminate\Http\Request;
use PhpOffice\PhpWord\TemplateProcessor;

class ExportDeng extends Action
{

    public    $name     = '批量导入';

    protected $vote;

    protected $selector = '.export-deng';

    public function handle(Request $request)
    {
        $templateProcessor = new TemplateProcessor(storage_path('app/public/DENG_E.docx'));

        $vote1 = Vote::find(1);
        $templateProcessor->setValue('TOTAL1', $vote1->logs()->distinct('user_id')->count());

        foreach ($vote1->items as $item) {
            $templateProcessor->setValue('ITEM_' . $item->id, $item->logs()->sum('result'));
        }
        $vote3 = Vote::find(3);
        $templateProcessor->setValue('TOTAL3', $vote1->logs()->distinct('user_id')->count());
        foreach ($vote3->items as $item) {
            $templateProcessor->setValue('ITEM_' . $item->id, $item->logs()->sum('result'));
        }

        $save = 'denge_1.docx';
        $templateProcessor->saveAs(storage_path('app/public/' . $save));

        return $this->response()->success('倒出成功')->download('/storage/' . $save);
    }

    public function html()
    {
        return <<<HTML
        <a class="btn btn-sm btn-info export-deng"><i class="fa fa-upload"></i> 等额数据倒出</a>
HTML;
    }

}
