<?php

namespace App\Admin\Actions\backstage\Coupon;

use App\Models\CouponType;
use Encore\Admin\Actions\RowAction;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Http\Request;

class UpdateCouponType extends RowAction
{
    public $name = '修改可用类型';

    public function handle(Model $model,Request $request)
    {
        $type = request('type');
        //先删除原来的数据，再添加新的数据
        CouponType::where('coupon_id',$this->row->id)->where('user_coupon_id',0)->delete();
        $arr = [
            'coupon_id' =>  $this->row->id,
            'user_coupon_id' =>  0,
            'product_type' =>  0,
        ];
        foreach ($type as $value)
        {
            $arr['product_type'] = $value;
            CouponType::create($arr);
        }
        return $this->response()->success('修改可用产品成功')->refresh();
    }
    public function form(){
        $value = CouponType::where('coupon_id',$this->row->id)->where('user_coupon_id',0)->pluck('product_type')->toArray();
        $this->multipleSelect('type','可用类型')->options([
            1=>'电话咨询',
            2=>'图文咨询',
            3=>'门诊咨询',
            4=>'疫苗接种',
            5=>'儿保预约',
            6=>'服务包',
            7=>'充值',
        ])->value($value);
    }

}