<?php

namespace app\controller;


use Illuminate\Support\Arr;
use Respect\Validation\Validator;
use support\Db;
use support\Request;
use hg\apidoc\annotation as Apidoc;

#[Apidoc\Title("申请")]
#[Apidoc\Group("Applyrecord")]
#[Apidoc\Sort(2)]
class ApplyrecordController
{

    #[Apidoc\Title("申请")]
    #[Apidoc\Url("api/applyrecord/applyrecord_add.html")]
    #[Apidoc\Method("POST")]
    #[Apidoc\Header("token", type: "string", require: true, desc: "身份令牌Token", mock: "@token")]
    #[Apidoc\Param(name: "name", type: "string", require: true, desc: '姓名', default: '')]
    #[Apidoc\Param(name: "number", type: "string", require: true, desc: '身份证', default: '')]
    #[Apidoc\Param(name: "mobile", type: "string", require: true, desc: '手机号', default: '')]
    #[Apidoc\Param(name: "sex", type: "string", require: true, desc: '性别', default: '')]
    #[Apidoc\Param(name: "is_poverty", type: "int", require: true, desc: '是否贫困：1=否，2=是', default: '')]
    public function applyrecordAdd(Request $request)
    {
        Db::beginTransaction();
        try {
            $param              = $request->param_data;
            $param['user_data'] = $request->user_data;
            Validator::input($param, [
                'name'   => Validator::notEmpty()->setName('姓名'),
                'number' => Validator::notEmpty()->setName('身份证'),
                'mobile' => Validator::notEmpty()->setName('手机号'),
                'sex'    => Validator::notEmpty()->setName('性别'),
            ]);
            $applylist = Db::table('wa_apply_record')
                ->where('uid', $param['user_data']['id'])
                ->first();
            if ($applylist) {
               Db::table('wa_apply_record')
                    ->where('id', $applylist->id)->update([
                        'money'      => $applylist->money,
                        'name'       => Arr::get($param, 'name', $applylist->name),
                        'mobile'     => Arr::get($param, 'mobile', $applylist->mobile),
                        'number'     => Arr::get($param, 'number', $applylist->number),
                        'sex'        => Arr::get($param, 'sex', $applylist->sex),
                        'is_poverty' => Arr::get($param, 'is_poverty', $applylist->is_poverty),
                        'updated_at' => date('Y-m-d H:i:s'),
                    ]);

            } else {
                $system  = Db::table('wa_system')->first();
                $applyID = Db::table('wa_apply_record')->insertGetId([
                    'uid'        => $param['user_data']['id'],
                    'money'      => $system->gift_gold,
                    'name'       => Arr::get($param, 'name', null),
                    'mobile'     => Arr::get($param, 'mobile', null),
                    'number'     => Arr::get($param, 'number', null),
                    'sex'        => Arr::get($param, 'sex', null),
                    'is_poverty' => Arr::get($param, 'is_poverty', null),
                    'created_at' => date('Y-m-d H:i:s'),
                    'updated_at' => date('Y-m-d H:i:s'),
                    'add_time'   => bcadd(time(), '86400'),
                ]);
            }
        } catch (\Throwable $exception) {
            Db::rollBack();
            return error($exception->getMessage());
        }
        Db::commit();
        return success([], '申请成功');
    }


    #[Apidoc\Title("申请详情")]
    #[Apidoc\Url("api/applyrecord/applyrecord_details.html")]
    #[Apidoc\Method("POST")]
    #[Apidoc\Header("token", type: "string", require: true, desc: "身份令牌Token", mock: "@token")]
    #[Apidoc\Returned(name: "name", type: "int", require: true, desc: '姓名', default: '')]
    #[Apidoc\Returned(name: "number", type: "int", require: true, desc: '身份证', default: '')]
    #[Apidoc\Returned(name: "mobile", type: "int", require: true, desc: '手机号', default: '')]
    #[Apidoc\Returned(name: "sex", type: "int", require: true, desc: '性别', default: '')]
    #[Apidoc\Returned(name: "is_poverty", type: "int", require: true, desc: '是否贫困：1=否，2=是', default: '')]
    #[Apidoc\Returned(name: "status", type: "int", require: true, desc: '状态： 1=待审核，2=审核通过，3=驳回', default: '')]
    public function applyrecordDetails(Request $request)
    {
        Db::beginTransaction();
        try {
            $param              = $request->param_data;
            $param['user_data'] = $request->user_data;
            $applylist          = Db::table('wa_apply_record')
                ->where('uid', $param['user_data']['id'])
                ->first();
            if (!$applylist) {
                Db::rollBack();
                return success('', '数据不存在');
            }
        } catch (\Throwable $exception) {
            Db::rollBack();
            return error($exception->getMessage());
        }
        Db::commit();
        return success($applylist, '获取成功');
    }


}
