<?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;
use App\Models\DistAdminDistributor;

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!');
        }
        if (empty($distId)) {
            return $this->response()->error('No distributor selected!');
        }
        //分配数据
        DistInquiry::assignData($ids, $distId);
        return $this->response()->success(admin_trans_label('update_success'))->refresh();
    }

    public function form()
    {
//        $this->selectTable('distId', admin_trans_label('select_distributor'))
//            ->title(admin_trans_label('select_distributor'))
//            ->model(DistAdminDistributor::class, 'id', 'client_code')
//            ->from(DistDistributorTable::make());

        $this->select('distId',admin_trans_label('select_distributor'))->options('/api/dist')->required();
        // 设置隐藏表单，传递用户id
        $this->hidden('id')->attribute('id', 'inquiryIds');
    }
}
