<?php

namespace App\Admin\Controllers;

use App\Admin\Actions\DestroyData;
use App\Admin\Actions\ExportCha;
use App\Admin\Actions\ExportDeng;
use App\Admin\Actions\Replicate;
use App\Models\Item;
use App\Models\User;
use App\Models\Vote;
use Encore\Admin\Controllers\AdminController;
use Encore\Admin\Form;
use Encore\Admin\Grid;
use Encore\Admin\Widgets\Table;

class VoteController extends AdminController
{

    protected $title = '投票管理';

    public function grid()
    {
        $grid = new Grid(new Vote());
//        $grid->tools(function (Grid\Tools $tools) {
//            $tools->append(new ExportDeng());
//            $tools->append(new ExportCha());
//        });
        $grid->actions(function ($actions) {
            $actions->add(new Replicate);
            $actions->add(new DestroyData);
        });
        $grid->column('id', '#ID#');
        $grid->column('title', '投票名称');
        $grid->column('type', '类型')->using(['diff' => '差额投票', 'equal' => '等额投票']);
        $grid->column('status', '状态')->switch();
        $grid->column('数量')->display(function () {
            return '<a href="'.admin_url('votes/'.$this->id.'/items').'">'.$this->items()->count().'</a>';
        });
        $grid->column('参与/签到/总人数')->display(function () {
            return $this->logs()->distinct('user_id')->count()
                .'/'.User::where('type', 0)->where('sign', 1)->count()
                .'/'.User::where('type', 0)->count();
        });
        $grid->column('created_at', '创建时间')->date('Y-m-d H:i:s');

        return $grid;
    }

    public function form()
    {
        $form = new Form(new Vote);

        $form->text('title', '投票名称')->required();
        $form->select('type', '类型')->options([
            'equal' => '等额',
            'diff'  => '差额',
        ]);
        $form->number('max')->default(0)
            ->help('差额选票，最大可选人数');

        $form->switch('status', '状态');

        $form->ueditor('rules', '投票规则');

        return $form;
    }

    public function detail($id)
    {
        $data = Item::where('vote_id', $id)->get();

        $data = $data->sortByDesc(function ($item, $key) {
            return $item->logs()->sum('result');
        });

        // table 2
        $headers = ['序号', '姓名', '得票', '投票用户', '签到用户', '通过率'];
        $rows    = [];
        $i       = 1;
        foreach ($data as $item) {
            $cent = $item->logs()->sum('result') / User::where('type', 0)->where('sign', 1)->count();

            $color = $cent > 0.5 ? 'green' : 'red';

            $cent   = '<span style="color:'.$color.'">'.number_format($cent * 100, 2).'%</span>';
            $rows[] = [
                $i++,
                $item->name,
                $item->logs()->sum('result'),
                $item->vote->logs()->distinct('user_id')->count(),
                User::where('type', 0)->where('sign', 1)->count(),
                $cent,
            ];
        }

        return new Table($headers, $rows);

        $grid = new Grid(new Item);

        // $grid->model()->with([
        //     'logs' => function ($query) {
        //         $query->orderBy(\DB::raw('SUM(`result`)'), 'desc');
        //     },
        // ]);
        // $grid->model()->orderByRaw('updated_at - created_at DESC')

        $grid->paginate(100);
        $grid->disableActions();
        $grid->disableCreateButton();
        $grid->model()->where('vote_id', $id);

        $grid->column('name', '姓名');
        $grid->column('票数')->display(function () {
            return $this->logs()->sum('result');
        });
        $grid->column('结果')->display(function () {
            $cent = $this->logs()->sum('result') / User::where('type', 0)->where('sign', 1)->count();

            $color = $cent > 0.51 ? 'green' : 'red';

            return '<span style="color:'.$color.'">'.number_format($cent * 100, 2).'%</span>';
        });

        $grid->column('签到用户')->display(function () {
            return User::where('type', 0)->where('sign', 1)->count();
        });
        // $grid->column('用户总数')->display(function () {
        //     return User::where('type', 0)->count();
        // });

        return $grid;
    }

}
