<?php
/**
 * Created by PhpStorm.
 * User: chenkuan
 * Date: 2017/12/15
 * Time: 下午4:07
 */

namespace frontend\controllers;

use common\helpers\PaginationHelper;
use common\helpers\ValidatorHelper;
use frontend\models\Enroll;
use frontend\models\PartnerApply;
use frontend\models\Register;

class IndexActivityController extends BaseController
{
    /**
     * 获取首页报名记录
     */
    public function actionGetRegisterList()
    {
        $data = \Yii::$app->getRequest()->get();
        $data['page'] = isset($data['page']) ? (int)$data['page'] : 1;
        $data['pageSize'] = isset($data['pageSize']) ? (int)$data['pageSize'] : 10;
        $data['orderBy'] = isset($data['orderBy']) ? $data['orderBy'] : 'id desc';
        $data['search'] = isset($data['search']) ? $data['search'] : '';
        $data = ValidatorHelper::validateData($data, [
            ['search', 'string'],
            ['page', 'integer', 'min' => 1],
            ['pageSize', 'integer', 'min' => 1],
            ['orderBy', 'string'],
        ], $errors);
        if ($data == false) {
            return $this->outJson(0, '', $errors);
        }

        $query = Register::find()->orderBy($data['orderBy']);
        // 搜索
        if (!empty($data['search'])) {
            $query->andFilterWhere([
                'or',
                ['like', 'name', $data['search']],
                ['like', 'mobile', $data['search']],
                ['like', 'email', $data['search']],
            ]);
        }

        $result = PaginationHelper::queryPage($query, $data['page'], $data['pageSize']);
        return $this->outJson(1, $result);
    }

    /**
     * 删除官网首页报名记录
     */
    public function actionDeleteRegister()
    {
        $data = \Yii::$app->getRequest()->get();
        if (!empty($data['registerId'])) {
            $model = Register::findOne(['id' => $data['registerId']]);
            $rs = $model->delete();
            return $this->outJson(1, $rs);
        } else {
            return $this->outJson(0, [], '参数错误');
        }
    }

    /**
     * 获取首页合伙人申请记录
     */
    public function actionGetPartnerApply()
    {
        $data = \Yii::$app->getRequest()->get();
        $data['page'] = isset($data['page']) ? (int)$data['page'] : 1;
        $data['pageSize'] = isset($data['pageSize']) ? (int)$data['pageSize'] : 10;
        $data['orderBy'] = isset($data['orderBy']) ? $data['orderBy'] : 'id desc';
        $data['search'] = isset($data['search']) ? $data['search'] : '';
        $data = ValidatorHelper::validateData($data, [
            ['search', 'string'],
            ['page', 'integer', 'min' => 1],
            ['pageSize', 'integer', 'min' => 1],
            ['orderBy', 'string'],
        ], $errors);
        if ($data == false) {
            return $this->outJson(0, '', $errors);
        }

        $query = PartnerApply::find()->orderBy($data['orderBy']);
        // 搜索
        if (!empty($data['search'])) {
            $query->andFilterWhere([
                'or',
                ['like', 'name', $data['search']],
                ['like', 'mobile', $data['search']],
                ['like', 'email', $data['search']],
            ]);
        }

        $result = PaginationHelper::queryPage($query, $data['page'], $data['pageSize']);
        return $this->outJson(1, $result);
    }

    /**
     * 删除官网首页合伙人申请记录
     */
    public function actionDeletePartnerApply()
    {
        $data = \Yii::$app->getRequest()->get();
        if (!empty($data['applyId'])) {
            $model = PartnerApply::findOne(['id' => $data['applyId']]);
            $rs = $model->delete();
            return $this->outJson(1, $rs);
        } else {
            return $this->outJson(0, [], '参数错误');
        }
    }

    /**
     * 获取活动报名数据列表
     */
    public function actionGetEnrollList()
    {
        $data = \Yii::$app->getRequest()->get();
        $data['page'] = isset($data['page']) ? (int)$data['page'] : 1;
        $data['pageSize'] = isset($data['pageSize']) ? (int)$data['pageSize'] : 10;
        $data['orderBy'] = isset($data['orderBy']) ? $data['orderBy'] : 'id desc';
        $data['search'] = isset($data['search']) ? $data['search'] : '';
        $data = ValidatorHelper::validateData($data, [
            ['search', 'string'],
            ['page', 'integer', 'min' => 1],
            ['pageSize', 'integer', 'min' => 1],
            ['orderBy', 'string'],
        ], $errors);
        if ($data == false) {
            return $this->outJson(0, '', $errors);
        }

        $query = Enroll::find()->orderBy($data['orderBy']);
        // 搜索
        if (!empty($data['search'])) {
            $query->andFilterWhere([
                'or',
                ['like', 'nickname', $data['search']],
                ['like', 'mobile', $data['search']],
                ['like', 'email', $data['search']],
            ]);
        }

        $result = PaginationHelper::queryPage($query, $data['page'], $data['pageSize']);
        return $this->outJson(1, $result);
    }

    /**
     * 删除活动报名数据
     */
    public function actionDeleteEnroll()
    {
        $data = \Yii::$app->getRequest()->get();
        if (!empty($data['enrollId'])) {
            $model = Enroll::findOne(['id' => $data['enrollId']]);
            $rs = $model->delete();
            return $this->outJson(1, $rs);
        } else {
            return $this->outJson(0, [], '参数错误');
        }
    }
}
