<?php

namespace App\Http\Controllers\Admin;

use App\Http\Models\Sign;
use App\Models\GetRelief;
use Illuminate\Http\Request;
use App\Http\Controllers\PublicController;

/**
 * 签到控制器
 */
class SignController extends PublicController
{


    /**
     * 视图加载
     *
     * @access public
     * @return array
     */
    public function index()
    {
        return view('admin.sign.index');

    }//end index()


    /**
     * 数据获取
     *
     * @access public
     * @param  mixed $request 参数.
     * @return array
     */
    public function data(Request $request)
    {
        $signModel = new Sign;
        $getSignWhere  = [];
        // 查询参数.
        if (strlen($request->get('userName'))) {
            $getSignWhere['name'] = $request->get('userName');
        }

        $startTime = strtotime(date('Y-m-d').'-6 day');
        $endTime   = strtotime(date('Y-m-d').'+1 day');
        if (strlen($request->get('time'))) {
            $time      = explode(',', ($request->get('time')));
            $startTime = strtotime($time[0]);
            $endTime   = strtotime($time[1].'+1 day');
        }

        // 时间区间.
        $getSignBetween = [
            $startTime,
            $endTime,
        ];

        $getSignSelect = [
            'getsign_id',
            'getsign_identity',
            'name',
            'getsign_mony',
            'getsign_begin_money',
            'getsign_end_money',
            'getsign_addtime',
        ];
        // 查询数据.
        $res = $signModel->getSignRecord($getSignSelect, $request->get('limit', 30), $getSignWhere, $getSignBetween);
        // 返回参数.
        $data = [
            'code'  => 0,
            'msg'   => '正在请求中...',
            'count' => $res['total'],
            'data'  => $res['data'],
        ];
        return response()->json($data);

    }//end data()


    /**
     * 视图加载
     *
     * @access public
     * @param  mixed $request 参数.
     * @return array
     */
    public function setting()
    {
        $signModel                = new Sign;
        $getConfigSelect          = [
            'sign_data',
            'sign_title',
            'sign_name',
        ];
        $getDayWhere['sign_type'] = '1';
        // 获取周设置信息.
        $getDayConfig               = $signModel->signConfig($getConfigSelect, $getDayWhere, '');
        $getMonthWhere['sign_type'] = '2';
        // 获取月设置信息.
        $getMonthConfig = $signModel->signConfig($getConfigSelect, $getMonthWhere, '');
        return view('admin.sign.setting', compact('getDayConfig', 'getMonthConfig'));

    }//end setting()


    /**
     * 设置
     *
     * @access public
     * @param  mixed $request 参数.
     * @return array
     */
    public function onSetting(Request $request)
    {
        $signModel       = new Sign;
        $allSignTitle    = $signModel->signConfig('sign_title', '', '');
        $allSignTitleArr = [];
        foreach ($allSignTitle as $v) {
            $allSignTitleArr[] = $v['sign_title'];
        }

        $success = true;
        // 设置数据.
        foreach ($_REQUEST as $k => $v) {
            if (in_array($k, $allSignTitleArr) === true) {
                $upSignWhere['sign_title'] = $k;
                $upSignData                = [
                    'sign_upTime' => time(),
                    'sign_data'   => $v,
                ];
                $back = $signModel->upSignConfig($upSignWhere, $upSignData);
                if (empty($back) === true) {
                    $success = false;
                }
            }
        }

        if ($success === true) {
            return redirect()->to(route('admin.sign.setting'))->with(['status' => '更新成功']);
        }

        return redirect()->to(route('admin.sign.setting'))->withErrors('系统错误');

    }//end onSetting()


}
