<?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("LicensePlate")]
    #[Apidoc\Sort(5)]
    class LicensePlateController
    {

        #[Apidoc\Title("省列表")]
        #[Apidoc\Url("api/license/provincelist.html")]
        #[Apidoc\Method("POST")]
        #[Apidoc\Header("token", type: "string", require: true, desc: "身份令牌Token", mock: "@token")]
        #[Apidoc\Returned("province", type: "string", require: true, desc: "省", mock: 1)]
        public function provincelist(Request $request)
        {
            $param              = $request->param_data;
            $param['user_data'] = $request->user_data;
            Db::beginTransaction();
            try {
                $datalist = Db::table('wa_abbreviation')->get()->toArray();
            } catch (\Throwable $exception) {
                Db::rollBack();
                return error($exception->getMessage());
            }
            Db::commit();
            return success($datalist, '获取成功');
        }

        #[Apidoc\Title("车牌列表")]
        #[Apidoc\Url("api/license/abbreviation.html")]
        #[Apidoc\Method("POST")]
        #[Apidoc\Header("token", type: "string", require: true, desc: "身份令牌Token", mock: "@token")]
        #[Apidoc\Param("province", type: "string", require: true, desc: "省", mock: 1)]
        public function abbreviation(Request $request)
        {
            $param              = $request->param_data;
            $param['user_data'] = $request->user_data;
            Db::beginTransaction();
            try {
                Validator::input($param, [
                    'province' => Validator::notEmpty()->setName('省名称'),
                ]);
                $datalist = Db::table('wa_abbreviation')->where('province', $param['province'])->first();
                if (!$datalist) {
                    throw new \Exception('非法操作');
                }
                $province = $datalist->abbreviation . 'A';
                // 数字（5位数字）
                $data = [];
                for ($i = 0; $i <= 50; $i++) {
                    $number = rand(10000, 99999); // 5位数字
                    $data[] = $province . $number;
                }
            } catch (\Throwable $exception) {
                Db::rollBack();
                return error($exception->getMessage());
            }
            Db::commit();
            return success($data, '获取成功');
        }

        #[Apidoc\Title("选择归属地，选择车牌")]
        #[Apidoc\Url("api/license/optionexid.html")]
        #[Apidoc\Method("POST")]
        #[Apidoc\Header("token", type: "string", require: true, desc: "身份令牌Token", mock: "@token")]
        #[Apidoc\Param("id", type: "int", require: true, desc: "id", mock: 1)]
        #[Apidoc\Param("belonging_region", type: "string", require: false, desc: "归属地", mock: 1)]
        #[Apidoc\Param("plate_numbe", type: "string", require: false, desc: "选择的车牌号", mock: 1)]
        public function optionexid(Request $request)
        {
            Db::beginTransaction();
            try {
                $param              = $request->param_data;
                $param['user_data'] = $request->user_data;
                Validator::input($param, [
                    'id' => Validator::notEmpty()->intType()->setName('ID'),
                ]);
                $signdata = Db::table('wa_sign_record')
                    ->where('uid', $param['user_data']['id'])
                    ->where('id', $param['id'])
                    ->first();
                if (empty($signdata)) {
                    throw new \Exception('数据不存在!');
                }
                Db::table('wa_sign_record')->where('id', $signdata->id)
                    ->update([
                        'belonging_region' => Arr::get($param, 'belonging_region', $signdata->belonging_region),
                        'plate_numbe'      => Arr::get($param, 'plate_numbe', $signdata->plate_numbe),
                        'updated_at'       => date('Y-m-d H:i:s'),
                    ]);

            } catch (\Throwable $exception) {
                Db::rollBack();
                return error($exception->getMessage());
            }
            Db::commit();
            return success([], '提交成功');
        }


    }
