<?php

namespace App\Admin\Forms;

use Dcat\Admin\Contracts\LazyRenderable;
use Dcat\Admin\Widgets\Form;
use App\Admin\Repositories\DistInquiry;
use App\Admin\Renderable\DistDistributorTable;
use Dcat\Admin\Models\DistAdminUser;
use Dcat\Admin\Traits\LazyWidget;

class InquiryAssignment extends Form
{
    // 处理请求
    public function handle(array $input)
    {
        $distId = $input['distId'] ?? null;
        $id = $input['id'] ?? null;
        if (empty($id)) {
            return $this->response()->error('No data selected!');
        }
        //检查数据是否可分配状态
        $ids = explode(',', $id);
        if (DistInquiry::assessTheStatus($ids) == false) {
            return $this->response()->error('The data cannot be assigned!');
        }
        //分配数据
        DistInquiry::assignData($ids, $distId);
        return $this->response()->success('allocation successful')->refresh();
    }

    public function form()
    {
        $this->selectTable('distId', 'Select Distributor')
            ->title('distId')
            ->from(DistDistributorTable::make());
        // 设置隐藏表单，传递用户id
        $this->hidden('id')->attribute('id', 'inquiryIds');
    }
}
