<?php

namespace App\Admin\Forms\User;

use App\Models\Cms\CmsUser;
use App\Models\Inquiry;
use App\Models\User;
use Dcat\Admin\Contracts\LazyRenderable;
use Dcat\Admin\Form\Row;
use Dcat\Admin\Traits\LazyWidget;
use Dcat\Admin\Widgets\Form;

class EditUserHandle extends Form implements LazyRenderable
{
    use LazyWidget;

    /**
     * Handle the form request.
     *
     * @param array $input
     *
     * @return mixed
     */
    public function handle(array $input)
    {
        try {
            $id = explode(",", $input['id']);
//            $userList = User::getListByIdArr($ids);
            $userInfo = User::getInfoByUserId($id);
            if (!$userInfo) {
                throw new \Exception('客户不存在');
            }
            User::updateById($id, $input);
            return $this
                ->response()
                ->success(trans('admin.update_succeeded'))
                ->refresh();
        } catch (\Throwable $throwable) {
//            var_dump((string)$throwable);
            return $this->response()->error(trans('admin.update_failed'));
        }
    }

    /**
     * Build a form here.
     */
    public function form()
    {
        $this->disableResetButton();
        $this->hidden('id')->attribute('id', 'id')->width(4);
        $this->row(function (Row $form) {
            $form->width(5)->text('company_name')->required();
            $form->width(5)->text('user_sn');
        });
        $this->row(function (Row $form) {
            $form->width(5)->text('first_name')->required();
            $form->width(5)->text('last_name');
        });
        $this->row(function (Row $form) {
            $form->width(5)->text('phone')->required();
            $form->width(5)->text('email');
        });
        $this->row(function (Row $form) {
            $form->width(5)->text('reg_source')->required();
            $form->width(5)->text('remark');
        });
    }

    /**
     * The data of the form.
     *
     * @return array
     */
    public function default()
    {
        $userInfo = User::getInfoByUserId($this->payload['key']);


        return $userInfo;
    }
}