<?php

namespace App\Http\Controllers\Admin;

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

/**
 * 救济金控制器
 */
class ReliefController extends PublicController
{


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

    }//end index()


    /**
     * 数据获取
     *
     * @access public
     * @param  mixed $request 参数.
     * @return array
     */
    public function data(Request $request)
    {
        $reliefDataModel = new ReliefData;
        $getReliefWhere  = [];
        // 查询参数.
        if (strlen($request->get('userName'))) {
            $getReliefWhere['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');
        }

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

        $getReliefSelect = [
            'getrelief_id',
            'getrelief_identity',
            'name',
            'getrelief_mony',
            'getrelief_begin_money',
            'getrelief_end_money',
            'getrelief_addtime',
        ];
        // 查询数据.
        $res = $reliefDataModel->getRelief($getReliefSelect, $request->get('limit', 30), $getReliefWhere, $getReliefBetween);
        // 返回参数.
        $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()
    {
        $reliefDataModel = new ReliefData;
        $getConfigSelect = [
            'systemconfig_data',
            'systemconfig_status',
            'systemconfig_id',
        ];
        $getConfigWhere['systemconfig_name'] = 'relief';
        // 获取系统设置信息.
        $getSystemconfig = $reliefDataModel->getSystemconfig($getConfigSelect, $getConfigWhere);
        // 重组数据.
        $data                    = json_decode($getSystemconfig[0]['systemconfig_data'], true);
        $data['systemconfig_id'] = $getSystemconfig[0]['systemconfig_id'];
        $data['systemconfig_status'] = $getSystemconfig[0]['systemconfig_status'];
        return view('admin.relief.setting', compact('data'));

    }//end setting()


    /**
     * 设置
     *
     * @access public
     * @param  mixed $request 参数.
     * @return array
     */
    public function onSetting(Request $request)
    {
        $reliefDataModel                   = new ReliefData;
        $getConfigWhere['systemconfig_id'] = $_REQUEST['systemconfig_id'];
        // 设置数据.
        $data          = [
            'condition' => $_REQUEST['condition'],
            'count'     => $_REQUEST['count'],
            'mony'      => $_REQUEST['mony'],
        ];
        $getConfigData = [
            'systemconfig_status' => $_REQUEST['systemconfig_status'],
            'systemconfig_data'   => json_encode($data),
            'systemconfig_uptime'   => time(),
        ];
        $back = $reliefDataModel->upSystemconfig($getConfigWhere, $getConfigData);

        if ($back){
            return redirect()->to(route('admin.relief.setting'))->with(['status'=>'更新成功']);
        }
        return redirect()->to(route('admin.relief.setting'))->withErrors('系统错误');

    }//end onSetting()


}
