<?php

namespace Modules\Manage\Http\Controllers;

use Catch\Base\CatchController;
use Illuminate\Foundation\Validation\ValidatesRequests;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\DB;
use Modules\Common\Services\CommonConfigService;

/**
 * 支付配置管理
 */
class PayConfigController extends CatchController
{
    use ValidatesRequests;
    /**
     * 配置列表
     * @param Request $request
     */
    public function list(Request $request) {
        $this->validate($request,[
            'name' => 'nullable|string|max:256',
            'pay_type' => 'nullable|in:1,2,3',
            'miniprogram_type' => 'nullable|in:1,2',
            'remark' => 'nullable|string|max:256',
            'pay_appid' => 'nullable|string|max:256',
        ]);
        $name = $request->input('name');
        $payType = $request->input('pay_type');
        $miniprogramType = $request->input('miniprogram_type');
        $payAppid = $request->input('pay_appid');
        $remark = $request->input('remark');
        $isAll = $request->input('is_all', 0);
        $commonConfig = config('common.common');


        $sql = DB::table('pay_merchants')
            ->when($name, function ($query, $name){
                return $query->where('name', 'like', '%'. $name .'%');
            })->when($payType, function ($query, $payType){
                return $query->where('pay_type', $payType);
            })->when($miniprogramType, function ($query, $miniprogramType) {
                return $query->where('miniprogram_type', $miniprogramType);
            })->when($payAppid, function ($query, $payAppid) {
                return $query->where('pay_appid', $payAppid);
            })->when($remark, function ($query, $remark){
                return $query->where('remark', 'like', '%'. $remark. '%');
            })->orderBy('id', 'desc');

        if($isAll) {
            $result =  $sql->get();
        } else {
            $result =  $sql->paginate($request->input('limit', 15));
        }

        $payTypeMap = CommonConfigService::getPayTypeMap();
        $miniprogramTypeMap = CommonConfigService::getMiniprogramTypeMap();
        foreach ($result as $item) {
            $item->pay_type_str = $payTypeMap[$item->pay_type]['label'] ?? '';
            $item->miniprogram_type_str = $miniprogramTypeMap[$item->miniprogram_type]['label'] ?? '';
        }

        return $result;
    }

    /**
     * 修改备注
     * @param Request $request
     */
    public function remark(Request $request) {
        $this->validate($request, ['id' => 'required']);
        DB::table('pay_merchants')
            ->where(['id' => $request->input('id')])
            ->update(['remark' => $request->input('remark', ''),
                'updated_at' => date('Y-m-d H:i:s')]);

        return 'ok';
    }

    /**
     * 添加支付配置信息
     * @param Request $request
     */
    public function addConfig(Request $request) {
        $this->validate($request, [
            'name' => 'required|string|max:256',
            'payee_name' => 'required|string|max:256',
            'pay_appid' => 'required',
            'pay_type' => 'required|integer|in:1,2,3',
            'pay_common_params' => 'required',
            'miniprogram_type' => 'required|in:1,2',
            'remark' => 'nullable|string|max:256'
        ]);
        $data = $request->only(['name', 'payee_name', 'pay_appid', 'pay_type', 'miniprogram_type']);
        $now = date('Y-m-d H:i:s');
        $data['created_at'] = $data['updated_at'] = $now;
        $data['pay_common_params'] = \json_encode($request->input('pay_common_params', []));
        $data['remark'] = $request->input('remark', '');
            DB::table('pay_merchants')
            ->insert($data);

        return 'ok';
    }
}
