<?php
// +----------------------------------------------------------------------
// | ThinkCMF [ WE CAN DO IT MORE SIMPLE ]
// +----------------------------------------------------------------------
// | Copyright (c) 2013-2019 http://www.thinkcmf.com All rights reserved.
// +----------------------------------------------------------------------
// | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
// +----------------------------------------------------------------------
// | Author: Powerless < wzxaini9@gmail.com>
// +----------------------------------------------------------------------

namespace app\love\controller;

use app\common\Excel;
use app\common\Fun;
use app\love\model\ActiveApplyModel;
use app\love\model\ActiveModel;
use cmf\controller\AdminBaseController;

class AdminActiveController extends AdminBaseController
{
    /**
     * 列表
     */
    public function index()
    {
        $list = ActiveModel::paginate(10);
        foreach ($list as $v) {
            $v['url'] = urlencode(url('love/Scene/wechat', ['active_id' => $v['id']], true, true));
        }

        // 获取分页显示
        $page = $list->render();
        $this->assign('list', $list);
        $this->assign('page', $page);

        // 渲染模板输出
        return $this->fetch();
    }

    /**
     * 添加
     */
    public function add()
    {
        return $this->fetch();
    }

    /**
     * 添加提交
     */
    public function addPost()
    {
        if ($this->request->isPost()) {
            $data = $this->request->param();
            $post = $data['post'];

            if (empty($post['start_time'])) {
                $this->error('请选择开始时间');
            }

            if (empty($post['end_time'])) {
                $this->error('请选择结束时间');
            }

            $post['create_time'] = $post['update_time'] = time();
            $post['start_time']  = strtotime($post['start_time']);
            $post['end_time']    = strtotime($post['end_time']);
            ActiveModel::create($post);

            $this->success('添加成功!', url('index'));
        }

    }

    /**
     * 编辑
     */
    public function edit()
    {
        $id   = $this->request->param('id', 0, 'intval');
        $post = ActiveModel::get($id);

        $this->assign('post', $post);
        return $this->fetch();
    }

    /**
     * 编辑提交
     */
    public function editPost()
    {

        if ($this->request->isPost()) {
            $data = $this->request->param();
            $post = $data['post'];

            if (empty($post['start_time'])) {
                $this->error('请选择开始时间');
            }

            if (empty($post['end_time'])) {
                $this->error('请选择结束时间');
            }

            $post['create_time'] = $post['update_time'] = time();
            $post['start_time']  = strtotime($post['start_time']);
            $post['end_time']    = strtotime($post['end_time']);
            ActiveModel::update($post, ['id' => $post['id']]);

            $this->success('编辑成功!', url('index'));
        }
    }

    /**
     * 审核列表
     */
    public function applyList()
    {
        $param = $this->request->param();
        $this->assign('id', $param['id']);
        $this->assign('check_status', $param['check_status'] ?? 0);
        $this->assign('sex', $param['sex'] ?? 0);

        $where = [
            ['active_id', '=', $param['id']],
        ];
        if (!empty($param['check_status'])) {
            $where[] = ['cmf_active_apply.check_status', '=', $param['check_status']];
        }
        $sexWhere = [];
        if (!empty($param['sex'])) {
            $sexWhere = ['sex'=>$param['sex']];
        }
        $total = ActiveApplyModel::hasWhere('user',$sexWhere)->where($where)->count();
        $this->assign('total', $total);

        $list  = ActiveApplyModel::hasWhere('user',$sexWhere)->with('user')->where($where)->order('check_status asc')->paginate(10, false, [
            'query' => $param,//不丢失已存在的url参数
        ]);
        foreach ($list as $v) {
            $v['age'] = Fun::getAgeByBirth($v['user']['birthday']);
        }

        $page = $list->render();
        $this->assign('list', $list);
        $this->assign('page', $page);

        return $this->fetch();
    }

    /**
     * 审核
     */
    public function checkPost()
    {
        $data = $this->request->post();
        ActiveApplyModel::update($data, ['id' => $data['id']]);

        $this->success('操作成功');
    }

    /**
     * 报名列表导出
     */
    public function applyExport()
    {
        $param = $this->request->param();

        $where = [
            ['active_id', '=', $param['id']],
        ];
        if (!empty($param['check_status'])) {
            $where[] = ['cmf_active_apply.check_status', '=', $param['check_status']];
        }
        $sexWhere = [];
        if (!empty($param['sex'])) {
            $sexWhere = ['sex'=>$param['sex']];
        }

        $list  = ActiveApplyModel::hasWhere('user',$sexWhere)->with('user')->where($where)->order('check_status asc')->select();
        $data = [];
        foreach ($list as $v) {
            $data[] = [
                'name' => $v['user']['realname'],
                'sex' => $v['user']['sex_text'],
                'age' => Fun::getAgeByBirth($v['user']['birthday']),
                'mobile' => $v['user']['mobile'],
                'company' => $v['user']['company'],
                'id_type' => $v['user']['id_type'],
                'native' => $v['user']['native'],
                'marry' => $v['user']['marry'],
                'check_status' => $v['status_text'],
            ];
        }

        if (empty($data)) {
            return '暂无数据';
        }

        $excel = new Excel();
        $title = [
            ['name','姓名'],
            ['sex','性别'],
            ['age','年龄'],
            ['mobile','电话'],
            ['company','单位'],
            ['id_type','身份类型'],
            ['native','籍贯'],
            ['marry','婚姻状况'],
            ['check_status','审核状态'],
        ];
        $excel->export('报名列表',$title,$data,['mobile']);
    }
}
