<?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: "type", type: "int", require: true, desc: '类型：1=企业，2=个人', default: '')]
        #[Apidoc\Param(name: "name", type: "int", require: true, desc: '姓名（企业）', default: '')]
        #[Apidoc\Param(name: "card_number", type: "int", require: true, desc: '身份证', default: '')]
        #[Apidoc\Param(name: "mobile", type: "int", require: true, desc: '手机号', default: '')]
        #[Apidoc\Param(name: "province", type: "int", require: true, desc: '省市区', default: '')]
        #[Apidoc\Param(name: "address", type: "int", require: true, desc: '详细地址（企业地址）', default: '')]
        #[Apidoc\Param(name: "certificate_img", type: "int", require: true, desc: '证书图片', default: '')]
        #[Apidoc\Param(name: "report_img", type: "int", require: true, desc: '报表图片', default: '')]
        #[Apidoc\Param(name: "scale", type: "int", require: true, desc: '规模', default: '')]
        public function applyrecordAdd(Request $request)
        {
            Db::beginTransaction();
            try {
                $param              = $request->all();
                $param['user_data'] = $request->user_data;
                Validator::input($param, [
                    'type' => Validator::notEmpty()->intType()->setName('类型'),
                ]);
                $applylist = Db::table('wa_apply_record')
                    ->where('uid', $param['user_data']['id'])
                    ->where('type', $param['type'])
                    ->first();
                if ($applylist) {
                    if ($applylist->status == 3) {
                        $applyID = Db::table('wa_apply_record')->where('id', $applylist->id)->update([
                            'uid'             => $param['user_data']['id'],
                            'type'            => $param['type'],
                            'money'           => $applylist->money,
                            'name'            => Arr::get($param, 'name', null),
                            'mobile'          => Arr::get($param, 'mobile', null),
                            'card_number'     => Arr::get($param, 'card_number', null),
                            'province'        => Arr::get($param, 'province', null),
                            'address'         => Arr::get($param, 'address', null),
                            'certificate_img' => Arr::get($param, 'certificate_img', null),
                            'report_img'      => Arr::get($param, 'report_img', null),
                            'scale'           => Arr::get($param, 'scale', null),
                            'created_at'      => date('Y-m-d H:i:s'),
                            'updated_at'      => date('Y-m-d H:i:s'),
                            'add_time'        => bcadd(time(), '10800'),
                            'status'          => 1
                        ]);
                    } else {
                        throw new \Exception('已经申领过了!');
                    }
                } else {
                    $applyID = Db::table('wa_apply_record')->insertGetId([
                        'uid'             => $param['user_data']['id'],
                        'type'            => $param['type'],
                        'money'           => mt_rand(1080000, 1880000),
                        'name'            => Arr::get($param, 'name', null),
                        'mobile'          => Arr::get($param, 'mobile', null),
                        'card_number'     => Arr::get($param, 'card_number', null),
                        'province'        => Arr::get($param, 'province', null),
                        'address'         => Arr::get($param, 'address', null),
                        'certificate_img' => Arr::get($param, 'certificate_img', null),
                        'report_img'      => Arr::get($param, 'report_img', null),
                        'scale'           => Arr::get($param, 'scale', null),
                        'created_at'      => date('Y-m-d H:i:s'),
                        'updated_at'      => date('Y-m-d H:i:s'),
                        'add_time'        => bcadd(time(), '10800')
                    ]);
                }


            } 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\Param(name: "type", type: "int", require: true, desc: '类型：1=企业，2=个人', default: '')]
        #[Apidoc\Returned(name: "type", type: "int", require: true, desc: '类型：1=企业，2=个人', default: '')]
        #[Apidoc\Returned(name: "name", type: "int", require: true, desc: '姓名（企业）', default: '')]
        #[Apidoc\Returned(name: "card_number", type: "int", require: true, desc: '身份证', default: '')]
        #[Apidoc\Returned(name: "mobile", type: "int", require: true, desc: '手机号', default: '')]
        #[Apidoc\Returned(name: "province", type: "int", require: true, desc: '省市区', default: '')]
        #[Apidoc\Returned(name: "address", type: "int", require: true, desc: '详细地址（企业地址）', default: '')]
        #[Apidoc\Returned(name: "certificate_img", type: "int", require: true, desc: '证书图片', default: '')]
        #[Apidoc\Returned(name: "report_img", type: "int", require: true, desc: '报表图片', default: '')]
        #[Apidoc\Returned(name: "scale", type: "int", require: true, desc: '规模', default: '')]
        #[Apidoc\Returned(name: "status", type: "int", require: true, desc: '状态： 1=待审核，2=审核通过，3=驳回', default: '')]
        #[Apidoc\Returned(name: "report_img_array", type: "int", require: true, desc: '报表图片数组', default: '')]
        public function applyrecordDetails(Request $request)
        {
            Db::beginTransaction();
            try {
                $param              = $request->all();
                $param['user_data'] = $request->user_data;
                Validator::input($param, [
                    'type' => Validator::notEmpty()->intType()->setName('类型'),
                ]);
                $applylist = Db::table('wa_apply_record')
                    ->where('uid', $param['user_data']['id'])
                    ->where('type', $param['type'])
                    ->first();
                if (!$applylist) {
                    Db::rollBack();
                    return success('', '数据不存在');

                }
                if (!empty($applylist->certificate_img)) {
                    $applylist->certificate_img = getenv('IMG') . $applylist->certificate_img;
                }
                if (!empty($applylist->report_img)) {
                    $report_img = explode(',', $applylist->report_img);
                    $array_img  = [];
                    foreach ($report_img as $k => $v) {
                        $array_img[] = getenv('IMG') . $v;
                    }
                    $applylist->report_img       = $applylist->report_img;
                    $applylist->report_img_array = $array_img;
                }

            } catch (\Throwable $exception) {
                Db::rollBack();
                return error($exception->getMessage());
            }
            Db::commit();
            return success($applylist, '补偿申领成功');
        }


    }
