<?php

namespace app\admin\controller;

use app\admin\AdminBaseController;
use app\common\model\VoteApplyModel;
use app\common\model\VoteModel;
use app\common\service\QrcodeService;
use app\common\validate\VoteValidate;
use think\exception\ValidateException;

class Vote extends AdminBaseController
{

    /**
     * 列表
     */
    public function index()
    {
        return view();
    }

    public function listVote()
    {
        $list  = VoteModel::limit(input('limit'))->page(input('page'))->select();
        $count = VoteModel::count();
        if ($count == 0) {
            ajax_return(1, '未查询到数据');
        }
        list_return($list, $count);
    }

    /**
     * 编辑
     */
    public function voteForm()
    {
        $id   = input('id/d, 0');
        $info = VoteModel::find($id);

        return view('', [
            'info' => $info,
        ]);
    }

    public function editVote()
    {
        $data = input('post.');
        try {
            validate(VoteValidate::class)->check($data);
        } catch (ValidateException $e) {
            ajax_return(1, $e->getError());
        }

        if (empty($data['id'])) {
            VoteModel::create($data);
        } else {
            VoteModel::update($data, ['id' => $data['id']]);
        }

        ajax_return();
    }

    public function delVote()
    {
        $id    = input('id/d');
        $check = VoteApplyModel::where('vote_id', $id)->find();
        if (!empty($check)) {
            ajax_return(1, '已有投票的活动无法删除！');
        }
        VoteModel::destroy($id);
        ajax_return();
    }

    /**
     * 二维码
     */
    public function qrcodeVote()
    {
        $id = input('id/d', 0);

        $file_name = "/vote_{$id}.png";
        $link      = url('/mobile/vote/detail') . '?id=' . $id;
        $file_url  = QrcodeService::getQrcode($file_name, $link, 600);

        ajax_return(0, '', $file_url);
    }

    /**
     * 查看票数
     */
    public function showNum()
    {
        $id = input('id/d', 0);

        $apply = VoteApplyModel::where('vote_id', $id)->select();
        $res   = [];
        foreach ($apply as $v) {
            if (empty($res[$v['option_name']])) {
                $res[$v['option_name']] = 0;
            }

            $res[$v['option_name']]++;
        }

        return view('', [
            'info' => $res,
        ]);
    }

    /**
     * 投票明细
     */
    public function apply()
    {
        $vote_list = VoteModel::select();

        return view('', [
            'vote_list' => $vote_list,
        ]);
    }

    public function listApply()
    {
        $map = $this->dealEqualInput([], ['vote_id']);

        $list  = VoteApplyModel::with(['user', 'vote'])->where($map)->limit(input('limit'))->page(input('page'))->select();
        $count = VoteApplyModel::where($map)->count();
        if ($count == 0) {
            ajax_return(1, '未查询到数据');
        }
        list_return($list, $count);
    }

    /**
     * 带活动id的投票明细
     */
    public function applyVote()
    {
        return view('', [
            'id' => input('id/d', 0),
        ]);
    }
}